bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
max_tabular_policy.h
Go to the documentation of this file.
1#ifndef MAX_TABULAR_POLICY_H
2#define MAX_TABULAR_POLICY_H
3
4#include "bitrl/base/bitrl_config.h"
8
9#ifdef USE_PYTORCH
10#include <torch/torch.h>
11#endif
12
13#include <iostream>
14#include <string>
15#include <type_traits>
16#include <vector>
17
18namespace bitrl
19{
20namespace rl
21{
22namespace policies
23{
24
28
33{
34 public:
41
45 template <typename MatType>
46 static output_type get_action(const MatType &q_map, uint_t state_idx);
47
53 template <typename VecTp> static output_type get_action(const VecTp &q_map);
54
55#ifdef USE_PYTORCH
61 static output_type get_action(const torch_tensor_t &vec);
62#endif
63
69
73 MaxTabularPolicy() = default;
74
79 void on_episode(uint_t) noexcept {}
80
84 void reset() noexcept { state_action_map_.clear(); }
85
89 action_type on_state(state_type s) const { return state_action_map_[s]; }
90
94 void save(const std::string &filename) const;
95
102 void load(const std::string &filename);
103
107 uint_t n_states() const noexcept { return state_action_map_.size(); }
108
109 private:
113 std::vector<uint_t> state_action_map_;
114};
115
116#ifdef USE_PYTORCH
118{
119 return torch::argmax(vec).item<uint_t>();
120}
121#endif
122
123template <typename VecTp>
125{
126
127 return std::distance(vec.begin(), std::max_element(vec.begin(), vec.end()));
128}
129
131{
132
133 template <typename EnvType>
134 void build_from_state_function(const EnvType &env, const DynVec<real_t> &v, real_t gamma,
135 MaxTabularPolicy &policy);
136
138};
139
140template <typename EnvType>
142 real_t gamma, MaxTabularPolicy &policy)
143{
144
145 static_assert(std::is_integral_v<typename EnvType::state_type>, "state type must be integral");
146 static_assert(std::is_integral_v<typename EnvType::action_type>,
147 "action type must be integral");
148
149 typedef typename EnvType::action_type action_type;
150 policy.state_action_map_.clear();
151 policy.state_action_map_.resize(env.n_states());
152
153 for (uint_t s = 0; s < env.n_states(); ++s)
154 {
155
156 auto state_vals = bitrl::rl::algos::state_actions_from_v(env, v, gamma, s);
157
158 action_type action = policy.get_action(state_vals);
159 policy.state_action_map_[s] = action;
160 }
161}
162
163} // namespace policies
164} // namespace rl
165} // namespace bitrl
166
167#endif // MAX_TABULAR_POLICY_H
class MaxTabularPolicy
Definition max_tabular_policy.h:33
uint_t n_states() const noexcept
The number of states the policy has an action for.
Definition max_tabular_policy.h:107
void on_episode(uint_t) noexcept
any actions the policy should perform on the given episode index
Definition max_tabular_policy.h:79
static output_type get_action(const MatType &q_map, uint_t state_idx)
get_action. Given a
uint_t output_type
The output type of operator().
Definition max_tabular_policy.h:38
action_type on_state(state_type s) const
Get the action from the given state.
Definition max_tabular_policy.h:89
void reset() noexcept
Reset the policy.
Definition max_tabular_policy.h:84
void load(const std::string &filename)
Load the state -> action map from a CSV file that save() wrote: a state and its action per row,...
friend struct MaxTabularPolicyBuilder
Make friends so the builder access private members.
Definition max_tabular_policy.h:68
uint_t state_type
Definition max_tabular_policy.h:39
uint_t action_type
Definition max_tabular_policy.h:40
void save(const std::string &filename) const
Save the state -> action map in a CSV file;.
static output_type get_action(const VecTp &q_map)
get_action. Given a vector always returns the position of the maximum occuring element....
MaxTabularPolicy()=default
Constructor.
auto state_actions_from_v(const WorldTp &env, const DynVec< real_t > &v, real_t gamma, uint_t state) -> DynVec< real_t >
Given the state index returns the list of actions under the provided value functions.
Definition utils.h:24
Definition policy_stochastic_adaptor.h:21
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
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitsim_types.h:51
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
Definition max_tabular_policy.h:131
void build_from_state_action_function(const DynMat< real_t > &q, MaxTabularPolicy &policy)
void build_from_state_function(const EnvType &env, const DynVec< real_t > &v, real_t gamma, MaxTabularPolicy &policy)
Definition max_tabular_policy.h:141