https://mooseframework.inl.gov
ActiveLearningGPDecision.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 
11 #include "Sampler.h"
13 
14 #include <math.h>
15 
16 registerMooseObject("StochasticToolsApp", ActiveLearningGPDecision);
17 
20 {
22  params.addClassDescription(
23  "Evaluates a GP surrogate model, determines its prediction quality, "
24  "launches full model if GP prediction is inadequate, and retrains GP.");
25  MooseEnum learning_function("Ufunction COV");
27  "learning_function", learning_function, "The learning function for active learning.");
28  params.addRequiredParam<Real>("learning_function_threshold", "The learning function threshold.");
29  params.addParam<Real>("learning_function_parameter",
30  std::numeric_limits<Real>::max(),
31  "The learning function parameter.");
32  params.addRequiredParam<UserObjectName>("al_gp", "Active learning GP trainer.");
33  params.addRequiredParam<UserObjectName>("gp_evaluator", "Evaluate the trained GP.");
34  params.addRequiredParam<SamplerName>("sampler", "The sampler object.");
35  params.addParam<ReporterValueName>("flag_sample", "flag_sample", "Flag samples.");
36  params.addRequiredParam<int>("n_train", "Number of training steps.");
37  params.addParam<ReporterValueName>("inputs", "inputs", "The inputs.");
38  params.addParam<ReporterValueName>("gp_mean", "gp_mean", "The GP mean prediction.");
39  params.addParam<ReporterValueName>("gp_std", "gp_std", "The GP standard deviation.");
40  return params;
41 }
42 
44  : ActiveLearningReporterTempl<Real>(parameters),
46  _learning_function(getParam<MooseEnum>("learning_function")),
47  _learning_function_threshold(getParam<Real>("learning_function_threshold")),
48  _learning_function_parameter(getParam<Real>("learning_function_parameter")),
49  _al_gp(getUserObject<ActiveLearningGaussianProcess>("al_gp")),
50  _gp_eval(getSurrogateModel<GaussianProcessSurrogate>("gp_evaluator")),
51  _flag_sample(declareValue<std::vector<bool>>(
52  "flag_sample", std::vector<bool>(sampler().getNumberOfRows(), false))),
53  _n_train(getParam<int>("n_train")),
54  _inputs(declareValue<std::vector<std::vector<Real>>>(
55  "inputs",
56  std::vector<std::vector<Real>>(sampler().getNumberOfRows(),
57  std::vector<Real>(sampler().getNumberOfCols())))),
58  _gp_mean(
59  declareValue<std::vector<Real>>("gp_mean", std::vector<Real>(sampler().getNumberOfRows()))),
60  _gp_std(
61  declareValue<std::vector<Real>>("gp_std", std::vector<Real>(sampler().getNumberOfRows()))),
62  _decision(true),
63  _inputs_global(getGlobalInputData()),
64  _outputs_global(getGlobalOutputData())
65 {
66  if (_learning_function == "Ufunction" &&
67  !parameters.isParamSetByUser("learning_function_parameter"))
68  paramError("learning_function",
69  "The Ufunction requires the model failure threshold ('learning_function_parameter') "
70  "to be specified.");
71 }
72 
73 bool
74 ActiveLearningGPDecision::learningFunction(const Real & gp_mean, const Real & gp_std) const
75 {
76  if (_learning_function == "Ufunction")
77  return (std::abs(gp_mean - _learning_function_parameter) / gp_std) >
79  else if (_learning_function == "COV")
80  return (gp_std / std::abs(gp_mean)) < _learning_function_threshold;
81  else
82  mooseError("Invalid learning function ", std::string(_learning_function));
83  return false;
84 }
85 
86 void
87 ActiveLearningGPDecision::setupData(const std::vector<std::vector<Real>> & inputs,
88  const std::vector<Real> & outputs)
89 {
90  _inputs_batch.insert(_inputs_batch.end(), inputs.begin(), inputs.end());
91  _outputs_batch.insert(_outputs_batch.end(), outputs.begin(), outputs.end());
92 }
93 
94 bool
96 {
97  for (dof_id_type i = 0; i < _inputs.size(); ++i)
98  {
101  }
102 
103  for (const auto & fs : _flag_sample)
104  if (!fs)
105  return false;
106  return true;
107 }
108 
109 void
111 {
112  // Accumulate inputs and outputs if we previously decided we needed a sample
113  if (_t_step > 1 && _decision)
114  {
115  // Accumulate data into _batch members
117 
118  // Retrain if we are outside the training phase
119  if (_t_step > _n_train)
121  }
122 
123  // Gather inputs for the current step
125 
126  // Evaluate GP and decide if we need more data if outside training phase
127  if (_t_step > _n_train)
129 }
130 
131 bool
132 ActiveLearningGPDecision::needSample(const std::vector<Real> &,
133  dof_id_type,
134  dof_id_type global_ind,
135  Real & val)
136 {
137  if (!_decision)
138  val = _gp_mean[global_ind];
139  return _decision;
140 }
registerMooseObject("StochasticToolsApp", ActiveLearningGPDecision)
virtual void setupData(const std::vector< std::vector< Real >> &inputs, const std::vector< Real > &outputs)
This sets up data for re-training the GP.
std::vector< Real > _outputs_batch
Store all the outputs used for training.
const int _n_train
Number of initial training points for GP.
static InputParameters validParams()
bool _decision
GP pass/fail decision.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
virtual void reTrain(const std::vector< std::vector< Real >> &inputs, const std::vector< Real > &outputs) const final
const Real & _learning_function_threshold
The learning function threshold.
virtual void preNeedSample() override
This is where most of the computations happen:
const std::vector< Real > & _outputs_global
Reference to global output data requested from base class.
const MooseEnum & _learning_function
The learning function for active learning.
void addRequiredParam(const std::string &name, const std::string &doc_string)
virtual Real evaluate(const std::vector< Real > &x) const
Evaluate surrogate model given a row of parameters.
void paramError(const std::string &param, Args... args) const
const Real & _learning_function_parameter
The learning function parameter.
std::vector< std::vector< Real > > _inputs_batch
Store all the input vectors used for training.
bool isParamSetByUser(const std::string &name) const
const ActiveLearningGaussianProcess & _al_gp
The active learning GP trainer that permits re-training.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Interface for objects that need to use samplers.
static InputParameters validParams()
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...
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
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 ...
const InputParameters & parameters() const
std::vector< Real > & _gp_mean
Broadcast the GP mean prediciton to JSON.
virtual bool facilitateDecision()
Make decisions whether to call the full model or not based on GP prediction and uncertainty.
const SurrogateModel & _gp_eval
The GP evaluator object that permits re-evaluations.
ActiveLearningGPDecision(const InputParameters &parameters)
std::vector< std::vector< Real > > & _inputs
Storage for the input vectors to be transferred to the output file.
std::vector< Real > & _gp_std
Broadcast the GP standard deviation to JSON.
void ErrorVector unsigned int
This is a base class for performing active learning routines, meant to be used in conjunction with Sa...
const std::vector< std::vector< Real > > & _inputs_global
Reference to global input data requested from base class.
uint8_t dof_id_type
std::vector< bool > & _flag_sample
Flag samples when the GP fails.