PaGMO  1.1.5
random_s_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 <vector>
27 
28 #include "../population.h"
29 #include "base.h"
30 #include "base_s_policy.h"
31 #include "random_s_policy.h"
32 #include "../exceptions.h"
33 
34 namespace pagmo { namespace migration {
35 
37 
43 random_s_policy::random_s_policy(const double &rate, rate_type type):base_s_policy(rate,type) {}
44 
46 {
47  return base_s_policy_ptr(new random_s_policy(*this));
48 }
49 
50 std::vector<population::individual_type> random_s_policy::select(population &pop) const
51 {
52  pagmo_assert(get_n_individuals(pop) <= pop.size());
53  // Gets the number of individuals to select
54  const population::size_type migration_rate = get_n_individuals(pop);
55 
56  // Create a temporary array of individuals.
57  std::vector<population::individual_type> result;
58 
59  // Create an array of indices
60  std::vector<population::size_type> candidates_idx(boost::numeric_cast<std::vector<population::size_type>::size_type>(pop.size()));
61 
62  // Fill it with indices
63  iota(candidates_idx.begin(),candidates_idx.end(),population::size_type(0));
64 
65  // shuffle
66  random_shuffle(candidates_idx.begin(),candidates_idx.end());
67 
68  // Selects the n first individuals
69  for (population::size_type i = 0; i< migration_rate; ++i) {
70  result.push_back(pop.get_individual(candidates_idx[i]));
71  }
72 
73  return result;
74 }
75 
76 }}
77 
78 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::migration::random_s_policy)
Root PaGMO namespace.
Base class for migration selection policies.
Definition: base_s_policy.h:54
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Definition: population.cpp:277
Population class.
Definition: population.h:70
population::size_type get_n_individuals(const population &) const
Get number of individuals to migrate from/to input population.
std::vector< population::individual_type > select(population &) const
Select individuals to emigrate from the given population.
random_s_policy(const double &rate=1, rate_type type=absolute)
Constructor from migration rate and type.
Random migration selection policy.
boost::shared_ptr< base_s_policy > base_s_policy_ptr
Shared pointer to base selection policy.
Definition: base_s_policy.h:39
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
base_s_policy_ptr clone() const
Clone method.
rate_type
Type of migration rate.