Utility classes and functions

Contents

Utility classes and functions#

Random number generation#

#include <utility/rng.hpp>
namespace rng#

Contains the PRNG classes and functions used for generating random numbers.

Functions

inline bool randomBool() noexcept#

Generate a random boolean value from a uniform distribution.

template<std::integral IntType = int>
IntType randomInt(
IntType lbound,
IntType ubound,
)#

Generate a random integer from a uniform distribution on the closed interval [lbound, ubound].

template<std::floating_point RealType = double>
RealType randomReal()#

Generate a random floating-point value from a uniform distribution on the half-open interval [0.0, 1.0).

template<std::floating_point RealType = double>
RealType randomReal(
RealType lbound,
RealType ubound,
)#

Generate a random floating-point value of from a uniform distribution on the half-open interval [lbound, ubound).

template<std::floating_point RealType = double>
RealType randomNormal(
RealType mean = 0.0,
RealType std_dev = 1.0,
)#

Generate a random floating-point value from a normal distribution with the specified mean and std deviation.

template<std::integral IntType = int>
IntType randomPoisson(double mean)#

Generate a random integer value from a poisson distribution with the specified mean.

template<std::integral IntType = int>
IntType randomBinomial(
IntType n,
double p,
)#

Generate a random integer value from an approximate binomial distribution with the parameters n and p.

template<std::ranges::random_access_range R>
detail::size_type<R> randomIndex(
const R &range,
)#

Generate a random index for a range.

template<std::forward_iterator Iter>
Iter randomElement(Iter first, Iter last)#

Pick a random element from a range.

template<std::integral IntType>
small_vector<IntType> sampleUnique(
IntType lbound,
IntType ubound,
size_t count,
)#

Generate count unique integers from the half-open range [lbound, ubound).

template<std::floating_point T>
size_t sampleCdf(std::span<const T> cdf)#

Select an index based on a discrete CDF.

template<std::integral IntType>
IntType randomBinomialApproxNormal(
IntType n,
double p,
)#
template<std::integral IntType>
IntType randomBinomialApproxPoisson(
IntType n,
double p,
)#
template<std::integral IntType>
IntType randomBinomialExact(
IntType n,
double p,
)#
template<std::integral IntType>
small_vector<IntType> sampleUniqueSet(
IntType lbound,
IntType ubound,
size_t count,
)#

Variables

constexpr ConcurrentXoroshiro128p prng#

The global pseudo-random number generator instance used in the algorithms.

template<std::floating_point RealType = double>
class QuasiRandom#
#include <qrng.hpp>

Quasi-random number generator based on: http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/. Generates points in the unit hypercube in dim dimensions.

Public Types

using result_type = std::vector<RealType>#
using state_type = std::vector<RealType>#
using size_type = std::size_t#

Public Functions

explicit QuasiRandom(size_type dim, NonNegative<RealType> seed = 0.5)#

Create a quasi-random number generator in dim dimensions.

inline result_type operator()()#

Generate the next quasi-random point of the sequence.

inline void discard(size_type n = 1)#

Discard the next n points of the sequence.

inline void reset(NonNegative<RealType> new_seed = 0.5)#

Reset the generator’s state using a specified seed.

inline size_type dim() const noexcept#
Returns:

The generator’s number of dimensions.

class Splitmix64#
#include <rng.hpp>

Splitmix64 pseudo-random number generator based on https://prng.di.unimi.it/splitmix64.c. This generator is only used for seeding the Xoroshiro generators.

Public Types

using result_type = std::uint64_t#

The generator generates 64 bit integers.

using state_type = std::uint64_t#

The generator has a 64 bit state.

Public Functions

inline explicit constexpr Splitmix64(state_type seed) noexcept#

Create a new Splitmix64 generator initialized from a 64 bit seed value.

Note

The global prng instance should be used instead of creating new instances of this generator.

Parameters:

seed – The seed used to initialize the state of the generator.

inline constexpr result_type operator()() noexcept#
Returns:

The next number of the sequence.

inline constexpr void seed(state_type seed) noexcept#

Set a new seed for the generator.

Public Static Functions

static inline constexpr result_type min() noexcept#
Returns:

The smallest possible value that can be generated.

static inline constexpr result_type max() noexcept#
Returns:

The largest possible value that can be generated.

Friends

friend constexpr friend bool operator== (const Splitmix64 &, const Splitmix64 &)=default

Compare the internal state of 2 generators.

Returns:

true if they are the same.

class Xoroshiro128p#
#include <rng.hpp>

Xoroshiro128+ pseudo-random number generator based on https://prng.di.unimi.it/xoroshiro128plus.c

See also

David Blackman and Sebastiano Vigna. “Scrambled linear pseudorandom number generators.” ACM Transactions on Mathematical Software 47, no. 4 (2021): 1-32.

Public Types

using result_type = std::uint64_t#

The generator generates 64 bit integers.

using state_type = std::array<uint64_t, 2>#

The generator has 128 bits of state.

Public Functions

inline explicit constexpr Xoroshiro128p(std::uint64_t seed) noexcept#

Create a Xoroshiro128+ generator initialized from a 64 bit seed value.

Note

The global prng instance should be used instead of creating new instances of this generator.

Parameters:

seed – The seed used to initialize the state of the generator.

inline constexpr result_type operator()() noexcept#
Returns:

The next number of the sequence.

inline constexpr Xoroshiro128p &jump() noexcept#

Advance the state of the generator by 2^96 steps.

inline constexpr void seed(std::uint64_t seed) noexcept#

Set a new seed for the generator.

Public Static Functions

static inline constexpr result_type min() noexcept#
Returns:

The smallest possible value that can be generated.

static inline constexpr result_type max() noexcept#
Returns:

The largest possible value that can be generated.

Friends

friend constexpr friend bool operator== (const Xoroshiro128p &, const Xoroshiro128p &)=default

Compare the internal state of 2 generators.

Returns:

True if they are the same.

class ConcurrentXoroshiro128p#
#include <rng.hpp>

The pseudo-random number generator class used in the library. This class is a simple wrapper around the Xoroshiro128p generator to make operator() thread-safe.

Note

The global prng instance should be used instead of creating new instances of this generator.

Public Types

using result_type = Xoroshiro128p::result_type#
using state_type = Xoroshiro128p::state_type#

Public Functions

inline result_type operator()() const noexcept#
Returns:

The next number of the sequence. Thread-safe.

Public Static Functions

static inline void seed(std::uint64_t seed)#

Set a new seed for the generator. This function is not thread-safe and shouldn’t be called concurrently with the random number generation functions (e.g. while a GA is running).

static inline constexpr result_type min() noexcept#
Returns:

The smallest possible value that can be generated.

static inline constexpr result_type max() noexcept#
Returns:

The largest possible value that can be generated.

Floating-point comparison tolerances#

#include <utility/math.hpp>
class ScopedTolerances#

This class can be used to set the absolute and relative tolerances used for comparing floating-point values in the GAs.

When an instance of the class is created, the tolerance values are set to the values specified by the parameters of the constructor, and these new tolerance values will be used for floating-point comparisons until the instance of the class is destroyed. The tolerances are reset to their old values when the instance is destroyed.

Warning

Creating an instance of this class modifies the global floating point tolerance values. This means that this class shouldn’t be instantiated on multiple threads at once.

Public Functions

inline ScopedTolerances(
NonNegative<double> abs,
NonNegative<double> rel,
) noexcept#

Create an instance of the class, setting new values for the tolerances used for floating-point comparisons.

Parameters:
  • abs – The absolute tolerance value that will be used for the comparisons. Can’t be negative.

  • rel – The relative tolerance value around 1.0 that will be used for the comparisons. Can’t be negative.

inline ~ScopedTolerances() noexcept#

Reset the tolerances to their previous values.

#include <utility/math.hpp>
class Tolerances#

This class contains the global absolute and relative tolerance values used for comparing floating-point values in the GAs.

New tolerances can be set using the ScopedTolerances class.

Public Static Functions

template<std::floating_point T = double>
static inline T abs() noexcept#
Returns:

The current absolute tolerance used for floating-point comparisons.

template<std::floating_point T = double>
static inline T rel(T at) noexcept#
Returns:

The current relative tolerance used for floating-point comparisons around at.

Execution#

#include <utility/thread_pool.hpp>
inline void gapp::execution_threads(size_t count)#

Set the number of threads that will be used by the library to run the genetic algorithms.

The value should be between 1 and the number of hardware threads. The default number of threads used will be the value returned by std::thread::hardware_concurrency.

@count The desired number of threads. Must be at least 1.

Note

This function is not thread-safe and shouldn’t be called while a genetic algorithm is running.

inline size_t gapp::execution_threads() noexcept#
Returns:

The number of threads used by the library.

Bounded value types#

#include <utility/bounded_value.hpp>
template<typename T, T Low, T High>
using gapp::BoundedValue = detail::BoundedValue<T, detail::Interval<T>{Low, High, true, true}>#

Type representing values in the closed interval [Low, High].

template<typename T>
using gapp::NonNegative = detail::BoundedValue<T, detail::Interval<T>{T(0), std::numeric_limits<T>::max(), true, true}>#

Type representing values in the closed interval [0, Tmax].

template<typename T>
using gapp::Negative = detail::BoundedValue<T, detail::Interval<T>{std::numeric_limits<T>::lowest(), T(0), true, false}>#

Type representing values in the half-closed interval [Tmin, 0).

template<typename T>
using gapp::Positive = detail::BoundedValue<T, detail::Interval<T>{T(0), std::numeric_limits<T>::max(), false, true}>#

Type representing values in the half-closed interval (0, Tmax].

using gapp::Probability = detail::BoundedValue<double, detail::Interval<double>{0.0, 1.0, true, true}>#

Type representing a probability value in the closed interval [0.0, 1.0].

template<typename T>
using gapp::Normalized = detail::BoundedValue<T, detail::Interval<T>{0.0, 1.0, true, true}>#

Type representing a value in the closed interval of [0.0, 1.0].