1#ifndef GRID_WORLD_ENV_H
2#define GRID_WORLD_ENV_H
11#include "bitrl/base/bitrl_config.h"
29#include <unordered_map>
133 :
name(std::move(name_)),
pos(std::move(pos_))
158 std::map<std::string, board_mask>
masks;
250template <u
int_t s
ide_size_>
252 :
public EnvBase<TimeStep<detail::board_state_type>, detail::GridWorldEnv<side_size_>>
255 static_assert(side_size_ >= 4,
"The side size should be greater than or equal to 4");
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;
335 void close() override final;
346 [[nodiscard]]
uint_t n_states() const noexcept {
return side_size_ * side_size_; }
392 bool randomize_state_;
422template <u
int_t s
ide_size> std::atomic<uint_t> Gridworld<side_size>::n_copies_ = 0;
424template <u
int_t s
ide_size_>
429 noise_factor_(0.0), board_()
434template <u
int_t s
ide_size_>
436 const std::unordered_map<std::string, std::any> &options,
437 const std::unordered_map<std::string, std::any> &
reset_options)
445 if (
const auto mode = options.find(
"mode"); mode != options.end())
447 init_mode_ = std::any_cast<GridWorldInitType>(mode->second);
454 if (
const auto seed = options.find(
"seed");
seed != options.end())
456 seed_ = std::any_cast<uint_t>(
seed->second);
459 board_.generator.seed(board_.seed);
463 noise_factor_ = std::any_cast<real_t>(
noise_factor->second);
466 if (
const auto randomize_state = options.find(
"randomize_state");
467 randomize_state != options.end())
469 randomize_state_ = std::any_cast<bool>(randomize_state->second);
473 board_.init_board(side_size_, init_mode_);
484template <u
int_t s
ide_size_>
490 auto reward = board_.get_reward();
501template <u
int_t s
ide_size_>
506 auto obs = board_.init_board(side_size_, init_mode_);
507 auto reward = board_.get_reward();
518 if (player == pit_pos)
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
virtual time_step_type reset()=0
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
std::string version() const noexcept
Definition env_base.h:143
time_step_type & get_current_time_step_() noexcept
Definition env_base.h:209
std::string idx() const noexcept
Definition env_base.h:125
void make_created_() noexcept
Definition env_base.h:206
bool is_created() const noexcept
Definition env_base.h:131
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