AIToolbox
A library that offers tools for AI problem solving.
GridWorld.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_MDP_GRIDWORLD_HEADER_FILE
2 #define AI_TOOLBOX_MDP_GRIDWORLD_HEADER_FILE
3 
4 #include <cstddef>
5 #include <array>
6 
7 namespace AIToolbox::MDP {
11  namespace GridWorldUtils {
15  enum Direction : size_t { UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, UP_RIGHT = 4, UP_LEFT = 5, DOWN_RIGHT = 6, DOWN_LEFT = 7 };
16  static constexpr std::array<Direction, 4> Directions4{{UP, RIGHT, DOWN, LEFT}};
17  static constexpr std::array<Direction, 8> Directions8{{UP, RIGHT, DOWN, LEFT, UP_RIGHT, UP_LEFT, DOWN_RIGHT, DOWN_LEFT}};
18  }
19 
23  class GridWorld {
24  public:
26  struct State {
27  operator size_t() const;
28  unsigned getX() const;
29  unsigned getY() const;
30 
31  bool operator==(const State & other) const;
32 
33  private:
34  State(int xx, int yy, size_t ss);
35  int x, y;
36  size_t s;
37  friend GridWorld;
38  };
39 
47  GridWorld(unsigned width, unsigned height, bool torus = false);
48 
60  State getAdjacent(Direction d, State s) const;
61 
70  State getAdjacent(size_t d, State s) const;
71 
77  unsigned distance(const State & s1, const State & s2) const;
78 
87  State operator()(int x, int y) const;
88 
94  State operator()(const size_t s) const;
95 
99  unsigned getWidth() const;
100 
104  unsigned getHeight() const;
105 
109  bool isTorus() const;
110 
114  size_t getS() const;
115 
116  private:
117  int boundX(int x) const;
118  int boundY(int y) const;
119 
120  unsigned width_, height_;
121  bool isTorus_;
122  };
123 }
124 
125 #endif
AIToolbox::MDP::GridWorld
This class represents a simple rectangular gridworld.
Definition: GridWorld.hpp:23
AIToolbox::MDP::GridWorldUtils::Directions8
static constexpr std::array< Direction, 8 > Directions8
Definition: GridWorld.hpp:17
AIToolbox::MDP::GridWorld::GridWorld
GridWorld(unsigned width, unsigned height, bool torus=false)
Basic constructor.
AIToolbox::MDP::GridWorldUtils::RIGHT
@ RIGHT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::getS
size_t getS() const
This function returns the number of cells in the grid.
AIToolbox::MDP::GridWorldUtils::UP_RIGHT
@ UP_RIGHT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::State::getX
unsigned getX() const
AIToolbox::MDP::GridWorld::isTorus
bool isTorus() const
This function returns whether the GridWorld represents a torus.
AIToolbox::MDP::GridWorld::getAdjacent
State getAdjacent(Direction d, State s) const
This function returns the state next to the input in the chosen Direction.
AIToolbox::MDP::GridWorldUtils::DOWN_LEFT
@ DOWN_LEFT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::operator()
State operator()(int x, int y) const
This function returns the State at the selected position.
AIToolbox::MDP
Definition: DoubleQLearning.hpp:10
AIToolbox::MDP::GridWorldUtils::DOWN_RIGHT
@ DOWN_RIGHT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::State::operator==
bool operator==(const State &other) const
AIToolbox::MDP::GridWorld::State
Definition: GridWorld.hpp:26
AIToolbox::MDP::GridWorldUtils::UP
@ UP
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorldUtils::Directions4
static constexpr std::array< Direction, 4 > Directions4
Definition: GridWorld.hpp:16
AIToolbox::MDP::GridWorldUtils::DOWN
@ DOWN
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorldUtils::UP_LEFT
@ UP_LEFT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::getWidth
unsigned getWidth() const
This function returns the width of the GridWorld.
AIToolbox::MDP::GridWorld::State::getY
unsigned getY() const
AIToolbox::MDP::GridWorldUtils::LEFT
@ LEFT
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::getHeight
unsigned getHeight() const
This function returns the height of the GridWorld.
AIToolbox::MDP::GridWorldUtils::Direction
Direction
The possible actions in a GridWorld-like environment.
Definition: GridWorld.hpp:15
AIToolbox::MDP::GridWorld::distance
unsigned distance(const State &s1, const State &s2) const
This function returns the Manhattan distance between the two states.