-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.cpp
149 lines (113 loc) · 3.99 KB
/
ga.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* GeneticAlgorithm.cpp
*
* Created on: 26-nov-2008
* Author: rafael
*/
#include "ga.h"
log4cxx::LoggerPtr GA::logger(log4cxx::Logger::getLogger("ga"));
GA::GA(Problem * problem, OperatorFactory * operatorFactory, Config * config,
Output * output) {
LOG4CXX_DEBUG(logger, "Creating a new GA.");
this->problem = problem;
this->generation = 0;
this->config = config;
this->output = output;
this->population = nullptr;
this->num_parents = config->getInt("NumParents");
// Build the operators
this->parentSelection = operatorFactory->createParentSelectionOperator();
this->replacementSelection =
operatorFactory->createReplacementSelectionOperator();
this->mutation = operatorFactory->createMutationOperator();
this->crossover = operatorFactory->createCrossoverOperator();
this->evaluation = operatorFactory->createEvaluationOperator();
this->individual_creator = operatorFactory->createIndividualCreator();
}
GA::~GA() {
}
void GA::init() {
LOG4CXX_DEBUG(logger, "Initializing population of the GA.");
int num_individuals = config->getInt(POPULATION_SIZE_PARAM);
int num_genes = config->getInt(NUMBER_OF_GENES_PARAM);
IContainer individuals;
LOG4CXX_TRACE(logger, "Create "<<num_individuals<<" of size "<<num_genes<<".");
for (int i = 0; i < num_individuals; i++) {
individuals.push_back(individual_creator(problem, 0));
}
LOG4CXX_TRACE(logger, "Ind created.");
// evaluate the individuals using the evaluation operator.
// Evaluation TODO how to add openCL here?
this->evaluation(problem, &individuals);
population = new Population();
population->add(&individuals);
LOG4CXX_DEBUG(logger, "Pop initialized.");
}
void GA::evolve(int generations) {
if (population == nullptr) {
throw invalid_argument("GA not initialized yet.");
}
LOG4CXX_TRACE(logger, "Running for "<<generations<<".");
for (int i = 0; i < generations; i++) {
// Selection
t0 = chrono::steady_clock::now();
IContainer * parents = this->parentSelection(population, num_parents);
t1 = chrono::steady_clock::now();
output->selection(chrono::duration<double, milli>(t1 - t0).count());
// Crossover
t0 = chrono::steady_clock::now();
IContainer * offspring = this->crossover(parents, generation);
t1 = chrono::steady_clock::now();
output->crossover(chrono::duration<double, milli>(t1 - t0).count());
// Mutation
t0 = chrono::steady_clock::now();
this->mutation(offspring);
t1 = chrono::steady_clock::now();
output->mutation(chrono::duration<double, milli>(t1 - t0).count());
// Evaluation TODO how to add openCL here?
t0 = chrono::steady_clock::now();
this->evaluation(problem, offspring);
t1 = chrono::steady_clock::now();
output->eval(chrono::duration<double, milli>(t1 - t0).count());
// Replacement selection
t0 = chrono::steady_clock::now();
IContainer * notSurvivors = this->replacementSelection(population,
offspring);
// remove the memory consumed by the removed individuals
this->getPopulation()->remove(notSurvivors);
t1 = chrono::steady_clock::now();
output->replacement(chrono::duration<double, milli>(t1 - t0).count());
// print the current status
this->output->print_generation(generation, population);
// clean temporary containers
delete parents;
delete offspring;
for (IContainer::iterator it = notSurvivors->begin();
it != notSurvivors->end(); ++it) {
delete (*it);
}
delete notSurvivors;
// end
this->generation++;
// is written this way because the macro will add an if
LOG4CXX_TRACE(logger,
"Generation: "<<this->generation<<". Best: " << population->best() << " ["<<problem->decode(population->best())<<"]");
}
LOG4CXX_DEBUG(logger,
"Generation: "<<this->generation<<". Best: " << population->best());
}
Population * GA::getPopulation() {
return this->population;
}
Individual* GA::best() {
return population->best();
}
Individual* GA::worst() {
return population->worst();
}
double GA::mean_fitness() {
return population->mean_fitness();
}
double GA::total_fitness() {
return population->total_fitness();
}