OPAL (Object Oriented Parallel Accelerator Library)  2024.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 <memory>
32 #include <vector>
33 #include <utility>
34 #include <queue>
35 #include <set>
36 #include <cmath>
37 #include <fstream>
38 #include <sstream>
39 
41 
42 
43 template< class Individual_t >
44 class Population {
45 
46 public:
48  last_identity = 0;
49  }
50 
52 
53  typedef typename Individual_t::genes_t genes_t;
54  typedef std::shared_ptr<Individual_t> individual;
55  typedef std::pair< unsigned int, individual > ind_t;
56 
59 
65  unsigned int add_individual(individual ind) {
66 
67  unsigned int id = getFreeID();
68  stagingArea.insert(ind_t(id, ind));
69  ind->id_m = id;
70  //std::cout << "+++ staging id = " << id << "\xd";
71  return id;
72  }
73 
75 
76  indItr_t it = stagingArea.begin();
77  while(it != stagingArea.end()) {
78  if(it->second == ind) {
79  if(it->first == last_identity-1)
80  last_identity--;
81  else
82  freeids.push(it->first);
83 
84  //std::cout << "--- removing id = " << it->first << "\xd";
85  stagingArea.erase(it);
86  break;
87  }
88  it++;
89  }
90  }
91 
98  individual get_individual(int identity) {
99  indItr_t it;
100  if(identity == -1)
101  it = individuals.begin();
102  else {
103  it = individuals.find(identity);
104  if( it == individuals.end() )
105  return individual();
106  }
107 
108  return it->second;
109  }
110 
117  individual get_staging(int identity) {
118  indItr_t it;
119  if(identity == -1)
120  it = stagingArea.begin();
121  else {
122  it = stagingArea.find(identity);
123  if( it == stagingArea.end() )
124  return individual();
125  }
126 
127  return it->second;
128  }
129 
130 
131  void commit_individuals(std::set<unsigned int> ids) {
132 
133  for (unsigned int id : ids) {
134  //std::cout << "--+ committing id = " << id << "\xd";
135  individual ind = get_staging(id);
136  individuals.insert(ind_t(id, ind));
137  stagingArea.erase(id);
138  }
139  }
140 
146  void keepSurvivors(std::set<unsigned int> survivors) {
147 
148  indItr_t it = individuals.begin();
149  while(it != individuals.end()) {
150  if( survivors.count(it->first) == 0 ) {
151  if(it->first == last_identity-1)
152  last_identity--;
153  else
154  freeids.push(it->first);
155 
156  individuals.erase(it++);
157  } else
158  it++;
159  }
160  }
161 
162 
164  //XXX: currently O(n): add a fast look-up table?
166 
167  for(ind_t ind : individuals) {
168  if( ind_genes == ind.second->genes_m )
169  return true;
170  }
171 
172  return false;
173  }
174 
175 
176  double computeHypervolume(size_t island_id, const std::vector<double>& referencePoint) {
177  // protection check
178  if (individuals.empty() == true) return -1;
179 
180  std::ofstream file;
181  std::ostringstream filename;
182  filename << "hypervol.dat_" << island_id;
183  file.open(filename.str().c_str(), std::ios::out);
184 
185  file << "#" << std::endl;
186 
187  indItr_t it;
188  for(it = individuals.begin(); it != individuals.end(); it++) {
189 
190  individual temp = it->second;
191  for(size_t i=0; i<temp->objectives_m.size(); i++)
192  file << temp->objectives_m[i] << " ";
193  if (!temp->objectives_m.empty())
194  file << std::endl;
195  }
196 
197  file << "#" << std::endl;
198 
199  file.flush();
200  file.close();
201 
202  return Hypervolume::FromFile(filename.str(), referencePoint);
203  }
204 
205 
207  individuals.insert(stagingArea.begin(), stagingArea.end());
208  stagingArea.clear();
209  }
210 
211 
213  indItr_t stagingBegin() { return stagingArea.begin(); }
215  indItr_t stagingEnd() { return stagingArea.end(); }
216 
217 
222  unsigned int size() const { return individuals.size(); }
223 
225  indItr_t begin() { return individuals.begin(); }
227  indItr_t end() { return individuals.end(); }
229  indItr_t erase(indItr_t it) { return individuals.erase(it); }
230 
231 private:
232 
234  std::map<unsigned int, individual > individuals;
235 
237  std::map<unsigned int, individual > stagingArea;
238 
240  std::queue<unsigned int> freeids;
241 
243  unsigned int last_identity;
244 
249  unsigned int getFreeID() {
250 
251  unsigned int id = 0;
252  if(freeids.empty()) {
253  id = last_identity;
254  last_identity++;
255  } else {
256  id = freeids.front();
257  freeids.pop();
258  }
259 
260  return id;
261  }
262 };
263 
264 #endif
individual get_staging(int identity)
Definition: Population.h:117
std::shared_ptr< Individual_t > individual
Definition: Population.h:54
indItr_t stagingBegin()
iterator begin on staging area
Definition: Population.h:213
std::map< unsigned int, individual > individuals
population container holding all individuals
Definition: Population.h:234
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
indItr_t end()
iterator end on population container
Definition: Population.h:227
unsigned int add_individual(individual ind)
Definition: Population.h:65
indItr_t begin()
iterator begin on population container
Definition: Population.h:225
std::queue< unsigned int > freeids
queue to handle free individual IDs
Definition: Population.h:240
unsigned int getFreeID()
Definition: Population.h:249
indItr_t erase(indItr_t it)
erase individual
Definition: Population.h:229
void commit_individuals(std::set< unsigned int > ids)
Definition: Population.h:131
bool isRepresentedInPopulation(genes_t ind_genes)
check if a gene set is already represented in the population
Definition: Population.h:165
std::pair< unsigned int, individual > ind_t
Definition: Population.h:55
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
indItr_t stagingEnd()
iterator end on staging area
Definition: Population.h:215
std::string::iterator iterator
Definition: MSLang.h:15
double FromFile(std::string file, const std::vector< double > &referencePoint)
unsigned int last_identity
last used (= next free) ID
Definition: Population.h:243
void commit_individuals()
Definition: Population.h:206
void flush()
Definition: Inform.h:114
void keepSurvivors(std::set< unsigned int > survivors)
Definition: Population.h:146
double computeHypervolume(size_t island_id, const std::vector< double > &referencePoint)
Definition: Population.h:176
void remove_individual(individual ind)
Definition: Population.h:74
unsigned int size() const
Definition: Population.h:222
individual get_individual(int identity)
Definition: Population.h:98
std::map< unsigned int, individual > stagingArea
staging area for individuals probably joining population
Definition: Population.h:237
Individual_t::genes_t genes_t
Definition: Population.h:53
std::map< unsigned int, individual >::iterator indItr_t
population iterator type
Definition: Population.h:58