https://mooseframework.inl.gov
Loading...
Searching...
No Matches
BiFidelityActiveLearningGPDecision.C
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#ifdef MOOSE_LIBTORCH_ENABLED
10
12#include "Sampler.h"
13
15
18{
20 params.addClassDescription("Perform active learning decision making in bi-fidelity modeling.");
21 params.addRequiredParam<SamplerName>("sampler", "The sampler object.");
22 params.addRequiredParam<ReporterName>("outputs_lf",
23 "Value of the LF model output from the SubApp.");
24 params.addParam<ReporterValueName>("lf_corrected", "lf_corrected", "GP-corrected LF prediciton.");
25 return params;
26}
27
29 const InputParameters & parameters)
30 : ActiveLearningGPDecision(parameters),
31 _sampler(getSampler("sampler")),
32 _outputs_lf(getReporterValue<std::vector<Real>>("outputs_lf", REPORTER_MODE_DISTRIBUTED)),
33 _lf_corrected(declareValue<std::vector<Real>>("lf_corrected",
34 std::vector<Real>(sampler().getNumberOfRows()))),
35 _local_comm(_sampler.getLocalComm())
36{
37}
38
39bool
41{
42 for (dof_id_type i = 0; i < _inputs.size(); ++i)
43 {
47 }
48
49 for (const auto & fs : _flag_sample)
50 if (!fs)
51 return false;
52 return true;
53}
54
55void
57{
60 // Accumulate inputs and outputs if we previously decided we needed a sample
61 if (_t_step > 1 && _decision)
62 {
63 std::vector<Real> differences(_outputs_global.size());
64 for (dof_id_type i = 0; i < _outputs_global.size(); ++i)
65 differences[i] = _outputs_global[i] - _outputs_lf_batch[i];
66
67 // Accumulate data into _batch members
68 setupData(_inputs, differences);
69
70 // Retrain if we are outside the training phase
71 if (_t_step >= _n_train)
73 }
74
75 // Gather inputs for the current step
77
78 // Evaluate GP and decide if we need more data if outside training phase
79 if (_t_step >= _n_train)
81}
82
83bool
85 dof_id_type,
86 dof_id_type global_ind,
87 Real & val)
88{
89 if (!_decision)
90 val = _outputs_lf_batch[global_ind] + _gp_mean[global_ind];
91 return _decision;
92}
93
94#endif
registerMooseObject("StochasticToolsApp", BiFidelityActiveLearningGPDecision)
const ReporterMode REPORTER_MODE_DISTRIBUTED
std::vector< bool > & _flag_sample
Flag samples when the GP fails.
const int _n_train
Number of initial training points for GP.
const std::vector< std::vector< Real > > & _inputs_global
Reference to global input data requested from base class.
std::vector< Real > & _gp_std
Broadcast the GP standard deviation to JSON.
const std::vector< Real > & _outputs_global
Reference to global output data requested from base class.
bool _decision
GP pass/fail decision.
std::vector< Real > _outputs_batch
Store all the outputs used for training.
const SurrogateModel & _gp_eval
The GP evaluator object that permits re-evaluations.
virtual void setupData(const std::vector< std::vector< Real > > &inputs, const std::vector< Real > &outputs)
This sets up data for re-training the GP.
static InputParameters validParams()
std::vector< std::vector< Real > > & _inputs
Storage for the input vectors to be transferred to the output file.
std::vector< std::vector< Real > > _inputs_batch
Store all the input vectors used for training.
const ActiveLearningGaussianProcess & _al_gp
The active learning GP trainer that permits re-training.
std::vector< Real > & _gp_mean
Broadcast the GP mean prediciton to JSON.
bool learningFunction(const Real &gp_mean, const Real &gp_std) const
This method evaluates the active learning acquisition function and returns bool that indicates whethe...
virtual void reTrain(const std::vector< std::vector< Real > > &inputs, const std::vector< Real > &outputs) const final
A class for performing active learning decision making in bi-fidelity modeling.
BiFidelityActiveLearningGPDecision(const InputParameters &parameters)
std::vector< Real > _outputs_lf_batch
Store all the outputs used for training from the LF model.
std::vector< Real > & _lf_corrected
Broadcast the GP-corrected LF prediciton to JSON.
const std::vector< Real > & _outputs_lf
Store all the outputs used for training from the LF model.
virtual bool facilitateDecision() override
This makes decisions whether to call the full model or not based on GP prediction and uncertainty.
virtual void preNeedSample() override
This is where most of the computations happen:
libMesh::Parallel::Communicator & _local_comm
Communicator that was split based on samples that have rows.
virtual bool needSample(const std::vector< Real > &row, dof_id_type local_ind, dof_id_type global_ind, Real &val) override
Based on the computations in preNeedSample, the decision to get more data is passed and results from ...
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
virtual Real evaluate(const std::vector< Real > &x) const
Evaluate surrogate model given a row of parameters.
void allgather(const T &send_data, std::vector< T, A > &recv_data) const