bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
multi_agent_time_step.h
Go to the documentation of this file.
1#ifndef MULTI_AGENT_TIME_STEP_H
2#define MULTI_AGENT_TIME_STEP_H
3
6
7#include <any>
8#include <numeric>
9#include <ostream>
10#include <stdexcept>
11#include <string>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16namespace bitrl
17{
18namespace envs::multi_agent
19{
20
30template <typename ObservationType> class MultiAgentTimeStep
31{
32 public:
33 typedef ObservationType observation_type;
34
35 MultiAgentTimeStep() = default;
36
40 MultiAgentTimeStep(TimeStepTp type, std::vector<observation_type> observations,
41 std::vector<real_t> rewards, std::vector<bool> dones, bool truncated = false,
42 std::unordered_map<std::string, std::any> extra = {});
43
47 TimeStepTp type() const noexcept { return type_; }
48
49 bool first() const noexcept { return type_ == TimeStepTp::FIRST; }
50
51 bool mid() const noexcept { return type_ == TimeStepTp::MID; }
52
56 bool last() const noexcept { return type_ == TimeStepTp::LAST; }
57
61 bool truncated() const noexcept { return truncated_; }
62
63 uint_t n_agents() const noexcept { return static_cast<uint_t>(observations_.size()); }
64
65 const observation_type &observation(uint_t agent) const { return observations_.at(agent); }
66
67 const std::vector<observation_type> &observations() const noexcept { return observations_; }
68
69 real_t reward(uint_t agent) const { return rewards_.at(agent); }
70
71 const std::vector<real_t> &rewards() const noexcept { return rewards_; }
72
76 real_t total_reward() const noexcept
77 {
78 return std::accumulate(rewards_.begin(), rewards_.end(), real_t(0));
79 }
80
81 bool done(uint_t agent) const { return dones_.at(agent); }
82
83 const std::vector<bool> &dones() const noexcept { return dones_; }
84
88 const std::unordered_map<std::string, std::any> &extra() const noexcept { return extra_; }
89
93 template <typename T> T get_extra(const std::string &name) const;
94
95 private:
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_;
102};
103
104template <typename ObservationType>
106 TimeStepTp type, std::vector<observation_type> observations, std::vector<real_t> rewards,
107 std::vector<bool> dones, bool truncated, std::unordered_map<std::string, std::any> extra)
108 : type_(type), observations_(std::move(observations)), rewards_(std::move(rewards)),
109 dones_(std::move(dones)), truncated_(truncated), extra_(std::move(extra))
110{
111 if (rewards_.size() != observations_.size() || dones_.size() != observations_.size())
112 {
113 throw std::invalid_argument("MultiAgentTimeStep: every agent needs an observation, a "
114 "reward and a done flag");
115 }
116}
117
118template <typename ObservationType>
119template <typename T>
121{
122 const auto itr = extra_.find(name);
123 if (itr == extra_.end())
124 {
125 throw std::logic_error("MultiAgentTimeStep: no extra entry " + name);
126 }
127 return std::any_cast<T>(itr->second);
128}
129
130template <typename ObservationType>
131std::ostream &operator<<(std::ostream &out, const MultiAgentTimeStep<ObservationType> &step)
132{
133 out << "MultiAgentTimeStep(" << TimeStepEnumUtils::to_string(step.type())
134 << ", agents=" << step.n_agents() << ", rewards=[";
135 for (uint_t a = 0; a < step.n_agents(); ++a)
136 {
137 out << (a ? ", " : "") << step.reward(a);
138 }
139 out << "], done=[";
140 for (uint_t a = 0; a < step.n_agents(); ++a)
141 {
142 out << (a ? ", " : "") << (step.done(a) ? "yes" : "no");
143 }
144 out << "]" << (step.truncated() ? ", truncated" : "") << ")";
145 return out;
146}
147
148} // namespace envs::multi_agent
149} // namespace bitrl
150
151#endif // MULTI_AGENT_TIME_STEP_H
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
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)