-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.cpp
124 lines (101 loc) · 2.93 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
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
/*
* Population.cpp
*
* Created on: 26-nov-2008
* Author: rafael
*/
#include "Population.h"
log4cxx::LoggerPtr Population::logger(log4cxx::Logger::getLogger("pupulation"));
Population::Population() {
}
Population::~Population() {
while (this->individuals.size() > 0) {
multiset<Individual*>::iterator it = this->individuals.begin();
this->individuals.erase(it);
delete *it;
}
}
IContainer * Population::get_individuals() {
IContainer * container = new IContainer(individuals.begin(),
individuals.end());
return container;
}
void required_not_null(IContainer * group, const char * msg) {
if (group == nullptr) {
throw invalid_argument(msg);
}
}
/*
* Remove fron the current population the set of individuals passed as
* argument. Each object is just removed from the internal structure but its
* memory is not liberated.
*
* @param individuals The set of individuals to remove from population.
*/
void Population::remove(IContainer * group) {
required_not_null(group, "null cant be removed from population. ");
// cnat remove using erase because it uses key_comp
// we just want to remove specific pointers
std::for_each(group->begin(), group->end(), [this](Individual * i) {
// fist we need to locate the exact pointer
pair<It, It> r = individuals.equal_range(i);
while (*r.first != i && r.first != r.second) {
r.first ++;
}
if (*r.first == i) {
individuals.erase(r.first);
}
});
}
/*
* Add a set of individuals to the current population. The individuals will be reevaluated
* when they are added to the population.
* @param individuals The set of individuals to add.
*/
void Population::add(IContainer * group) {
required_not_null(group, "null cant be added to population. ");
std::for_each(group->begin(), group->end(), [this](Individual * i) {
individuals.insert(i);
});
}
Individual* Population::best() {
return (*this->individuals.rbegin());
}
Individual* Population::worst() {
return (*this->individuals.begin());
}
IContainer * Population::worsts(int n) {
It it = individuals.begin();
std::advance(it, n);
return new IContainer(this->individuals.begin(), it);
}
IContainer * Population::bests(int n) {
multiset<Individual*>::reverse_iterator it = individuals.rbegin();
std::advance(it, n);
return new IContainer(it.base(), individuals.end());
}
double Population::mean_fitness() {
return total_fitness() / individuals.size();
}
double Population::total_fitness() {
double sum = 0;
for (It it = individuals.begin(); it != individuals.end(); ++it) {
sum += (*it)->fitness();
}
return sum;
}
double Population::stdev_fitness(double mean) {
double sum = 0;
for (It it = individuals.begin(); it != individuals.end(); ++it) {
sum += pow((*it)->fitness()- mean, 2);
}
return std::sqrt(sum / (individuals.size()-1));
}
unsigned int Population::size() {
return individuals.size();
}
Individual * Population::at(int i) {
It it = individuals.begin();
std::advance(it, i);
return *it;
}