26 #include <boost/numeric/conversion/cast.hpp>
31 #include "../population.h"
33 #include "base_r_policy.h"
34 #include "hv_greedy_r_policy.h"
35 #include "fair_r_policy.h"
37 #include "../util/hypervolume.h"
41 namespace pagmo {
namespace migration {
51 hv_greedy_r_policy::hv_greedy_r_policy(
const double &rate,
rate_type type,
const double nadir_eps):
base_r_policy(rate,type), m_nadir_eps(nadir_eps) {}
59 std::vector<std::pair<population::size_type,std::vector<population::individual_type>::size_type> >
67 std::vector<population::individual_type> filtered_immigrants;
68 filtered_immigrants.reserve(immigrants.size());
71 std::vector<unsigned int> original_immigrant_indices;
72 original_immigrant_indices.reserve(immigrants.size());
75 std::vector<population::individual_type>::iterator im_it = (
const_cast<std::vector<population::individual_type> &
>(immigrants)).begin();
76 unsigned int im_idx = 0;
77 for( ; im_it != immigrants.end() ; ++im_it) {
81 for (
unsigned int idx = 0 ; idx < dest.size() ; ++idx ) {
84 for (
unsigned int d_idx = 0 ; d_idx < im_x.size() ; ++d_idx) {
85 if (im_x[d_idx] != isl_x[d_idx]) {
95 filtered_immigrants.push_back(*im_it);
96 original_immigrant_indices.push_back(im_idx);
105 std::vector<std::pair<population::size_type, std::vector<population::individual_type>::size_type> > result;
108 if (rate_limit == 0) {
117 pop_copy.push_back(filtered_immigrants[i].cur_x);
124 std::vector< std::vector<fitness_vector> > fronts_f (fronts_i.size());
130 for (
unsigned int f_idx = 0 ; f_idx < fronts_i.size() ; ++f_idx) {
131 fronts_f[f_idx].resize(fronts_i[f_idx].size());
132 for (
unsigned int p_idx = 0 ; p_idx < fronts_i[f_idx].size() ; ++p_idx) {
136 for (
unsigned int d_idx = 0 ; d_idx < fronts_f[f_idx][p_idx].size() ; ++d_idx) {
137 refpoint[d_idx] = std::max(refpoint[d_idx], fronts_f[f_idx][p_idx][d_idx]);
143 for (
unsigned int d_idx = 0 ; d_idx < refpoint.size() ; ++d_idx) {
144 refpoint[d_idx] += m_nadir_eps;
148 unsigned int front_idx = fronts_i.size() - 1;
149 unsigned int processed_individuals = 0;
152 std::vector<unsigned int> orig_indices(fronts_i[front_idx].size());
153 iota(orig_indices.begin(), orig_indices.end(), 0);
156 std::vector<unsigned int> g_orig_indices(pop_copy.size(), 1);
158 unsigned int no_discarded_immigrants = 0;
162 std::vector<std::pair<unsigned int, double> > discarded_islanders;
165 while (processed_individuals < filtered_immigrants.size() && discarded_islanders.size() < rate_limit) {
167 if (fronts_f[front_idx].size() == 0) {
170 orig_indices.resize(fronts_i[front_idx].size());
171 iota(orig_indices.begin(), orig_indices.end(), 0);
180 unsigned int orig_lc_idx = fronts_i[front_idx][orig_indices[lc_idx]];
182 if (orig_lc_idx < dest.size()) {
183 discarded_islanders.push_back(std::make_pair(orig_lc_idx, 0.0));
185 ++no_discarded_immigrants;
189 g_orig_indices[orig_lc_idx] = 0;
192 orig_indices.erase(orig_indices.begin() + lc_idx);
193 fronts_f[front_idx].erase(fronts_f[front_idx].begin() + lc_idx);
194 ++processed_individuals;
198 unsigned int no_available_immigrants = boost::numeric_cast<
unsigned int>(filtered_immigrants.size() - no_discarded_immigrants);
202 std::vector<std::pair<unsigned int, double> > available_immigrants;
203 available_immigrants.reserve(no_available_immigrants);
204 for(
unsigned int idx = dest.size() ; idx < pop_copy.size() ; ++idx) {
206 if ( g_orig_indices[idx] == 1 ) {
207 available_immigrants.push_back(std::make_pair(idx, 0.0));
212 std::vector<fitness_vector> merged_fronts;
213 merged_fronts.reserve(pop_copy.size());
215 for(
unsigned int idx = 0 ; idx < pop_copy.size() ; ++idx) {
220 std::vector<std::pair<unsigned int, double> >::iterator it;
222 for(it = available_immigrants.begin() ; it != available_immigrants.end() ; ++it) {
223 (*it).second = hv.
exclusive((*it).first, refpoint);
226 for(it = discarded_islanders.begin() ; it != discarded_islanders.end() ; ++it) {
227 (*it).second = hv.
exclusive((*it).first, refpoint);
231 sort(available_immigrants.begin(), available_immigrants.end(), hv_greedy_r_policy::ind_cmp);
232 sort(discarded_islanders.begin(), discarded_islanders.end(), hv_greedy_r_policy::ind_cmp);
235 unsigned int no_exchanges = std::min(boost::numeric_cast<unsigned int>(available_immigrants.size()), boost::numeric_cast<unsigned int>(discarded_islanders.size()));
237 it = available_immigrants.begin();
238 std::vector<std::pair<unsigned int, double> >::reverse_iterator r_it = discarded_islanders.rbegin();
241 for(
unsigned int i = 0 ; i < no_exchanges ; ++i) {
243 if ((*r_it).second > (*it).second) {
247 result.push_back(std::make_pair((*r_it).first, original_immigrant_indices[(*it).first - dest.size()]));
256 bool hv_greedy_r_policy::ind_cmp(
const std::pair<unsigned int, double> &a,
const std::pair<unsigned int, double> &b) {
257 return a.second > b.second;
hv_greedy_r_policy(const double &rate=1, rate_type type=absolute, double nadir_eps=1.0)
Constructor from rate and rate type.
std::vector< double > decision_vector
Decision vector type.
Base class for migration replacement policies.
fitness_vector cur_f
Current fitness vector.
boost::shared_ptr< base_r_policy > base_r_policy_ptr
Shared pointer to base replacement policy.
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.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
base_r_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.
unsigned int least_contributor(const fitness_vector &, const hv_algorithm::base_ptr) const
Find the least contributing individual.
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.
std::vector< double > fitness_vector
Fitness vector type.
Replace the 'n' successive least contributors with the incoming set.
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.
decision_vector cur_x
Current decision vector.
rate_type m_type
Migration rate type.
f_size_type get_f_dimension() const
Return fitness dimension.
double m_rate
Migration rate.
double exclusive(const unsigned int, const fitness_vector &, const hv_algorithm::base_ptr) const
Compute exclusive contribution.
void set_copy_points(const bool)
Setter for 'copy_points' flag.
std::vector< std::vector< size_type > > compute_pareto_fronts() const
Computes and returns the population Pareto fronts.
rate_type
Type of migration rate.