bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType > Class Template Reference

Proximal policy optimisation that learns from several episodes at once, played by the copies of a bitrl::envs::ParallelEnvs. More...

#include <parallel_ppo.h>

Inheritance diagram for bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >:
Collaboration diagram for bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >:

Public Types

typedef envs::ParallelEnvs< EnvType > env_type
typedef PolicyType policy_type
typedef CriticType critic_type
typedef EnvType::state_type state_type
typedef EnvType::action_type action_type
typedef ACSolverBase< env_type, PolicyType, CriticType, A2CMonitor< action_type, state_type >, PPOConfig > base_type
Public Types inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
typedef EnvType env_type
 The environment type.
typedef PolicyType policy_type
 The policy or action type.
typedef CriticType critic_type
 The critic type.
typedef env_type::state_type state_type
typedef env_type::action_type action_type
typedef A2CMonitor< EnvType::action_type, EnvType::state_type > monitor_type
 The monitor type to use.
typedef monitor_type::experience_buffer_type experience_buffer_type
typedef monitor_type::experience_tuple_type experience_tuple_type
typedef PPOConfig config_type
 The solver configuration type.
Public Types inherited from bitrl::rl::algos::RLSolverBase< EnvType >
typedef EnvType env_type

Public Member Functions

 ParallelPPOSolver (const PPOConfig &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer)
EpisodeInfo on_training_episode (env_type &envs, uint_t round) override
 Play an episode in every copy and learn from all of them; the reward of the EpisodeInfo is the mean return of the episodes, its iterations the steps of all of them.
const std::vector< real_t > & last_returns () const noexcept
 The return of every episode of the last round.
Public Member Functions inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
virtual ~ACSolverBase ()=default
 Destructor.
virtual void actions_after_training_ends (env_type &)
 actions_after_training_ends. Actions to execute after the training iterations have finisehd
virtual void actions_before_episode_begins (env_type &, uint_t)
 actions_before_training_episode
virtual void actions_after_episode_ends (env_type &, uint_t, const EpisodeInfo &)
 actions_after_training_episode
virtual void actions_before_training_begins (env_type &)
 actions_before_training_begins. Execute any actions the algorithm needs before starting the iterations
void set_train_mode () noexcept
 set_train_mode for both the Actor and the Critic
void set_evaluation_mode () noexcept
 set_evaluation_mode for both the Actor and the Critic
monitor_type & get_monitor ()
 Read-write access to the monitor object.
Public Member Functions inherited from bitrl::rl::algos::RLSolverBase< EnvType >
virtual ~RLSolverBase ()=default
 Destructor.
virtual EpisodeInfo on_training_episode (env_type &, uint_t)=0
 on_episode Do one on_episode of the algorithm

Protected Member Functions

virtual void on_gradients_ ()
 Called after every backward pass, before the optimisers step, with the gradients of both networks in place; DistributedPPOSolver averages them over its ranks.
Protected Member Functions inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
 ACSolverBase (const config_type &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer)
 A2C.
uint_t create_episode_batch_ (env_type &env, uint_t, experience_buffer_type &buffer)
Protected Member Functions inherited from bitrl::rl::algos::RLSolverBase< EnvType >
 RLSolverBase ()=default
 Constructor.

Additional Inherited Members

Protected Attributes inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
config_type config_
 config_
policy_type & policy_
 policy_
critic_type & critic_
 The action network.
monitor_type monitor_
 Helper class to monitor the algorithm.
std::unique_ptr< torch::optim::Optimizer > policy_optimizer_
 The policy_ optimzer.
std::unique_ptr< torch::optim::Optimizer > critic_optimizer_
 The optimizer for the critic network.

Detailed Description

template<typename EnvType, typename PolicyType, typename CriticType>
class bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >

Proximal policy optimisation that learns from several episodes at once, played by the copies of a bitrl::envs::ParallelEnvs.

Every training round, on_training_episode(), resets every copy and plays an episode in each, all at the same time: at every step the policy acts for all the copies whose episode is not over with one call on a batch of their states, and ParallelEnvs steps the copies on its threads. A copy's episode ends when it is done or after config.max_itrs_per_episode steps. Then the networks learn from all the episodes together, with the clipped objective of PPOSolver: config.max_passes_over_batch passes over the whole batch, the advantage being the discounted return of the step, within its own episode, minus the critic's value, normalised if config.normalize_advantages, and the loss actor + 0.5 critic - 0.01 entropy.

A round of N copies plays N episodes; the actions are sampled on this thread with libtorch's generator, so a training is the same whatever the number of threads, and repeatable with torch::manual_seed().

The policy must have

std::tuple<torch::Tensor, torch::Tensor> act_batch(torch::Tensor states)

that gives an action and its log probability for every row of states, and the evaluate() of PPOSolver; the critic must have forward(states), a value per row.

Template Parameters
EnvTypethe environment of a copy; the solver trains on ParallelEnvs<EnvType>

Member Typedef Documentation

◆ action_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef EnvType::action_type bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::action_type

◆ base_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef ACSolverBase<env_type, PolicyType, CriticType, A2CMonitor<action_type, state_type>, PPOConfig> bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::base_type

◆ critic_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef CriticType bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::critic_type

◆ env_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef envs::ParallelEnvs<EnvType> bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::env_type

◆ policy_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef PolicyType bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::policy_type

◆ state_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef EnvType::state_type bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::state_type

Constructor & Destructor Documentation

◆ ParallelPPOSolver()

template<typename EnvType, typename PolicyType, typename CriticType>
bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::ParallelPPOSolver ( const PPOConfig & config,
policy_type & policy,
critic_type & critic,
std::unique_ptr< torch::optim::Optimizer > & policy_optimizer,
std::unique_ptr< torch::optim::Optimizer > & critic_optimizer )
inline

Member Function Documentation

◆ last_returns()

template<typename EnvType, typename PolicyType, typename CriticType>
const std::vector< real_t > & bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::last_returns ( ) const
inlinenoexcept

The return of every episode of the last round.

◆ on_gradients_()

template<typename EnvType, typename PolicyType, typename CriticType>
virtual void bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::on_gradients_ ( )
inlineprotectedvirtual

Called after every backward pass, before the optimisers step, with the gradients of both networks in place; DistributedPPOSolver averages them over its ranks.

Reimplemented in bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >.

◆ on_training_episode()

template<typename EnvType, typename PolicyType, typename CriticType>
EpisodeInfo bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >::on_training_episode ( env_type & envs,
uint_t round )
override

Play an episode in every copy and learn from all of them; the reward of the EpisodeInfo is the mean return of the episodes, its iterations the steps of all of them.


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