1#ifndef DISTRIBUTED_AGENT_ENV_H
2#define DISTRIBUTED_AGENT_ENV_H
27template <
typename T,
typename Enable =
void>
struct AgentValues;
30struct AgentValues<T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>>>
32 static void append(
const T &value, std::vector<real_t> &out)
34 out.push_back(
static_cast<real_t>(value));
36 static T
read(
const std::vector<real_t> &values, std::size_t first, std::size_t )
38 if constexpr (std::is_enum_v<T>)
40 return static_cast<T
>(
static_cast<std::underlying_type_t<T>
>(values.at(first)));
44 return static_cast<T
>(values.at(first));
49template <
typename T, std::
size_t N>
struct AgentValues<std::array<T, N>>
51 static void append(
const std::array<T, N> &value, std::vector<real_t> &out)
53 for (
const auto &v : value)
55 out.push_back(
static_cast<real_t>(v));
58 static std::array<T, N>
read(
const std::vector<real_t> &values, std::size_t first,
63 throw std::invalid_argument(
"AgentValues: expected " + std::to_string(N) +
64 " values, got " + std::to_string(count));
67 for (std::size_t i = 0; i < N; ++i)
69 a[i] =
static_cast<T
>(values.at(first + i));
77 static void append(
const std::vector<T> &value, std::vector<real_t> &out)
79 for (
const auto &v : value)
81 out.push_back(
static_cast<real_t>(v));
84 static std::vector<T>
read(
const std::vector<real_t> &values, std::size_t first,
87 std::vector<T> v(count);
88 for (std::size_t i = 0; i < count; ++i)
90 v[i] =
static_cast<T
>(values.at(first + i));
102 std::vector<real_t> values;
104 std::uint64_t h = 1469598103934665603ULL;
105 for (
const real_t v : values)
107 const auto bits = std::bit_cast<std::uint64_t>(v + 0.0);
108 for (
int b = 0; b < 64; b += 8)
110 h ^= (bits >> b) & 0xFFu;
111 h *= 1099511628211ULL;
114 return static_cast<real_t>(h & ((std::uint64_t(1) << 53) - 1));
193 const EnvType &
env() const noexcept {
return env_; }
207 std::vector<real_t> checksums_;
209 bool started_{
false};
212template <
typename EnvType>
214DistributedAgentEnv<EnvType>::sync_config_(
const EnvType &env,
uint_t agent,
215 const DistributedAgentConfig &c)
217 if (!env.is_created())
219 throw std::logic_error(
"DistributedAgentEnv: make() the environment first");
221 if (agent >=
env.n_agents())
223 throw std::invalid_argument(
"DistributedAgentEnv: no agent " + std::to_string(agent));
225 bitsim::distributed::SynchroniserConfig s;
229 for (
uint_t a = 0; a <
env.n_agents(); ++a)
233 s.
peers.push_back(
env.agent_name(a));
239template <
typename EnvType>
243 : env_(
env), agent_(
agent), config_(config),
248template <
typename EnvType>
250DistributedAgentEnv<EnvType>::remember_(time_step_type step)
253 checksums_.resize(step.n_agents());
254 for (
uint_t a = 0; a < step.n_agents(); ++a)
261template <
typename EnvType>
267 throw std::logic_error(
"DistributedAgentEnv: call reset() before step()");
270 std::vector<real_t> values;
272 values.push_back(checksums_[agent_]);
274 const auto &peers = sync_.exchange(n_exchanges_++, {}, values);
277 actions[agent_] = action;
278 for (
const auto &peer : peers)
281 while (a <
n_agents() && env_.agent_name(a) != peer.participant)
286 if (peer.step != n_exchanges_ - 1 || peer.values.empty())
288 throw std::runtime_error(
"DistributedAgentEnv " + env_.agent_name(agent_) +
": " +
289 peer.participant +
" has left");
291 if (config_.check_replicas && peer.values.back() != checksums_[a])
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));
300 return remember_(env_.step(actions));
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
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