20template <
typename EnvType>
21concept agent_env =
requires(EnvType env,
const typename EnvType::action_type &action)
25 } -> std::convertible_to<typename EnvType::time_step_type>;
28 } -> std::convertible_to<typename EnvType::time_step_type>;
34template <
typename ActorType,
typename EnvType>
35concept agent_actor =
requires(ActorType actor,
const typename EnvType::time_step_type &step)
39 } -> std::convertible_to<typename EnvType::action_type>;
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)
51 learner.update(step, action, step);
52 learner.on_episode_begin(episode);
53 learner.on_episode_end(episode);
61template <
typename EnvType,
typename ActorType>
64 const std::function<
void(
const typename EnvType::time_step_type &)> &on_step = {})
66 MultiAgentEpisodeResult result;
67 auto step = env.reset();
68 result.returns.assign(step.n_agents(), 0.0);
75 step = env.step(actor.act(step));
94template <
typename EnvType,
typename LearnerType>
100 : config_(config), learner_(learner)
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)
114 learner_.on_episode_begin(episode);
116 auto step = env.reset();
117 result.
returns.assign(step.n_agents(), 0.0);
120 const auto action = learner_.act(step);
121 auto next = env.step(action);
122 learner_.update(step, action, next);
124 step = std::move(next);
127 learner_.on_episode_end(episode);
128 training.
episodes.push_back(std::move(result));
130 training.
total_time = std::chrono::steady_clock::now() - start;
136 LearnerType &learner_;
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