bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
grid_world_env.h
Go to the documentation of this file.
1#ifndef GRID_WORLD_ENV_H
2#define GRID_WORLD_ENV_H
3
10
11#include "bitrl/base/bitrl_config.h"
13#include "bitrl/envs/env_base.h"
18
19#ifdef BITRL_DEBUG
20#include <cassert>
21#endif
22
23#include <any>
24#include <atomic>
25#include <map>
26#include <memory>
27#include <random>
28#include <string>
29#include <unordered_map>
30#include <utility>
31#include <vector>
32
33namespace bitrl
34{
36{
37
38enum class GridWorldInitType : int
39{
40 STATIC = 0,
41 RANDOM = 1,
42 PLAYER = 2,
44};
46{
47 UP = 0,
48 DOWN = 1,
49 LEFT = 2,
50 RIGHT = 3,
52};
53
54namespace detail
55{
56
59typedef std::pair<int, int> board_position;
60
64typedef std::vector<std::vector<std::vector<real_t>>> board_state_type;
65
70{
71 PLAYER = 0,
72 GOAL = 1,
73 PIT = 2,
74 WALL = 3
75};
76
81{
82 VALID = 0,
85};
86
90bool operator==(const board_position &p1, const board_position &p2);
91
95bool operator!=(const board_position &p1, const board_position &p2);
96
101
106
111
116{
120 std::string name;
121
126
132 board_piece(std::string name_, board_position pos_)
133 : name(std::move(name_)), pos(std::move(pos_))
134 {
135 }
136
140 board_piece() = default;
141};
142
144
148struct board
149{
152
153 // draws the random boards: seeded once, so that every reset in the random
154 // and player modes gives a new board, and the sequence of boards is the
155 // same from run to run
156 std::mt19937 generator{42};
157 std::map<board_component_type, board_piece> components;
158 std::map<std::string, board_mask> masks;
159 bool is_board_init = {false};
160
165
170
177
183
189
193 void close();
194
199
204
209
216 void check_and_move(int_t row, int_t col);
217
224 board_position pos) const;
225};
226
227template <uint_t size_size> struct GridWorldEnv
228{
229
232 static constexpr uint_t STATE_SPACE_SIZE = size_size * size_size;
236};
237} // namespace detail
238
249
250template <uint_t side_size_>
251class Gridworld final
252 : public EnvBase<TimeStep<detail::board_state_type>, detail::GridWorldEnv<side_size_>>
253{
254 public:
255 static_assert(side_size_ >= 4, "The side size should be greater than or equal to 4");
256
260 static const std::string name;
261
265 static const uint_t n_components;
266
270 static const uint_t side_size;
271
276
282
287
292
297
302
306 using base_type::reset;
307
311 Gridworld();
312
317 void make(const std::string &version, const std::unordered_map<std::string, std::any> &options,
318 const std::unordered_map<std::string, std::any> &reset_options) override final;
319
323 time_step_type reset() override final;
324
330 time_step_type step(const action_type &action) override final;
331
335 void close() override final;
336
341 [[nodiscard]] bool has_random_state() const noexcept { return randomize_state_; }
342
346 [[nodiscard]] uint_t n_states() const noexcept { return side_size_ * side_size_; }
347
351 [[nodiscard]] uint_t n_actions() const noexcept { return action_space_type::size; }
352
357 [[nodiscard]] uint_t seed() const noexcept { return seed_; }
358
363 [[nodiscard]] real_t noise_factor() const noexcept { return noise_factor_; }
364
369 [[nodiscard]] bool is_game_lost() const;
370
375 [[nodiscard]] GridWorldInitType init_type() const noexcept { return init_mode_; }
376
381 static uint_t n_copies() { return n_copies_.load(); }
382
383 private:
387 GridWorldInitType init_mode_;
388
392 bool randomize_state_;
393
397 uint_t seed_;
398
403 static std::atomic<uint_t> n_copies_;
404
408 real_t noise_factor_;
409
413 detail::board board_;
414};
415
416template <uint_t side_size> const std::string Gridworld<side_size>::name = "Gridworld";
417
418template <uint_t side_size_> const uint_t Gridworld<side_size_>::side_size = side_size_;
419
420template <uint_t side_size_> const uint_t Gridworld<side_size_>::n_components = 4;
421
422template <uint_t side_size> std::atomic<uint_t> Gridworld<side_size>::n_copies_ = 0;
423
424template <uint_t side_size_>
426 : EnvBase<TimeStep<detail::board_state_type>, detail::GridWorldEnv<side_size_>>(
428 init_mode_(GridWorldInitType::INVALID_TYPE), randomize_state_(false), seed_(0),
429 noise_factor_(0.0), board_()
430{
431 ++n_copies_;
432}
433
434template <uint_t side_size_>
435void Gridworld<side_size_>::make(const std::string &version,
436 const std::unordered_map<std::string, std::any> &options,
437 const std::unordered_map<std::string, std::any> &reset_options)
438{
439
440 if (this->is_created())
441 {
442 return;
443 }
444
445 if (const auto mode = options.find("mode"); mode != options.end())
446 {
447 init_mode_ = std::any_cast<GridWorldInitType>(mode->second);
448 }
449 else
450 {
451 init_mode_ = GridWorldInitType::STATIC;
452 }
453
454 if (const auto seed = options.find("seed"); seed != options.end())
455 {
456 seed_ = std::any_cast<uint_t>(seed->second);
457 board_.seed = seed_;
458 }
459 board_.generator.seed(board_.seed);
460
461 if (const auto noise_factor = options.find("noise_factor"); noise_factor != options.end())
462 {
463 noise_factor_ = std::any_cast<real_t>(noise_factor->second);
464 }
465
466 if (const auto randomize_state = options.find("randomize_state");
467 randomize_state != options.end())
468 {
469 randomize_state_ = std::any_cast<bool>(randomize_state->second);
470 }
471
472 // initialize the board
473 board_.init_board(side_size_, init_mode_);
474
475 // set the version and set the board
476 // to created
477 this->set_version_(version);
478 auto idx = bitsim::utils::uuid4();
479 this->set_idx_(idx);
480 this->base_type::make(version, options, reset_options);
481 this->make_created_();
482}
483
484template <uint_t side_size_>
487{
488
489 auto obs = board_.step(static_cast<GridWorldActionType>(action));
490 auto reward = board_.get_reward();
491
492 // if the reward is not -1.0 then either
493 // we reached the goal or we hit the PIT
494 // in any case the game is over
495 auto step_type = reward != -1.0 ? TimeStepTp::LAST : TimeStepTp::MID;
496
497 this->get_current_time_step_() = time_step_type(step_type, reward, obs);
498 return this->get_current_time_step_();
499}
500
501template <uint_t side_size_>
503{
504
505 // reinitialize the board
506 auto obs = board_.init_board(side_size_, init_mode_);
507 auto reward = board_.get_reward();
509 return this->get_current_time_step_();
510}
511
512template <uint_t side_size_> bool Gridworld<side_size_>::is_game_lost() const
513{
514
515 auto player = board_.components.find(detail::board_component_type::PLAYER)->second.pos;
516 auto pit_pos = board_.components.find(detail::board_component_type::PIT)->second.pos;
517
518 if (player == pit_pos)
519 {
520 return true;
521 }
522
523 return false;
524}
525
526template <uint_t side_size_> void Gridworld<side_size_>::close() { board_.close(); }
527
528} // namespace envs::grid_world
529} // namespace bitrl
530
531#endif // GRID_WORLD_ENV_H
Forward declaration.
Definition time_step.h:22
detail::GridWorldEnv< side_size_ >::state_space state_space_type
Definition env_base.h:45
detail::GridWorldEnv< side_size_ >::state_type state_type
Definition env_base.h:48
const std::unordered_map< std::string, std::any > & reset_options() const noexcept
Definition env_base.h:105
EnvBase(const std::string &idx=bitsim::consts::INVALID_STR, const std::string &name=bitsim::consts::INVALID_STR)
void set_idx_(const std::string &idx) noexcept
Definition env_base.h:175
detail::GridWorldEnv< side_size_ >::action_space action_space_type
Definition env_base.h:51
TimeStep< detail::board_state_type > time_step_type
Definition env_base.h:42
detail::GridWorldEnv< side_size_ >::action_type action_type
Definition env_base.h:54
virtual 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)=0
void set_version_(const std::string &version) noexcept
Definition env_base.h:169
base_type::state_type state_type
The type of the action to be undertaken in the environment.
Definition grid_world_env.h:301
bool has_random_state() const noexcept
has_random_state
Definition grid_world_env.h:341
Gridworld()
Constructor.
Definition grid_world_env.h:425
static const uint_t n_components
n_components
Definition grid_world_env.h:265
void make(const std::string &version, const std::unordered_map< std::string, std::any > &options, const std::unordered_map< std::string, std::any > &reset_options) override final
make. Builds the environment. Optionally we can choose if the environment will be slippery
Definition grid_world_env.h:435
static uint_t n_copies()
Definition grid_world_env.h:381
base_type::action_type action_type
The type of the action to be undertaken in the environment.
Definition grid_world_env.h:296
uint_t n_actions() const noexcept
n_actions. Returns the number of actions
Definition grid_world_env.h:351
static const std::string name
name
Definition grid_world_env.h:260
GridWorldInitType init_type() const noexcept
init_type
Definition grid_world_env.h:375
static const uint_t side_size
side_size
Definition grid_world_env.h:270
base_type::time_step_type time_step_type
The time step type we return every time a step in the environment is performed.
Definition grid_world_env.h:281
base_type::state_space_type state_space_type
The type describing the state space for the environment.
Definition grid_world_env.h:286
uint_t seed() const noexcept
seed
Definition grid_world_env.h:357
EnvBase< TimeStep< detail::board_state_type >, detail::GridWorldEnv< side_size_ > > base_type
The base_type.
Definition grid_world_env.h:275
base_type::action_space_type action_space_type
The type of the action space for the environment.
Definition grid_world_env.h:291
time_step_type reset() override final
Reset the environment.
Definition grid_world_env.h:502
time_step_type step(const action_type &action) override final
step
Definition grid_world_env.h:486
uint_t n_states() const noexcept
n_states. Returns the number of states
Definition grid_world_env.h:346
bool is_game_lost() const
Returns true if the PLAYER position is the same as the PIT position.
Definition grid_world_env.h:512
void close() override final
close
Definition grid_world_env.h:526
real_t noise_factor() const noexcept
noise_factor
Definition grid_world_env.h:363
Definition grid_world_env.h:55
int_t min(const board_position &p)
Returns the min component of a position.
board_component_type
The BoardComponentType enum.
Definition grid_world_env.h:70
@ PLAYER
Definition grid_world_env.h:71
@ PIT
Definition grid_world_env.h:73
@ WALL
Definition grid_world_env.h:74
@ GOAL
Definition grid_world_env.h:72
bool operator!=(const board_position &p1, const board_position &p2)
Test if two positions are equal.
board_piece board_mask
Definition grid_world_env.h:143
std::pair< int, int > board_position
Models a position on the board.
Definition grid_world_env.h:59
int_t max(const board_position &p)
Returns the max component of a position.
std::vector< std::vector< std::vector< real_t > > > board_state_type
Array specifying the state of the board.
Definition grid_world_env.h:64
bool operator==(const board_position &p1, const board_position &p2)
Test if two positions are equal.
board_position operator+(const board_position &p1, const board_position &p2)
Add two positions and return their result.
board_move_type
The MoveType enum.
Definition grid_world_env.h:81
@ VALID
Definition grid_world_env.h:82
@ LOST_GAME
Definition grid_world_env.h:84
@ INVALID
Definition grid_world_env.h:83
Definition grid_world_env.h:36
GridWorldActionType
Definition grid_world_env.h:46
@ RIGHT
Definition grid_world_env.h:50
@ LEFT
Definition grid_world_env.h:49
@ DOWN
Definition grid_world_env.h:48
@ UP
Definition grid_world_env.h:47
GridWorldInitType
Definition grid_world_env.h:39
@ PLAYER
Definition grid_world_env.h:42
@ INVALID_TYPE
Definition grid_world_env.h:43
@ RANDOM
Definition grid_world_env.h:41
@ STATIC
Definition grid_world_env.h:40
std::atomic< uint_t > Gridworld< side_size >::n_copies_
Definition grid_world_env.h:422
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
@ MID
Definition time_step_type.h:17
@ FIRST
Definition time_step_type.h:16
@ LAST
Definition time_step_type.h:18
std::string uuid4()
Definition bitsim_utils.h:17
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
A scalar discrete space can be used to denote a space that only has a single value at each time....
Definition space_type.h:22
uint_t space_item_type
Definition space_type.h:27
static constexpr uint_t size
Definition space_type.h:38
Definition grid_world_env.h:228
detail::board state_space
Definition grid_world_env.h:230
detail::board_state_type state_type
Definition grid_world_env.h:231
action_space::space_item_type action_type
Definition grid_world_env.h:234
static constexpr uint_t STATE_SPACE_SIZE
Definition grid_world_env.h:232
static constexpr uint_t ACTION_SPACE_SIZE
Definition grid_world_env.h:235
ScalarDiscreteSpace< 0, 4 > action_space
Definition grid_world_env.h:233
The BoardPiece struct.
Definition grid_world_env.h:116
board_piece()=default
Default constructor.
board_position pos
pos 2-tuple e.g. (1,4)
Definition grid_world_env.h:125
std::string name
Name of the piece.
Definition grid_world_env.h:120
board_piece(std::string name_, board_position pos_)
BoardPiece.
Definition grid_world_env.h:132
Definition grid_world_env.h:149
std::mt19937 generator
Definition grid_world_env.h:156
uint_t seed
Definition grid_world_env.h:151
void build_random_mode()
build_random_mode
void move_piece(board_component_type piece, board_position pos)
move_piece Move the pice to the given position
void build_static_mode()
build_static_mode
board_move_type validate_move(board_component_type piece, board_position pos) const
validate_move_
uint_t board_size
Definition grid_world_env.h:150
board_state_type init_board(uint_t board_s, GridWorldInitType init_type)
initialize the board
void check_and_move(int_t row, int_t col)
check if the given move is valid and change the position of the player if the move either causes the ...
board_state_type get_state() const
get_state. Returns the state of the board
void build_player_mode()
build_player_mode
board_state_type step(GridWorldActionType action)
Execute the action on the board.
bool is_board_init
Definition grid_world_env.h:159
real_t get_reward() const
Get the reward the board currently returns depending on the position of the player.
std::map< std::string, board_mask > masks
Definition grid_world_env.h:158
std::map< board_component_type, board_piece > components
Definition grid_world_env.h:157