bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
policy_stochastic_adaptor.h
Go to the documentation of this file.
1#ifndef STOCHASTIC_ADAPTOR_POLICY_H
2#define STOCHASTIC_ADAPTOR_POLICY_H
3
4#include "bitrl/base/bitrl_config.h"
7
8#ifdef BITRL_DEBUG
9#include <cassert>
10#endif
11
12#include <any>
13#include <map>
14#include <memory>
15
16namespace bitrl
17{
18namespace rl
19{
20namespace policies
21{
22
23class DiscretePolicyBase;
24
28template <typename PolicyType> class StochasticAdaptorPolicy /*: public DiscretePolicyAdaptorBase*/
29{
30
31 public:
33
38 StochasticAdaptorPolicy(uint_t state_space_size, uint_t action_space_size, policy_type &policy);
39
44
50 virtual policy_type &operator()(const std::map<std::string, std::any> &options);
51
52 private:
56 uint_t state_space_size_;
57
61 uint_t action_space_size_;
62
66 policy_type &policy_;
67};
68
69template <typename PolicyType>
71 uint_t action_space_size,
72 policy_type &policy)
73 : // DiscretePolicyAdaptorBase(),
74 state_space_size_(state_space_size), action_space_size_(action_space_size), policy_(policy)
75{
76}
77
78template <typename PolicyType>
80StochasticAdaptorPolicy<PolicyType>::operator()(const std::map<std::string, std::any> &options)
81{
82
83 auto state = std::any_cast<uint_t>(options.find("state")->second);
84 auto state_actions = std::any_cast<DynVec<real_t>>(options.find("state_actions")->second);
85 auto best_actions = maths::max_indices(state_actions);
86
87#ifdef BITRL_DEBUG
88 assert(best_actions.size() <= action_space_size_ &&
89 "Incompatible number of best actions. Cannot exccedd the action space size");
90#endif
91
92 std::vector<std::pair<uint_t, real_t>> best_actions_vals(best_actions.size());
93
94 for (uint_t i = 0; i < best_actions.size(); ++i)
95 {
96 best_actions_vals[i] = {best_actions[i], 1.0 / best_actions.size()};
97 }
98
99 auto &state_action_vals = this->policy_.state_actions_values();
100
101 auto &view = state_action_vals[state];
102
103 // collect all the actions in a map
104 auto act_val_map = std::unordered_map<uint_t, real_t>();
105
106 for (uint_t a = 0; a < best_actions_vals.size(); ++a)
107 {
108 act_val_map.insert({best_actions_vals[a].first, best_actions_vals[a].second});
109 }
110
111 for (uint_t a = 0; a < view.size(); ++a)
112 {
113 auto action = view[a].first;
114
115 if (act_val_map.contains(action))
116 {
117 view[a].second = act_val_map[action];
118 }
119 else
120 {
121 view[a].second = 0.0;
122 }
123 }
124
125 return this->policy_;
126}
127
128} // namespace policies
129
130} // namespace rl
131
132} // namespace bitrl
133
134#endif // STOCHASTIC_ADAPTOR_POLICY_H
virtual policy_type & operator()(const std::map< std::string, std::any > &options)
operator ()
Definition policy_stochastic_adaptor.h:80
StochasticAdaptorPolicy(uint_t state_space_size, uint_t action_space_size, policy_type &policy)
StochasticAdaptorPolicy.
Definition policy_stochastic_adaptor.h:70
PolicyType policy_type
Definition policy_stochastic_adaptor.h:32
std::vector< uint_t > max_indices(const DynVec< T > &vec)
Definition vector_math.h:400
Definition policy_stochastic_adaptor.h:21
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