bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
ppo_parallel_rollouts::CartPole Class Referencefinal

The cart-pole of Barto, Sutton and Anderson, simulated in this process with bitsim::dynamics::CartPoleDynamics. More...

#include <cart_pole.h>

Inheritance diagram for ppo_parallel_rollouts::CartPole:
Collaboration diagram for ppo_parallel_rollouts::CartPole:

Public Types

typedef EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > > base_type
typedef base_type::time_step_type time_step_type
typedef base_type::state_type state_type
typedef base_type::action_type action_type
Public Types inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > >
typedef TimeStep< std::vector< real_t > > time_step_type
 Alias for the type returned when stepping the environment.
typedef ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::state_space state_space_type
 Type describing the environment state space.
typedef ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::state_type state_type
 Type describing an individual state.
typedef ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::action_space action_space_type
 Type describing the environment action space.
typedef ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::action_type action_type
 Type representing an individual action.
Public Types inherited from bitrl::envs::ContinuousVectorStateDiscreteActionEnv< StateSpaceSize, action_end, action_start, StateSpaceItemType >
typedef ContinuousVectorSpace< StateSpaceSize, StateSpaceItemType > state_space
typedef state_space::space_item_type state_type
 the State type
typedef ScalarDiscreteSpace< action_start, action_end > action_space
 item_t
typedef action_space::space_item_type action_type
 the Action type

Public Member Functions

 CartPole ()
void make (const std::string &version, const std::unordered_map< std::string, std::any > &make_options, const std::unordered_map< std::string, std::any > &reset_options) override
 Construct the environment instance.
void close () override
 Close and release any acquired environment resources.
time_step_type reset () override
 Start an episode with the next seed.
time_step_type reset (uint_t seed)
 Start an episode with the given seed.
time_step_type step (const action_type &action) override
 Push the cart: 0 to the left, 1 to the right.
uint_t n_steps () const noexcept
uint_t max_steps () const noexcept
const bitsim::dynamics::CartPoleDynamics & dynamics () const noexcept
Public Member Functions inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > >
virtual ~EnvBase ()=default
 Virtual destructor.
virtual time_step_type step (const action_type &action)=0
 Perform one step in the environment using an action.
const std::unordered_map< std::string, std::any > & make_options () const noexcept
 Access the configuration options provided to make().
const std::unordered_map< std::string, std::any > & reset_options () const noexcept
 Access the configuration options provided to make().
T read_option (const std::string &op_name) const
 Read a specific make() option and cast it to the requested type.
std::string idx () const noexcept
 Get the id identifying this environment within a simulation batch. The id is valid only if make has been called.
bool is_created () const noexcept
 Check if make() has successfully initialized the environment.
std::string env_name () const noexcept
 Get the name of this environment instance.
std::string version () const noexcept
 Get the environment version set during make().
uint_t last_reset_seed () const noexcept
 The seed of the latest reset(), for the environments that take one. The first reset after make() uses the "seed" reset option, or DEFAULT_ENV_SEED, and every reset after it the next seed, so that the episodes start differently and a run is the same every time.

Static Public Attributes

static constexpr real_t THETA_LIMIT = 12.0 * 3.14159265358979323846 / 180.0
Static Public Attributes inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > >
static constexpr uint_t DEFAULT_ENV_SEED
 Default seed used in reset() if none provided.
Static Public Attributes inherited from bitrl::envs::ContinuousVectorStateDiscreteActionEnv< StateSpaceSize, action_end, action_start, StateSpaceItemType >
static constexpr uint_t STATE_SPACE_SIZE = state_space::size
 state space size
static constexpr uint_t ACTION_SPACE_SIZE = action_space::size
 action space size

Additional Inherited Members

Static Public Member Functions inherited from bitrl::envs::ContinuousVectorStateDiscreteActionEnv< StateSpaceSize, action_end, action_start, StateSpaceItemType >
static action_type sample_action ()
 sample
Protected Member Functions inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > >
 EnvBase (const std::string &idx=bitsim::consts::INVALID_STR, const std::string &name=bitsim::consts::INVALID_STR)
 Constructor (protected — for subclassing only).
 EnvBase (const EnvBase &)
 Copy constructor.
void set_version_ (const std::string &version) noexcept
 Set internal version string.
void set_idx_ (const std::string &idx) noexcept
 Set the id of the environment.
void set_make_options_ (const std::unordered_map< std::string, std::any > &options) noexcept
 Store options for future access.
void set_reset_options_ (const std::unordered_map< std::string, std::any > &options) noexcept
 Store reset options for future access.
uint_t next_reset_seed_ ()
 The seed for the next reset: the "seed" reset option, or DEFAULT_ENV_SEED, plus the number of resets since make().
void invalidate_is_created_flag_ () noexcept
 Mark environment as not created.
void make_created_ () noexcept
 Mark environment creation as successful.
time_step_type & get_current_time_step_ () noexcept
 Mutable access to the current time step.
const time_step_type & get_current_time_step_ () const noexcept
 Read-only access to the current time step.

Detailed Description

The cart-pole of Barto, Sutton and Anderson, simulated in this process with bitsim::dynamics::CartPoleDynamics.

It is the task of Gymnasium's CartPole, without a server: push the cart left (action 0) or right (action 1) with max_force, every dt seconds, and keep the pole within 12 degrees of upright and the cart within 2.4 m of the centre. The observation is [x, x_dot, theta, theta_dot]; every step is worth 1, the step that fails included. An episode starts with every value uniform in [-0.05, 0.05], drawn with the seed of the reset, and ends when the pole falls, the cart leaves the track, or after max_steps steps; then the time step's extra "truncated" is true.

Unlike Gymnasium, which takes one Euler step, the dynamics integrate with fourth order Runge-Kutta, so the numbers are close to Gymnasium's but not the same.

Make options, all optional: "max_steps" (uint_t, 500), "dt" (real_t, 0.02), "max_force" (real_t, 10.0). Reset option: "seed" (uint_t), the seed of the first reset; every reset after it takes the next seed.

Member Typedef Documentation

◆ action_type

◆ base_type

typedef EnvBase<TimeStep<std::vector<real_t> >, ContinuousVectorStateDiscreteActionEnv<4, 2, 0, real_t> > bitrl::envs::classic_control::CartPole::base_type

◆ state_type

◆ time_step_type

typedef base_type::time_step_type bitrl::envs::classic_control::CartPole::time_step_type

Constructor & Destructor Documentation

◆ CartPole()

Member Function Documentation

◆ close()

◆ dynamics()

◆ make()

void bitrl::envs::classic_control::CartPole::make ( const std::string & version,
const std::unordered_map< std::string, std::any > & make_options,
const std::unordered_map< std::string, std::any > & reset_options )
overridevirtual

Construct the environment instance.

Parameters
versionVersion string used to control environment variant
make_optionsKey-value configuration options for environment creation.
reset_optionsKey-value configuration how the environment should be reset
Note
Derived classes should use set_version_() and set_make_options_() internally. They may store selected options for later use.

Implements bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t > >.

◆ max_steps()

◆ n_steps()

◆ reset() [1/2]

◆ reset() [2/2]

Start an episode with the given seed.

◆ step()

Push the cart: 0 to the left, 1 to the right.

Member Data Documentation

◆ THETA_LIMIT

real_t bitrl::envs::classic_control::CartPole::THETA_LIMIT = 12.0 * 3.14159265358979323846 / 180.0
staticconstexpr

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