bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
A vector environment: Acrobot

Reinforcement learning collects experience faster from many copies of an environment at once. AcrobotV runs several copies of Gymnasium's Acrobot, a two-link pendulum that has to swing its tip above a line, on the bitrl-rest-api server, and steps them all with one call.

What you need

The bitrl-rest-api server running on http://0.0.0.0:8001; see its README. See Using Gymnasium environments (Part 1) for how bitrl talks to it.

Running the example

./acrobot_vector_env

The example makes three copies with the option num_envs, resets them with the seed 42, steps them with one action each, 0, 1 and 2, and prints the time steps.

The code

#include <any>
#include <ios>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main()
{
using namespace bitrl::envs::gymnasium;
const std::string SERVER_URL = "http://0.0.0.0:8001/api";
RESTRLEnvClient server(SERVER_URL, true);
// Acrobot vector environment
AcrobotV env(server);
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
std::unordered_map<std::string, std::any> options;
options["num_envs"] = std::any(static_cast<uint_t>(3));
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
// make the environment
env.make("v1", options, reset_ops);
std::cout << "Reseting the environment... " << std::endl;
auto time_step = env.reset();
std::cout << "Time step: " << time_step << std::endl;
std::cout << "Acting on the environment... " << std::endl;
// step in the environment
std::vector<uint_t> actions(3, 0);
actions[1] = 1;
actions[2] = 2;
time_step = env.step(actions);
std::cout << "Time step after action: " << time_step << std::endl;
std::cout << "Closing the environment... " << std::endl;
env.close();
std::cout << "Is active? " << env.is_alive() << std::noboolalpha << std::endl;
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The CartPole class Interface for CartPole environment.
Definition acrobot_vec_env.h:67
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:28
int main()
Definition discounted_returns.cpp:19
Definition lunar_lander_env.h:18
std::size_t uint_t
uint_t
Definition bitsim_types.h:40