Generic utilities#

A number of utilities to compute quantities that are of general relevance.


template<typename Rng>
inline double pagmo::uniform_real_from_range(double lb, double ub, Rng &r_engine)#

Generate a random real number within some lower and upper bounds.

This function will create a random real number within a half-open range. If both the lower and upper bounds are finite numbers, then the generated value \( x \) will be such that \(lb \le x < ub\). If \(lb = ub\), then \(lb\) is returned.

Example:

std::mt19937 r_engine(32u);
auto x = uniform_real_from_range(3,5,r_engine); // a random real in the [3, 5) range.
auto x = uniform_real_from_range(2,2,r_engine); // the value 2.
Parameters
  • lb – lower bound

  • ub – upper bound

  • r_engine – a C++ random engine

Throws

std::invalid_argument – if:

  • the bounds are not finite,

  • \( lb > ub \),

  • \( ub - lb \) is larger than an implementation-defined value.

Returns

a random floating-point value


template<typename Rng>
inline double pagmo::uniform_integral_from_range(double lb, double ub, Rng &r_engine)#

Generate a random integral number within some lower and upper bounds.

This function will create a random integral number within a closed range. If both the lower and upper bounds are finite numbers, then the generated value \( x \) will be such that \(lb \le x \le ub\).

Example:

std::mt19937 r_engine(32u);
auto x = uniform_integral_from_range(3,5,r_engine); // one of [3, 4, 5].
auto x = uniform_integral_from_range(2,2,r_engine); // the value 2.

Note

The return value is created internally via an integral random number generator based on the long long type, and then cast back to double. Thus, if the absolute values of the lower/upper bounds are large enough, any of the following may happen:

  • the conversion of the lower/upper bounds to long long may produce an overflow error,

  • the conversion of the randomly-generated long long integer back to double may yield an inexact result.

In practice, on modern mainstream computer architectures, this function will produce uniformly-distributed integral values as long as the absolute values of the bounds do not exceed \(2^{53}\).

Parameters
  • lb – lower bound

  • ub – upper bound

  • r_engine – a C++ random engine

Throws

std::invalid_argument – if:

  • the bounds are not finite,

  • \( lb > ub \),

  • \( lb \) and/or \( ub \) are not integral values,

  • \( lb \) and/or \( ub \) are not within the bounds of the long long type.

Returns

a random integral value


template<typename Rng>
inline vector_double pagmo::random_decision_vector(const problem &prob, Rng &r_engine)#

Generate a random decision vector compatible with a problem.

This function will generate a decision vector whose values are randomly chosen with uniform probability within the input problem’s bounds.

For the continuous part of the decision vector, the values will be generated via pagmo::uniform_real_from_range().

For the discrete part of the decision vector, the values will be generated via pagmo::uniform_integral_from_range().

Parameters
Throws

unspecified – any exception thrown by pagmo::uniform_real_from_range() or pagmo::uniform_integral_from_range().

Returns

a pagmo::vector_double containing a random decision vector


template<typename Rng>
inline vector_double pagmo::batch_random_decision_vector(const problem &prob, vector_double::size_type n, Rng &r_engine)#

Generate a batch of random decision vectors compatible with a problem.

This function will generate n decision vectors whose values are randomly chosen with uniform probability within the input problem’s bounds. The decision vectors are laid out contiguously in the return value: for a problem with dimension \( d \), the first decision vector in the return value occupies the index range \( \left[0, d\right) \), the second decision vector occupies the range \( \left[d, 2d\right) \), and so on.

For the continuous parts of the decision vectors, the values will be generated via pagmo::uniform_real_from_range().

For the discrete parts of the decision vectors, the values will be generated via pagmo::uniform_integral_from_range().

Parameters
  • prob – the input pagmo::problem

  • n – how many decision vectors will be generated

  • r_engine – a C++ random engine

Throws
Returns

a pagmo::vector_double containing n random decision vectors


double pagmo::binomial_coefficient(vector_double::size_type n, vector_double::size_type k)#

Binomial coefficient.

An implementation of the binomial coefficient using gamma functions

Parameters
  • n – first parameter \(n\)

  • k – second parameter \(k\)

Returns

the binomial coefficient \( n \choose k \)


std::vector<std::vector<vector_double::size_type>> pagmo::kNN(const std::vector<vector_double> &points, std::vector<vector_double>::size_type k)#

K-Nearest Neighbours.

Computes the indexes of the k nearest neighbours (euclidean distance) to each of the input points. The algorithm complexity (naive implementation) is \( O(MN^2)\) where \(N\) is the number of points and \(M\) their dimensionality

Example:

auto res = kNN({{1, 1}, {2, 2}, {3.1, 3.1}, {5, 5}}, 2u);

Parameters
  • points – the \(N\) points having dimension \(M\)

  • k – number of neighbours to detect

Throws

std::invalid_argument – If the points do not all have the same dimension.

Returns

An std::vector<std::vector<population::size_type> > containing the indexes of the k nearest neighbours sorted by distance