-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.cpp
46 lines (40 loc) · 1 KB
/
population.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
#include "population.hpp"
Population::Population() {
id_best = -1;
}
void Population::addCromossome(Cromossome c) {
pool.push_back(c);
if (id_best == -1 || c.getScore() > pool[id_best].getScore()) {
id_best = pool.size() - 1;
}
}
Cromossome Population::getCromossome(int id) {
return pool[id];
}
int Population::getIdBest() {
return id_best;
}
void Population::removeWorst() {
double mini = pool[0].getScore();
int id = 0;
for (int i = 1; i < pool.size(); i++) {
if (pool[i].getScore() < mini) {
mini = pool[i].getScore();
id = i;
}
}
swap(pool[id], pool[pool.size() - 1]);
pool.pop_back();
double maxi = pool[0].getScore();
id_best = 0;
for (int i = 1; i < pool.size(); i++) {
if (pool[i].getScore() > maxi) {
maxi = pool[i].getScore();
id_best = i;
}
}
}
void Population::reset() {
id_best = -1;
pool.clear();
}