1#ifndef MAX_TABULAR_POLICY_H
2#define MAX_TABULAR_POLICY_H
4#include "bitrl/base/bitrl_config.h"
10#include <torch/torch.h>
45 template <
typename MatType>
84 void reset() noexcept { state_action_map_.clear(); }
94 void save(
const std::string &filename)
const;
102 void load(
const std::string &filename);
113 std::vector<uint_t> state_action_map_;
119 return torch::argmax(vec).item<
uint_t>();
123template <
typename VecTp>
127 return std::distance(vec.begin(), std::max_element(vec.begin(), vec.end()));
133 template <
typename EnvType>
140template <
typename EnvType>
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");
149 typedef typename EnvType::action_type action_type;
150 policy.state_action_map_.clear();
151 policy.state_action_map_.resize(env.n_states());
153 for (
uint_t s = 0; s < env.n_states(); ++s)
158 action_type action = policy.get_action(state_vals);
159 policy.state_action_map_[s] = action;
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