PaGMO  1.1.5
best_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 "best_s_policy.h"
32 #include "../exceptions.h"
33 
34 namespace pagmo { namespace migration {
35 
37 
43 best_s_policy::best_s_policy(const double &rate, rate_type type):base_s_policy(rate,type) {}
44 
46 {
47  return base_s_policy_ptr(new best_s_policy(*this));
48 }
49 
50 std::vector<population::individual_type> best_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  // Create a temporary array of individuals.
56  std::vector<population::individual_type> result;
57  // Gets the indexes of the best individuals
58  std::vector<population::size_type> best_idx = pop.get_best_idx(migration_rate);
59  // Puts the best individuals in results
60  for (population::size_type i =0; i< migration_rate; ++i) {
61  result.push_back(pop.get_individual(best_idx[i]));
62  }
63  return result;
64 }
65 
66 }}
67 
68 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::migration::best_s_policy)
Root PaGMO namespace.
best_s_policy(const double &rate=1, rate_type type=absolute)
Constructor from migration rate and type.
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
"Choose best" migration selection policy.
Definition: best_s_policy.h:48
base_s_policy_ptr clone() const
Clone method.
population::size_type get_n_individuals(const population &) const
Get number of individuals to migrate from/to input population.
boost::shared_ptr< base_s_policy > base_s_policy_ptr
Shared pointer to base selection policy.
Definition: base_s_policy.h:39
container_type::size_type size_type
Population size type.
Definition: population.h:192
std::vector< population::individual_type > select(population &) const
Select individuals to emigrate from the given population.
rate_type
Type of migration rate.