bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
agent_trainer.h
Go to the documentation of this file.
1#ifndef AGENT_TRAINER_H
2#define AGENT_TRAINER_H
3
6
7#include <chrono>
8#include <concepts>
9#include <functional>
10
11namespace bitrl
12{
14{
15
20template <typename EnvType>
21concept agent_env = requires(EnvType env, const typename EnvType::action_type &action)
22{
23 {
24 env.reset()
25 } -> std::convertible_to<typename EnvType::time_step_type>;
26 {
27 env.step(action)
28 } -> std::convertible_to<typename EnvType::time_step_type>;
29};
30
34template <typename ActorType, typename EnvType>
35concept agent_actor = requires(ActorType actor, const typename EnvType::time_step_type &step)
36{
37 {
38 actor.act(step)
39 } -> std::convertible_to<typename EnvType::action_type>;
40};
41
46template <typename LearnerType, typename EnvType>
48 requires(LearnerType learner, const typename EnvType::time_step_type &step,
49 const typename EnvType::action_type &action, uint_t episode)
50{
51 learner.update(step, action, step);
52 learner.on_episode_begin(episode);
53 learner.on_episode_end(episode);
54};
55
61template <typename EnvType, typename ActorType>
63run_agent_episode(EnvType &env, ActorType &actor,
64 const std::function<void(const typename EnvType::time_step_type &)> &on_step = {})
65{
66 MultiAgentEpisodeResult result;
67 auto step = env.reset();
68 result.returns.assign(step.n_agents(), 0.0);
69 if (on_step)
70 {
71 on_step(step);
72 }
73 while (!step.last())
74 {
75 step = env.step(actor.act(step));
76 detail::accumulate_step(result, step);
77 if (on_step)
78 {
79 on_step(step);
80 }
81 }
82 result.all_done = detail::all_done(step);
83 return result;
84}
85
94template <typename EnvType, typename LearnerType>
97{
98 public:
99 AgentTrainer(const MultiAgentTrainerConfig &config, LearnerType &learner)
100 : config_(config), learner_(learner)
101 {
102 }
103
108 {
109 const auto start = std::chrono::steady_clock::now();
111 training.episodes.reserve(config_.n_episodes);
112 for (uint_t episode = 0; episode < config_.n_episodes; ++episode)
113 {
114 learner_.on_episode_begin(episode);
116 auto step = env.reset();
117 result.returns.assign(step.n_agents(), 0.0);
118 while (!step.last())
119 {
120 const auto action = learner_.act(step);
121 auto next = env.step(action);
122 learner_.update(step, action, next);
123 detail::accumulate_step(result, next);
124 step = std::move(next);
125 }
126 result.all_done = detail::all_done(step);
127 learner_.on_episode_end(episode);
128 training.episodes.push_back(std::move(result));
129 }
130 training.total_time = std::chrono::steady_clock::now() - start;
131 return training;
132 }
133
134 private:
136 LearnerType &learner_;
137};
138
139} // namespace rl::multi_agent
140} // namespace bitrl
141
142#endif // AGENT_TRAINER_H
MultiAgentTrainingResult train(EnvType &env)
Run config.n_episodes episodes of training.
Definition agent_trainer.h:107
AgentTrainer(const MultiAgentTrainerConfig &config, LearnerType &learner)
Definition agent_trainer.h:99
Something that chooses the action of one agent from a multi-agent time step.
Definition agent_trainer.h:35
An environment seen by one of its agents: it takes this agent's action alone, and gives the multi-age...
Definition agent_trainer.h:21
One agent that learns from its transitions, and hears when an episode begins and ends; QLearningAgent...
Definition agent_trainer.h:47
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_agent_episode(EnvType &env, ActorType &actor, const std::function< void(const typename EnvType::time_step_type &)> &on_step={})
Play one episode of env with the action of one agent chosen by actor.
Definition agent_trainer.h:63
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
What one episode of a multi-agent environment gave.
Definition multi_agent_trainer.h:48
std::vector< real_t > returns
the undiscounted return of every agent
Definition multi_agent_trainer.h:49
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
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