Utility classes#
-
class not_population_based#
Base class for non population based solvers.
This class contains common methods useful in the implementation of user-defined algorithms that are not population based and establishes a common interface to a population
Currently, this class implements policies for the selection and replacement of a single individual in a population, which are meant to be used in the implementation of the
evolve()
method of the user-defined algorithm (see, e.g., pagmo::nlopt::evolve()).Subclassed by pagmo::compass_search, pagmo::ipopt, pagmo::nlopt, pagmo::simulated_annealing
Public Functions
-
not_population_based()#
Default constructor.
The default constructor sets the selection and replacement policies to
"best"
, and it initialises the RNG of the"random"
selection/replacements policies via random_device::next().
-
void set_random_sr_seed(unsigned)#
Set the seed for the
"random"
selection/replacement policies.- Parameters
seed – the value that will be used to seed the random number generator used by the
"random"
selection/replacement policies.
-
void set_selection(const std::string&)#
Set the individual selection policy.
This method will set the policy that is used to select the individual that will be optimised when calling the
evolve()
method of the algorithm.The input string must be one of
"best"
,"worst"
and"random"
:"best"
will select the best individual in the population,"worst"
will select the worst individual in the population,"random"
will randomly choose one individual in the population.
set_random_sr_seed() can be used to seed the random number generator used by the
"random"
policy.Instead of a selection policy, a specific individual in the population can be selected via set_selection(population::size_type).
- Parameters
select – the selection policy.
- Throws
std::invalid_argument – if
select
is not one of"best"
,"worst"
or"random"
.
-
inline void set_selection(population::size_type n)#
Set the individual selection index.
This method will set the index of the individual that is selected for optimisation in the
evolve()
method of the algorithm.- Parameters
n – the index in the population of the individual to be selected for optimisation.
-
boost::any get_selection() const#
Get the individual selection policy or index.
This method will return a
boost::any
containing either the individual selection policy (as anstd::string
) or the individual selection index (as a population::size_type). The selection policy or index is set via set_selection(const std::string &) and set_selection(population::size_type).- Returns
the individual selection policy or index.
-
void set_replacement(const std::string&)#
Set the individual replacement policy.
This method will set the policy that is used in the
evolve()
method of the algorithm to select the individual that will be replaced by the optimised individual.The input string must be one of
"best"
,"worst"
and"random"
:"best"
will select the best individual in the population,"worst"
will select the worst individual in the population,"random"
will randomly choose one individual in the population.
set_random_sr_seed() can be used to seed the random number generator used by the
"random"
policy.Instead of a replacement policy, a specific individual in the population can be selected via set_replacement(population::size_type).
- Parameters
replace – the replacement policy.
- Throws
std::invalid_argument – if
replace
is not one of"best"
,"worst"
or"random"
.
-
inline void set_replacement(population::size_type n)#
Set the individual replacement index.
This method will set the index of the individual that is replaced after the optimisation in the
evolve()
method of the algorithm.- Parameters
n – the index in the population of the individual to be replaced after the optimisation.
-
boost::any get_replacement() const#
Get the individual replacement policy or index.
This method will return a
boost::any
containing either the individual replacement policy (as anstd::string
) or the individual replacement index (as a population::size_type). The replacement policy or index is set via set_replacement(const std::string &) and set_replacement(population::size_type).- Returns
the individual replacement policy or index.
Protected Functions
-
std::pair<vector_double, vector_double> select_individual(const population&) const#
Select individual.
This method will select a single individual from the input population
pop
, returning its decision vector and fitness as a pair. The selection is done according to the currently active selection policy:if not_population_based::m_select is
"best"
, then the decision and fitness vectors of the best individual are returned,if not_population_based::m_select is
"worst"
, then the decision and fitness vectors of the worst individual are returned,if not_population_based::m_select is
"random"
, then the decision and fitness vectors of a randomly-selected individual are returned,if not_population_based::m_select is an index, then the decision and fitness vectors of the individual at the corresponding position in the population are returned.
Note that selecting a best or worst individual is meaningful only in single-objective problems.
- Parameters
pop – the input population.
- Throws
std::invalid_argument – if not_population_based::m_select is an index and the index is not smaller than the size of
pop
.unspecified – any exception thrown by population::best_idx() or population::worst_idx().
- Returns
a pair containing the decision and fitness vectors of the selected individual.
-
void replace_individual(population&, const vector_double&, const vector_double&) const#
Replace individual.
This method will replace a single individual in the input population
pop
, setting its decision vector tox
and its fitness vector tof
. The selection is done according to the currently active selection policy:if not_population_based::m_replace is
"best"
, then the best individual will be replaced,if not_population_based::m_replace is
"worst"
, then the worst individual will be replaced,if not_population_based::m_replace is
"random"
, then a randomly-selected individual will be replaced,if not_population_based::m_replace is an index, then the individual at the corresponding position in the population will be replaced.
Note that selecting a best or worst individual is meaningful only in single-objective problems.
- Parameters
pop – the input population.
x – the decision vector of the new individual.
f – the fitness vector of the new individual.
- Throws
std::invalid_argument – if not_population_based::m_replace is an index and the index is not smaller than the size of
pop
.unspecified – any exception thrown by population::best_idx(), population::worst_idx(), or population::set_xf().
Protected Attributes
-
boost::any m_select#
Individual selection policy.
This
boost::any
instance must contain either anstd::string
or a population::size_type, otherwise the behaviour will be undefined.
-
boost::any m_replace#
Individual replacement policy.
This
boost::any
instance must contain either anstd::string
or a population::size_type, otherwise the behaviour will be undefined.
-
unsigned m_rselect_seed#
Seed for the
"random"
selection/replacement policies.
-
mutable detail::random_engine_type m_e#
Random engine for the
"random"
selection/replacement policies.
-
not_population_based()#