bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Using Gymnasium environments (Part 2)

In this example we will see how to interact with Gymnasium environments and specifically how to create an interact with CartPole environment.

As already mentioned in Using Gymnasium environments (Part 1), Gymnasium-based environments are interacted over a REST-like API maintained here: bitsim-envs-api. bitrl itself implements classes that hide this interaction from the client code. In general, environment classes in bitrl, have to implement the bitrl::envs::EnvBase API.

In this example we will use the bitrl::envs::gymnasium::CartPole
class. This is a template class, see the example below, that itself inherits from bitrl::envs::gymnasium::GymnasiumEnvBase class.

Below is the driver code.

#ifdef BITSIM_DEBUG
#include <cassert>
#endif
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
namespace example
{
using namespace bitsim;
using namespace bitrl::envs::gymnasium;
void test_cart_pole(RESTRLEnvClient &server)
{
std::cout << "Is environment registered: " << server.is_env_registered(CartPole::name)
<< std::endl;
// create the environment
CartPole env(server);
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
// make the environment
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
env.make("v1", options, reset_ops);
auto time_step = env.reset();
std::cout << "Time step: " << time_step << std::endl;
// step in the environment
time_step = env.step(0);
std::cout << "Time step after action: " << time_step << std::endl;
env.close();
}
{
std::cout << "Is environment registered: " << server.is_env_registered(Acrobot::name)
<< std::endl;
// create the environment
Acrobot env(server);
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
// make the environment
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
env.make("v1", options, reset_ops);
auto time_step = env.reset();
std::cout << "Time step: " << time_step << std::endl;
// step in the environment
time_step = env.step(0);
std::cout << "Time step after action: " << time_step << std::endl;
env.close();
}
{
std::cout << "Is environment registered: " << server.is_env_registered(Pendulum::name)
<< std::endl;
// create the environment
Pendulum env(server);
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
// make the environment
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
env.make("v1", options, reset_ops);
auto time_step = env.reset();
std::cout << "Time step: " << time_step << std::endl;
// step in the environment
time_step = env.step(0.0);
std::cout << "Time step after action: " << time_step << std::endl;
env.close();
}
{
std::cout << "Is environment registered: " << server.is_env_registered(MountainCar::name)
<< std::endl;
// create the environment
MountainCar env(server);
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
// make the environment
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
env.make("v0", options, reset_ops);
auto time_step = env.reset();
std::cout << "Time step: " << time_step << std::endl;
// step in the environment
time_step = env.step(0.0);
std::cout << "Time step after action: " << time_step << std::endl;
env.close();
}
} // namespace example
int main()
{
using namespace example;
const std::string SERVER_URL = "http://0.0.0.0:8001/api";
RESTRLEnvClient server(SERVER_URL, true);
std::cout << "Testing CartPole..." << std::endl;
std::cout << "====================" << std::endl;
std::cout << "Testing Pendulum..." << std::endl;
std::cout << "====================" << std::endl;
std::cout << "Testing Acrobot..." << std::endl;
std::cout << "====================" << std::endl;
std::cout << "Testing MountainCar..." << std::endl;
std::cout << "====================" << std::endl;
return 0;
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
class Acrobot. The Acrobot class. Interface for Gymnasium::Acrobot environment.
Definition acrobot_env.h:154
static const std::string name
The name of the environment.
Definition acrobot_env.h:160
The CartPole class Interface for CartPole environment.
Definition cart_pole_env.h:70
static const std::string name
name
Definition cart_pole_env.h:76
The MountainCar class.
Definition mountain_car_env.h:28
static const std::string name
name
Definition mountain_car_env.h:34
The Pendulum class. Interface for Pendulum environment.
Definition pendulum_env.h:106
static const std::string name
name
Definition pendulum_env.h:112
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:28
bool is_env_registered(const std::string &env_name) const noexcept
int main()
Definition discounted_returns.cpp:19
Definition lunar_lander_env.h:18
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Definition pytorch_linear_regression.cpp:17
void test_pendulum(RESTRLEnvClient &server)
Definition gymnasium_part_2.cpp:79
void test_cart_pole(RESTRLEnvClient &server)
Definition gymnasium_part_2.cpp:25
void test_acrobot(RESTRLEnvClient &server)
Definition gymnasium_part_2.cpp:52
void test_mountain_car(RESTRLEnvClient &server)
Definition gymnasium_part_2.cpp:106
const std::string SERVER_URL
Definition gym_walk.cpp:14
env
Definition play.py:33

In order to run the example you will need an instance of the bitsim-envs-api server running on your machine listening at port 8001. Note the actual example also shows how to use bitrl::envs::gymnasium::Pendulum, bitrl::envs::gymnasium::Acrobot and bitrl::envs::gymnasium::MountainCar environments.