https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchRLTrajectoryAssembler.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
16
17#include <string>
18#include <vector>
19
30{
31public:
33 using ReporterData = std::vector<const std::vector<std::vector<Real>> *>;
34
42 LibtorchRLTrajectoryAssembler(unsigned int input_timesteps,
43 unsigned int timestep_window,
44 bool shift_outputs,
45 bool average_reward_over_timestep_window);
46
55 std::vector<LibtorchRLTrajectoryBuffer::Trajectory>
56 assemble(const ReporterData & observations,
57 const ReporterData & actions,
58 const ReporterData & log_probabilities,
59 const std::vector<std::vector<Real>> & rewards) const;
60
66 unsigned int numTransitions(std::size_t raw_sequence_size) const;
67
68private:
75 static void checkSampleCount(const ReporterData & data,
76 const std::string & quantity,
77 std::size_t num_samples);
78
86 std::vector<Real>
87 downsample(const std::vector<Real> & sample, unsigned int offset, unsigned int num_entries) const;
88
95 std::vector<Real> windowAverage(const std::vector<Real> & sample, unsigned int num_entries) const;
96
98 const unsigned int _timestep_window;
100 const bool _shift_outputs;
105};
106
107#endif
Builds fixed-horizon RL trajectories from raw per-sample reporter sequences.
std::vector< Real > windowAverage(const std::vector< Real > &sample, unsigned int num_entries) const
Average a raw reporter sequence over consecutive timestep windows after the initial entry.
const bool _average_reward_over_timestep_window
Whether rewards are averaged over each window instead of sampled at its end.
unsigned int numTransitions(std::size_t raw_sequence_size) const
Count the transitions available in a raw reporter sequence.
const LibtorchObservationHistoryHelper _observation_history
Observation history stacking helper.
const unsigned int _timestep_window
Raw reporter entries per transition.
std::vector< Real > downsample(const std::vector< Real > &sample, unsigned int offset, unsigned int num_entries) const
Take every timestep_window-th entry of a raw reporter sequence.
std::vector< LibtorchRLTrajectoryBuffer::Trajectory > assemble(const ReporterData &observations, const ReporterData &actions, const ReporterData &log_probabilities, const std::vector< std::vector< Real > > &rewards) const
Assemble one trajectory per sample that holds at least one transition.
const bool _shift_outputs
Whether actions and log probabilities are read one raw entry after the observation.
static void checkSampleCount(const ReporterData &data, const std::string &quantity, std::size_t num_samples)
Check that every component of a rollout quantity holds one sequence per sample.