23template <
typename WorldTp>
29 std::for_each(q.begin(), q.end(), [](
auto &item) { item = 0.0; });
31 for (
uint_t a = 0; a < env.n_actions(); ++a)
34 const auto &transition_dyn = env.p(state, a);
36 for (
auto &dyn : transition_dyn)
38 auto prob = std::get<0>(dyn);
39 auto next_state = std::get<1>(dyn);
40 auto reward = std::get<2>(dyn);
42 q[a] += prob * (reward + gamma * v[next_state]);
59 std::vector<T> points(npoints, 0.0);
60 for (
uint_t i = 0; i < npoints; ++i)
62 points[i] = std::pow(base, i);
74 std::vector<T> returns(rewards.size(), 0.0);
75 for (
uint_t t = 0; t < rewards.size(); ++t)
77 returns[t] = std::pow(gamma, t) * rewards[t];
87template <
typename TimeStepType,
typename T>
95 rewards[t] = std::pow(gamma, t) *
trajectory[t].reward();
130template <
typename TimeStepType,
typename T>
138template <
typename TimeStepType,
typename T>
153 std::vector<T> discounted_returns(rewards.size());
154 for (
uint_t t = 0; t < rewards.size(); ++t)
158 auto begin = rewards.begin();
160 std::advance(begin, t);
162 for (; begin != rewards.end(); ++begin)
164 G += std::pow(gamma, counter++) * (*begin);
167 discounted_returns[t] = G;
170 return discounted_returns;
real_t mean(IteratorType begin, IteratorType end, bool parallel=true)
mean Compute the mean value of the values in the provided iterator range
Definition vector_math.h:119
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:89
Definition dp_algo_base.h:15
std::vector< T > calculate_step_discounted_return(const std::vector< T > &rewards, T gamma)
Given an array of rewards, for each entry calculate the following: $$G = \sum_{k=t+1}...
Definition utils.h:151
std::vector< T > create_discounts_array(T base, uint_t npoints)
create_discounts_array
Definition utils.h:57
std::vector< T > calculate_discounted_return_vector(const std::vector< T > &rewards, T gamma)
Create a vector where element i is the product $$\gamma^i * rewards[i]$$.
Definition utils.h:72
T calculate_discounted_return(const std::vector< T > &rewards, T gamma)
calculate_discounted_return. Calculates the sum of the discounted rewards for the given rewards array...
Definition utils.h:108
T calculate_mean_discounted_return(const std::vector< T > &rewards, T gamma)
calculate_mean_discounted_return. Same as calculate_discounted_return but the result is weighted by 1...
Definition utils.h:121
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
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
Definition line_segment_link.h:11
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101