1#ifndef MULTI_AGENT_TIME_STEP_H
2#define MULTI_AGENT_TIME_STEP_H
12#include <unordered_map>
42 std::unordered_map<std::string, std::any>
extra = {});
61 bool truncated() const noexcept {
return truncated_; }
67 const std::vector<observation_type> &
observations() const noexcept {
return observations_; }
71 const std::vector<real_t> &
rewards() const noexcept {
return rewards_; }
78 return std::accumulate(rewards_.begin(), rewards_.end(),
real_t(0));
81 bool done(
uint_t agent)
const {
return dones_.at(agent); }
83 const std::vector<bool> &
dones() const noexcept {
return dones_; }
88 const std::unordered_map<std::string, std::any> &
extra() const noexcept {
return extra_; }
93 template <
typename T> T
get_extra(
const std::string &name)
const;
97 std::vector<observation_type> observations_;
98 std::vector<real_t> rewards_;
99 std::vector<bool> dones_;
100 bool truncated_{
false};
101 std::unordered_map<std::string, std::any> extra_;
104template <
typename ObservationType>
111 if (rewards_.size() != observations_.size() || dones_.size() != observations_.size())
113 throw std::invalid_argument(
"MultiAgentTimeStep: every agent needs an observation, a "
114 "reward and a done flag");
118template <
typename ObservationType>
122 const auto itr = extra_.find(name);
123 if (itr == extra_.end())
125 throw std::logic_error(
"MultiAgentTimeStep: no extra entry " + name);
127 return std::any_cast<T>(itr->second);
130template <
typename ObservationType>
134 <<
", agents=" << step.n_agents() <<
", rewards=[";
135 for (
uint_t a = 0; a < step.n_agents(); ++a)
137 out << (a ?
", " :
"") << step.reward(a);
140 for (
uint_t a = 0; a < step.n_agents(); ++a)
142 out << (a ?
", " :
"") << (step.done(a) ?
"yes" :
"no");
144 out <<
"]" << (step.truncated() ?
", truncated" :
"") <<
")";
What a multi-agent environment returns after a reset or a joint step: an observation,...
Definition multi_agent_time_step.h:31
const std::unordered_map< std::string, std::any > & extra() const noexcept
Anything else the environment reports, collisions for example.
Definition multi_agent_time_step.h:88
ObservationType observation_type
Definition multi_agent_time_step.h:33
real_t reward(uint_t agent) const
Definition multi_agent_time_step.h:69
bool truncated() const noexcept
Whether the step limit ended the episode before every agent was done.
Definition multi_agent_time_step.h:61
bool first() const noexcept
Definition multi_agent_time_step.h:49
const std::vector< observation_type > & observations() const noexcept
Definition multi_agent_time_step.h:67
const std::vector< real_t > & rewards() const noexcept
Definition multi_agent_time_step.h:71
real_t total_reward() const noexcept
The sum of the rewards of the agents, the team's reward.
Definition multi_agent_time_step.h:76
bool last() const noexcept
Whether the episode is over.
Definition multi_agent_time_step.h:56
uint_t n_agents() const noexcept
Definition multi_agent_time_step.h:63
bool done(uint_t agent) const
Definition multi_agent_time_step.h:81
bool mid() const noexcept
Definition multi_agent_time_step.h:51
const observation_type & observation(uint_t agent) const
Definition multi_agent_time_step.h:65
T get_extra(const std::string &name) const
Read an entry of extra().
Definition multi_agent_time_step.h:120
MultiAgentTimeStep()=default
const std::vector< bool > & dones() const noexcept
Definition multi_agent_time_step.h:83
TimeStepTp type() const noexcept
The type of the step: FIRST after a reset, LAST once the episode is over.
Definition multi_agent_time_step.h:47
Definition distributed_agent_env.h:19
std::ostream & operator<<(std::ostream &out, const MultiAgentTimeStep< ObservationType > &step)
Definition multi_agent_time_step.h:131
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
TimeStepTp
The TimeStepTp enum.
Definition time_step_type.h:15
@ INVALID_TYPE
Definition time_step_type.h:19
@ 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
double real_t
real_t
Definition bitsim_types.h:20
static std::string to_string(TimeStepTp type)