Use references to pass arguments, to reduce memory allocations and copying around.
[PaGMO.git] / GOclasses / basic / population.cpp
blob060c7c22b7beaf466f22fff7ad5b793abbf9fd39
1 /*
2 * population.cpp
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.
8 */
10 #include <cmath>
12 #include "population.h"
13 #include "rng.h"
15 void Population::createRandomPopulation(const std::vector<double> &LB, const std::vector<double> &UB, int N, rng_double_type &drng){
16 Individual x;
17 pop.clear();
19 for (int i=0; i < N; i++){
20 x.createRandomIndividual(LB,UB, drng);
21 pop.push_back(x);
22 }//for
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){
39 pop.push_back(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 {
49 return pop.size();
52 Individual Population::extractBestIndividual() const {
54 double f = pop[0].getFitness();
55 int index = 0;
57 for (unsigned int i=1; i<pop.size();i++){
58 if( pop[i].getFitness() < f ){
59 index=i;
60 f = pop[i].getFitness();
63 return pop[index];
66 Individual Population::extractWorstIndividual() const {
68 double f = pop[0].getFitness();
69 int index = 0;
71 for (unsigned int i=1; i<pop.size();i++){
72 if( pop[i].getFitness() > f ){
73 index=i;
74 f = pop[i].getFitness();
77 return pop[index];
80 Population Population::extractRandomDeme(int N, std::vector<int> &picks, rng_double_type &drng){
81 Population deme;
82 std::vector<int> PossiblePicks;
83 int Pick;
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());
92 //and store it
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);
99 return deme;
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();
119 int bestindeme = 0;
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){
126 bestindeme = i;
127 best = deme[i].getFitness();
129 if ( pop[picks[i]].getFitness() > worst) {
130 worstinpicks = i;
131 worst = pop[picks[i]].getFitness();
134 pop[picks[worstinpicks]] = deme[bestindeme];
138 double Population::evaluateMean() const {
139 double mean=0;
140 int size = 0;
141 size = pop.size();
142 for (int i=0; i<size; i++){
143 mean=mean+pop[i].getFitness();
145 mean = mean/(double)size;
146 return mean;
149 double Population::evaluateStd() const {
150 double Std=0,mean=0;
151 int size = 0;
152 size = pop.size();
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);
159 return Std;
162 Individual &Population::operator[](int index){
163 return pop[index];
166 const Individual &Population::operator[](int index) const{
167 return pop[index];
170 void Population::operator=(const Population &newpop){
171 pop.clear();
172 for (unsigned int i=0 ; i<newpop.size(); i++){
173 pop.push_back(newpop[i]);
177 void Population::operator=(const Individual &x){
178 pop.clear();
179 pop.push_back(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;
186 return s;