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 AbstractSolver(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 AbstractSolver::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 AbstractSolver::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: AbstractSolver.h:42
virtual void createInitialPopulation()=0
Creates the initial population of individuals. Must be implemented in the subclasses to adapt special...
void setPopulation(const vector< S > &pop)
Set the Population.
Definition: AbstractSolver.h:470
virtual void initProgress()=0
Initialises the evolutionary progress. This method must be implemented in the subclasses and should p...
virtual bool isStoppingConditionReached()=0
Check whether the number of performed evaluations has reached the maximum allowed....
int getPopulationSize() const
Get the population size.
Definition: AbstractSolver.h:92
virtual string getID() const =0
Returns the identificator of the algorithm, this is used in the to_json method. Must be implemented i...
double getElapsedTime() const
Get the elapsed time of the evolutionary process.
Definition: AbstractSolver.h:101
virtual void finishProgress()=0
Method which finishes the evolutionary progress. This method must be implemented in the subclasses an...
const Problem< S > * getProblem() const
Gets a pointer to the problem which is being solved.
Definition: AbstractSolver.h:131
void setPerformedEvaluations(int pEvals)
Set the Performed Evaluations. Useful for the updateProgress method.
Definition: AbstractSolver.h:181
virtual void setProblem(Problem< S > *prob)
Set the new problem to solve. Uses a raw pointer and takes the ownership of the object.
Definition: AbstractSolver.h:149
virtual json to_json() const
Generates and returns the JSON representation of the EA.
Definition: AbstractSolver.h:481
static const int DEFAULT_POPULATION_SIZE
Default population size equal to 100 individuals.
Definition: AbstractSolver.h:283
PopulationEvaluator< S > * getEvaluator() const
Gets a pointer to the population evaluator system.
Definition: AbstractSolver.h:156
virtual Front< S > getResults() const =0
Returns a Front object with all the solutions of the evolutionary process. This method must be implem...
int getPrintingInterval() const
Get the interval of checkpoints.
Definition: AbstractSolver.h:204
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: AbstractSolver.h:164
int getMaxEvaluations() const
Get the maximum number of evaluations to perform.
Definition: AbstractSolver.h:115
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 void evaluatePopulation(vector< S > &pop)=0
Evaluates the entire population of solutions. This is a virtual method that must be implemented in th...
virtual void updateProgress()=0
Method which updates the evolutionary progress. This method must be implemented in the subclasses and...
const vector< S > & getPopulation() const
Gets a reference of the population.
Definition: AbstractSolver.h:108
virtual Evolution< S > getEvolution() const
Get the Evolution data.
Definition: AbstractSolver.h:447
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: AbstractSolver.h:139
void setPopulationSize(int pSize)
Setter to update the population size.
Definition: AbstractSolver.h:458
virtual void run()=0
Main method of the EA. Runs the algorithm but must be implemented in the subclasses.
int getPerformedEvaluations() const
Get the performed evaluations.
Definition: AbstractSolver.h:173
int populationSize
Definition: AbstractSolver.h:261
int maxEvaluations
Definition: AbstractSolver.h:259
static const int DEFAULT_EVALUATIONS_LIMIT
Default evaluation limit equal to 100000 evaluations.
Definition: AbstractSolver.h:284
AbstractSolver()
Construct a new Abstract EA object with default parameter values.
Definition: AbstractSolver.h:329
void setMaxEvaluations(int maxEval)
Set a new maximum number of evaluations to perform.
Definition: AbstractSolver.h:122
virtual void updateEvolution(vector< S > &pop)
Definition: AbstractSolver.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