5 #ifndef DIGNEA_PARALLELGENETICALGORITHM_H
6 #define DIGNEA_PARALLELGENETICALGORITHM_H
9 #include <dignea/evaluators/ParallelOMPEvaluator.h>
11 #include <dignea/utilities/random/ParallelPRNG.h>
40 return "Parallel Genetic Algorithm";
43 virtual void run()
override;
57 void setProblem(shared_ptr<
Problem<S>> prob)
override;
65 json to_json()
const override;
68 virtual void initProgress();
74 virtual void createInitialPopulation();
76 virtual void reproduction(S &, S &);
78 virtual void replacement(vector<S> &offsp){};
80 string getID()
const override {
return "ParGA"; };
83 S parallelSelection(
const int &init,
const int &end);
92 vector<S> individuals;
93 shared_ptr<Problem<S>> problem;
94 vector<float> bestEvolution;
107 this->evaluator = make_unique<ParallelOMPEvaluator<S>>(
numberOfCores);
116 this->individuals.resize(this->populationSize);
117 this->performedEvaluations = 0;
118 this->chunks = this->populationSize / this->numberOfCores;
119 omp_set_num_threads(this->numberOfCores);
132 const int popDim = this->populationSize;
133 this->individuals.resize(popDim);
134 #pragma omp parallel for shared(problem) num_threads(numberOfCores)
135 for (
int i = 0; i < popDim; i++) {
136 individuals[i] = problem->createSolution();
137 problem->evaluate(individuals[i]);
139 this->nextCheckpoint = 0;
140 this->initProgress();
151 this->performedEvaluations = this->populationSize;
166 this->configureEnv();
167 this->bestEvolution.clear();
168 this->startTime = chrono::system_clock::now();
169 this->createInitialPopulation();
170 this->runEvolution();
171 this->endTime = chrono::system_clock::now();
172 this->finishProgress();
174 throw std::runtime_error(
"Problem is not set in ParallelGA::run");
186 const float eps = 1e-9;
187 const int GENERATIONS = this->maxEvaluations / this->populationSize;
188 int performedGenerations = 0;
189 const int popDim = this->populationSize - 1;
191 vector<S> offspring(this->populationSize);
192 #pragma omp parallel for schedule(dynamic)
193 for (
int i = 0; i < this->populationSize; i++) {
198 int idx1 = min(prng.randInt(0, popDim), prng.randInt(0, popDim));
199 int idx2 = min(prng.randInt(0, popDim), prng.randInt(0, popDim));
200 S child1 = individuals[idx1];
201 S child2 = individuals[idx2];
202 this->reproduction(child1, child2);
203 this->problem->evaluate(child1);
206 double childPenalty = child1.getConstraintCoeff();
207 double individualPenalty = individuals[i].getConstraintCoeff();
208 double childFitness = child1.getFitness();
209 double individualFitness = individuals[i].getFitness();
212 if (childPenalty < individualPenalty) {
214 }
else if ((abs(childPenalty - individualPenalty) < eps) &&
215 (childFitness > individualFitness)) {
219 offspring[i] = child1;
222 offspring[i] = individuals[i];
226 #pragma omp parallel for schedule(dynamic)
227 for (
int i = 0; i < this->populationSize; i++) {
228 this->individuals[i] = offspring[i];
232 auto it = std::ranges::max_element(individuals, {}, &S::getFitness);
233 if (it != end(individuals)) {
234 const auto pos = std::distance(begin(individuals), it);
235 this->bestEvolution.push_back(individuals[pos].getFitness());
239 performedGenerations++;
240 }
while (performedGenerations < GENERATIONS);
243 this->population = this->individuals;
244 this->performedEvaluations = this->maxEvaluations;
254 if (prng.randDouble() < this->crossRate) {
255 this->crossover->run(child1, child2);
257 this->mutation->run(child1, this->mutationRate, problem.get());
273 int firstOption = prng.randInt(init, end);
274 int secondOption = prng.randInt(init, end);
275 S child = individuals[min(firstOption, secondOption)];
286 if (prob->getNumberOfObjs() != 1) {
287 std::string where =
"Parallel Genetic Algorithm::setProblem";
288 throw(NoMOAllowed(where));
290 this->problem = prob;
302 data[
"num_cores"] = this->numberOfCores;
304 data[
"evolution"] = this->bestEvolution;
Class to represent an Abstract Genetic Algorithm. Base skeleton is defined here, to extend in particu...
Definition: AbstractGA.h:38
json to_json() const override
Creates and returns JSON object with the GA information.
Definition: AbstractGA.h:463
Class to represents a Parallel Genetic Algorithm (ParGA). This algorithm runs the evolution in number...
Definition: ParallelGeneticAlgorithm.h:29
virtual void initProgress()
Starts the progress of the parallel algorithm Sets the performedEvaluations to the number of individu...
Definition: ParallelGeneticAlgorithm.h:150
void configureEnv()
Configures the parallel environment.
Definition: ParallelGeneticAlgorithm.h:115
virtual void reproduction(S &, S &)
Applies the genetic operators to the given individuals.
Definition: ParallelGeneticAlgorithm.h:253
virtual void createInitialPopulation()
Creates the initial population using parallel cores.
Definition: ParallelGeneticAlgorithm.h:131
json to_json() const override
Generates and returns a JSON representation of the ParallelGeneticAlgorithm.
Definition: ParallelGeneticAlgorithm.h:300
int numberOfCores
Definition: ParallelGeneticAlgorithm.h:88
string getID() const override
Returns the identificator of the algorithm, this is used in the to_json method. Must be implemented i...
Definition: ParallelGeneticAlgorithm.h:80
virtual void run() override
Run the parallel Algorithm.
Definition: ParallelGeneticAlgorithm.h:164
const Problem< S > * getProblem() const
Get a raw pointer to the problem to solve.
Definition: ParallelGeneticAlgorithm.h:63
ParallelPRNG prng
Definition: ParallelGeneticAlgorithm.h:90
void runEvolution()
Main method of the ParallelGeneticAlgorithm. Runs the evolution of the algorithm.
Definition: ParallelGeneticAlgorithm.h:185
virtual void updateProgress()
Method which updates the evolutionary progress. This method must be implemented in the subclasses and...
Definition: ParallelGeneticAlgorithm.h:70
virtual string getName() const override
Get the Name.
Definition: ParallelGeneticAlgorithm.h:39
ParallelGeneticAlgorithm()
Creates a default ParallelGeneticAlgorithm. Use the ParGABuilder class instead.
Definition: ParallelGeneticAlgorithm.h:104
S parallelSelection(const int &init, const int &end)
Parallel selection operator. This methods performs a binary tournament selection in the range [int,...
Definition: ParallelGeneticAlgorithm.h:271
int chunks
Definition: ParallelGeneticAlgorithm.h:89
int getNumberOfCores() const
Get the number of cores used.
Definition: ParallelGeneticAlgorithm.h:49
void setNumberOfCores(int nCores)
Set the number of cores to use.
Definition: ParallelGeneticAlgorithm.h:55
void setProblem(shared_ptr< Problem< S >> prob) override
Sets the problem to solve. Receives a share_ptr.
Definition: ParallelGeneticAlgorithm.h:285
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29