bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitrl::envs::classic_control::CartPoleSwingUp Class Referencefinal

Swing the pole of a cart-pole up from hanging down, and keep it up, with a force that can take any value, simulated with bitsim::dynamics::CartPoleDynamics. More...

#include <cart_pole_swing_up.h>

Inheritance diagram for bitrl::envs::classic_control::CartPoleSwingUp:
Collaboration diagram for bitrl::envs::classic_control::CartPoleSwingUp:

Public Types

typedef EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, 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 > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t > >
typedef TimeStep< std::vector< real_t > > time_step_type
 Alias for the type returned when stepping the environment.
typedef ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t >::state_space state_space_type
 Type describing the environment state space.
typedef ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t >::state_type state_type
 Type describing an individual state.
typedef ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t >::action_space action_space_type
 Type describing the environment action space.
typedef ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t >::action_type action_type
 Type representing an individual action.
Public Types inherited from bitrl::envs::ContinuousVectorStateContinuousVectorActionEnv< StateSpaceSize, ActionSpaceSize, StateSpaceItemType, ActionSpaceItemType >
typedef ContinuousVectorSpace< StateSpaceSize, StateSpaceItemType > state_space
 The type of the state space.
typedef state_space::space_item_type state_type
 The type of state.
typedef ContinuousVectorSpace< ActionSpaceSize, ActionSpaceItemType > action_space
 The type of the action space.
typedef action_space::space_item_type action_type
 The type of action.

Public Member Functions

 CartPoleSwingUp ()
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 with action[0] newtons, positive towards +x.
uint_t n_steps () const noexcept
uint_t max_steps () const noexcept
real_t dt () const noexcept
real_t angle () const
 The angle of the pole from upright, in (-pi, pi].
const bitsim::dynamics::CartPoleDynamics & dynamics () const noexcept
Public Member Functions inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t > >
virtual ~EnvBase ()=default
 Virtual destructor.
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 UPRIGHT = 12.0 * 3.14159265358979323846 / 180.0
 Within this of upright [rad] the pole counts as up.
Static Public Attributes inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t > >
static constexpr uint_t DEFAULT_ENV_SEED
 Default seed used in reset() if none provided.
Static Public Attributes inherited from bitrl::envs::ContinuousVectorStateContinuousVectorActionEnv< StateSpaceSize, ActionSpaceSize, StateSpaceItemType, ActionSpaceItemType >
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

Protected Member Functions inherited from bitrl::envs::EnvBase< TimeStep< std::vector< real_t > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, 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

Swing the pole of a cart-pole up from hanging down, and keep it up, with a force that can take any value, simulated with bitsim::dynamics::CartPoleDynamics.

The cart and the pole are those of CartPole, but the pole starts hanging down, and no push of max_force lifts it at once: the cart has to pump energy into the swing, back and forth, and then catch the pole at the top and balance it, all within the track. The action is the force on the cart [N], a vector of one value, limited to [-max_force, max_force].

The observation is [x, x_dot, sin(theta), cos(theta), theta_dot], theta from upright. Every step is worth (1 + cos(theta)) / 2, from 0 hanging down to 1 upright, minus 0.02 x^2 to keep the cart near the centre. An episode starts with the pole within 0.05 rad of hanging down and every other value uniform in [-0.05, 0.05], drawn with the seed of the reset. It ends when the cart leaves the track, which costs off_track_penalty, or after max_steps steps; then the time step's extra "truncated" is true. The extra "upright" says whether the pole is within 12 degrees of upright.

Make options, all optional: "max_steps" (uint_t, 500), "dt" (real_t, 0.02), "max_force" (real_t, 10.0), "off_track_penalty" (real_t, 100.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

◆ state_type

◆ time_step_type

Constructor & Destructor Documentation

◆ CartPoleSwingUp()

bitrl::envs::classic_control::CartPoleSwingUp::CartPoleSwingUp ( )

Member Function Documentation

◆ angle()

real_t bitrl::envs::classic_control::CartPoleSwingUp::angle ( ) const

The angle of the pole from upright, in (-pi, pi].

◆ close()

void bitrl::envs::classic_control::CartPoleSwingUp::close ( )
overridevirtual

◆ dt()

real_t bitrl::envs::classic_control::CartPoleSwingUp::dt ( ) const
inlinenoexcept

◆ dynamics()

const bitsim::dynamics::CartPoleDynamics & bitrl::envs::classic_control::CartPoleSwingUp::dynamics ( ) const
inlinenoexcept

◆ make()

void bitrl::envs::classic_control::CartPoleSwingUp::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 > >, ContinuousVectorStateContinuousVectorActionEnv< 5, 1, real_t, real_t > >.

◆ max_steps()

uint_t bitrl::envs::classic_control::CartPoleSwingUp::max_steps ( ) const
inlinenoexcept

◆ n_steps()

uint_t bitrl::envs::classic_control::CartPoleSwingUp::n_steps ( ) const
inlinenoexcept

◆ reset() [1/2]

time_step_type bitrl::envs::classic_control::CartPoleSwingUp::reset ( )
overridevirtual

◆ reset() [2/2]

time_step_type bitrl::envs::classic_control::CartPoleSwingUp::reset ( uint_t seed)

Start an episode with the given seed.

◆ step()

time_step_type bitrl::envs::classic_control::CartPoleSwingUp::step ( const action_type & action)
overridevirtual

Member Data Documentation

◆ UPRIGHT

real_t bitrl::envs::classic_control::CartPoleSwingUp::UPRIGHT = 12.0 * 3.14159265358979323846 / 180.0
staticconstexpr

Within this of upright [rad] the pole counts as up.


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