bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
a2c_monitor.h
Go to the documentation of this file.
1#ifndef A2C_MONITOR_H
2#define A2C_MONITOR_H
3
4#include "bitrl/base/bitrl_config.h"
5
6#ifdef USE_PYTORCH
7
10
11#include <tuple>
12#include <vector>
13
14namespace bitrl
15{
16namespace rl
17{
18namespace algos
19{
20namespace pg
21{
22
23template <typename ActionType, typename StateType> struct A2CMonitor
24{
25
26 typedef StateType state_type;
27 typedef ActionType action_type;
28 typedef std::tuple<state_type, // the state observed
29 action_type, // the action taken
30 real_t, // the reward received
31 bool, // done?
32 torch_tensor_t, // log prob
33 torch_tensor_t // critic values
34 >
36
38
40 std::vector<real_t> rewards;
41 std::vector<real_t> policy_loss_values;
42 std::vector<real_t> critic_loss_values;
43 std::vector<uint_t> episode_duration;
44
45 void reset() noexcept;
46
47 template <typename T, uint_t index>
48 std::vector<T> get(const std::vector<experience_tuple_type> &experience) const;
49};
50
51template <typename ActionType, typename StateType>
52template <typename T, uint_t index>
53std::vector<T>
54A2CMonitor<ActionType, StateType>::get(const std::vector<experience_tuple_type> &experience) const
55{
56
57 std::vector<T> result;
58 result.reserve(experience.size());
59
60 auto b = experience.begin();
61 auto e = experience.end();
62
63 for (; b != e; ++b)
64 {
65 auto item = *b;
66 result.push_back(std::get<index>(item));
67 }
68
69 return result;
70}
71
72template <typename ActionType, typename StateType>
74{
75
76 policy_loss_values.clear();
77 rewards.clear();
78 episode_duration.clear();
79}
80
81} // namespace pg
82} // namespace algos
83} // namespace rl
84} // namespace bitrl
85
86#endif
87#endif
The ExperienceBuffer class. A buffer based on boost::circular_buffer to accumulate items of type Expe...
Definition experience_buffer.h:28
Definition a2c.h:37
Definition dp_algo_base.h:15
Definition dummy_agent.h:9
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Definition a2c_monitor.h:24
action_type action_type
Definition a2c_monitor.h:27
std::vector< real_t > critic_loss_values
Definition a2c_monitor.h:42
void reset() noexcept
Definition a2c_monitor.h:73
std::vector< real_t > rewards
Definition a2c_monitor.h:40
std::tuple< state_type, action_type, real_t, bool, torch_tensor_t, torch_tensor_t > experience_tuple_type
Definition a2c_monitor.h:35
bitrl::utils::ExperienceBuffer< experience_tuple_type > experience_buffer_type
Definition a2c_monitor.h:37
std::vector< T > get(const std::vector< experience_tuple_type > &experience) const
std::vector< uint_t > episode_duration
Definition a2c_monitor.h:43
std::vector< real_t > policy_loss_values
Definition a2c_monitor.h:41
state_type state_type
Definition a2c_monitor.h:26