Population

class pagmo::population

Population class.

../_images/population.jpg

This class represents a population of individuals, i.e. potential candidate solutions to a given problem. In PaGMO an individual is determined

  • by a unique ID used to track him across generations and migrations
  • by a chromosome (a decision vector)
  • by the fitness of the chromosome as evaluated by a pagmo::problem. and thus including objectives, equality constraints and inequality constraints if present.

Public Types

typedef std::vector<vector_double>::size_type size_type

A shortcut to std::vector<vector_double>::size_type.

Public Functions

population()

Default constructor.

Constructs an empty population with a pagmo::null_problem. The random seed is initialised to zero.

template <typename T, generic_ctor_enabler< T > = 0>
population(T &&x, size_type pop_size = 0u, unsigned int seed = pagmo::random_device::next ())

Constructor from a problem of type T.

Constructs a population with pop_size individuals associated to the problem x and setting the population random seed to seed. In order for the construction to be succesfull, x must be such that a pagmo::problem can be constructed from it.

Parameters
  • x -

    the user problem the population refers to

  • pop_size -

    population size (i.e. number of individuals therein)

  • seed -

    seed of the random number generator used, for example, to create new random individuals within the bounds

Exceptions

population(const population&)

Defaulted copy constructor.

population(population&&)

Defaulted move constructor.

population &operator=(const population &other)

Copy assignment operator.

Copy assignment is implemented via copy+move.

Return
a reference to this.
Parameters
  • other -

    assignment argument.

Exceptions
  • unspecified -

    any exception thrown by the copy constructor.

population &operator=(population&&)

Defaulted move assignment operator.

~population()

Trivial destructor.

The destructor will run sanity checks in debug mode.

void push_back(const vector_double &x)

Adds one decision vector (chromosome) to the population.

Appends a new chromosome x to the population, evaluating its fitness and creating a new unique identifier for the newly born individual.

In case of exceptions, the population will not be altered.

Parameters
  • x -

    decision vector to be added to the population.

Exceptions
  • unspecified -

    any exception thrown by memory errors in standard containers or by problem::fitness(). Wrong dimensions for the input decision vector or the output fitness will trigger an exception.

vector_double decision_vector() const

Creates a random decision vector.

Creates a random decision vector within the problem’s bounds. It calls internally pagmo::decision_vector().

Return
a random decision vector
Exceptions

vector_double::size_type best_idx(const vector_double &tol) const

Index of best individual (accounting for a vector tolerance)

If the problem is single-objective and unconstrained, the best is simply the individual with the smallest fitness. If the problem is, instead, single objective, but with constraints, the best will be defined using the criteria specified in pagmo::sort_population_con(). If the problem is multi-objective one single best is not well defined. In this case the user can still obtain a strict ordering of the population individuals by calling the pagmo::sort_population_mo() function.

Return
the index of the best individual
Parameters
  • tol -

    vector of tolerances to be applied to each constraints

Exceptions
  • std::invalid_argument -

    if the problem is multiobjective and thus a best individual is not well defined

vector_double::size_type best_idx(double tol = 0.) const

Index of best individual (accounting for a scalar tolerance)

Return
index of the best individual
Parameters
  • tol -

    scalar tolerance to be considered for each constraint

vector_double::size_type worst_idx(const vector_double &tol) const

Index of worst individual (accounting for a vector tolerance)

If the problem is single-objective and unconstrained, the worst is simply the individual with the largest fitness. If the problem is, instead, single objective, but with constraints, the best individual will be defined using the criteria specified in pagmo::sort_population_con(). If the problem is multi-objective one single worst is not defined. In this case the user can still obtain a strict ordering of the population individuals by calling the pagmo::sort_population_mo() function.

Return
the index of the best individual
Parameters
  • tol -

    vector of tolerances to be applied to each constraints

Exceptions
  • std::invalid_argument -

    if the problem is multiobjective and thus a best individual is not well defined

vector_double::size_type worst_idx(double tol = 0.) const

Index of worst individual (accounting for a scalar tolerance)

Return
index of the best individual
Parameters
  • tol -

    scalar tolerance to be considered for each constraint

size_type size() const

Number of individuals in the population.

void set_xf(size_type i, const vector_double &x, const vector_double &f)

Sets the \(i\)-th individual decision vector, and fitness.

Sets simultaneously the \(i\)-th individual decision vector and fitness thus avoiding to trigger a fitness function evaluation.

Note
: The user must make sure that the input fitness f makes sense as pagmo will only check its dimension.
Parameters
  • i -

    individual’s index in the population

  • x -

    a decision vector (chromosome)

  • f -

    a fitness vector

Exceptions
  • std::invalid_argument -

    if i is invalid (i.e. larger or equal to the population size)

  • std::invalid_argument -

    if x has not the correct dimension

  • std::invalid_argument -

    if f has not the correct dimension

void set_x(size_type i, const vector_double &x)

Sets the \(i\)-th individual’s chromosome.

Sets the chromosome of the \(i\)-th individual to the value x and changes its fitness accordingly. The individual’s ID remains the same.

Note
a call to this method triggers one fitness function evaluation
Parameters
  • i -

    individual’s index in the population

  • x -

    decision vector

Exceptions
  • unspecified -

    any exception thrown by set_xf

void set_problem_seed(unsigned int seed)

Setter for the problem seed.

const problem &get_problem() const

Getter for the pagmo::problem.

const std::vector<vector_double> &get_f() const

Getter for the fitness vectors.

const std::vector<vector_double> &get_x() const

Getter for the decision vectors.

const std::vector<unsigned long long> &get_ID() const

Getter for the individual IDs.

unsigned int get_seed() const

Getter for the seed of the population random engine.

template <typename Archive>
void serialize(Archive &ar)

Serialization.

Friends

std::ostream &operator<<(std::ostream &os, const population &p)

Streaming operator for the class pagmo::population.