bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#ifndef UTILS_H
2#define UTILS_H
3
8
11
12#include <vector>
13
14namespace bitrl
15{
16namespace rl::algos
17{
18
23template <typename WorldTp>
24auto state_actions_from_v(const WorldTp &env, const DynVec<real_t> &v, real_t gamma, uint_t state)
26{
27
28 auto q = DynVec<real_t>(env.n_actions());
29 std::for_each(q.begin(), q.end(), [](auto &item) { item = 0.0; });
30
31 for (uint_t a = 0; a < env.n_actions(); ++a)
32 {
33
34 const auto &transition_dyn = env.p(state, a);
35
36 for (auto &dyn : transition_dyn)
37 {
38 auto prob = std::get<0>(dyn);
39 auto next_state = std::get<1>(dyn);
40 auto reward = std::get<2>(dyn);
41 // auto done = std::get<3>(dyn);
42 q[a] += prob * (reward + gamma * v[next_state]);
43 }
44 }
45
46 return q;
47}
48
57template <typename T> std::vector<T> create_discounts_array(T base, uint_t npoints)
58{
59 std::vector<T> points(npoints, 0.0);
60 for (uint_t i = 0; i < npoints; ++i)
61 {
62 points[i] = std::pow(base, i);
63 }
64 return points;
65}
66
71template <typename T>
72std::vector<T> calculate_discounted_return_vector(const std::vector<T> &rewards, T gamma)
73{
74 std::vector<T> returns(rewards.size(), 0.0);
75 for (uint_t t = 0; t < rewards.size(); ++t)
76 {
77 returns[t] = std::pow(gamma, t) * rewards[t];
78 }
79
80 return returns;
81}
82
87template <typename TimeStepType, typename T>
88std::vector<T> calculate_discounted_return_vector(const std::vector<TimeStepType> &trajectory,
89 T gamma)
90{
91
92 std::vector<T> rewards(trajectory.size());
93 for (uint_t t = 0; t < trajectory.size(); ++t)
94 {
95 rewards[t] = std::pow(gamma, t) * trajectory[t].reward();
96 }
97
98 return rewards;
99}
100
108template <typename T> T calculate_discounted_return(const std::vector<T> &rewards, T gamma)
109{
110 auto discounted_vector = calculate_discounted_return_vector(rewards, gamma);
111 return bitrl::maths::sum(discounted_vector);
112}
113
121template <typename T> T calculate_mean_discounted_return(const std::vector<T> &rewards, T gamma)
122{
123 auto discounted_vector = calculate_discounted_return_vector(rewards, gamma);
124 return bitrl::maths::mean(discounted_vector);
125}
126
130template <typename TimeStepType, typename T>
131T calculate_discounted_return(const std::vector<TimeStepType> &trajectory, T gamma)
132{
133
134 auto discounted_vector = calculate_discounted_return_vector(trajectory, gamma);
135 return bitrl::maths::sum(discounted_vector);
136}
137
138template <typename TimeStepType, typename T>
139T calculate_mean_discounted_return(const std::vector<TimeStepType> &trajectory, T gamma)
140{
141 auto discounted_vector = calculate_discounted_return_vector(trajectory, gamma);
142 return bitrl::maths::mean(discounted_vector);
143}
144
150template <typename T>
151std::vector<T> calculate_step_discounted_return(const std::vector<T> &rewards, T gamma)
152{
153 std::vector<T> discounted_returns(rewards.size());
154 for (uint_t t = 0; t < rewards.size(); ++t)
155 {
156
157 T G = 0.0;
158 auto begin = rewards.begin();
159 // advance the iterator t positions
160 std::advance(begin, t);
161 auto counter = 0;
162 for (; begin != rewards.end(); ++begin)
163 {
164 G += std::pow(gamma, counter++) * (*begin);
165 }
166
167 discounted_returns[t] = G;
168 }
169
170 return discounted_returns;
171}
172} // namespace rl::algos
173} // namespace bitrl
174
175#endif // UTILS_H
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