1#ifndef MULTI_AGENT_ENV_BASE_H
2#define MULTI_AGENT_ENV_BASE_H
11#include <unordered_map>
56 const std::unordered_map<std::string, std::any> &
make_options,
57 const std::unordered_map<std::string, std::any> &reset_options);
77 virtual void close() { is_created_ =
false; }
88 std::string
env_name() const noexcept {
return name_; }
90 std::string
version() const noexcept {
return version_; }
104 const std::unordered_map<std::string, std::any> &
make_options() const noexcept
106 return make_options_;
115 void set_agents_(std::vector<std::string> names) { agent_names_ = std::move(names); }
140 template <
typename T> T
option_(
const std::string &name,
const T &default_value)
const;
143 uint_t next_reset_seed_();
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};
157template <
typename ObservationType,
typename ActionType>
160 const std::unordered_map<std::string, std::any> &reset_options)
164 reset_options_ = reset_options;
169template <
typename ObservationType,
typename ActionType>
170uint_t MultiAgentEnvBase<ObservationType, ActionType>::next_reset_seed_()
172 const uint_t seed = reset_options_.contains(
"seed")
175 last_reset_seed_ = seed + n_resets_++;
176 return last_reset_seed_;
179template <
typename ObservationType,
typename ActionType>
185 throw std::logic_error(name_ +
": call make() before step()");
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()));
195template <
typename ObservationType,
typename ActionType>
198 const T &default_value)
const
200 const auto itr = make_options_.find(name);
201 return itr == make_options_.end() ? default_value : std::any_cast<T>(itr->second);
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
virtual ~MultiAgentEnvBase()=default
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.