PaGMO  1.1.5
archipelago.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_ARCHIPELAGO_H
26 #define PAGMO_ARCHIPELAGO_H
27 
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/thread/barrier.hpp>
30 #include <boost/thread/locks.hpp>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/tuple/tuple.hpp>
33 #include <boost/serialization/map.hpp>
34 #include <boost/unordered_map.hpp>
35 #include <iostream>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 #include "algorithm/base.h"
41 #include "base_island.h"
42 #include "config.h"
43 #include "population.h"
44 #include "problem/base.h"
45 #include "rng.h"
46 #include "serialization.h"
47 #include "topology/base.h"
48 #include "topology/unconnected.h"
49 
50 namespace pagmo {
51 
53 
57 class __PAGMO_VISIBLE archipelago
58 {
59  public:
61  friend class base_island;
63  typedef std::vector<base_island_ptr> container_type;
65  typedef container_type::size_type size_type;
70  {
72 
75  point_to_point = 0,
77 
80  broadcast = 1
81  };
83 
88  {
90 
98  source = 0,
100 
106  destination = 1
107  };
108  private:
109  // Iterators.
110  typedef container_type::iterator iterator;
111  typedef container_type::const_iterator const_iterator;
112  // Container for migrating individuals. This a hash map containing hash maps as values.
113  // Please NOTE carefully: in case of desination migration, item n in the outer hash map is supposed to contain a hash map with a single
114  // (n,emigrants vector) pair (in other words, containing redundantly n twice). In case of source migration, item n will contain a map of
115  // emigrants from other islands.
116  typedef boost::unordered_map<size_type,boost::unordered_map<size_type,std::vector<individual_type> > > migration_map_type;
117  // Lock type.
118  typedef boost::lock_guard<boost::mutex> lock_type;
119  // Migration history item: (n_individuals,orig_island,dest_island) tuple.
120  typedef boost::tuple<population::size_type,size_type,size_type> migr_hist_item;
121  // Container of migration history: vector of history items.
122  typedef std::vector<migr_hist_item> migr_hist_type;
123  public:
124  explicit archipelago(distribution_type = point_to_point, migration_direction = destination);
125  explicit archipelago(const topology::base &, distribution_type = point_to_point, migration_direction = destination);
126  explicit archipelago(const algorithm::base &, const problem::base &, int, int, const topology::base & = topology::unconnected(),
127  distribution_type = point_to_point, migration_direction = destination);
128  archipelago(const archipelago &);
129  archipelago &operator=(const archipelago &);
130  ~archipelago();
131  void join() const;
132  void set_algorithm(const size_type &, const algorithm::base &);
133  void push_back(const base_island &);
134  size_type get_size() const;
135  std::string human_readable() const;
136  bool check_island(const base_island &) const;
137  topology::base_ptr get_topology() const;
138  void set_topology(const topology::base &);
139  distribution_type get_distribution_type() const;
140  void set_distribution_type(const distribution_type &);
141  void evolve(int = 1);
142  void evolve_batch(int, unsigned int, bool = true);
143  void evolve_t(int);
144  bool busy() const;
145  void interrupt();
146  std::string dump_migr_history() const;
147  void clear_migr_history();
148  void set_island(const size_type &, const base_island &);
149  std::vector<base_island_ptr> get_islands() const;
150  base_island_ptr get_island(const size_type &) const;
151  void set_seeds(unsigned int);
152  private:
153  void pre_evolution(base_island &);
154  void post_evolution(base_island &);
155  void reset_barrier(const size_type &);
156  void build_immigrants_vector(std::vector<std::pair<population::size_type, individual_type > > &,
157  const base_island &, base_island &,
158  const std::vector<individual_type> &) const;
159  void check_migr_attributes() const;
160  void sync_island_start() const;
161  size_type locate_island(const base_island &) const;
162  bool destruction_checks() const;
163  void reevaluate_immigrants(std::vector<std::pair<population::size_type, individual_type> > &,
164  const base_island &) const;
165  private:
166  friend class boost::serialization::access;
167  template <class Archive>
168  void serialize(Archive &ar, const unsigned int version)
169  {
170  join();
171  ar & m_container;
172  ar & m_topology;
173  ar & m_dist_type;
174  ar & m_migr_dir;
175  ar & m_migr_map;
176  ar & m_drng;
177  ar & m_urng;
178  // NOTE: this would need tuple serialization...
179  //ar & m_migr_hist;
180  boost::serialization::split_member(ar, *this, version);
181  }
182 
183  template <class Archive>
184  void save(Archive &, const unsigned int) const
185  {}
186  template <class Archive>
187  void load(Archive &, const unsigned int)
188  {
189  // NOTE: archi pointer is not saved during island serialization. Hence, upon loading,
190  // we are going to set the archi pointer of the islands to this.
191  for (size_type i = 0; i < m_container.size(); ++i) {
192  m_container[i]->m_archi = this;
193  }
194  // NOTE: migr history is not saved, so upon loading we clear it.
195  m_migr_hist.clear();
196  }
197  // Container of islands.
198  container_type m_container;
199  // A barrier used to synchronise the start time of islands.
200  boost::scoped_ptr<boost::barrier> m_islands_sync_point;
201  // Topology.
202  topology::base_ptr m_topology;
203  // Distribution type.
204  distribution_type m_dist_type;
205  // Migration direction.
206  migration_direction m_migr_dir;
207  // Migration container.
208  migration_map_type m_migr_map;
209  // Rngs used during migration.
210  rng_double m_drng;
211  rng_uint32 m_urng;
212  // Migration mutex.
213  boost::mutex m_migr_mutex;
214  // Migration history.
215  migr_hist_type m_migr_hist;
216 
217 };
218 
219 std::ostream __PAGMO_VISIBLE_FUNC &operator<<(std::ostream &, const archipelago &);
220 
221 }
222 
223 #endif
Root PaGMO namespace.
migration_direction
Migration direction.
Definition: archipelago.h:87
container_type::size_type size_type
Archipelago size type.
Definition: archipelago.h:65
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base topology.
Definition: topology/base.h:47
std::ostream & operator<<(std::ostream &s, const archipelago &a)
Overload stream operator for pagmo::archipelago.
Base topology class.
Definition: topology/base.h:75
Base algorithm class.
Individuals stored in the population.
Definition: population.h:80
Base problem class.
Definition: problem/base.h:148
distribution_type
Distribution type for migrating individuals.
Definition: archipelago.h:69
population::individual_type individual_type
Individual type.
Definition: archipelago.h:67
Base island class.
Definition: base_island.h:81
boost::shared_ptr< base_island > base_island_ptr
Alias for the shared pointer to a pagmo::base_island.
Definition: base_island.h:49
Unconnected topology.
Definition: unconnected.h:45
Archipelago class.
Definition: archipelago.h:57
std::vector< base_island_ptr > container_type
Internal container of islands.
Definition: archipelago.h:63