PaGMO  1.1.5
random_r_policy.cpp
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 #include <algorithm>
26 #include <boost/numeric/conversion/cast.hpp>
27 #include <utility>
28 #include <vector>
29 
30 #include "../population.h"
31 #include "../rng.h"
32 #include "base.h"
33 #include "base_r_policy.h"
34 #include "random_r_policy.h"
35 
36 namespace pagmo { namespace migration {
37 
39 
45 random_r_policy::random_r_policy(const double &rate, rate_type type):base_r_policy(rate,type),m_urng(rng_generator::get<rng_uint32>()) {}
46 
48 {
49  return base_r_policy_ptr(new random_r_policy(*this));
50 }
51 
52 std::vector<std::pair<population::size_type,std::vector<population::individual_type>::size_type> >
53  random_r_policy::select(const std::vector<population::individual_type> &immigrants, const population &dest) const
54 {
55  const population::size_type rate_limit = std::min<population::size_type>(get_n_individuals(dest),boost::numeric_cast<population::size_type>(immigrants.size()));
56  // Temporary vectors to store sorted indices of the populations.
57  std::vector<population::size_type> immigrants_idx(boost::numeric_cast<std::vector<population::size_type>::size_type>(immigrants.size()));
58  std::vector<population::size_type> dest_idx(boost::numeric_cast<std::vector<population::size_type>::size_type>(dest.size()));
59  // Fill in the arrays of indices.
60  iota(immigrants_idx.begin(),immigrants_idx.end(),population::size_type(0));
61  iota(dest_idx.begin(),dest_idx.end(),population::size_type(0));
62  // Permute the indices (immigrants).
63  for (population::size_type i = 0; i < rate_limit; ++i) {
64  population::size_type next_idx = i + (m_urng() % (rate_limit - i));
65  if (next_idx != i) {
66  std::swap(immigrants_idx[i], immigrants_idx[next_idx]);
67  }
68  }
69  // Permute the indices (destination).
70  for (population::size_type i = 0; i < rate_limit; ++i) {
71  population::size_type next_idx = i + (m_urng() % (dest.size() - i));
72  if (next_idx != i) {
73  std::swap(dest_idx[i], dest_idx[next_idx]);
74  }
75  }
76  // Return value.
77  std::vector<std::pair<population::size_type,std::vector<population::individual_type>::size_type> >
78  retval;
79  for (population::size_type i = 0; i < rate_limit; ++i) {
80  retval.push_back(std::make_pair(dest_idx[i],immigrants_idx[i]));
81  }
82  return retval;
83 }
84 
85 } }
86 
87 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::migration::random_r_policy)
Root PaGMO namespace.
Generic thread-safe generator of pseudo-random number generators.
Definition: rng.h:138
Base class for migration replacement policies.
Definition: base_r_policy.h:57
boost::shared_ptr< base_r_policy > base_r_policy_ptr
Shared pointer to base replacement policy.
Definition: base_r_policy.h:40
This rng returns an unsigned integer in the [0,2**32-1] range.
Definition: rng.h:47
Population class.
Definition: population.h:70
base_r_policy_ptr clone() const
Clone method.
std::vector< std::pair< population::size_type, std::vector< population::individual_type >::size_type > > select(const std::vector< population::individual_type > &, const population &) const
Assign pairs of individuals for replacement during migration.
population::size_type get_n_individuals(const population &) const
Get number of individuals to migrate from/to input population.
Random replacement policy.
static void iota(ForwardIterator first, ForwardIterator last, T value)
Iota function, usefull to fill iterator range with increasing values.
container_type::size_type size_type
Population size type.
Definition: population.h:192
random_r_policy(const double &rate=1, rate_type type=absolute)
Constructor.
rate_type
Type of migration rate.