bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
cliff_walk.h
Go to the documentation of this file.
1#ifndef EXAMPLES_CLIFF_WALK_H
2#define EXAMPLES_CLIFF_WALK_H
3
4// The cliff walk of Sutton and Barto, example 6.6, written in C++ so that the
5// experiments examples need no environment server:
6//
7// . . . . . . . . . . . .
8// . . . . . . . . . . . .
9// . . . . . . . . . . . .
10// S C C C C C C C C C C G
11//
12// The agent starts at S and the episode ends at G. Every step costs 1;
13// stepping onto the cliff, C, costs 100 and ends the episode. The actions
14// are 0 up, 1 right, 2 down, 3 left, as in Gymnasium's CliffWalking; moving
15// off the grid leaves the agent where it is. States are row * 12 + column.
16
19
20#include <string>
21
22namespace examples
23{
24
26{
27 public:
28 static constexpr bitrl::uint_t N_ROWS = 4;
29 static constexpr bitrl::uint_t N_COLS = 12;
30 static constexpr bitrl::uint_t START = (N_ROWS - 1) * N_COLS;
31 static constexpr bitrl::uint_t GOAL = N_ROWS * N_COLS - 1;
32
36
37 bitrl::uint_t n_states() const noexcept { return N_ROWS * N_COLS; }
38 bitrl::uint_t n_actions() const noexcept { return 4; }
39
40 static bool is_cliff(bitrl::uint_t s)
41 {
42 return s / N_COLS == N_ROWS - 1 && s % N_COLS > 0 && s % N_COLS < N_COLS - 1;
43 }
44
46 {
47 bitrl::uint_t row = s / N_COLS;
48 bitrl::uint_t col = s % N_COLS;
49 if (a == 0 && row > 0)
50 row -= 1;
51 else if (a == 1 && col < N_COLS - 1)
52 col += 1;
53 else if (a == 2 && row < N_ROWS - 1)
54 row += 1;
55 else if (a == 3 && col > 0)
56 col -= 1;
57 return row * N_COLS + col;
58 }
59
61 {
62 state_ = START;
63 return time_step_type(bitrl::TimeStepTp::FIRST, 0.0, state_);
64 }
65
67 {
68 state_ = move(state_, a);
69 if (is_cliff(state_))
70 {
71 return time_step_type(bitrl::TimeStepTp::LAST, -100.0, state_);
72 }
73 if (state_ == GOAL)
74 {
75 return time_step_type(bitrl::TimeStepTp::LAST, -1.0, state_);
76 }
77 return time_step_type(bitrl::TimeStepTp::MID, -1.0, state_);
78 }
79
80 // The grid with the path of a policy on it, for printing
81 template <typename Policy> static std::string draw(const Policy &policy)
82 {
83 std::string grid(N_ROWS * (N_COLS + 1), '.');
84 for (bitrl::uint_t r = 0; r < N_ROWS; ++r)
85 {
86 grid[r * (N_COLS + 1) + N_COLS] = '\n';
87 }
88 auto at = [](bitrl::uint_t s) { return (s / N_COLS) * (N_COLS + 1) + s % N_COLS; };
89 for (bitrl::uint_t s = START + 1; s < GOAL; ++s)
90 {
91 grid[at(s)] = 'C';
92 }
94 for (bitrl::uint_t i = 0; i < 2 * N_ROWS * N_COLS && s != GOAL && !is_cliff(s); ++i)
95 {
96 grid[at(s)] = '*';
97 s = move(s, policy(s));
98 }
99 grid[at(START)] = 'S';
100 grid[at(GOAL)] = 'G';
101 return grid;
102 }
103
104 private:
105 bitrl::uint_t state_{START};
106};
107
108} // namespace examples
109
110#endif // EXAMPLES_CLIFF_WALK_H
Forward declaration.
Definition time_step.h:22
Definition cliff_walk.h:26
static std::string draw(const Policy &policy)
Definition cliff_walk.h:81
static bool is_cliff(bitrl::uint_t s)
Definition cliff_walk.h:40
bitrl::uint_t state_type
Definition cliff_walk.h:33
static bitrl::uint_t move(bitrl::uint_t s, bitrl::uint_t a)
Definition cliff_walk.h:45
time_step_type step(action_type a)
Definition cliff_walk.h:66
static constexpr bitrl::uint_t START
Definition cliff_walk.h:30
bitrl::uint_t n_actions() const noexcept
Definition cliff_walk.h:38
static constexpr bitrl::uint_t N_COLS
Definition cliff_walk.h:29
static constexpr bitrl::uint_t N_ROWS
Definition cliff_walk.h:28
bitrl::uint_t action_type
Definition cliff_walk.h:34
static constexpr bitrl::uint_t GOAL
Definition cliff_walk.h:31
time_step_type reset()
Definition cliff_walk.h:60
bitrl::uint_t n_states() const noexcept
Definition cliff_walk.h:37
bitrl::TimeStep< bitrl::uint_t > time_step_type
Definition cliff_walk.h:35
@ MID
Definition time_step_type.h:17
@ FIRST
Definition time_step_type.h:16
@ LAST
Definition time_step_type.h:18
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Definition cliff_walk.h:23