11 #ifndef DIGNEA_ABSTRACTGA_H
12 #define DIGNEA_ABSTRACTGA_H
14 #include <dignea/core/Crossover.h>
15 #include <dignea/core/Mutation.h>
16 #include <dignea/core/Problem.h>
17 #include <dignea/core/Replacement.h>
18 #include <dignea/core/Selection.h>
19 #include <dignea/evaluators/PopulationEvaluator.h>
20 #include <dignea/utilities/exceptions/NoMOAllowed.h>
21 #include <dignea/utilities/random/PseudoRandom.h>
46 json to_json()
const override;
52 void setProblem(shared_ptr<
Problem<S>> prob)
override;
57 virtual void initProgress();
61 virtual void finishProgress();
63 virtual bool isStoppingConditionReached();
65 virtual void createInitialPopulation();
67 virtual void evaluatePopulation(vector<S> &pop);
69 virtual vector<S> createMating();
71 virtual void reproduction(S &, S &);
74 double getMutationRate()
const;
76 void setMutationRate(
double mutationRate);
78 double getCrossRate()
const;
80 void setCrossRate(
double crossRate);
92 void setSelection(unique_ptr<
Selection<S>> selectionOperator);
141 mutationRate(DEFAULT_MUTATION_RATE),
142 crossRate(DEFAULT_CROSSOVER_RATE) {
147 this->evaluator = make_unique<PopulationEvaluator<S>>();
158 this->population.resize(this->populationSize);
159 for (
int i = 0; i < this->getPopulationSize(); i++) {
160 this->population[i] = this->problem->createSolution();
163 this->evaluatePopulation(this->population);
166 this->nextCheckpoint = 0;
167 this->updateEvolution(0, this->population);
168 this->initProgress();
180 this->evaluator->evaluate(pop, this->problem.get());
193 return (this->performedEvaluations >= this->maxEvaluations);
206 this->performedEvaluations = this->populationSize;
225 this->createInitialPopulation();
226 this->startTime = chrono::system_clock::now();
227 while (!this->isStoppingConditionReached()) {
228 vector mating = this->createMating();
229 this->evaluatePopulation(mating);
230 this->population = replacement->replace(this->population, mating);
231 this->updateProgress();
232 this->updateEvolution(this->performedEvaluations, this->population);
234 this->endTime = chrono::system_clock::now();
235 this->finishProgress();
237 throw std::runtime_error(
"Problem is not set in GA::run");
251 vector<S> offspring(this->populationSize);
252 for (
int i = 0; i < this->populationSize; i++) {
253 S child1 = selection->select(this->population);
254 S child2 = selection->select(this->population);
255 reproduction(child1, child2);
256 offspring[i] = child1;
273 crossover->run(child1, child2);
275 mutation->run(child1, mutationRate, this->problem.get());
330 return mutation.get();
342 this->mutation = move(newMut);
353 return crossover.get();
365 this->crossover = move(newCX);
376 return selection.get();
388 this->selection = move(newSelection);
400 if (prob->getNumberOfObjs() != 1) {
401 std::string where =
"AbstractGA setProblem";
402 throw(NoMOAllowed(where));
404 this->problem = prob;
416 std::string where =
"AbstractGA setProblem";
417 throw(NoMOAllowed(where));
419 this->problem.reset(prob);
429 std::chrono::duration<double> diff = this->endTime - this->startTime;
430 this->elapsedTime = diff.count();
443 if (!this->population.empty()) {
446 for (
const S &individual : this->population) {
447 if (individual.getConstraintCoeff() == 0.0) {
448 results.push_back(S(individual));
451 results.erase(unique(results.begin(), results.end()), results.end());
465 if (mutation) data[
"mutation"] = this->mutation->getName();
466 if (crossover) data[
"crossover"] = this->crossover->getName();
467 if (selection) data[
"selection"] = this->selection->getName();
468 if (replacement) data[
"replacement"] = this->replacement->getName();
469 data[
"mutation_rate"] = this->mutationRate;
470 data[
"crossover_rate"] = this->crossRate;
Class to define an Abstract Evolutionary Algorithm. This is the base skeleton for future extensions.
Definition: AbstractEA.h:42
virtual json to_json() const
Generates and returns the JSON representation of the EA.
Definition: AbstractEA.h:481
int populationSize
Definition: AbstractEA.h:261
Class to represent an Abstract Genetic Algorithm. Base skeleton is defined here, to extend in particu...
Definition: AbstractGA.h:38
static int DEFAULT_POPULATION_SIZE
Default population size to GAs set to 32 individuals.
Definition: AbstractGA.h:106
json to_json() const override
Creates and returns JSON object with the GA information.
Definition: AbstractGA.h:463
void run() override
Runs the evolutionary process. This is the main EA method. This methods is in charge of:
Definition: AbstractGA.h:223
void setProblem(shared_ptr< Problem< S >> prob) override
Uptades the problem to solve using a share_ptr. The problem must be single-objective.
Definition: AbstractGA.h:399
virtual void updateProgress()=0
Method which updates the evolutionary progress. This method must be implemented in the subclasses and...
const Mutation< S > * getMutation() const
Gets a pointer to the mutation operator.
Definition: AbstractGA.h:329
virtual vector< S > createMating()
Generates the mating population of individuals to be evaluated. The individuals are selected and afte...
Definition: AbstractGA.h:250
virtual void initProgress()
Starts the evolutionary process. By default this methods sets the number of performed evaluations to ...
Definition: AbstractGA.h:205
const Crossover< S > * getCrossover() const
Returns a raw pointer to the crossover operator.
Definition: AbstractGA.h:352
AbstractGA()
Creates a new AbstractGA which initialises all parameter to default. The operators will be set to nul...
Definition: AbstractGA.h:139
void setCrossRate(double crossRate)
Updates the crossover rate.
Definition: AbstractGA.h:318
double crossRate
Definition: AbstractGA.h:96
static double DEFAULT_CROSSOVER_RATE
Default crossover rate to GAs set to 0.8.
Definition: AbstractGA.h:105
const Selection< S > * getSelection() const
Returns a raw pointer of the selection operator.
Definition: AbstractGA.h:375
virtual void evaluatePopulation(vector< S > &pop)
Evaluates the entire population of individuals using the problem evaluation function.
Definition: AbstractGA.h:179
void setMutation(unique_ptr< Mutation< S >> mutation)
Updates the mutation operator. Takes a unique_ptr pointing to the new Mutation operator and takes the...
Definition: AbstractGA.h:341
virtual void reproduction(S &, S &)
Applies the genetic operator. This methods is invoked from createMating.
Definition: AbstractGA.h:270
double mutationRate
Definition: AbstractGA.h:95
virtual void createInitialPopulation()
Creates the initial population of individuals before starting the evolutionary process.
Definition: AbstractGA.h:157
static double DEFAULT_MUTATION_RATE
Default mutation rate for GAs set to 0.05.
Definition: AbstractGA.h:104
virtual bool isStoppingConditionReached()
Checks whether the evolutionary process has reached the maximum limit.
Definition: AbstractGA.h:192
unique_ptr< Selection< S > > selection
Definition: AbstractGA.h:100
void setCrossover(unique_ptr< Crossover< S >> crossover)
Updates the crossover operator. Takes a unique_ptr pointing to the new Crossover operator and takes t...
Definition: AbstractGA.h:364
virtual void finishProgress()
Finishes the evolutionary process by computing the elapsed time.
Definition: AbstractGA.h:428
unique_ptr< Replacement< S > > replacement
Definition: AbstractGA.h:101
unique_ptr< Mutation< S > > mutation
Definition: AbstractGA.h:98
virtual Front< S > getResults() const
Returns a front with the feasible non repeated individuals in the last population of the Genetic Algo...
Definition: AbstractGA.h:442
void setSelection(unique_ptr< Selection< S >> selectionOperator)
Updates the selection operator. Takes a unique_ptr pointing to the new Selection operator and takes t...
Definition: AbstractGA.h:387
virtual string getName() const =0
Returns the name of the algorithm, this is used in the to_json method. Must be implemented in the sub...
double getCrossRate() const
Gets the crossover rate.
Definition: AbstractGA.h:307
void setMutationRate(double mutationRate)
Updates the mutation rate.
Definition: AbstractGA.h:296
double getMutationRate() const
Gets the mutation rate.
Definition: AbstractGA.h:285
unique_ptr< Crossover< S > > crossover
Definition: AbstractGA.h:99
Abstract Crossover interface.
Definition: Crossover.h:19
Front class which stores the final results of an EA execution.
Definition: Front.h:26
Abstract Mutation interface.
Definition: Mutation.h:21
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
int getNumberOfObjs() const
Get the number of objectives of the problem.
Definition: Problem.h:135
static double randDouble()
Generates a random double value between 0.0 and 1.0.
Definition: PseudoRandom.cpp:31
Abstract Selection interface.
Definition: Selection.h:21