1#ifndef ACTOR_CRITIC_SOLVER_BASE_H
2#define ACTOR_CRITIC_SOLVER_BASE_H
11#include "bitrl/base/bitrl_config.h"
25#include <torch/torch.h>
62template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
150 std::unique_ptr<torch::optim::Optimizer> &policy_optimizer,
151 std::unique_ptr<torch::optim::Optimizer> &critic_optimizer);
191template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
195 std::unique_ptr<torch::optim::Optimizer> &policy_optimizer,
196 std::unique_ptr<torch::optim::Optimizer> &critic_optimizer)
202template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
204void ACSolverBase<EnvType, PolicyType, CriticType, MonitorType,
211template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
213void ACSolverBase<EnvType, PolicyType, CriticType, MonitorType,
220template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
222void ACSolverBase<EnvType, PolicyType, CriticType, MonitorType,
234template <
typename EnvType,
typename PolicyType,
typename CriticType,
typename MonitorType,
242 BITRL_LOG_INFO <<
"Collecting batch for episode: " << episode_idx;
252 auto old_timestep = env.reset();
256 for (; itrs <
config_.max_itrs_per_episode; ++itrs)
259 auto [action, log_prob] =
policy_->act(old_timestep.observation());
260 auto values =
critic_->evaluate(old_timestep.observation());
263 auto next_time_step = env.step(action);
264 auto next_state = next_time_step.observation();
265 auto reward = next_time_step.reward();
268 next_time_step.done(), log_prob, values};
274 if (next_time_step.done())
279 old_timestep = next_time_step;
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
RLSolverBase()=default
Constructor.
A2C solver assuming separate networks for the actor and the critic. Similar to the Reinforce algorith...
Definition actor_critic_solver_base.h:65
void set_train_mode() noexcept
set_train_mode for both the Actor and the Critic
Definition actor_critic_solver_base.h:205
virtual void actions_before_training_begins(env_type &)
actions_before_training_begins. Execute any actions the algorithm needs before starting the iteration...
Definition actor_critic_solver_base.h:223
void set_evaluation_mode() noexcept
env_type::action_type action_type
Definition actor_critic_solver_base.h:83
uint_t create_episode_batch_(env_type &env, uint_t, experience_buffer_type &buffer)
Definition actor_critic_solver_base.h:237
ACSolverBase(const config_type &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer)
A2C.
Definition actor_critic_solver_base.h:193
EnvType env_type
The environment type.
Definition actor_critic_solver_base.h:70
virtual void actions_after_training_ends(env_type &)
actions_after_training_ends. Actions to execute after the training iterations have finisehd
Definition actor_critic_solver_base.h:108
virtual void actions_after_episode_ends(env_type &, uint_t, const EpisodeInfo &)
actions_after_training_episode
Definition actor_critic_solver_base.h:118
ConfigType config_type
The solver configuration type.
Definition actor_critic_solver_base.h:97
std::unique_ptr< torch::optim::Optimizer > policy_optimizer_
Definition actor_critic_solver_base.h:176
policy_type & policy_
Definition actor_critic_solver_base.h:161
CriticType critic_type
The critic type.
Definition actor_critic_solver_base.h:80
std::unique_ptr< torch::optim::Optimizer > critic_optimizer_
Definition actor_critic_solver_base.h:181
MonitorType monitor_type
The monitor type to use.
Definition actor_critic_solver_base.h:88
monitor_type::experience_tuple_type experience_tuple_type
Definition actor_critic_solver_base.h:92
PolicyType policy_type
The policy or action type.
Definition actor_critic_solver_base.h:75
config_type config_
Definition actor_critic_solver_base.h:156
virtual ~ACSolverBase()=default
Destructor.
monitor_type::experience_buffer_type experience_buffer_type
Definition actor_critic_solver_base.h:91
virtual void actions_before_episode_begins(env_type &, uint_t)
actions_before_training_episode
Definition actor_critic_solver_base.h:113
monitor_type monitor_
Definition actor_critic_solver_base.h:171
critic_type & critic_
Definition actor_critic_solver_base.h:166
monitor_type & get_monitor()
Definition actor_critic_solver_base.h:141
env_type::state_type state_type
Definition actor_critic_solver_base.h:82
Definition dp_algo_base.h:15
PolicyType
Definition policy_type.h:12
Definition dummy_agent.h:9
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
The EpisodeInfo struct.
Definition episode_info.h:21