10#ifdef MOOSE_LIBTORCH_ENABLED
16#include "libmesh/utility.h"
22valueEstimatorMatrixToTensor(
const std::vector<std::vector<Real>> & rows)
27 const auto num_columns = rows.front().size();
28 auto tensor = torch::zeros({
static_cast<long>(rows.size()),
static_cast<long>(num_columns)},
29 torch::TensorOptions().dtype(torch::kDouble));
30 auto accessor = tensor.accessor<
Real, 2>();
32 for (
const auto row_i :
make_range(rows.size()))
34 accessor[row_i][column_i] = rows[row_i][column_i];
42 const Real lambda_factor)
43 : _discount_factor(discount_factor), _lambda_factor(lambda_factor)
53 const auto targets =
estimate(trajectory, value_network);
54 trajectory.advantages = targets.advantages;
55 trajectory.value_targets = targets.value_targets;
68 const auto num_steps = trajectory.
rewards.size();
73 for (
const auto reverse_step : make_range(num_steps))
75 const auto step = num_steps - reverse_step - 1;
76 const Real continuation =
78 const auto delta = trajectory.
rewards[step] +
92 if (observations.empty())
95 torch::NoGradGuard no_grad;
97 auto tensor = valueEstimatorMatrixToTensor(observations);
98 auto value_tensor = value_network.
forward(tensor);
99 auto flattened_value_tensor = value_tensor.reshape({-1});
std::array< Real, 2 > values
for(PetscInt i=0;i< nvars;++i)
On-policy trajectory storage for fixed-horizon RL training.
std::vector< Trajectory > & trajectories()
Return mutable access to the stored trajectories.
Targets estimate(const LibtorchRLTrajectoryBuffer::Trajectory &trajectory, Moose::LibtorchArtificialNeuralNet &value_network) const
Compute value targets and advantages for one trajectory.
const Real _discount_factor
Reward discount factor used in the temporal-difference recursion.
std::vector< Real > evaluate(const std::vector< std::vector< Real > > &observations, Moose::LibtorchArtificialNeuralNet &value_network) const
Evaluate the critic on a batch of observations.
void computeValueTargets(LibtorchRLTrajectoryBuffer &buffer, Moose::LibtorchArtificialNeuralNet &value_network) const
Fill every trajectory in the buffer with value targets and advantages.
LibtorchRLValueEstimator(Real discount_factor, Real lambda_factor)
Build the GAE helper.
const Real _lambda_factor
GAE lambda factor used in the reverse-time advantage recursion.
virtual torch::Tensor forward(const torch::Tensor &x) override
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
Vector data for one on-policy trajectory.
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.
Generalized-advantage estimates and critic targets for one trajectory.
std::vector< Real > value_targets
Critic regression targets.
std::vector< Real > advantages
Generalized-advantage estimates.