https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchDRLControl.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 <cstdint>
15
18
27{
28public:
33
38
42 virtual void initialSetup() override;
43
47 virtual void execute() override;
48
54 Real getSignalLogProbability(const unsigned int signal_index) const;
55
61
66 virtual void loadControlNeuralNet(const Moose::LibtorchArtificialNeuralNet & input_nn) override;
67
71 virtual void loadControlNeuralNetFromFile() override;
72
77 void setPolicySampleSeed(uint64_t seed);
78
82 unsigned int numStepsInPeriod() const { return _num_steps_in_period; }
83
87 unsigned int inputTimesteps() const { return _input_timesteps; }
88
92 unsigned int numberOfObservations() const { return _observation_names.size(); }
93
94protected:
99
104 Real computeControlOffset(unsigned int control_i) const;
105
108
110 std::vector<Real> & _previous_control_signal;
112 std::vector<Real> & _current_smoothed_signal;
113
115 std::vector<Real> _control_offsets;
118
120 std::shared_ptr<Moose::LibtorchActorNeuralNet> _actor_nn;
122 at::Generator _policy_generator;
124 std::vector<std::uint8_t> & _policy_generator_state;
125
127 const unsigned int _num_steps_in_period;
131 const bool _stochastic;
132
133private:
137 bool shouldEvaluatePolicy() const;
138
143
148};
149
150#endif
A time-dependent, neural-network-based controller which is associated with a Proximal Policy Optimiza...
bool _has_control_offsets
Whether any offset data was provided by the user.
virtual void loadControlNeuralNetFromFile() override
Load the actor neural network from the configured checkpoint file.
std::vector< Real > _control_offsets
Constant offsets added to each control signal before it is applied.
at::Generator _policy_generator
Owned libtorch CPU generator used for policy sampling.
void restorePolicyGeneratorState()
Restore the owned libtorch generator state from restartable storage.
void savePolicyGeneratorState()
Mirror the owned libtorch generator state into restartable storage.
Real getSignalLogProbability(const unsigned int signal_index) const
Get the logarithmic probability of (signal_index)-th signal of the control neural net.
virtual void execute() override
Compute the actions and their corresponding logarithmic probabilities.
std::vector< Real > & _previous_control_signal
The smoothed control signal from the previous execution, saved for restart/recover.
std::vector< Real > & _current_control_signal_log_probabilities
The log probability of control signals from the last evaluation of the controller.
Real computeControlOffset(unsigned int control_i) const
Return the offset to add to a control signal at the current time.
static InputParameters validParams()
Return the input parameters supported by this control.
const bool _stochastic
Whether to sample actions stochastically instead of using the deterministic actor output.
const Real _control_smoothing_factor
Exponential smoothing factor in (0, 1] applied to the control signal at each time step.
unsigned int numStepsInPeriod() const
Return the number of time steps between policy evaluations.
void loadControlNeuralNet(const Moose::LibtorchActorNeuralNet &input_nn)
Copy an actor network into this DRL controller.
bool shouldEvaluatePolicy() const
Report whether the policy is evaluated at the current time step.
std::vector< Real > & _current_smoothed_signal
The current smoothed control signal applied to the controllable parameters.
const unsigned int _num_steps_in_period
Number of time steps between policy evaluations.
void setPolicySampleSeed(uint64_t seed)
Reset the owned policy-sampling generator to a known seed.
std::shared_ptr< Moose::LibtorchActorNeuralNet > _actor_nn
Actor network used when the controller operates as a stochastic policy.
std::vector< std::uint8_t > & _policy_generator_state
Restartable serialized state for the owned policy-sampling generator.
virtual void initialSetup() override
Restore any restartable controller state after base setup completes.
unsigned int numberOfObservations() const
Return the number of observations read at each time level.
unsigned int inputTimesteps() const
Return the number of observation time levels stacked into the actor input.
void applyControlSignals()
Apply the current smoothed control signals, including optional offsets.
const unsigned int _input_timesteps
const std::vector< PostprocessorName > & _observation_names
const InputParameters & parameters() const
Feed-forward actor network coupled to a Gaussian or Beta action distribution.