Line data Source code
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 : 11 : #pragma once 12 : 13 : #include "ActiveLearningReporterBase.h" 14 : #include "ActiveLearningGaussianProcess.h" 15 : #include "GaussianProcessSurrogate.h" 16 : #include "SurrogateModelInterface.h" 17 : 18 : class ActiveLearningGPDecision : public ActiveLearningReporterTempl<Real>, 19 : public SurrogateModelInterface 20 : { 21 : public: 22 : static InputParameters validParams(); 23 : ActiveLearningGPDecision(const InputParameters & parameters); 24 : 25 : /// Access the number of training samples 26 8 : const int & getTrainingSamples() const { return _n_train; } 27 : 28 : protected: 29 : /** 30 : * This is where most of the computations happen: 31 : * - Data is accumulated for training 32 : * - GP models are trained 33 : * - Decision is made whether more data is needed for GP training 34 : */ 35 : virtual void preNeedSample() override; 36 : 37 : /** 38 : * Based on the computations in preNeedSample, the decision to get more data is passed and results 39 : * from the GP fills @param val 40 : * 41 : * @param row Input parameters to the model 42 : * @param local_ind Current processor row index 43 : * @param global_ind All processors row index 44 : * @param val Output predicted by either the LF model + GP correction or the HF model 45 : * @return bool Whether a full order model evaluation is required 46 : */ 47 : virtual bool needSample(const std::vector<Real> & row, 48 : dof_id_type local_ind, 49 : dof_id_type global_ind, 50 : Real & val) override; 51 : 52 : /** 53 : * Make decisions whether to call the full model or not based on 54 : * GP prediction and uncertainty. 55 : * 56 : * @return bool Whether a full order model evaluation is required 57 : */ 58 : virtual bool facilitateDecision(); 59 : 60 : /** 61 : * This sets up data for re-training the GP. 62 : * 63 : * @param inputs Matrix of inputs for the current step 64 : * @param outputs Vector of outputs for the current step 65 : */ 66 : virtual void setupData(const std::vector<std::vector<Real>> & inputs, 67 : const std::vector<Real> & outputs); 68 : 69 : /** 70 : * This method evaluates the active learning acquisition function and returns bool 71 : * that indicates whether the GP model failed. 72 : * 73 : * @param gp_mean Mean of the gaussian process model 74 : * @param gp_mean Standard deviation of the gaussian process model 75 : * @return bool If the GP model failed 76 : */ 77 : bool learningFunction(const Real & gp_mean, const Real & gp_std) const; 78 : 79 : /// The learning function for active learning 80 : const MooseEnum & _learning_function; 81 : /// The learning function threshold 82 : const Real & _learning_function_threshold; 83 : /// The learning function parameter 84 : const Real & _learning_function_parameter; 85 : 86 : /// Store all the input vectors used for training 87 : std::vector<std::vector<Real>> _inputs_batch; 88 : /// Store all the outputs used for training 89 : std::vector<Real> _outputs_batch; 90 : 91 : /// The active learning GP trainer that permits re-training 92 : const ActiveLearningGaussianProcess & _al_gp; 93 : /// The GP evaluator object that permits re-evaluations 94 : const SurrogateModel & _gp_eval; 95 : 96 : /// Flag samples when the GP fails 97 : std::vector<bool> & _flag_sample; 98 : 99 : /// Number of initial training points for GP 100 : const int _n_train; 101 : 102 : /// Storage for the input vectors to be transferred to the output file 103 : std::vector<std::vector<Real>> & _inputs; 104 : 105 : /// Broadcast the GP mean prediciton to JSON 106 : std::vector<Real> & _gp_mean; 107 : /// Broadcast the GP standard deviation to JSON 108 : std::vector<Real> & _gp_std; 109 : 110 : /// GP pass/fail decision 111 : bool _decision; 112 : 113 : /// Reference to global input data requested from base class 114 : const std::vector<std::vector<Real>> & _inputs_global; 115 : /// Reference to global output data requested from base class 116 : const std::vector<Real> & _outputs_global; 117 : }; 118 : 119 : #endif