5 #ifndef DIGNEA_SIMULATEDANNEALING_H
6 #define DIGNEA_SIMULATEDANNEALING_H
9 #include <dignea/utilities/exceptions/NoMOAllowed.h>
10 #include <dignea/utilities/random/PseudoRandom.h>
26 const float &tempVariation);
36 string getName()
const override {
return "Simulated Annealing"; };
42 string getID()
const override {
return "SA"; }
44 Front<S> getResults()
const override;
46 void setProblem(shared_ptr<
Problem<S>> prob)
override;
61 SimulatedAnnealing::tempVariation = tempVariation;
75 SimulatedAnnealing::currentTemp = currentTemp;
78 void setPopulationSize(
const int &pSize);
81 void initProgress()
override;
83 void updateProgress()
override;
85 void finishProgress()
override;
87 bool isStoppingConditionReached()
override;
89 void createInitialPopulation()
override;
93 S evaluateInds(S &popInd, S &offspring);
96 S applyPerturbations();
98 void updateTemp() { currentTemp *= tempVariation; }
115 const float &initialTemp,
116 const float &tempVariation)
118 tempVariation(tempVariation),
119 currentTemp(initialTemp) {}
129 this->createInitialPopulation();
130 this->startTime = chrono::system_clock::now();
131 while (!this->isStoppingConditionReached()) {
132 S offspring = applyPerturbations();
133 this->population[0] = evaluateInds(this->population[0], offspring);
135 this->updateProgress();
137 this->endTime = chrono::system_clock::now();
138 this->finishProgress();
140 throw std::runtime_error(
"Problem is not set in SA::run");
164 this->performedEvaluations = 1;
175 this->performedEvaluations += 2;
185 std::chrono::duration<double> diff = this->endTime - this->startTime;
186 this->elapsedTime = diff.count();
198 return (this->performedEvaluations >= this->maxEvaluations);
209 this->population.resize(1);
210 this->population[0] = this->problem->createSolution();
211 this->evaluatePopulation(this->population);
212 this->nextCheckpoint = 0;
213 this->updateEvolution(0, this->population);
214 this->initProgress();
227 this->evaluator->evaluate(popInd, this->problem.get());
228 this->evaluator->evaluate(offspring, this->problem.get());
229 double diff = offspring.getFitness() - popInd.getFitness();
234 const double e = exp(-diff / this->currentTemp);
235 const double prob = min(1.0, e);
236 return (randProb < prob) ? offspring : popInd;
249 S offspring = this->population[0];
251 vector vars = offspring.getVariables();
252 for (
unsigned int i = 0; i < vars.size(); i++) {
253 double newValue = pert * (this->problem->getUpperLimit(i) -
254 this->problem->getLowerLimit(i)) +
255 this->problem->getLowerLimit(i);
258 offspring.setVariables(vars);
270 if (prob->getNumberOfObjs() != 1) {
271 std::string where =
"SimulatedAnnealing setProblem";
272 throw(NoMOAllowed(where));
274 this->problem = prob;
286 std::string where =
"SimulatedAnnealing setProblem";
287 throw(NoMOAllowed(where));
289 this->problem.reset(prob);
301 std::string where =
"SimulatedAnnealing setPopulationSize";
302 throw(NotImplemented(where));
Class to define an Abstract Evolutionary Algorithm. This is the base skeleton for future extensions.
Definition: AbstractEA.h:42
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
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
Simulated Annealing Algorithm.
Definition: SimulatedAnnealing.h:23
void evaluatePopulation(vector< S > &vector1) override
Evaluates the entire population of solutions. This is a virtual method that must be implemented in th...
Definition: SimulatedAnnealing.h:91
void run() override
Runs the Simulated Annealing algorithm during maxEvaluations.
Definition: SimulatedAnnealing.h:127
void setTempVariation(float tempVariation)
Set the temperature variation constant for each generation.
Definition: SimulatedAnnealing.h:60
Front< S > getResults() const override
Returns the results obtained by the SA algorithm at the end of the process.
Definition: SimulatedAnnealing.h:152
void finishProgress() override
Finishes the evolutionary process and computes the elapsed time.
Definition: SimulatedAnnealing.h:184
S evaluateInds(S &popInd, S &offspring)
Evaluates the parent and offspring individuals at each generation.
Definition: SimulatedAnnealing.h:226
void initProgress() override
Initialises the evolutionary process. Sets the number of performedEvaluations to 1.
Definition: SimulatedAnnealing.h:163
string getID() const override
Get the ID of the algorithm.
Definition: SimulatedAnnealing.h:42
string getName() const override
Get the Name.
Definition: SimulatedAnnealing.h:36
float getCurrentTemp() const
Get the current temperature.
Definition: SimulatedAnnealing.h:68
void setPopulationSize(const int &pSize)
This methods is not implemented for this class. The population size of a SA algorithm is always 1.
Definition: SimulatedAnnealing.h:300
bool isStoppingConditionReached() override
Checks whether the maximum number of evaluations have been performed.
Definition: SimulatedAnnealing.h:197
float getTempVariation() const
Get the temperature variation constant for each generation.
Definition: SimulatedAnnealing.h:54
void setProblem(shared_ptr< Problem< S >> prob) override
Sets the problem to solve using Simulated Annealing.
Definition: SimulatedAnnealing.h:269
void createInitialPopulation() override
Creates the initial population for the SA. The population of the SA is just one individual.
Definition: SimulatedAnnealing.h:208
void updateProgress() override
Updates the evolutionary process by increasing the performedEvaluations in two.
Definition: SimulatedAnnealing.h:174
void setCurrentTemp(float currentTemp)
Set the current temperature of the process.
Definition: SimulatedAnnealing.h:74
SimulatedAnnealing(const int &maxEvaluations, const float &initialTemp, const float &tempVariation)
Construct a new Simulated Annealing object with all the parameters.
Definition: SimulatedAnnealing.h:114