12 #ifndef DIGNEA_ABSTRACTEA_H 
   13 #define DIGNEA_ABSTRACTEA_H 
   15 #include <dignea/core/Front.h> 
   16 #include <dignea/core/Problem.h> 
   17 #include <dignea/core/Solution.h> 
   18 #include <dignea/evaluators/PopulationEvaluator.h> 
   19 #include <dignea/evolution/AvgEvolution.h> 
   20 #include <dignea/evolution/Evolution.h> 
   22 #include <dignea/utilities/exceptions/NotImplemented.h> 
   30 #include <nlohmann/json.hpp> 
   48     AbstractEA(unique_ptr<PopulationEvaluator<S>>, 
const int &maxEvals,
 
   58     virtual void run() = 0;
 
   75     virtual string getID() 
const = 0;
 
  140         this->problem = prob;
 
  156     PopulationEvaluator<S> *
getEvaluator()
 const { 
return evaluator.get(); }
 
  165         this->evaluator = move(eval);
 
  182         this->performedEvaluations = pEvals;
 
  260     int performedEvaluations;
 
  264     vector<S> population;
 
  266     shared_ptr<Problem<S>> problem;
 
  268     unique_ptr<PopulationEvaluator<S>> evaluator;
 
  270     Evolution<S> evolution;
 
  271     AvgEvolution<S> avgEvolution;
 
  274     std::chrono::system_clock::time_point startTime;
 
  275     std::chrono::system_clock::time_point endTime;
 
  280     int evolutionInterval;
 
  285     static const std::string NAME;
 
  286     static const std::string MAX_EVALUATIONS;
 
  287     static const std::string POP_SIZE;
 
  288     static const std::string ELAPSED_TIME;
 
  289     static const std::string EVALUATOR;
 
  290     static const int EVOLUTION_SIZE;
 
  330     : maxEvaluations(DEFAULT_EVALUATIONS_LIMIT),
 
  331       performedEvaluations(0),
 
  332       populationSize(DEFAULT_POPULATION_SIZE),
 
  350     : maxEvaluations(maxEvals),
 
  351       performedEvaluations(0),
 
  352       populationSize(popsize),
 
  357     this->evaluator = make_unique<PopulationEvaluator<S>>();
 
  372                           const int &maxEvals, 
const int &popsize)
 
  373     : maxEvaluations(maxEvals),
 
  374       performedEvaluations(0),
 
  375       populationSize(popsize),
 
  379     evaluator = move(eval);
 
  388     population.shrink_to_fit();
 
  400     if (nextCheckpoint > maxEvaluations) {
 
  401         string where = 
"nextCheckpoint > maxEvals(" +
 
  402                        to_string(maxEvaluations) +
 
  403                        ") in AbstractEA::updateEvolution with: " +
 
  404                        to_string(nextCheckpoint) + 
" evals";
 
  405         throw(OutOfRange(where));
 
  407     if (performedEvaluations >= nextCheckpoint) {
 
  408         evolution.update(nextCheckpoint, solutions);
 
  409         avgEvolution.update(nextCheckpoint, solutions);
 
  410         nextCheckpoint += evolutionInterval;
 
  424                                     vector<S> &solutions) {
 
  425     if (checkpoint > (maxEvaluations + evolutionInterval)) {
 
  427             "checkpoint > maxEvals in AbstractEA::updateEvolution with: " +
 
  428             to_string(checkpoint) +
 
  429             " evals and maxEvals: " + to_string(maxEvaluations);
 
  430         throw(OutOfRange(where));
 
  432     if (checkpoint >= nextCheckpoint) {
 
  433         evolution.update(checkpoint, solutions);
 
  434         avgEvolution.update(nextCheckpoint, solutions);
 
  435         nextCheckpoint += evolutionInterval;
 
  448     return this->evolution;
 
  459     this->populationSize = pSize;
 
  460     this->population.clear();
 
  461     this->population.reserve(populationSize);
 
  471     this->population = pop;
 
  472     this->populationSize = pop.size();
 
  483     Evolution copy = this->evolution;
 
  484     AvgEvolution avgCopy = this->avgEvolution;
 
  485     info[
"name"] = this->getName();
 
  486     info[
"max_evals"] = this->maxEvaluations;
 
  487     info[
"pop_size"] = this->populationSize;
 
  488     info[
"elapsed_time"] = this->elapsedTime;
 
  489     info[
"evaluator"] = this->evaluator->getName();
 
  490     info[
"evolution"] = copy.results();
 
  491     info[
"avg_evolution"] = avgCopy.results();
 
nlohmann::json json
Definition: MinKnap.h:85
 
Class to define an Abstract Evolutionary Algorithm. This is the base skeleton for future extensions.
Definition: AbstractEA.h:42
 
virtual void initProgress()=0
Initialises the evolutionary progress. This method must be implemented in the subclasses and should p...
 
virtual void run()=0
Main method of the EA. Runs the algorithm but must be implemented in the subclasses.
 
AbstractEA(unique_ptr< PopulationEvaluator< S >>, const int &maxEvals, const int &popsize)
AbstractEA constructor which initialises the parameters maxEvalautions, the populationSize and the ev...
Definition: AbstractEA.h:371
 
void setPopulation(const vector< S > &pop)
Set the Population.
Definition: AbstractEA.h:470
 
const vector< S > & getPopulation() const
Gets a reference of the population.
Definition: AbstractEA.h:108
 
virtual json to_json() const
Generates and returns the JSON representation of the EA.
Definition: AbstractEA.h:481
 
virtual Front< S > getResults() const =0
Returns a Front object with all the solutions of the evolutionary process. This method must be implem...
 
AbstractEA()
Construct a new Abstract EA object with default parameter values.
Definition: AbstractEA.h:329
 
void setMaxEvaluations(int maxEval)
Set a new maximum number of evaluations to perform.
Definition: AbstractEA.h:122
 
int maxEvaluations
Definition: AbstractEA.h:259
 
virtual void updateProgress()=0
Method which updates the evolutionary progress. This method must be implemented in the subclasses and...
 
const Problem< S > * getProblem() const
Gets a pointer to the problem which is being solved.
Definition: AbstractEA.h:131
 
virtual void evaluatePopulation(vector< S > &pop)=0
Evaluates the entire population of solutions. This is a virtual method that must be implemented in th...
 
void setPerformedEvaluations(int pEvals)
Set the Performed Evaluations. Useful for the updateProgress method.
Definition: AbstractEA.h:181
 
virtual void updateEvolution(const int &checkpoint, vector< S > &)
Definition: AbstractEA.h:423
 
PopulationEvaluator< S > * getEvaluator() const
Gets a pointer to the population evaluator system.
Definition: AbstractEA.h:156
 
static const int DEFAULT_POPULATION_SIZE
Default population size equal to 100 individuals.
Definition: AbstractEA.h:283
 
virtual void createInitialPopulation()=0
Creates the initial population of individuals. Must be implemented in the subclasses to adapt special...
 
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...
 
virtual bool isStoppingConditionReached()=0
Check whether the number of performed evaluations has reached the maximum allowed....
 
int getPrintingInterval() const
Get the interval of checkpoints.
Definition: AbstractEA.h:204
 
void setPopulationSize(int pSize)
Setter to update the population size.
Definition: AbstractEA.h:458
 
virtual string getID() const =0
Returns the identificator of the algorithm, this is used in the to_json method. Must be implemented i...
 
int getPopulationSize() const
Get the population size.
Definition: AbstractEA.h:92
 
int populationSize
Definition: AbstractEA.h:261
 
static const int DEFAULT_EVALUATIONS_LIMIT
Default evaluation limit equal to 100000 evaluations.
Definition: AbstractEA.h:284
 
virtual void setProblem(shared_ptr< Problem< S >> prob)
Set the new problem to solve. Uses a shared_ptr pointer which updates the reference counter....
Definition: AbstractEA.h:139
 
int getPerformedEvaluations() const
Get the performed evaluations.
Definition: AbstractEA.h:173
 
AbstractEA(const int &maxEvals, const int &popsize)
AbstractEA constructor which initialises the parameters maxEvalautions and populationSize.
Definition: AbstractEA.h:349
 
virtual void setProblem(Problem< S > *prob)
Set the new problem to solve. Uses a raw pointer and takes the ownership of the object.
Definition: AbstractEA.h:149
 
virtual Evolution< S > getEvolution() const
Get the Evolution data.
Definition: AbstractEA.h:447
 
double getElapsedTime() const
Get the elapsed time of the evolutionary process.
Definition: AbstractEA.h:101
 
void setEvaluator(unique_ptr< PopulationEvaluator< S >> eval)
Updates the population evaluator system with the unique_ptr. The method takes the ownership of the ob...
Definition: AbstractEA.h:164
 
int getMaxEvaluations() const
Get the maximum number of evaluations to perform.
Definition: AbstractEA.h:115
 
virtual void finishProgress()=0
Method which finishes the evolutionary progress. This method must be implemented in the subclasses an...
 
virtual void updateEvolution(vector< S > &pop)
Definition: AbstractEA.h:399
 
Front class which stores the final results of an EA execution.
Definition: Front.h:26
 
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29