bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
reinforce_monitor.h
Go to the documentation of this file.
1#ifndef REINFORCE_MONITOR_H
2#define REINFORCE_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 ReinforceMonitor
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 >
35
37
39 std::vector<real_t> rewards;
40 std::vector<real_t> policy_loss_values;
41 std::vector<uint_t> episode_duration;
42
43 void reset() noexcept;
44
45 template <typename T, uint_t index>
46 std::vector<T> get(const std::vector<experience_tuple_type> &experience) const;
47};
48
49template <typename ActionType, typename StateType>
50template <typename T, uint_t index>
51std::vector<T> ReinforceMonitor<ActionType, StateType>::get(
52 const std::vector<experience_tuple_type> &experience) const
53{
54
55 std::vector<T> result;
56 result.reserve(experience.size());
57
58 auto b = experience.begin();
59 auto e = experience.end();
60
61 for (; b != e; ++b)
62 {
63 auto item = *b;
64 result.push_back(std::get<index>(item));
65 }
66
67 return result;
68}
69
70template <typename ActionType, typename StateType>
72{
73
74 policy_loss_values.clear();
75 rewards.clear();
76 episode_duration.clear();
77}
78
79} // namespace pg
80} // namespace algos
81} // namespace rl
82} // namespace bitrl
83
84#endif
85#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 reinforce_monitor.h:24
void reset() noexcept
Definition reinforce_monitor.h:71
std::vector< real_t > rewards
Definition reinforce_monitor.h:39
std::vector< T > get(const std::vector< experience_tuple_type > &experience) const
bitrl::utils::ExperienceBuffer< experience_tuple_type > experience_buffer_type
Definition reinforce_monitor.h:36
std::vector< real_t > policy_loss_values
Definition reinforce_monitor.h:40
std::tuple< state_type, action_type, real_t, bool, torch_tensor_t > experience_tuple_type
Definition reinforce_monitor.h:34
std::vector< uint_t > episode_duration
Definition reinforce_monitor.h:41
action_type action_type
Definition reinforce_monitor.h:27
state_type state_type
Definition reinforce_monitor.h:26