PaGMO  1.1.5
best_kill_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_kill_s_policy.h"
32 
33 namespace pagmo { namespace migration {
34 
36 
42 best_kill_s_policy::best_kill_s_policy(const double &rate, rate_type type):base_s_policy(rate,type) {}
43 
45 {
46  return base_s_policy_ptr(new best_kill_s_policy(*this));
47 }
48 
49 std::vector<population::individual_type> best_kill_s_policy::select(population &pop) const
50 {
51  pagmo_assert(get_n_individuals(pop) <= pop.size());
52  // Gets the number of individuals to select
53  const population::size_type migration_rate = get_n_individuals(pop);
54  // Create a temporary array of individuals.
55  std::vector<population::individual_type> result;
56  // Gets the indexes of the best individuals
57  std::vector<population::size_type> best_idx = pop.get_best_idx(migration_rate);
58  // Puts the best individuals in results
59  for (population::size_type i =0; i< migration_rate; ++i) {
60  result.push_back(pop.get_individual(best_idx[i]));
61  }
62  // Remove them from the original population
63  // (note: the champion will still carry information on the best guy ...)
64  for (population::size_type i=0 ; i<migration_rate; ++i) {
65  pop.reinit(best_idx[i]);
66  }
67  return result;
68 }
69 
70 }}
71 
72 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::migration::best_kill_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
std::vector< population::individual_type > select(population &) const
Select individuals to emigrate from the given population.
population::size_type get_n_individuals(const population &) const
Get number of individuals to migrate from/to input population.
base_s_policy_ptr clone() const
Clone method.
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
void reinit(const size_type &)
Re-initialise individual at position idx.
Definition: population.cpp:236
best_kill_s_policy(const double &rate=1, rate_type type=absolute)
Constructor from migration rate and type.
"Choose best and kill him" migration selection policy.
rate_type
Type of migration rate.