PaGMO  1.1.5
migration/base.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 <boost/numeric/conversion/cast.hpp>
26 #include <climits>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <typeinfo>
31 
32 #include "../exceptions.h"
33 #include "../population.h"
34 #include "../types.h"
35 #include "base.h"
36 
37 namespace pagmo { namespace migration {
38 
40 
49 base::base(const double &rate, rate_type type):m_rate(rate),m_type(type)
50 {
51  switch (m_type) {
52  case absolute:
53  if (m_rate < 0 || m_rate > INT_MAX) {
54  pagmo_throw(value_error,"invalid absolute migration rate");
55  }
56  // Convert to nearest integer.
57  m_rate = double_to_int::convert(m_rate);
58  break;
59  case fractional:
60  if (m_rate < 0 || m_rate > 1) {
61  pagmo_throw(value_error,"invalid fractional migration rate");
62  }
63  break;
64  default:
65  pagmo_throw(value_error,"invalid type for migration rate");
66  }
67 }
68 
70 
76 {
77  population::size_type retval = 0;
78  switch (m_type) {
79  case absolute:
80  retval = boost::numeric_cast<population::size_type>(m_rate);
81  if (retval > pop.size()) {
82  pagmo_throw(value_error,"absolute migration rate is higher than population size");
83  }
84  break;
85  case fractional:
86  retval = boost::numeric_cast<population::size_type>(double_to_int::convert(m_rate * pop.size()));
87  pagmo_assert(retval <= pop.size());
88  }
89  return retval;
90 }
91 
93 
102 std::string base::human_readable() const
103 {
104  std::ostringstream oss;
105  oss << "Policy name: " << typeid(*this).name() << '\n';
106  oss << "\tMigration type: " << ((m_type) ? "fractional" : "absolute") << '\n';
107  oss << "\tMigration rate: " << ((m_type) ? (m_rate * 100) : m_rate) << ((m_type) ? "%" : "") << '\n';
108  oss << human_readable_extra();
109  return oss.str();
110 }
111 
113 
118 std::string base::human_readable_extra() const
119 {
120  return std::string();
121 }
122 
124 
132 std::ostream &operator<<(std::ostream &s, const base &p)
133 {
134  s << p.human_readable();
135  return s;
136 }
137 
139 
143 {}
144 
145 }}
Root PaGMO namespace.
Population class.
Definition: population.h:70
std::ostream & operator<<(std::ostream &s, const base &p)
Overload stream operator for migration::base.
base(const double &, rate_type)
Constructor from migration rate and type.
Migration rate is interpreted as the fraction of individuals to migrate with respect to the orign/des...
population::size_type get_n_individuals(const population &) const
Get number of individuals to migrate from/to input population.
virtual ~base()
Destructor.
virtual std::string human_readable_extra() const
Return extra information in human readable representation.
Base migration class.
container_type::size_type size_type
Population size type.
Definition: population.h:192
rate_type m_type
Migration rate type.
std::string human_readable() const
Return human readable representation.
double m_rate
Migration rate.
Migration rate is interpreted as the absolute number of individuals to migrate.
rate_type
Type of migration rate.