https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchRLTrajectoryBuffer.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_LIBTORCH_ENABLED
11
12#pragma once
13
14#include "MooseTypes.h"
15
16#include <torch/torch.h>
17
18#include <cstdint>
19#include <vector>
20
25{
26public:
31 {
33 std::vector<std::vector<Real>> observations;
35 std::vector<std::vector<Real>> next_observations;
37 std::vector<std::vector<Real>> actions;
39 std::vector<std::vector<Real>> log_probabilities;
41 std::vector<Real> rewards;
43 std::vector<bool> terminals;
45 std::vector<Real> value_targets;
47 std::vector<Real> advantages;
48 };
49
54 {
56 torch::Tensor observations;
58 torch::Tensor next_observations;
60 torch::Tensor actions;
62 torch::Tensor log_probabilities;
64 torch::Tensor rewards;
66 torch::Tensor value_targets;
68 torch::Tensor advantages;
69
73 std::int64_t size() const { return observations.defined() ? observations.size(0) : 0; }
74 };
75
80 {
82 Real mean = 0.0;
84 Real std = 0.0;
86 std::vector<Real> trajectory_means;
88 std::vector<Real> trajectory_stds;
89 };
90
95 void addTrajectory(Trajectory trajectory);
96
100 void clear();
101
105 bool empty() const { return _trajectories.empty(); }
106
110 std::size_t numTrajectories() const { return _trajectories.size(); }
111
116 std::size_t numTransitions() const;
117
121 std::vector<Trajectory> & trajectories() { return _trajectories; }
122
126 const std::vector<Trajectory> & trajectories() const { return _trajectories; }
127
132 TensorBatch flatten() const;
133
138 RewardStatistics rewardStatistics() const;
139
140private:
145 static void validateTrajectory(const Trajectory & trajectory);
146
148 std::vector<Trajectory> _trajectories;
149};
150
151#endif
On-policy trajectory storage for fixed-horizon RL training.
void addTrajectory(Trajectory trajectory)
Append one trajectory to the on-policy buffer.
std::vector< Trajectory > & trajectories()
Return mutable access to the stored trajectories.
TensorBatch flatten() const
Flatten every stored trajectory into one tensor batch.
std::size_t numTrajectories() const
Return the number of stored trajectories.
RewardStatistics rewardStatistics() const
Compute reward statistics over the stored trajectories.
static void validateTrajectory(const Trajectory &trajectory)
Validate a trajectory before it is stored.
std::vector< Trajectory > _trajectories
Stored on-policy trajectories.
bool empty() const
Return true if the buffer contains no trajectories.
void clear()
Clear every stored trajectory.
std::size_t numTransitions() const
Count the total number of transitions stored across every trajectory.
const std::vector< Trajectory > & trajectories() const
Return read-only access to the stored trajectories.
Reward statistics over the stored trajectories.
std::vector< Real > trajectory_stds
Reward standard deviation of each non-empty trajectory about its own mean.
std::vector< Real > trajectory_means
Mean reward of each non-empty trajectory.
Real mean
Mean reward over every stored transition.
Flattened tensor data assembled from the stored trajectories.
torch::Tensor actions
Flattened action matrix.
torch::Tensor value_targets
Flattened critic targets.
torch::Tensor next_observations
Flattened next-observation matrix.
torch::Tensor observations
Flattened observation matrix.
torch::Tensor advantages
Flattened advantages.
torch::Tensor log_probabilities
Flattened action log-probabilities.
std::int64_t size() const
Return the number of transitions represented by the batch.
Vector data for one on-policy trajectory.
std::vector< Real > advantages
Advantage estimates aligned with each transition.
std::vector< Real > rewards
Scalar rewards for each transition.
std::vector< std::vector< Real > > observations
Observations for each transition.
std::vector< std::vector< Real > > actions
Actions applied at each transition.
std::vector< std::vector< Real > > next_observations
Next observations for each transition.
std::vector< Real > value_targets
Critic targets aligned with each transition.
std::vector< std::vector< Real > > log_probabilities
Action log-probabilities recorded during rollout.
std::vector< bool > terminals
Terminal flags for each transition. A true value cuts value bootstrapping at that step.