bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
multi_agent_grid_world.h
Go to the documentation of this file.
1#ifndef MULTI_AGENT_GRID_WORLD_H
2#define MULTI_AGENT_GRID_WORLD_H
3
6
7#include <any>
8#include <string>
9#include <unordered_map>
10#include <utility>
11#include <vector>
12
13namespace bitrl
14{
15namespace envs::multi_agent
16{
17
21enum class GridMove : uint_t
22{
23 STAY = 0,
24 UP = 1,
25 RIGHT = 2,
26 DOWN = 3,
27 LEFT = 4
28};
29
38
64class MultiAgentGridWorld final : public MultiAgentEnvBase<uint_t, uint_t>
65{
66 public:
70
74 typedef std::pair<uint_t, uint_t> cell_type;
75
76 static constexpr uint_t N_ACTIONS = 5;
77
83 static std::vector<std::string> default_layout();
84
86
87 void make(const std::string &version,
88 const std::unordered_map<std::string, std::any> &make_options,
89 const std::unordered_map<std::string, std::any> &reset_options) override;
90
91 using base_type::reset;
92
93 time_step_type reset(uint_t seed) override;
94
95 time_step_type step(const joint_action_type &actions) override;
96
97 uint_t n_rows() const noexcept { return n_rows_; }
98
99 uint_t n_cols() const noexcept { return n_cols_; }
100
101 uint_t n_cells() const noexcept { return n_rows_ * n_cols_; }
102
106 uint_t n_states() const noexcept { return n_states_; }
107
108 uint_t n_actions() const noexcept { return N_ACTIONS; }
109
110 GridObservation observation_type() const noexcept { return observation_; }
111
115 bool is_free(const cell_type &cell) const noexcept;
116
120 uint_t cell_index(const cell_type &cell) const noexcept
121 {
122 return cell.first * n_cols_ + cell.second;
123 }
124
125 const std::vector<cell_type> &positions() const noexcept { return positions_; }
126
127 const std::vector<cell_type> &goals() const noexcept { return goals_; }
128
132 uint_t n_collisions() const noexcept { return n_collisions_; }
133
137 std::string render() const;
138
139 private:
140 time_step_type make_time_step_(TimeStepTp type, std::vector<real_t> rewards, bool truncated,
141 uint_t collisions) const;
142
143 uint_t observe_(uint_t agent) const;
144
145 std::vector<std::string> layout_;
146 std::vector<cell_type> starts_;
147 std::vector<cell_type> goals_;
148 std::vector<cell_type> positions_;
149 std::vector<bool> dones_;
150 uint_t n_rows_{0};
151 uint_t n_cols_{0};
152 uint_t n_states_{0};
153 uint_t n_collisions_{0};
155 bool shared_reward_{false};
156 real_t step_reward_{-0.05};
157 real_t goal_reward_{1.0};
158 real_t collision_reward_{-0.5};
159};
160
161} // namespace envs::multi_agent
162} // namespace bitrl
163
164#endif // MULTI_AGENT_GRID_WORLD_H
std::string version() const noexcept
Definition multi_agent_env_base.h:90
MultiAgentEnvBase(const std::string &name)
Definition multi_agent_env_base.h:110
const std::unordered_map< std::string, std::any > & make_options() const noexcept
Definition multi_agent_env_base.h:104
time_step_type reset()
Definition multi_agent_env_base.h:62
std::vector< action_type > joint_action_type
Definition multi_agent_env_base.h:43
MultiAgentTimeStep< uint_t > time_step_type
Definition multi_agent_env_base.h:38
std::string render() const
The map with the agents in upper case and their goals in lower case.
time_step_type step(const joint_action_type &actions) override
Apply one action per agent.
uint_t n_collisions() const noexcept
The moves cancelled by collisions in the current episode.
Definition multi_agent_grid_world.h:132
uint_t cell_index(const cell_type &cell) const noexcept
The index of a cell, row * n_cols() + col.
Definition multi_agent_grid_world.h:120
base_type::joint_action_type joint_action_type
Definition multi_agent_grid_world.h:69
MultiAgentEnvBase< uint_t, uint_t > base_type
Definition multi_agent_grid_world.h:67
const std::vector< cell_type > & goals() const noexcept
Definition multi_agent_grid_world.h:127
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) override
Build the environment.
GridObservation observation_type() const noexcept
Definition multi_agent_grid_world.h:110
base_type::time_step_type time_step_type
Definition multi_agent_grid_world.h:68
uint_t n_cells() const noexcept
Definition multi_agent_grid_world.h:101
static std::vector< std::string > default_layout()
Two rooms joined by a corridor one cell wide. Agent A goes from the top left to the bottom right,...
std::pair< uint_t, uint_t > cell_type
A cell: row, column.
Definition multi_agent_grid_world.h:74
uint_t n_states() const noexcept
The number of observations of one agent.
Definition multi_agent_grid_world.h:106
uint_t n_cols() const noexcept
Definition multi_agent_grid_world.h:99
bool is_free(const cell_type &cell) const noexcept
Whether a cell is inside the map and not a wall.
uint_t n_rows() const noexcept
Definition multi_agent_grid_world.h:97
uint_t n_actions() const noexcept
Definition multi_agent_grid_world.h:108
static constexpr uint_t N_ACTIONS
Definition multi_agent_grid_world.h:76
const std::vector< cell_type > & positions() const noexcept
Definition multi_agent_grid_world.h:125
time_step_type reset(uint_t seed) override
Start a new episode with the given seed.
Definition distributed_agent_env.h:19
GridMove
The moves of an agent of MultiAgentGridWorld.
Definition multi_agent_grid_world.h:22
@ RIGHT
Definition multi_agent_grid_world.h:25
@ LEFT
Definition multi_agent_grid_world.h:27
@ STAY
Definition multi_agent_grid_world.h:23
@ DOWN
Definition multi_agent_grid_world.h:26
@ UP
Definition multi_agent_grid_world.h:24
GridObservation
What an agent of MultiAgentGridWorld observes.
Definition multi_agent_grid_world.h:34
@ JOINT_POSITIONS
its own cell and the cells of the others
Definition multi_agent_grid_world.h:36
@ OWN_POSITION
its own cell only
Definition multi_agent_grid_world.h:35
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
TimeStepTp
The TimeStepTp enum.
Definition time_step_type.h:15
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20