OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Population.h
Go to the documentation of this file.
1//
2// Class Population
3// Managing a population of individuals. We maintain two sets: a set of all
4// (evaluated) individuals in the population and a set of new potential
5// individuals (the selector decides which individuals join the population),
6// called 'stagingArea'.
7// Most operations work on the 'stagingArea', population is kept for
8// visualization purposes.
9//
10// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
11// All rights reserved
12//
13// Implemented as part of the PhD thesis
14// "Toward massively parallel multi-objective optimization with application to
15// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
16//
17// This file is part of OPAL.
18//
19// OPAL is free software: you can redistribute it and/or modify
20// it under the terms of the GNU General Public License as published by
21// the Free Software Foundation, either version 3 of the License, or
22// (at your option) any later version.
23//
24// You should have received a copy of the GNU General Public License
25// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
26//
27#ifndef __POPULATION_H__
28#define __POPULATION_H__
29
30#include <map>
31#include <vector>
32#include <utility>
33#include <queue>
34#include <set>
35#include <cmath>
36#include <fstream>
37#include <sstream>
38
39#include "boost/smart_ptr.hpp"
40
42
43
44template< class Individual_t >
46
47public:
49 last_identity = 0;
50 }
51
53
54 typedef typename Individual_t::genes_t genes_t;
55 typedef boost::shared_ptr<Individual_t> individual;
56 typedef std::pair< unsigned int, individual > ind_t;
57
60
66 unsigned int add_individual(individual ind) {
67
68 unsigned int id = getFreeID();
69 stagingArea.insert(ind_t(id, ind));
70 ind->id_m = id;
71 //std::cout << "+++ staging id = " << id << "\xd";
72 return id;
73 }
74
76
77 indItr_t it = stagingArea.begin();
78 while(it != stagingArea.end()) {
79 if(it->second == ind) {
80 if(it->first == last_identity-1)
82 else
83 freeids.push(it->first);
84
85 //std::cout << "--- removing id = " << it->first << "\xd";
86 stagingArea.erase(it);
87 break;
88 }
89 it++;
90 }
91 }
92
99 individual get_individual(int identity) {
100 indItr_t it;
101 if(identity == -1)
102 it = individuals.begin();
103 else {
104 it = individuals.find(identity);
105 if( it == individuals.end() )
106 return individual();
107 }
108
109 return it->second;
110 }
111
118 individual get_staging(int identity) {
119 indItr_t it;
120 if(identity == -1)
121 it = stagingArea.begin();
122 else {
123 it = stagingArea.find(identity);
124 if( it == stagingArea.end() )
125 return individual();
126 }
127
128 return it->second;
129 }
130
131
132 void commit_individuals(std::set<unsigned int> ids) {
133
134 for (unsigned int id : ids) {
135 //std::cout << "--+ committing id = " << id << "\xd";
136 individual ind = get_staging(id);
137 individuals.insert(ind_t(id, ind));
138 stagingArea.erase(id);
139 }
140 }
141
147 void keepSurvivors(std::set<unsigned int> survivors) {
148
149 indItr_t it = individuals.begin();
150 while(it != individuals.end()) {
151 if( survivors.count(it->first) == 0 ) {
152 if(it->first == last_identity-1)
154 else
155 freeids.push(it->first);
156
157 individuals.erase(it++);
158 } else
159 it++;
160 }
161 }
162
163
165 //XXX: currently O(n): add a fast look-up table?
167
168 for(ind_t ind : individuals) {
169 if( ind_genes == ind.second->genes_m )
170 return true;
171 }
172
173 return false;
174 }
175
176
177 double computeHypervolume(size_t island_id, const std::vector<double>& referencePoint) {
178 // protection check
179 if (individuals.empty() == true) return -1;
180
181 std::ofstream file;
182 std::ostringstream filename;
183 filename << "hypervol.dat_" << island_id;
184 file.open(filename.str().c_str(), std::ios::out);
185
186 file << "#" << std::endl;
187
188 indItr_t it;
189 for(it = individuals.begin(); it != individuals.end(); it++) {
190
191 individual temp = it->second;
192 for(size_t i=0; i<temp->objectives_m.size(); i++)
193 file << temp->objectives_m[i] << " ";
194 if (!temp->objectives_m.empty())
195 file << std::endl;
196 }
197
198 file << "#" << std::endl;
199
200 file.flush();
201 file.close();
202
203 return Hypervolume::FromFile(filename.str(), referencePoint);
204 }
205
206
208 individuals.insert(stagingArea.begin(), stagingArea.end());
209 stagingArea.clear();
210 }
211
212
214 indItr_t stagingBegin() { return stagingArea.begin(); }
216 indItr_t stagingEnd() { return stagingArea.end(); }
217
218
223 unsigned int size() const { return individuals.size(); }
224
226 indItr_t begin() { return individuals.begin(); }
228 indItr_t end() { return individuals.end(); }
230 indItr_t erase(indItr_t it) { return individuals.erase(it); }
231
232private:
233
235 std::map<unsigned int, individual > individuals;
236
238 std::map<unsigned int, individual > stagingArea;
239
241 std::queue<unsigned int> freeids;
242
244 unsigned int last_identity;
245
250 unsigned int getFreeID() {
251
252 unsigned int id = 0;
253 if(freeids.empty()) {
254 id = last_identity;
256 } else {
257 id = freeids.front();
258 freeids.pop();
259 }
260
261 return id;
262 }
263};
264
265#endif
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string::iterator iterator
Definition: MSLang.h:16
double FromFile(std::string file, const std::vector< double > &referencePoint)
void flush()
Definition: Inform.h:114
std::map< unsigned int, individual > stagingArea
staging area for individuals probably joining population
Definition: Population.h:238
unsigned int getFreeID()
Definition: Population.h:250
indItr_t erase(indItr_t it)
erase individual
Definition: Population.h:230
std::pair< unsigned int, individual > ind_t
Definition: Population.h:56
unsigned int size() const
Definition: Population.h:223
std::map< unsignedint, individual >::iterator indItr_t
population iterator type
Definition: Population.h:59
boost::shared_ptr< Individual_t > individual
Definition: Population.h:55
double computeHypervolume(size_t island_id, const std::vector< double > &referencePoint)
Definition: Population.h:177
Individual_t::genes_t genes_t
Definition: Population.h:54
std::map< unsigned int, individual > individuals
population container holding all individuals
Definition: Population.h:235
void commit_individuals()
Definition: Population.h:207
bool isRepresentedInPopulation(genes_t ind_genes)
check if a gene set is already represented in the population
Definition: Population.h:166
individual get_individual(int identity)
Definition: Population.h:99
unsigned int add_individual(individual ind)
Definition: Population.h:66
void remove_individual(individual ind)
Definition: Population.h:75
unsigned int last_identity
last used (= next free) ID
Definition: Population.h:244
indItr_t stagingBegin()
iterator begin on staging area
Definition: Population.h:214
std::queue< unsigned int > freeids
queue to handle free individual IDs
Definition: Population.h:241
indItr_t end()
iterator end on population container
Definition: Population.h:228
void commit_individuals(std::set< unsigned int > ids)
Definition: Population.h:132
indItr_t begin()
iterator begin on population container
Definition: Population.h:226
indItr_t stagingEnd()
iterator end on staging area
Definition: Population.h:216
individual get_staging(int identity)
Definition: Population.h:118
void keepSurvivors(std::set< unsigned int > survivors)
Definition: Population.h:147