bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
multi_agent_trainer.h
Go to the documentation of this file.
1#ifndef MULTI_AGENT_TRAINER_H
2#define MULTI_AGENT_TRAINER_H
3
7
8#include <chrono>
9#include <concepts>
10#include <functional>
11#include <vector>
12
13namespace bitrl
14{
15namespace rl::multi_agent
16{
17
22template <typename ActorType, typename EnvType>
23concept multi_agent_actor = requires(ActorType actor, const typename EnvType::time_step_type &step)
24{
25 {
26 actor.act(step)
27 } -> std::convertible_to<typename EnvType::joint_action_type>;
28};
29
34template <typename LearnerType, typename EnvType>
36 requires(LearnerType learner, const typename EnvType::time_step_type &step,
37 const typename EnvType::joint_action_type &actions, uint_t episode)
38{
39 learner.update(step, actions, step);
40 learner.on_episode_begin(episode);
41 learner.on_episode_end(episode);
42};
43
48{
49 std::vector<real_t> returns;
52 bool all_done{false};
53
57 real_t team_return() const noexcept;
58};
59
66template <typename EnvType, typename ActorType>
67requires multi_agent_actor<ActorType, EnvType> MultiAgentEpisodeResult
68run_episode(EnvType &env, ActorType &actor,
69 const std::function<void(const typename EnvType::time_step_type &)> &on_step = {});
70
79
84{
85 std::vector<MultiAgentEpisodeResult> episodes;
86 std::chrono::duration<real_t> total_time{0.0};
87};
88
96template <typename EnvType, typename LearnerType>
97requires multi_agent_learner<LearnerType, EnvType>
99{
100 public:
101 typedef EnvType env_type;
102 typedef LearnerType learner_type;
103
105 : config_(config), learner_(learner)
106 {
107 }
108
112 MultiAgentTrainingResult train(env_type &env);
113
114 private:
116 learner_type &learner_;
117};
118
120{
121 real_t total = 0.0;
122 for (const real_t r : returns)
123 {
124 total += r;
125 }
126 return total;
127}
128
129namespace detail
130{
131template <typename TimeStepType>
132void accumulate_step(MultiAgentEpisodeResult &result, const TimeStepType &step)
133{
134 for (uint_t a = 0; a < step.n_agents(); ++a)
135 {
136 result.returns[a] += step.reward(a);
137 }
138 if (step.extra().contains("collisions"))
139 {
140 result.n_collisions += step.template get_extra<uint_t>("collisions");
141 }
142 ++result.n_steps;
143}
144
145template <typename TimeStepType> bool all_done(const TimeStepType &step)
146{
147 for (uint_t a = 0; a < step.n_agents(); ++a)
148 {
149 if (!step.done(a))
150 {
151 return false;
152 }
153 }
154 return true;
155}
156} // namespace detail
157
158template <typename EnvType, typename ActorType>
159requires multi_agent_actor<ActorType, EnvType> MultiAgentEpisodeResult
160run_episode(EnvType &env, ActorType &actor,
161 const std::function<void(const typename EnvType::time_step_type &)> &on_step)
162{
164 auto step = env.reset();
165 result.returns.assign(step.n_agents(), 0.0);
166 if (on_step)
167 {
168 on_step(step);
169 }
170 while (!step.last())
171 {
172 step = env.step(actor.act(step));
173 detail::accumulate_step(result, step);
174 if (on_step)
175 {
176 on_step(step);
177 }
178 }
179 result.all_done = detail::all_done(step);
180 return result;
181}
182
183template <typename EnvType, typename LearnerType>
184requires multi_agent_learner<LearnerType, EnvType>
186{
187 const auto start = std::chrono::steady_clock::now();
189 training.episodes.reserve(config_.n_episodes);
190
191 for (uint_t episode = 0; episode < config_.n_episodes; ++episode)
192 {
193 learner_.on_episode_begin(episode);
194
196 auto step = env.reset();
197 result.returns.assign(step.n_agents(), 0.0);
198 while (!step.last())
199 {
200 const auto actions = learner_.act(step);
201 auto next = env.step(actions);
202 learner_.update(step, actions, next);
203 detail::accumulate_step(result, next);
204 step = std::move(next);
205 }
206 result.all_done = detail::all_done(step);
207
208 learner_.on_episode_end(episode);
209
210 if (config_.output_msg_frequency != bitsim::consts::INVALID_ID &&
211 config_.output_msg_frequency > 0 && episode % config_.output_msg_frequency == 0)
212 {
213 BITRL_LOG_INFO << "Episode " << episode << ": team return " << result.team_return()
214 << ", steps " << result.n_steps << ", collisions "
215 << result.n_collisions;
216 }
217 training.episodes.push_back(std::move(result));
218 }
219
220 training.total_time = std::chrono::steady_clock::now() - start;
221 return training;
222}
223
224} // namespace rl::multi_agent
225} // namespace bitrl
226
227#endif // MULTI_AGENT_TRAINER_H
bitrl's logging: the logger of bitsim, bitsim/utils/logger.h, with bitrl's messages going to the logg...
#define BITRL_LOG_INFO
Definition bitrl_log.h:36
MultiAgentTrainer(const MultiAgentTrainerConfig &config, learner_type &learner)
Definition multi_agent_trainer.h:104
LearnerType learner_type
Definition multi_agent_trainer.h:102
MultiAgentTrainingResult train(env_type &env)
Run config.n_episodes episodes of training.
Definition multi_agent_trainer.h:185
EnvType env_type
Definition multi_agent_trainer.h:101
Something that chooses one action per agent from a multi-agent time step: a team of controllers,...
Definition multi_agent_trainer.h:23
A team that learns: it acts, learns from every joint transition, and hears when an episode begins and...
Definition multi_agent_trainer.h:35
Definition multi_agent_trainer.h:130
bool all_done(const TimeStepType &step)
Definition multi_agent_trainer.h:145
void accumulate_step(MultiAgentEpisodeResult &result, const TimeStepType &step)
Definition multi_agent_trainer.h:132
Definition agent_trainer.h:14
MultiAgentEpisodeResult run_episode(EnvType &env, ActorType &actor, const std::function< void(const typename EnvType::time_step_type &)> &on_step={})
Play one episode: reset env, then let actor choose the joint actions until the episode is over.
Definition multi_agent_trainer.h:160
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
const uint_t INVALID_ID
Invalid id.
Definition bitsim_consts.h:16
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
What one episode of a multi-agent environment gave.
Definition multi_agent_trainer.h:48
uint_t n_steps
the joint steps of the episode
Definition multi_agent_trainer.h:50
uint_t n_collisions
the sum of the "collisions" the time steps reported
Definition multi_agent_trainer.h:51
std::vector< real_t > returns
the undiscounted return of every agent
Definition multi_agent_trainer.h:49
real_t team_return() const noexcept
The sum of the returns of the agents.
Definition multi_agent_trainer.h:119
bool all_done
whether every agent finished, rather than the step limit
Definition multi_agent_trainer.h:52
The settings of MultiAgentTrainer.
Definition multi_agent_trainer.h:75
uint_t n_episodes
Definition multi_agent_trainer.h:76
uint_t output_msg_frequency
log every so many episodes
Definition multi_agent_trainer.h:77
What MultiAgentTrainer::train() gave: an entry per episode.
Definition multi_agent_trainer.h:84
std::vector< MultiAgentEpisodeResult > episodes
Definition multi_agent_trainer.h:85
std::chrono::duration< real_t > total_time
Definition multi_agent_trainer.h:86