bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
multi_agent_env_base.h
Go to the documentation of this file.
1#ifndef MULTI_AGENT_ENV_BASE_H
2#define MULTI_AGENT_ENV_BASE_H
3
7
8#include <any>
9#include <stdexcept>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14namespace bitrl
15{
16namespace envs::multi_agent
17{
18
33template <typename ObservationType, typename ActionType> class MultiAgentEnvBase
34{
35 public:
36 typedef ObservationType observation_type;
37 typedef ActionType action_type;
39
43 typedef std::vector<action_type> joint_action_type;
44
45 static constexpr uint_t DEFAULT_ENV_SEED = 42;
46
47 virtual ~MultiAgentEnvBase() = default;
48
55 virtual void make(const std::string &version,
56 const std::unordered_map<std::string, std::any> &make_options,
57 const std::unordered_map<std::string, std::any> &reset_options);
58
62 time_step_type reset() { return reset(next_reset_seed_()); }
63
67 virtual time_step_type reset(uint_t seed) = 0;
68
72 virtual time_step_type step(const joint_action_type &actions) = 0;
73
77 virtual void close() { is_created_ = false; }
78
79 uint_t n_agents() const noexcept { return static_cast<uint_t>(agent_names_.size()); }
80
84 const std::string &agent_name(uint_t agent) const { return agent_names_.at(agent); }
85
86 bool is_created() const noexcept { return is_created_; }
87
88 std::string env_name() const noexcept { return name_; }
89
90 std::string version() const noexcept { return version_; }
91
95 uint_t n_steps() const noexcept { return n_steps_; }
96
100 uint_t max_steps() const noexcept { return max_steps_; }
101
102 uint_t last_reset_seed() const noexcept { return last_reset_seed_; }
103
104 const std::unordered_map<std::string, std::any> &make_options() const noexcept
105 {
106 return make_options_;
107 }
108
109 protected:
110 explicit MultiAgentEnvBase(const std::string &name) : name_(name) {}
111
115 void set_agents_(std::vector<std::string> names) { agent_names_ = std::move(names); }
116
117 void set_max_steps_(uint_t max_steps) noexcept { max_steps_ = max_steps; }
118
119 void make_created_() noexcept { is_created_ = true; }
120
124 void start_episode_() noexcept { n_steps_ = 0; }
125
130 bool count_step_() noexcept { return ++n_steps_ >= max_steps_; }
131
135 void check_step_(const joint_action_type &actions) const;
136
140 template <typename T> T option_(const std::string &name, const T &default_value) const;
141
142 private:
143 uint_t next_reset_seed_();
144
145 std::string name_;
146 std::string version_;
147 std::vector<std::string> agent_names_;
148 std::unordered_map<std::string, std::any> make_options_;
149 std::unordered_map<std::string, std::any> reset_options_;
150 bool is_created_{false};
151 uint_t n_steps_{0};
152 uint_t max_steps_{1000};
153 uint_t n_resets_{0};
154 uint_t last_reset_seed_{DEFAULT_ENV_SEED};
155};
156
157template <typename ObservationType, typename ActionType>
159 const std::string &version, const std::unordered_map<std::string, std::any> &make_options,
160 const std::unordered_map<std::string, std::any> &reset_options)
161{
162 version_ = version;
163 make_options_ = make_options;
164 reset_options_ = reset_options;
165 n_resets_ = 0;
166 max_steps_ = option_<uint_t>("max_steps", max_steps_);
167}
168
169template <typename ObservationType, typename ActionType>
170uint_t MultiAgentEnvBase<ObservationType, ActionType>::next_reset_seed_()
171{
172 const uint_t seed = reset_options_.contains("seed")
173 ? bitsim::utils::resolve<uint_t>("seed", reset_options_)
174 : DEFAULT_ENV_SEED;
175 last_reset_seed_ = seed + n_resets_++;
176 return last_reset_seed_;
177}
178
179template <typename ObservationType, typename ActionType>
181 const joint_action_type &actions) const
182{
183 if (!is_created_)
184 {
185 throw std::logic_error(name_ + ": call make() before step()");
186 }
187 if (actions.size() != n_agents())
188 {
189 throw std::invalid_argument(name_ + ": step() needs one action per agent, " +
190 std::to_string(n_agents()) + " actions, but got " +
191 std::to_string(actions.size()));
192 }
193}
194
195template <typename ObservationType, typename ActionType>
196template <typename T>
198 const T &default_value) const
199{
200 const auto itr = make_options_.find(name);
201 return itr == make_options_.end() ? default_value : std::any_cast<T>(itr->second);
202}
203
204} // namespace envs::multi_agent
205} // namespace bitrl
206
207#endif // MULTI_AGENT_ENV_BASE_H
bool is_created() const noexcept
Definition multi_agent_env_base.h:86
std::string env_name() const noexcept
Definition multi_agent_env_base.h:88
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)
Build the environment.
Definition multi_agent_env_base.h:158
std::string version() const noexcept
Definition multi_agent_env_base.h:90
virtual time_step_type step(const joint_action_type &actions)=0
Apply one action per agent.
MultiAgentEnvBase(const std::string &name)
Definition multi_agent_env_base.h:110
void check_step_(const joint_action_type &actions) const
Throw unless make() was called and actions has one action per agent.
Definition multi_agent_env_base.h:180
void set_agents_(std::vector< std::string > names)
Name the agents; their number is the number of names.
Definition multi_agent_env_base.h:115
static constexpr uint_t DEFAULT_ENV_SEED
Definition multi_agent_env_base.h:45
uint_t n_agents() const noexcept
Definition multi_agent_env_base.h:79
virtual void close()
Release the environment; make() builds it again.
Definition multi_agent_env_base.h:77
const std::unordered_map< std::string, std::any > & make_options() const noexcept
Definition multi_agent_env_base.h:104
uint_t max_steps() const noexcept
The steps after which an episode is truncated.
Definition multi_agent_env_base.h:100
void make_created_() noexcept
Definition multi_agent_env_base.h:119
time_step_type reset()
Start a new episode with the next seed.
Definition multi_agent_env_base.h:62
ActionType action_type
Definition multi_agent_env_base.h:37
void start_episode_() noexcept
Start counting the steps of a new episode, for reset().
Definition multi_agent_env_base.h:124
uint_t last_reset_seed() const noexcept
Definition multi_agent_env_base.h:102
void set_max_steps_(uint_t max_steps) noexcept
Definition multi_agent_env_base.h:117
const std::string & agent_name(uint_t agent) const
The name of an agent, for printing.
Definition multi_agent_env_base.h:84
std::vector< action_type > joint_action_type
One action per agent, in the order of the agents.
Definition multi_agent_env_base.h:43
virtual time_step_type reset(uint_t seed)=0
Start a new episode with the given seed.
ObservationType observation_type
Definition multi_agent_env_base.h:36
T option_(const std::string &name, const T &default_value) const
Read a make option, or give default_value when it is missing.
Definition multi_agent_env_base.h:197
bool count_step_() noexcept
Count a step, for step().
Definition multi_agent_env_base.h:130
MultiAgentTimeStep< ObservationType > time_step_type
Definition multi_agent_env_base.h:38
uint_t n_steps() const noexcept
The steps of the current episode so far.
Definition multi_agent_env_base.h:95
What a multi-agent environment returns after a reset or a joint step: an observation,...
Definition multi_agent_time_step.h:31
Definition distributed_agent_env.h:19
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:24
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Various utilities for std::map.