https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchNeuralNetControl.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#include "Control.h"
17
27{
28public:
33
38
42 virtual void initialSetup() override;
43
47 virtual void execute() override;
48
54 Real getSignal(const unsigned int signal_index) const;
55
59 unsigned int numberOfControlSignals() const { return _control_names.size(); }
60
66 virtual void loadControlNeuralNet(const Moose::LibtorchArtificialNeuralNet & input_nn);
67
71 virtual void loadControlNeuralNetFromFile();
72
77
81 bool hasControlNeuralNet() const { return _nn != nullptr; };
82
83protected:
91 void conditionalParameterError(const std::string & param_name,
92 const std::vector<std::string> & conditional_param,
93 bool should_be_defined = true);
94
99
103 torch::Tensor prepareInputTensor();
104
106 std::vector<Real> _current_observation;
109 std::vector<std::vector<Real>> & _old_observations;
110
112 const std::vector<std::string> & _control_names;
114 std::vector<Real> & _current_control_signals;
115
117 const std::vector<PostprocessorName> & _observation_names;
118
121 std::vector<const Real *> _observation_values;
122
125 const unsigned int _input_timesteps;
126
128 const std::vector<Real> _observation_shift_factors;
130 const std::vector<Real> _observation_scaling_factors;
132 const std::vector<Real> _action_scaling_factors;
133
136
140 std::shared_ptr<Moose::LibtorchNeuralNetBase> _nn;
141};
142
143#endif
Base class for Control objects.
Definition Control.h:44
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
A time-dependent, neural network-based control of multiple input parameters.
const std::vector< Real > _action_scaling_factors
Multipliers for the actions.
virtual void execute() override
Execute the neural network to determine the controllable parameter values.
void conditionalParameterError(const std::string &param_name, const std::vector< std::string > &conditional_param, bool should_be_defined=true)
Function responsible for checking for potential user errors in the input file.
bool hasControlNeuralNet() const
Return true if the object already has a neural network.
virtual void initialSetup() override
Load any file-backed controller state after full object construction.
const unsigned int _input_timesteps
Number of timesteps to use as input data from the reporters (this influences how many past results ar...
virtual void loadControlNeuralNet(const Moose::LibtorchArtificialNeuralNet &input_nn)
Function responsible for loading the neural network for the controller.
Real getSignal(const unsigned int signal_index) const
Get the (signal_index)-th signal of the control neural net.
unsigned int numberOfControlSignals() const
Get the number of controls this object is computing.
const std::vector< PostprocessorName > & _observation_names
Names of the postprocessors which contain the observations of the system.
const std::vector< Real > _observation_shift_factors
Shifting constants for the observations.
static InputParameters validParams()
Return the input parameters supported by this control.
const std::vector< Real > _observation_scaling_factors
Scaling constants (multipliers) for the observations.
std::vector< Real > _current_observation
The values of the current observed postprocessor values.
std::vector< Real > & _current_control_signals
The control signals from the last evaluation of the controller, saved for recover/restart.
void updateCurrentObservation()
Refresh the current observation values from the linked postprocessors.
virtual void loadControlNeuralNetFromFile()
Load the controller neural network from the configured checkpoint file.
const std::vector< std::string > & _control_names
The names of the controllable parameters.
torch::Tensor prepareInputTensor()
Prepare the input tensor for the controller neural network.
std::shared_ptr< Moose::LibtorchNeuralNetBase > _nn
Pointer to the neural net object which is supposed to be used to control the parameter values.
const LibtorchObservationHistoryHelper _observation_history
Shared observation history stacking and factor-expansion helper.
std::vector< std::vector< Real > > & _old_observations
This variable is populated if the controller needs access to older values of the observed postprocess...
std::vector< const Real * > _observation_values
Links to the current observation postprocessor values.
const Moose::LibtorchNeuralNetBase & controlNeuralNet() const
Return a reference to the stored neural network.
Shared observation history stacking and factor-expansion logic for libtorch-based controls and traine...
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
Simple feed-forward neural net with optional affine input and output scaling.
This base class is meant to gather the functions and members common in every neural network based on ...