PaGMO  1.1.5
base_island.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_BASE_ISLAND_H
26 #define PAGMO_BASE_ISLAND_H
27 
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/thread/thread.hpp>
31 #include <cstddef>
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 #include "config.h"
37 #include "algorithm/base.h"
38 #include "migration/base_r_policy.h"
39 #include "migration/base_s_policy.h"
40 #include "population.h"
41 #include "problem/base.h"
42 #include "serialization.h"
43 #include "types.h"
44 
45 namespace pagmo
46 {
47 // Forward declarations.
48 class archipelago;
50 
52 typedef boost::shared_ptr<base_island> base_island_ptr;
53 
55 
81 class __PAGMO_VISIBLE base_island
82 {
83  public:
85  friend class archipelago;
88  base_island(const base_island &);
89  explicit base_island(const algorithm::base &, const problem::base &, int,
91  const migration::base_r_policy &);
92  explicit base_island(const algorithm::base &, const population &,
94  const migration::base_r_policy &);
95  base_island &operator=(const base_island &);
97 
106  virtual base_island_ptr clone() const = 0;
107  virtual ~base_island();
109 
111  std::string human_readable_terse() const;
112  std::string human_readable() const;
113  virtual std::string get_name() const;
115 
119  virtual void join() const;
120  bool busy() const;
121  void evolve(int = 1);
122  void evolve_t(int);
123  void interrupt();
124  std::size_t get_evolution_time() const;
125  protected:
127  virtual void perform_evolution(const algorithm::base &, population &) const = 0;
128  virtual void thread_entry();
129  virtual void thread_exit();
131  public:
134  algorithm::base_ptr get_algorithm() const;
135  void set_algorithm(const algorithm::base &);
136  void set_x(population::size_type, const decision_vector &);
137  void set_v(population::size_type, const decision_vector &);
138  problem::base_ptr get_problem() const;
139  population::size_type get_size() const;
140  migration::base_s_policy_ptr get_s_policy() const;
141  migration::base_r_policy_ptr get_r_policy() const;
142  population get_population() const;
143  void set_population(const population &);
145  private:
146  // NOTE: in the next code line, it should be std::vector<std::pair<population::size_type, archipelago::size_type> >,
147  // but this creates problems as at this point archipelago::siz_type is not defined and cannot be!!!
148  std::vector<std::pair<population::size_type, population::size_type> > accept_immigrants(std::vector<std::pair<population::size_type, population::individual_type> > &);
149  std::vector<population::individual_type> get_emigrants();
150  // Evolver thread object. This is a callable helper object used to launch an evolution for a given number of iterations.
151  struct int_evolver;
152  // Time-dependent evolver thread object. This is a callable helper object used to launch an evolution for a specified amount of time.
153  struct t_evolver;
154  // RAII threads hook object.
155  struct raii_thread_hook;
156  friend struct raii_thread_hook;
157  protected:
165  std::size_t m_evo_time;
171  boost::scoped_ptr<boost::thread> m_evo_thread;
172  private:
173  friend class boost::serialization::access;
174  template <class Archive>
175  void serialize(Archive &ar, const unsigned int version)
176  {
177  // Sync the island before doing anything.
178  join();
179  // TODO: Also, consider relation to save/load constructor data in island and mpi_island.
180  ar & m_algo;
181  ar & m_pop;
182  ar & m_evo_time;
183  ar & m_s_policy;
184  ar & m_r_policy;
185  boost::serialization::split_member(ar, *this, version);
186  }
187  template <class Archive>
188  void save(Archive &, const unsigned int) const
189  {}
190  template <class Archive>
191  void load(Archive &, const unsigned int)
192  {
193  // Upon loading we are going to set the archi pointer and the evo thread to 0.
194  m_archi = 0;
195  m_evo_thread.reset(0);
196  }
197 };
198 
199 std::ostream __PAGMO_VISIBLE_FUNC &operator<<(std::ostream &, const base_island &);
200 
201 // Fake problem and algorithm used in the (de)serialization of island pointers.
202 namespace problem
203 {
204 
205 class island_init: public base
206 {
207  public:
208  island_init():base(1) {}
209  base_ptr clone() const
210  {
211  return base_ptr(new island_init(*this));
212  }
213  protected:
214  void objfun_impl(fitness_vector &, const decision_vector &) const {}
215 };
216 
217 }
218 
219 namespace algorithm
220 {
221 
222 class island_init: public base
223 {
224  public:
225  island_init():base() {}
226  base_ptr clone() const
227  {
228  return base_ptr(new island_init(*this));
229  }
230  void evolve(population &) const {}
231 };
232 
233 }
234 
235 }
236 
237 BOOST_SERIALIZATION_ASSUME_ABSTRACT(pagmo::base_island)
238 
239 #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
Base class for migration replacement policies.
Definition: base_r_policy.h:57
Base class for migration selection policies.
Definition: base_s_policy.h:54
boost::shared_ptr< base_r_policy > base_r_policy_ptr
Shared pointer to base replacement policy.
Definition: base_r_policy.h:40
std::ostream & operator<<(std::ostream &s, const archipelago &a)
Overload stream operator for pagmo::archipelago.
migration::base_s_policy_ptr m_s_policy
Migration selection policy.
Definition: base_island.h:167
Base algorithm class.
migration::base_r_policy_ptr m_r_policy
Migration replacement policy.
Definition: base_island.h:169
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
std::size_t m_evo_time
Total time spent by the island on evolution (in milliseconds).
Definition: base_island.h:165
algorithm::base_ptr m_algo
Algorithm.
Definition: base_island.h:159
Base island class.
Definition: base_island.h:81
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
archipelago * m_archi
Pointer that, if not null, points to the archipelago containing the island.
Definition: base_island.h:163
boost::shared_ptr< base_island > base_island_ptr
Alias for the shared pointer to a pagmo::base_island.
Definition: base_island.h:49
boost::shared_ptr< base_s_policy > base_s_policy_ptr
Shared pointer to base selection policy.
Definition: base_s_policy.h:39
population m_pop
Population.
Definition: base_island.h:161
container_type::size_type size_type
Population size type.
Definition: population.h:192
Archipelago class.
Definition: archipelago.h:57
boost::scoped_ptr< boost::thread > m_evo_thread
Evolution thread.
Definition: base_island.h:171