https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
LibtorchRLValueEstimator Class Reference

Computes generalized-advantage estimates and value targets for an on-policy trajectory buffer, following Schulman et al., "High-Dimensional Continuous Control Using Generalized Advantage Estimation. More...

#include <LibtorchRLValueEstimator.h>

Classes

struct  Targets
 Generalized-advantage estimates and critic targets for one trajectory. More...
 

Public Member Functions

 LibtorchRLValueEstimator (Real discount_factor, Real lambda_factor)
 Build the GAE helper.
 
void computeValueTargets (LibtorchRLTrajectoryBuffer &buffer, Moose::LibtorchArtificialNeuralNet &value_network) const
 Fill every trajectory in the buffer with value targets and advantages.
 
Targets estimate (const LibtorchRLTrajectoryBuffer::Trajectory &trajectory, Moose::LibtorchArtificialNeuralNet &value_network) const
 Compute value targets and advantages for one trajectory.
 

Private Member Functions

std::vector< Real > evaluate (const std::vector< std::vector< Real > > &observations, Moose::LibtorchArtificialNeuralNet &value_network) const
 Evaluate the critic on a batch of observations.
 

Private Attributes

const Real _discount_factor
 Reward discount factor used in the temporal-difference recursion.
 
const Real _lambda_factor
 GAE lambda factor used in the reverse-time advantage recursion.
 

Detailed Description

Computes generalized-advantage estimates and value targets for an on-policy trajectory buffer, following Schulman et al., "High-Dimensional Continuous Control Using Generalized Advantage Estimation.

"

Definition at line 24 of file LibtorchRLValueEstimator.h.

Constructor & Destructor Documentation

◆ LibtorchRLValueEstimator()

LibtorchRLValueEstimator::LibtorchRLValueEstimator ( Real  discount_factor,
Real  lambda_factor 
)

Build the GAE helper.

Parameters
discount_factorReward discount factor.
lambda_factorGAE lambda factor.

Definition at line 41 of file LibtorchRLValueEstimator.C.

43 : _discount_factor(discount_factor), _lambda_factor(lambda_factor)
44{
45}
const Real _discount_factor
Reward discount factor used in the temporal-difference recursion.
const Real _lambda_factor
GAE lambda factor used in the reverse-time advantage recursion.

Member Function Documentation

◆ computeValueTargets()

void LibtorchRLValueEstimator::computeValueTargets ( LibtorchRLTrajectoryBuffer &  buffer,
Moose::LibtorchArtificialNeuralNet &  value_network 
) const

Fill every trajectory in the buffer with value targets and advantages.

Parameters
bufferOn-policy trajectory buffer to update.
value_networkCritic used for target estimation.

Definition at line 48 of file LibtorchRLValueEstimator.C.

50{
51 for (auto & trajectory : buffer.trajectories())
52 {
53 const auto targets = estimate(trajectory, value_network);
54 trajectory.advantages = targets.advantages;
55 trajectory.value_targets = targets.value_targets;
56 }
57}
Targets estimate(const LibtorchRLTrajectoryBuffer::Trajectory &trajectory, Moose::LibtorchArtificialNeuralNet &value_network) const
Compute value targets and advantages for one trajectory.

Referenced by LibtorchDRLControlTrainer::execute().

◆ estimate()

LibtorchRLValueEstimator::Targets LibtorchRLValueEstimator::estimate ( const LibtorchRLTrajectoryBuffer::Trajectory &  trajectory,
Moose::LibtorchArtificialNeuralNet &  value_network 
) const

Compute value targets and advantages for one trajectory.

Parameters
trajectoryTrajectory to evaluate.
value_networkCritic used for target estimation.

Definition at line 60 of file LibtorchRLValueEstimator.C.

62{
63 Targets targets;
64
65 const auto values = evaluate(trajectory.observations, value_network);
66 const auto next_values = evaluate(trajectory.next_observations, value_network);
67
68 const auto num_steps = trajectory.rewards.size();
69 targets.advantages.resize(num_steps, 0.0);
70 targets.value_targets.resize(num_steps, 0.0);
71
72 Real gae = 0.0;
73 for (const auto reverse_step : make_range(num_steps))
74 {
75 const auto step = num_steps - reverse_step - 1;
76 const Real continuation =
77 trajectory.terminals.empty() || !trajectory.terminals[step] ? 1.0 : 0.0;
78 const auto delta = trajectory.rewards[step] +
79 _discount_factor * continuation * next_values[step] - values[step];
80 gae = delta + _discount_factor * _lambda_factor * continuation * gae;
81 targets.advantages[step] = gae;
82 targets.value_targets[step] = gae + values[step];
83 }
84
85 return targets;
86}
std::array< Real, 2 > values
std::vector< Real > evaluate(const std::vector< std::vector< Real > > &observations, Moose::LibtorchArtificialNeuralNet &value_network) const
Evaluate the critic on a batch of observations.
int delta(unsigned int i, unsigned int j)
Delta function, which returns zero if $i\ne j$ and unity if $i=j$.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
std::vector< Real > rewards
Scalar rewards for each transition.
std::vector< std::vector< Real > > observations
Observations for each transition.
std::vector< std::vector< Real > > next_observations
Next observations for each transition.
std::vector< bool > terminals
Terminal flags for each transition. A true value cuts value bootstrapping at that step.

Referenced by computeValueTargets().

◆ evaluate()

std::vector< Real > LibtorchRLValueEstimator::evaluate ( const std::vector< std::vector< Real > > &  observations,
Moose::LibtorchArtificialNeuralNet &  value_network 
) const
private

Evaluate the critic on a batch of observations.

Parameters
observationsObservation matrix to feed through the critic.
value_networkCritic used for the evaluation.

Definition at line 89 of file LibtorchRLValueEstimator.C.

91{
92 if (observations.empty())
93 return {};
94
95 torch::NoGradGuard no_grad;
96
97 auto tensor = valueEstimatorMatrixToTensor(observations);
98 auto value_tensor = value_network.forward(tensor);
99 auto flattened_value_tensor = value_tensor.reshape({-1});
100
101 std::vector<Real> values;
102 LibtorchUtils::tensorToVector(flattened_value_tensor, values);
103 return values;
104}
virtual torch::Tensor forward(const torch::Tensor &x) override
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)

Referenced by estimate().

Member Data Documentation

◆ _discount_factor

const Real LibtorchRLValueEstimator::_discount_factor
private

Reward discount factor used in the temporal-difference recursion.

Definition at line 71 of file LibtorchRLValueEstimator.h.

Referenced by estimate().

◆ _lambda_factor

const Real LibtorchRLValueEstimator::_lambda_factor
private

GAE lambda factor used in the reverse-time advantage recursion.

Definition at line 73 of file LibtorchRLValueEstimator.h.

Referenced by estimate().


The documentation for this class was generated from the following files: