bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Discounted returns

The return of an episode is the sum of the rewards the agent collects in it. Most algorithms discount the rewards that come later by a factor \(0 \le \gamma \le 1\) per step, so that a reward \(k\) steps away is worth \(\gamma^k\) of a reward now. This example uses the two functions in bitrl/rl/algorithms/utils.h that compute it.

The return of a whole episode

calculate_discounted_return(rewards, gamma) gives the discounted return from the start of the episode,

\[G = \sum_{t=0}^{T} \gamma^{t} r_t . \]

With \(\gamma = 1\) nothing is discounted and \(G\) is the plain sum. The example checks that for ten rewards of 1 the return is 10.

The return from every step

Algorithms such as REINFORCE, see REINFORCE algorithm on CartPole, need the return from every step of the episode to its end,

\[G_t = \sum_{k=t}^{T} \gamma^{k-t} r_k = r_t + \gamma G_{t+1} , \]

which calculate_step_discounted_return(rewards, gamma) computes. For the rewards 0, 1, 2, 3, 4 and \(\gamma = 0.99\), the last step's return is its reward, 4, the one before it is \(3 + 0.99 \cdot 4 = 6.96\), and so on back to the first step.

Output

The example prints each value next to the one worked out by hand:

Discounted reward: 10 expected: 10
step_discounted_reward size :5
Reward: 9.70348 expected: 9.70348
Reward: 9.8015 expected: 9.8015
Reward: 8.8904 expected: 8.8904
Reward: 6.96 expected: 6.96
Reward: 4 expected: 4

The driver code

#include "bitrl/base/bitrl_config.h"
#include <iostream>
#include <unordered_map>
#include <vector>
namespace example_1
{
using namespace bitrl;
} // namespace example_1
int main()
{
using namespace example_1;
using namespace bitrl::rl::algos;
try
{
{
std::vector<real_t> rewards(10, 1);
auto discounted_reward = calculate_discounted_return(rewards, 1.0);
auto true_reward = bitrl::maths::sum(rewards);
std::cout << "Discounted reward: " << discounted_reward << " expected: " << true_reward
<< std::endl;
}
{
// frequently we want to calculated the discounted
// returns for a series of steps we have taken over
// an episode. see for example the REINFORCE algorithm
std::vector<real_t> rewards = {0.0, 1.0, 2.0, 3.0, 4.0};
auto step_discounted_reward = calculate_step_discounted_return(rewards, 0.99);
const real_t expected_step_discounted_reward[] = {9.70348104, 9.801496, 8.8904, 6.96,
4.0};
std::cout << "step_discounted_reward size :" << step_discounted_reward.size()
<< std::endl;
for (uint_t i = 0; i < step_discounted_reward.size(); ++i)
{
std::cout << "Reward: " << step_discounted_reward[i]
<< " expected: " << expected_step_discounted_reward[i] << std::endl;
}
}
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unknown exception occured" << std::endl;
}
return 0;
}
int main()
Definition discounted_returns.cpp:19
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
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
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
double real_t
real_t
Definition bitsim_types.h:20
Definition discounted_returns.cpp:11