PaGMO  1.1.5
population.h
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #ifndef PAGMO_POPULATION_H
26 #define PAGMO_POPULATION_H
27 
28 #include <cstddef>
29 #include <iostream>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 #include "config.h"
35 #include "problem/base.h"
36 #include "rng.h"
37 #include "serialization.h"
38 #include "types.h"
39 
40 namespace pagmo
41 {
42 
43 // Forward declarations.
44 class base_island;
45 struct population_access;
46 
47 namespace algorithm {
48 class base;
49 typedef boost::shared_ptr<base> base_ptr;
50 }
51 
53 
70 class __PAGMO_VISIBLE population
71 {
72  friend class base_island;
73  friend struct population_access;
74  public:
76 
81  {
97 
100  std::string human_readable() const
101  {
102  std::ostringstream oss;
103  oss << "\tDecision vector:\t\t" << cur_x << '\n';
104  oss << "\tVelocity vector:\t\t" << cur_v << '\n';
105  oss << "\tConstraint vector:\t\t" << cur_c << '\n';
106  oss << "\tFitness vector:\t\t\t" << cur_f << '\n';
107  oss << "\tBest decision vector:\t\t" << best_x << '\n';
108  oss << "\tBest constraint vector:\t\t" << best_c << '\n';
109  oss << "\tBest fitness vector:\t\t" << best_f << '\n';
110  return oss.str();
111  }
112  private:
113  friend class boost::serialization::access;
114  template <class Archive>
115  void save(Archive &ar, const unsigned int version) const
116  {
117  custom_vector_double_save(ar,cur_x,version);
118  custom_vector_double_save(ar,cur_v,version);
119  custom_vector_double_save(ar,cur_c,version);
120  custom_vector_double_save(ar,cur_f,version);
121  custom_vector_double_save(ar,best_x,version);
122  custom_vector_double_save(ar,best_c,version);
123  custom_vector_double_save(ar,best_f,version);
124  }
125  template <class Archive>
126  void load(Archive &ar, const unsigned int version)
127  {
128  custom_vector_double_load(ar,cur_x,version);
129  custom_vector_double_load(ar,cur_v,version);
130  custom_vector_double_load(ar,cur_c,version);
131  custom_vector_double_load(ar,cur_f,version);
132  custom_vector_double_load(ar,best_x,version);
133  custom_vector_double_load(ar,best_c,version);
134  custom_vector_double_load(ar,best_f,version);
135  }
136  template <class Archive>
137  void serialize(Archive &ar, const unsigned int version)
138  {
139  boost::serialization::split_member(ar,*this,version);
140  }
141  };
143 
147  {
155 
158  std::string human_readable() const
159  {
160  std::ostringstream oss;
161  oss << "\tDecision vector:\t" << x << '\n';
162  oss << "\tConstraints vector:\t" << c << '\n';
163  oss << "\tFitness vector:\t\t" << f << '\n';
164  return oss.str();
165  }
166  private:
167  friend class boost::serialization::access;
168  template <class Archive>
169  void save(Archive &ar, const unsigned int version) const
170  {
171  custom_vector_double_save(ar,x,version);
172  custom_vector_double_save(ar,c,version);
173  custom_vector_double_save(ar,f,version);
174  }
175  template <class Archive>
176  void load(Archive &ar, const unsigned int version)
177  {
178  custom_vector_double_load(ar,x,version);
179  custom_vector_double_load(ar,c,version);
180  custom_vector_double_load(ar,f,version);
181  }
182  template <class Archive>
183  void serialize(Archive &ar, const unsigned int version)
184  {
185  boost::serialization::split_member(ar,*this,version);
186  }
187  };
189  typedef std::vector<individual_type> container_type;
190 
192  typedef container_type::size_type size_type;
193 
195  typedef container_type::const_iterator const_iterator;
196  explicit population(const problem::base &, int = 0, const boost::uint32_t &seed = getSeed());
197  static boost::uint32_t getSeed(){
198  return rng_generator::get<rng_uint32>()();
199  }
200  population(const population &);
201  population &operator=(const population &);
202  const individual_type &get_individual(const size_type &) const;
203 
204  // Multi-Objective stuff
205  const std::vector<size_type> &get_domination_list(const size_type &) const;
206  size_type get_domination_count(const size_type &) const;
207  size_type get_pareto_rank(const size_type &) const;
208  double get_crowding_d(const size_type &) const;
209  void update_pareto_information() const;
210  size_type n_dominated(const individual_type &) const;
211  std::vector<std::vector<size_type> > compute_pareto_fronts() const;
212  fitness_vector compute_ideal() const;
213  fitness_vector compute_nadir() const;
214 
215  const problem::base &problem() const;
216  const champion_type &champion() const;
217  std::string human_readable_terse() const;
218  std::string human_readable() const;
219  size_type get_best_idx() const;
220  std::vector<size_type> get_best_idx(const size_type & N) const;
221  size_type get_worst_idx() const;
222  void set_x(const size_type &, const decision_vector &);
223  void set_v(const size_type &, const decision_vector &);
224  void push_back(const decision_vector &);
225  void erase(const size_type &);
226  size_type size() const;
227  const_iterator begin() const;
228  const_iterator end() const;
229 
230  void reinit(const size_type &);
231  void reinit();
232  void clear();
233  double mean_velocity() const;
234 
235  // Constraints repairing methods
236  void repair(const size_type &, const algorithm::base_ptr &);
237 
238  // Race routine wrappers
239  std::pair<std::vector<population::size_type>, unsigned int> race(const size_type n_final,
240  const unsigned int min_trials = 0,
241  const unsigned int max_count = 1000,
242  double delta = 0.05,
243  const std::vector<size_type>& = std::vector<size_type>(),
244  const bool race_best = true,
245  const bool screen_output = false) const;
246 
247  struct crowded_comparison_operator {
248  crowded_comparison_operator(const population &);
249  bool operator()(const individual_type &i1, const individual_type &i2) const;
250  bool operator()(const size_type &idx1, const size_type &idx2) const;
251  const population &m_pop;
252  };
253 
254  struct trivial_comparison_operator {
255  trivial_comparison_operator(const population &);
256  bool operator()(const individual_type &i1, const individual_type &i2) const;
257  bool operator()(const size_type &idx1, const size_type &idx2) const;
258  const population &m_pop;
259  };
260 
261  private:
262  void init_velocity(const size_type &);
263  void update_champion(const size_type &);
264 
265  // Multi-objective stuff
266  void update_crowding_d(std::vector<size_type>) const;
267 
268  protected:
269  void update_dom(const size_type &);
270 
271  private:
272  // Data members + their serialization
273  friend class boost::serialization::access;
274  template <class Archive>
275  void serialize(Archive &ar, const unsigned int)
276  {
277  ar & m_prob;
278  ar & m_container;
279  ar & m_dom_list;
280  ar & m_dom_count;
281  ar & m_pareto_rank;
282  ar & m_crowding_d;
283  ar & m_champion;
284  ar & m_drng;
285  ar & m_urng;
286  }
287  // Problem.
288  problem::base_ptr m_prob;
289  protected:
290  // Container of individuals. Needs to be protected so that a derived class can override
291  // the set_x mechanism avoiding function re-evaluations. (use this option at your own risk)
292  container_type m_container;
293  // List of dominated individuals.
294  std::vector<std::vector<size_type> > m_dom_list;
295  // Domination Count (number of dominant individuals)
296  std::vector<size_type> m_dom_count;
297  private:
298  // Population champion.
299  champion_type m_champion;
300  // Pareto rank
301  mutable std::vector<size_type> m_pareto_rank;
302  // Crowding distance
303  mutable std::vector<double> m_crowding_d;
304  // Double precision random number generator.
305  mutable rng_double m_drng;
306  // uint32 random number generator.
307  mutable rng_uint32 m_urng;
308 };
309 
310 // Streaming operator for the population
311 __PAGMO_VISIBLE_FUNC std::ostream &operator<<(std::ostream &, const population &);
312 // Streaming operator for the individual
313 __PAGMO_VISIBLE_FUNC std::ostream &operator<<(std::ostream &, const population::individual_type &);
314 // Streaming operator for the champion
315 __PAGMO_VISIBLE_FUNC std::ostream &operator<<(std::ostream &, const population::champion_type &);
316 
317 struct __PAGMO_VISIBLE population_access
318 {
319  static problem::base_ptr &get_problem_ptr(population &);
320 };
321 
322 }
323 
324 namespace boost { namespace serialization {
325 
326 template <class Archive>
327 inline void save_construct_data(Archive &ar, const pagmo::population *pop, const unsigned int)
328 {
329  // Save data required to construct instance.
330  pagmo::problem::base_ptr prob = pop->problem().clone();
331  ar << prob;
332 }
333 
334 template <class Archive>
335 inline void load_construct_data(Archive &ar, pagmo::population *pop, const unsigned int)
336 {
337  // Retrieve data from archive required to construct new instance.
339  ar >> prob;
340  // Invoke inplace constructor to initialize instance of the population.
341  ::new(pop)pagmo::population(*prob);
342 }
343 
344 }} //namespaces
345 
346 #endif
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
fitness_vector cur_f
Current fitness vector.
Definition: population.h:89
void custom_vector_double_save(Archive &ar, const std::vector< double > &v, const unsigned int)
Custom save function for the serialization of vector of doubles that handle also inf and NaN...
Definition: serialization.h:67
std::ostream & operator<<(std::ostream &s, const archipelago &a)
Overload stream operator for pagmo::archipelago.
constraint_vector cur_c
Current constraint vector.
Definition: population.h:87
Individuals stored in the population.
Definition: population.h:80
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
decision_vector x
Decision vector.
Definition: population.h:149
fitness_vector f
Fitness vector.
Definition: population.h:153
std::vector< individual_type > container_type
Underlying container type.
Definition: population.h:189
virtual base_ptr clone() const =0
Clone method.
std::string human_readable() const
Human-readable representation.
Definition: population.h:158
Base island class.
Definition: base_island.h:81
void custom_vector_double_load(Archive &ar, std::vector< double > &v, const unsigned int)
Custom load function for the serialization of vector of doubles that handle also inf and NaN...
Definition: serialization.h:92
fitness_vector best_f
Best fitness vector so far.
Definition: population.h:95
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::string human_readable() const
Human-readable representation.
Definition: population.h:100
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
container_type::const_iterator const_iterator
Const iterator.
Definition: population.h:195
decision_vector cur_v
Current velocity vector.
Definition: population.h:85
constraint_vector c
Constraint vector.
Definition: population.h:151
container_type::size_type size_type
Population size type.
Definition: population.h:192
Population champion.
Definition: population.h:146
decision_vector cur_x
Current decision vector.
Definition: population.h:83
constraint_vector best_c
Best constraint vector so far.
Definition: population.h:93
decision_vector best_x
Best decision vector so far.
Definition: population.h:91