bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
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 "cuberl/base/cubeai_config.h"
5
6#ifdef USE_PYTORCH
7
9#include "cuberl/data_structs/experience_buffer.h"
10
11
12#include <vector>
13#include <tuple>
14
15namespace cuberl {
16namespace rl {
17namespace algos {
18namespace pg {
19
20template<typename ActionType, typename StateType>
21struct ReinforceMonitor
22{
23
24 typedef StateType state_type;
25 typedef ActionType action_type;
26 typedef std::tuple<state_type, // the state observed
27 action_type, // the action taken
28 real_t, // the reward received
29 bool, // done?
30 torch_tensor_t // log prob
31 > experience_tuple_type;
32
33 typedef cuberl::containers::ExperienceBuffer<experience_tuple_type> experience_buffer_type;
34
36 std::vector<real_t> rewards;
37 std::vector<real_t> policy_loss_values;
38 std::vector<uint_t> episode_duration;
39
40
41 void reset()noexcept;
42
43 template<typename T, uint_t index>
44 std::vector<T>
45 get(const std::vector<experience_tuple_type>& experience)const;
46
47};
48
49
50template<typename ActionType, typename StateType>
51template<typename T, uint_t index>
52std::vector<T>
53ReinforceMonitor<ActionType, StateType>::get(const std::vector<experience_tuple_type>& experience)const{
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 auto item = *b;
63 result.push_back(std::get<index>(item));
64 }
65
66 return result;
67}
68
69template<typename ActionType, typename StateType>
70void
71ReinforceMonitor<ActionType, StateType>::reset()noexcept{
72
73 policy_loss_values.clear();
74 rewards.clear();
75 episode_duration.clear();
76}
77
78}
79}
80}
81}
82
83
84
85
86#endif
87#endif
double real_t
real_t
Definition bitrl_types.h:23
Various utilities used when working with RL problems.
Definition cuberl_types.h:16
std::pair< uint_t, uint_t > state_type
Definition example_15.cpp:28