bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
A grid world

A grid world is the simplest environment for reinforcement learning: an agent moves on a grid, one cell a step, and is rewarded for reaching a goal. This example makes Gridworld<4>, a 4 x 4 grid, and steps in it. Unlike the Gymnasium examples it runs in C++, with no server.

What you need

Nothing beyond bitrl itself.

The environment

Like every bitsim environment, it is made with make(version, options, reset_options) and then stepped with step(action), which returns a time step with the type of the step, the reward, the observation and the discount. The actions are the four moves, and every step costs a reward of -1.

The mode option places the agent, the goal, a pit and a wall:

  • STATIC: always in the same cells;
  • RANDOM: all of them at random, with the seed of the reset options.

The observation has one 4 x 4 layer for each of the four things, with a 1 where it is.

Running the example

./grid_world

The code

#ifdef BITSIM_DEBUG
#include <cassert>
#endif
#include <any>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
namespace grid_world
{
using namespace bitsim;
using namespace bitrl::envs::grid_world;
{
std::cout << "Creating STATIC Gridworld..." << std::endl;
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)});
options["mode"] = std::any(GridWorldInitType::STATIC);
env.make("v0", options, reset_ops);
std::cout << "Number of actions: " << env.n_actions() << std::endl;
std::cout << "Number of states: " << env.n_states() << std::endl;
std::cout << "Version: " << env.version() << std::endl;
std::cout << "Name: " << env.env_name() << std::endl;
auto time_step = env.step(0);
std::cout << "Time step:" << time_step << std::endl;
time_step = env.step(1);
std::cout << "Time step:" << time_step << std::endl;
env.close();
}
{
std::cout << "Creating RANDOM Gridworld..." << std::endl;
bitrl::envs::grid_world::Gridworld<4> env;
std::unordered_map<std::string, std::any> options;
options["mode"] = std::any(GridWorldInitType::RANDOM);
std::unordered_map<std::string, std::any> reset_ops;
reset_ops.insert({"seed", static_cast<uint_t>(42)});
env.make("v0", options, reset_ops);
std::cout << "Number of actions: " << env.n_actions() << std::endl;
std::cout << "Number of states: " << env.n_states() << std::endl;
std::cout << "Version: " << env.version() << std::endl;
std::cout << "Name: " << env.env_name() << std::endl;
env.close();
}
} // namespace grid_world
int main()
{
using namespace grid_world;
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Definition grid_world_env.h:253
int main()
Definition discounted_returns.cpp:19
Definition grid_world_env.h:36
@ RANDOM
Definition grid_world_env.h:41
@ STATIC
Definition grid_world_env.h:40
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Definition grid_world.cpp:17
void create_random()
Definition grid_world.cpp:50
void create_static()
Definition grid_world.cpp:23
env
Definition play.py:33