bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitrl::rl::algos::es::EvolutionStrategy Class Reference

The evolution strategy of Salimans et al. (2017): a search for the parameters that maximise a fitness, which needs only the fitness, not its gradient. More...

#include <evolution_strategy.h>

Public Types

typedef std::function< real_t(const std::vector< real_t > &, uint_t, uint_t)> fitness_type

Public Member Functions

 EvolutionStrategy (const ESConfig &config, std::vector< real_t > initial_parameters)
GenerationResult step (const fitness_type &fitness, bitsim::utils::concurrency::ThreadPool &pool=bitsim::utils::concurrency::default_thread_pool())
 One generation, evaluated on the threads of pool.
GenerationResult step (const fitness_type &fitness, rl::distributed::Communicator &communicator, bitsim::utils::concurrency::ThreadPool &pool=bitsim::utils::concurrency::default_thread_pool())
 One generation, the pairs shared out among the ranks of communicator: rank r evaluates the pairs k with k % world_size == r, on the threads of pool, and sends their fitnesses to the others. Every rank must call it, with an EvolutionStrategy made alike; afterwards every rank has the same parameters as a run on one rank.
const std::vector< real_t > & parameters () const noexcept
 The parameters: the mean of the search distribution.
std::vector< real_t > noise (uint_t generation, uint_t pair) const
 The noise of pair k in generation generation.
uint_t generation () const noexcept
 The generations taken so far.
const std::vector< real_t > & last_fitnesses () const noexcept
 The fitnesses of the last generation: entry 2k is pair k plus, 2k + 1 pair k minus.
const ESConfig & config () const noexcept
std::uint64_t checksum () const noexcept
 A checksum of the parameters, to tell that two runs, or two ranks, agree.

Static Public Member Functions

static std::vector< real_t > centred_ranks (const std::vector< real_t > &values)
 The centred ranks of values, in [-0.5, 0.5]; equal values share their mean rank.

Detailed Description

The evolution strategy of Salimans et al. (2017): a search for the parameters that maximise a fitness, which needs only the fitness, not its gradient.

Every generation draws n_pairs directions eps_k of standard normal noise, and evaluates the fitness at theta + sigma eps_k and at theta - sigma eps_k: an antithetic pair. The fitnesses are replaced by their centred ranks, in [-0.5, 0.5], which ignores how far apart they are and so copes with outliers; then

g = sum_k (F(theta + sigma eps_k) - F(theta - sigma eps_k)) eps_k / (2 n_pairs sigma)

estimates the gradient of the fitness smoothed by the noise, and Adam takes a step up it, with weight decay.

The fitness can be anything: the return of an episode, a cost a controller should keep low negated, something that is not differentiable. The evaluations of a generation do not depend on each other, so step() runs them on the threads of a pool, or shares them out among the ranks of a distributed run. The noise of pair k in generation g is drawn with the seed task_seed(task_seed(seed, g), k), so every rank can make every direction itself, and only the fitnesses travel: 2 * n_pairs numbers per generation, however many parameters there are. The result does not depend on the number of threads or ranks, to the last bit.

A fitness is called as fitness(parameters, generation, pair), and must be safe to call from several threads at once. Both members of a pair get the same pair index, so that they can play the same episode, with the same seed: that cancels the luck of the episode out of their difference.

Member Typedef Documentation

◆ fitness_type

typedef std::function<real_t(const std::vector<real_t> &, uint_t, uint_t)> bitrl::rl::algos::es::EvolutionStrategy::fitness_type

Constructor & Destructor Documentation

◆ EvolutionStrategy()

bitrl::rl::algos::es::EvolutionStrategy::EvolutionStrategy ( const ESConfig & config,
std::vector< real_t > initial_parameters )
inline

Member Function Documentation

◆ centred_ranks()

std::vector< real_t > bitrl::rl::algos::es::EvolutionStrategy::centred_ranks ( const std::vector< real_t > & values)
inlinestatic

The centred ranks of values, in [-0.5, 0.5]; equal values share their mean rank.

◆ checksum()

std::uint64_t bitrl::rl::algos::es::EvolutionStrategy::checksum ( ) const
inlinenoexcept

A checksum of the parameters, to tell that two runs, or two ranks, agree.

◆ config()

const ESConfig & bitrl::rl::algos::es::EvolutionStrategy::config ( ) const
inlinenoexcept

◆ generation()

uint_t bitrl::rl::algos::es::EvolutionStrategy::generation ( ) const
inlinenoexcept

The generations taken so far.

◆ last_fitnesses()

const std::vector< real_t > & bitrl::rl::algos::es::EvolutionStrategy::last_fitnesses ( ) const
inlinenoexcept

The fitnesses of the last generation: entry 2k is pair k plus, 2k + 1 pair k minus.

◆ noise()

std::vector< real_t > bitrl::rl::algos::es::EvolutionStrategy::noise ( uint_t generation,
uint_t pair ) const
inline

The noise of pair k in generation generation.

◆ parameters()

const std::vector< real_t > & bitrl::rl::algos::es::EvolutionStrategy::parameters ( ) const
inlinenoexcept

The parameters: the mean of the search distribution.

◆ step() [1/2]

GenerationResult bitrl::rl::algos::es::EvolutionStrategy::step ( const fitness_type & fitness,
bitsim::utils::concurrency::ThreadPool & pool = bitsim::utils::concurrency::default_thread_pool() )
inline

One generation, evaluated on the threads of pool.

◆ step() [2/2]

GenerationResult bitrl::rl::algos::es::EvolutionStrategy::step ( const fitness_type & fitness,
rl::distributed::Communicator & communicator,
bitsim::utils::concurrency::ThreadPool & pool = bitsim::utils::concurrency::default_thread_pool() )
inline

One generation, the pairs shared out among the ranks of communicator: rank r evaluates the pairs k with k % world_size == r, on the threads of pool, and sends their fitnesses to the others. Every rank must call it, with an EvolutionStrategy made alike; afterwards every rank has the same parameters as a run on one rank.


The documentation for this class was generated from the following file: