Generic optimization utilities#

pygmo.random_decision_vector(prob)#

This function will generate a decision vector whose values are randomly chosen with uniform probability within the lower and upper bounds \(lb\) and \(ub\) of the input problem prob.

For the continuous part of the decision vector, the \(i\)-th component of the randomly generated decision vector will be such that \(lb_i \le x_i < ub_i\).

For the discrete part of the decision vector, the \(i\)-th component of the randomly generated decision vector is guaranteed to be an integral value such that \(lb_i \le x_i \le ub_i\).

For both the continuous and discrete parts of the decision vector, if \(lb_i == ub_i\) then \(lb_i\) is returned.

Parameters

prob (problem) – the input problem

Returns

a random decision vector within the problem’s bounds

Return type

numpy.ndarray

Raises
  • ValueError – if the problem’s bounds are not finite or larger than an implementation-defined limit

  • unspecified – any exception thrown by failures at the intersection between C++ and Python (e.g., type conversion errors, mismatched function signatures, etc.)


pygmo.batch_random_decision_vector(prob, n)#

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

For the continuous parts of the decision vectors, the \(i\)-th components of the randomly generated decision vectors will be such that \(lb_i \le x_i < ub_i\).

For the discrete parts of the decision vectors, the \(i\)-th components of the randomly generated decision vectors are guaranteed to be integral values such that \(lb_i \le x_i \le ub_i\).

For both the continuous and discrete parts of the decision vectors, if \(lb_i == ub_i\) then \(lb_i\) is returned.

Parameters
  • prob (problem) – the input problem

  • n (int) – the number of decision vectors that will be generated

Returns

a batch of random decision vectors within the problem’s bounds, laid out contiguously in a 1D array

Return type

numpy.ndarray

Raises
  • OverflowError – in case of (unlikely) overflows

  • ValueError – if the problem’s bounds are not finite or larger than an implementation-defined limit

  • unspecified – any exception thrown by failures at the intersection between C++ and Python (e.g., type conversion errors, mismatched function signatures, etc.)


pygmo.set_global_rng_seed(seed)#

In pygmo it is, in general, possible to control the seed of all random generators by a dedicated seed kwarg passed on via various constructors. If no seed is passed pygmo randomly creates a seed for you using its global random number generator.

This function allows to be able to reset the seed of such a global random number generator. This can be useful to create a deterministic behaviour of pygmo easily.

Parameters

seed (int) – the new global seed for random number generation

Note

In complex parallel evolutions obtaining a deterministic behaviour is not possible even setting the global seed as pygmo implements an asynchronous model for parallel execution and the exact interplay between threads and processes cannot be reproduced deterministically.

Examples

>>> import pygmo as pg
>>> pg.set_global_rng_seed(seed = 32)
>>> pop = pg.population(prob = pg.ackley(5), size = 20)
>>> print(pop.champion_f) 
[17.26891503]
>>> pg.set_global_rng_seed(seed = 32)
>>> pop = pg.population(prob = pg.ackley(5), size = 20)
>>> print(pop.champion_f) 
[17.26891503]