10#ifdef MOOSE_LIBTORCH_ENABLED
26 "Copies a DRL actor from a trainer object on the main app to a LibtorchDRLControl object "
32 "trainer_name",
"Trainer object that owns the latest controller network.");
33 params.
addRequiredParam<std::string>(
"control_name",
"Controller object name.");
40 _control_name(getParam<
std::string>(
"control_name")),
42 getParam<UserObjectName>(
"trainer_name")))
55 for (MooseIndex(n) i = 0; i < n; i++)
99 const uint64_t sample_seed =
static_cast<uint64_t
>(
_trainer.
seed()) +
125 paramError(
"control_name",
"The given control is not a LibtorchDRLControl!");
130 const unsigned int num_steps_in_period = control_object->
numStepsInPeriod();
131 if (timestep_window != num_steps_in_period)
133 const auto divisor = std::gcd(timestep_window, num_steps_in_period);
134 const auto numerator = std::to_string(timestep_window / divisor);
135 const auto denominator = num_steps_in_period / divisor;
136 const std::string ratio = denominator == 1
137 ? numerator +
" times"
138 : numerator +
"/" + std::to_string(denominator) +
" of";
142 "' has timestep_window = ",
146 " this control's num_steps_in_period = ",
148 ". They must be equal so that each training transition corresponds to exactly one "
149 "policy evaluation.");
156 "' has input_timesteps = ",
158 ", but this control has input_timesteps = ",
160 ". They must be equal so that the control stacks the same observation history "
161 "that the actor was trained on.");
169 " observation(s), but this control reads ",
171 ". They must be equal so that the actor receives the observations it was trained "
180 " control signal(s), but this control sets ",
182 " parameter(s). They must be equal so that each actor output drives exactly one "
183 "controllable parameter.");
185 return *control_object;
registerMooseObject("StochasticToolsApp", SamplerDRLControlTransfer)
ExecuteMooseObjectWarehouse< Control > & getControlWarehouse()
virtual int & timeStep() const
Fixed-horizon actor-critic trainer that collects trajectories from MOOSE reporters and runs a PPO upd...
unsigned int numberOfObservations() const
Return the number of observation reporters read at each time level.
const Moose::LibtorchActorNeuralNet & controlNeuralNet() const
Return the current actor network.
unsigned int seed() const
Return the trainer seed used for sampling and shuffling.
unsigned int timestepWindow() const
Return the number of raw reporter entries per training transition.
unsigned int numberOfControlSignals() const
Return the number of control signals produced by the actor.
unsigned int inputTimesteps() const
Return the number of observation time levels stacked into the network inputs.
A time-dependent, neural-network-based controller which is associated with a Proximal Policy Optimiza...
virtual void execute() override
Compute the actions and their corresponding logarithmic probabilities.
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.
void setPolicySampleSeed(uint64_t seed)
Reset the owned policy-sampling generator to a known seed.
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.
unsigned int numberOfControlSignals() const
const std::string & name() const
void paramError(const std::string ¶m, Args... args) const
std::shared_ptr< T > getActiveObject(const std::string &name, THREAD_ID tid=0) const
Feed-forward actor network coupled to a Gaussian or Beta action distribution.
const std::shared_ptr< MultiApp > getToMultiApp() const
std::shared_ptr< MultiApp > _multi_app
Transfers a trained DRL controller from a trainer to controls on sampler subapps.
const LibtorchDRLControlTrainer & _trainer
The trainer object which will contains the control neural net.
virtual void execute() override
Execute the transfer in the standard non-batch path.
const std::string _control_name
The name of the control object on the other app where we want to copy our neural net.
static InputParameters validParams()
Return the input parameters supported by this transfer.
virtual void initializeToMultiapp() override
Initialize a batch transfer to the multiapp.
virtual void finalizeFromMultiapp() override
Finalize a batch transfer from the multiapp.
virtual void finalizeToMultiapp() override
Finalize a batch transfer to the multiapp.
LibtorchDRLControl & getDRLControl(unsigned int app_index)
Find the DRL control on a local subapp and check that its control period, observation history length,...
SamplerDRLControlTransfer(const InputParameters ¶meters)
Build the transfer that pushes a trained controller into subapps.
virtual void executeToMultiapp() override
Execute a batch transfer to the multiapp.
virtual void executeFromMultiapp() override
Execute a batch transfer from the multiapp.
virtual void initializeFromMultiapp() override
Initialize a batch transfer from the multiapp.
virtual void initialSetup() override
Prepare the transfer for batch-mode execution.
dof_id_type getNumberOfRows() const
Interface for objects that need to use samplers.
static InputParameters validParams()
FEProblemBase & _fe_problem