3 * SeGMO, a Sequential Global Multiobjective Optimiser
5 * Created by Dario Izzo on 5/16/08.
6 * Copyright 2008 ¿dvanced Concepts Team (European Space Agency). All rights reserved.
12 #include "population.h"
15 void Population::createRandomPopulation(const std::vector
<double> &LB
, const std::vector
<double> &UB
, int N
, rng_double_type
&drng
){
19 for (int i
=0; i
< N
; i
++){
20 x
.createRandomIndividual(LB
,UB
, drng
);
23 };//createRandomPopulation
25 void Population::resetVelocities(const std::vector
<double> &LB
, const std::vector
<double> &UB
, rng_double_type
&drng
){
26 for (unsigned int j
=0 ;j
<pop
.size();j
++){
27 pop
[j
].resetVelocity(LB
,UB
, drng
);
32 void Population::evaluatePopulation(GOProblem
&problem
){
34 for (unsigned int i
=0; i
< pop
.size(); i
++)
35 pop
[i
].evaluateFitness(problem
);
36 };//evaluatePopulation
38 void Population::addIndividual(const Individual
&x
){
42 void Population::substituteIndividual(const Individual
&x
, int n
){
43 pop
[n
].setDecisionVector(x
.getDecisionVector());
44 pop
[n
].setVelocity(x
.getVelocity());
45 pop
[n
].setFitness(x
.getFitness());
48 unsigned int Population::size() const {
52 Individual
Population::extractBestIndividual() const {
54 double f
= pop
[0].getFitness();
57 for (unsigned int i
=1; i
<pop
.size();i
++){
58 if( pop
[i
].getFitness() < f
){
60 f
= pop
[i
].getFitness();
66 Individual
Population::extractWorstIndividual() const {
68 double f
= pop
[0].getFitness();
71 for (unsigned int i
=1; i
<pop
.size();i
++){
72 if( pop
[i
].getFitness() > f
){
74 f
= pop
[i
].getFitness();
80 Population
Population::extractRandomDeme(int N
, std::vector
<int> &picks
, rng_double_type
&drng
){
82 std::vector
<int> PossiblePicks
;
85 for (unsigned int i
=0; i
< pop
.size(); i
++)
86 PossiblePicks
.push_back(i
);
89 for (int i
=0; i
< N
; i
++){
90 //we pick a random position between 0 and popsize-1
91 Pick
= (int)(drng() * PossiblePicks
.size());
93 picks
.push_back(PossiblePicks
[Pick
]);
94 //we insert the corresponding individual in the deme
95 deme
.addIndividual(pop
[PossiblePicks
[Pick
]]);
96 //and erase it from the possible picks
97 PossiblePicks
.erase(PossiblePicks
.begin() + Pick
);
102 void Population::insertDeme(const Population
&deme
, const std::vector
<int> &picks
){
103 for (unsigned int i
=0; i
<picks
.size(); i
++){
104 if ( deme
[i
].getFitness() < pop
[picks
[i
]].getFitness() ){
105 pop
[picks
[i
]] = deme
[i
];
110 void Population::insertDemeForced(const Population
&deme
, const std::vector
<int> &picks
){
111 for (unsigned int i
=0; i
<picks
.size(); i
++){
112 pop
[picks
[i
]] = deme
[i
];
116 void Population::insertBestInDeme(const Population
&deme
, const std::vector
<int> &picks
){
117 const int Ndeme
= deme
.size();
120 int worstinpicks
= 0;
121 double best
= deme
[0].getFitness();
122 double worst
= pop
[picks
[0]].getFitness();
124 for (int i
=1; i
<Ndeme
; i
++){
125 if ( deme
[i
].getFitness() < best
){
127 best
= deme
[i
].getFitness();
129 if ( pop
[picks
[i
]].getFitness() > worst
) {
131 worst
= pop
[picks
[i
]].getFitness();
134 pop
[picks
[worstinpicks
]] = deme
[bestindeme
];
138 double Population::evaluateMean() const {
142 for (int i
=0; i
<size
; i
++){
143 mean
=mean
+pop
[i
].getFitness();
145 mean
= mean
/(double)size
;
149 double Population::evaluateStd() const {
153 mean
= evaluateMean();
155 for (int i
=0; i
<size
; i
++){
156 Std
=Std
+pow((pop
[i
].getFitness()-mean
),2.0);
158 Std
= sqrt(Std
/size
);
162 Individual
&Population::operator[](int index
){
166 const Individual
&Population::operator[](int index
) const{
170 void Population::operator=(const Population
&newpop
){
172 for (unsigned int i
=0 ; i
<newpop
.size(); i
++){
173 pop
.push_back(newpop
[i
]);
177 void Population::operator=(const Individual
&x
){
182 std::ostream
& operator<<(std::ostream
& s
, Population
& pop
){
183 for (unsigned int i
=0;i
<pop
.size(); i
++){
184 s
<< "Individual #" << i
<< ": " << pop
[i
].getFitness() << " " << pop
[i
] << std::endl
;