bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
distributed_agent_env.h
Go to the documentation of this file.
1#ifndef DISTRIBUTED_AGENT_ENV_H
2#define DISTRIBUTED_AGENT_ENV_H
3
7
8#include <array>
9#include <bit>
10#include <cstdint>
11#include <stdexcept>
12#include <string>
13#include <type_traits>
14#include <vector>
15
16namespace bitrl
17{
19{
20
27template <typename T, typename Enable = void> struct AgentValues;
28
29template <typename T>
30struct AgentValues<T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>>>
31{
32 static void append(const T &value, std::vector<real_t> &out)
33 {
34 out.push_back(static_cast<real_t>(value));
35 }
36 static T read(const std::vector<real_t> &values, std::size_t first, std::size_t /*count*/)
37 {
38 if constexpr (std::is_enum_v<T>)
39 {
40 return static_cast<T>(static_cast<std::underlying_type_t<T>>(values.at(first)));
41 }
42 else
43 {
44 return static_cast<T>(values.at(first));
45 }
46 }
47};
48
49template <typename T, std::size_t N> struct AgentValues<std::array<T, N>>
50{
51 static void append(const std::array<T, N> &value, std::vector<real_t> &out)
52 {
53 for (const auto &v : value)
54 {
55 out.push_back(static_cast<real_t>(v));
56 }
57 }
58 static std::array<T, N> read(const std::vector<real_t> &values, std::size_t first,
59 std::size_t count)
60 {
61 if (count != N)
62 {
63 throw std::invalid_argument("AgentValues: expected " + std::to_string(N) +
64 " values, got " + std::to_string(count));
65 }
66 std::array<T, N> a{};
67 for (std::size_t i = 0; i < N; ++i)
68 {
69 a[i] = static_cast<T>(values.at(first + i));
70 }
71 return a;
72 }
73};
74
75template <typename T> struct AgentValues<std::vector<T>>
76{
77 static void append(const std::vector<T> &value, std::vector<real_t> &out)
78 {
79 for (const auto &v : value)
80 {
81 out.push_back(static_cast<real_t>(v));
82 }
83 }
84 static std::vector<T> read(const std::vector<real_t> &values, std::size_t first,
85 std::size_t count)
86 {
87 std::vector<T> v(count);
88 for (std::size_t i = 0; i < count; ++i)
89 {
90 v[i] = static_cast<T>(values.at(first + i));
91 }
92 return v;
93 }
94};
95
100template <typename T> real_t observation_checksum(const T &observation)
101{
102 std::vector<real_t> values;
103 AgentValues<T>::append(observation, values);
104 std::uint64_t h = 1469598103934665603ULL; // FNV-1a
105 for (const real_t v : values)
106 {
107 const auto bits = std::bit_cast<std::uint64_t>(v + 0.0); // -0 and 0 alike
108 for (int b = 0; b < 64; b += 8)
109 {
110 h ^= (bits >> b) & 0xFFu;
111 h *= 1099511628211ULL;
112 }
113 }
114 return static_cast<real_t>(h & ((std::uint64_t(1) << 53) - 1));
115}
116
126
150template <typename EnvType> class DistributedAgentEnv
151{
152 public:
153 typedef EnvType env_type;
154 typedef typename EnvType::time_step_type time_step_type;
155 typedef typename EnvType::action_type action_type;
156 typedef typename EnvType::observation_type observation_type;
157 typedef typename EnvType::joint_action_type joint_action_type;
158
165 const DistributedAgentConfig &config = {});
166
170 time_step_type reset() { return remember_(env_.reset()); }
171
175 time_step_type reset(uint_t seed) { return remember_(env_.reset(seed)); }
176
183
187 void leave() { sync_.leave(); }
188
189 uint_t agent() const noexcept { return agent_; }
190
191 uint_t n_agents() const noexcept { return env_.n_agents(); }
192
193 const EnvType &env() const noexcept { return env_; }
194
195 const bitsim::distributed::Synchroniser &synchroniser() const noexcept { return sync_; }
196
197 private:
198 static bitsim::distributed::SynchroniserConfig sync_config_(const EnvType &env, uint_t agent,
199 const DistributedAgentConfig &c);
200
202
203 EnvType &env_;
204 uint_t agent_;
207 std::vector<real_t> checksums_;
208 uint_t n_exchanges_{0};
209 bool started_{false};
210};
211
212template <typename EnvType>
214DistributedAgentEnv<EnvType>::sync_config_(const EnvType &env, uint_t agent,
215 const DistributedAgentConfig &c)
216{
217 if (!env.is_created())
218 {
219 throw std::logic_error("DistributedAgentEnv: make() the environment first");
220 }
221 if (agent >= env.n_agents())
222 {
223 throw std::invalid_argument("DistributedAgentEnv: no agent " + std::to_string(agent));
224 }
225 bitsim::distributed::SynchroniserConfig s;
227 s.timeout = c.timeout;
228 s.resend_period = c.resend_period;
229 for (uint_t a = 0; a < env.n_agents(); ++a)
230 {
231 if (a != agent)
232 {
233 s.peers.push_back(env.agent_name(a));
234 }
235 }
236 return s;
237}
238
239template <typename EnvType>
242 const DistributedAgentConfig &config)
243 : env_(env), agent_(agent), config_(config),
244 sync_(transport, env.agent_name(agent), sync_config_(env, agent, config))
245{
246}
247
248template <typename EnvType>
250DistributedAgentEnv<EnvType>::remember_(time_step_type step)
251{
252 started_ = true;
253 checksums_.resize(step.n_agents());
254 for (uint_t a = 0; a < step.n_agents(); ++a)
255 {
256 checksums_[a] = config_.check_replicas ? observation_checksum(step.observation(a)) : 0.0;
257 }
258 return step;
259}
260
261template <typename EnvType>
264{
265 if (!started_)
266 {
267 throw std::logic_error("DistributedAgentEnv: call reset() before step()");
268 }
269 // the action, then the checksum of what it was chosen from
270 std::vector<real_t> values;
271 AgentValues<action_type>::append(action, values);
272 values.push_back(checksums_[agent_]);
273
274 const auto &peers = sync_.exchange(n_exchanges_++, {}, values);
275
276 joint_action_type actions(n_agents());
277 actions[agent_] = action;
278 for (const auto &peer : peers)
279 {
280 uint_t a = 0;
281 while (a < n_agents() && env_.agent_name(a) != peer.participant)
282 {
283 ++a;
284 }
285 // a peer that left at this step sent it with its leave; one that left before did not
286 if (peer.step != n_exchanges_ - 1 || peer.values.empty())
287 {
288 throw std::runtime_error("DistributedAgentEnv " + env_.agent_name(agent_) + ": " +
289 peer.participant + " has left");
290 }
291 if (config_.check_replicas && peer.values.back() != checksums_[a])
292 {
293 throw std::runtime_error("DistributedAgentEnv " + env_.agent_name(agent_) +
294 ": the copies of the environment differ; " + peer.participant +
295 " observed something else at step " +
296 std::to_string(n_exchanges_ - 1));
297 }
298 actions[a] = AgentValues<action_type>::read(peer.values, 0, peer.values.size() - 1);
299 }
300 return remember_(env_.step(actions));
301}
302
303} // namespace envs::multi_agent
304} // namespace bitrl
305
306#endif // DISTRIBUTED_AGENT_ENV_H
time_step_type reset(uint_t seed)
Start a new episode with seed; every participant must use the same.
Definition distributed_agent_env.h:175
EnvType::action_type action_type
Definition distributed_agent_env.h:155
uint_t n_agents() const noexcept
Definition distributed_agent_env.h:191
uint_t agent() const noexcept
Definition distributed_agent_env.h:189
const bitsim::distributed::Synchroniser & synchroniser() const noexcept
Definition distributed_agent_env.h:195
DistributedAgentEnv(EnvType &env, uint_t agent, bitsim::distributed::Transport &transport, const DistributedAgentConfig &config={})
Definition distributed_agent_env.h:240
time_step_type reset()
Start a new episode with the next seed of the copy.
Definition distributed_agent_env.h:170
EnvType::time_step_type time_step_type
Definition distributed_agent_env.h:154
const MultiAgentGridWorld & env() const noexcept
Definition distributed_agent_env.h:193
EnvType::observation_type observation_type
Definition distributed_agent_env.h:156
EnvType::joint_action_type joint_action_type
Definition distributed_agent_env.h:157
EnvType env_type
Definition distributed_agent_env.h:153
time_step_type step(const action_type &action)
Send this agent's action, wait for the others', and step the copy.
Definition distributed_agent_env.h:263
void leave()
Tell the others that this participant stops.
Definition distributed_agent_env.h:187
Keeps the participants of a distributed simulation in step.
Definition synchroniser.h:99
How the participants of a distributed simulation reach each other: everything one publishes,...
Definition transport.h:34
Definition distributed_agent_env.h:19
real_t observation_checksum(const T &observation)
A checksum of an observation that a real_t holds exactly, to tell that two copies of an environment a...
Definition distributed_agent_env.h:100
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
@ LOCKSTEP
Definition synchroniser.h:31
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
env
Definition play.py:33
static void append(const T &value, std::vector< real_t > &out)
Definition distributed_agent_env.h:32
static T read(const std::vector< real_t > &values, std::size_t first, std::size_t)
Definition distributed_agent_env.h:36
static void append(const std::array< T, N > &value, std::vector< real_t > &out)
Definition distributed_agent_env.h:51
static std::array< T, N > read(const std::vector< real_t > &values, std::size_t first, std::size_t count)
Definition distributed_agent_env.h:58
static std::vector< T > read(const std::vector< real_t > &values, std::size_t first, std::size_t count)
Definition distributed_agent_env.h:84
static void append(const std::vector< T > &value, std::vector< real_t > &out)
Definition distributed_agent_env.h:77
How an action or an observation travels as numbers, for DistributedAgentEnv.
Definition distributed_agent_env.h:27
The settings of DistributedAgentEnv.
Definition distributed_agent_env.h:121
real_t timeout
how long a step waits for the others [s]
Definition distributed_agent_env.h:122
real_t resend_period
how often a waiting step sends its last steps again [s]
Definition distributed_agent_env.h:123
bool check_replicas
throw if the copies of the environment differ
Definition distributed_agent_env.h:124
The settings of Synchroniser.
Definition synchroniser.h:45
SyncMode mode
Definition synchroniser.h:46
real_t timeout
lockstep: the longest wait for a step [s]
Definition synchroniser.h:56
std::vector< std::string > peers
Definition synchroniser.h:54
real_t resend_period
lockstep: send the last steps again this often while waiting [s]
Definition synchroniser.h:57