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 : #include "BiFidelityActiveLearningGPDecision.h" 12 : #include "Sampler.h" 13 : 14 : registerMooseObject("StochasticToolsApp", BiFidelityActiveLearningGPDecision); 15 : 16 : InputParameters 17 26 : BiFidelityActiveLearningGPDecision::validParams() 18 : { 19 26 : InputParameters params = ActiveLearningGPDecision::validParams(); 20 26 : params.addClassDescription("Perform active learning decision making in bi-fidelity modeling."); 21 52 : params.addRequiredParam<SamplerName>("sampler", "The sampler object."); 22 52 : params.addRequiredParam<ReporterName>("outputs_lf", 23 : "Value of the LF model output from the SubApp."); 24 52 : params.addParam<ReporterValueName>("lf_corrected", "lf_corrected", "GP-corrected LF prediciton."); 25 26 : return params; 26 0 : } 27 : 28 13 : BiFidelityActiveLearningGPDecision::BiFidelityActiveLearningGPDecision( 29 13 : const InputParameters & parameters) 30 : : ActiveLearningGPDecision(parameters), 31 13 : _sampler(getSampler("sampler")), 32 26 : _outputs_lf(getReporterValue<std::vector<Real>>("outputs_lf", REPORTER_MODE_DISTRIBUTED)), 33 26 : _lf_corrected(declareValue<std::vector<Real>>("lf_corrected", 34 26 : std::vector<Real>(sampler().getNumberOfRows()))), 35 26 : _local_comm(_sampler.getLocalComm()) 36 : { 37 13 : } 38 : 39 : bool 40 169 : BiFidelityActiveLearningGPDecision::facilitateDecision() 41 : { 42 498 : for (dof_id_type i = 0; i < _inputs.size(); ++i) 43 : { 44 329 : _gp_mean[i] = _gp_eval.evaluate(_inputs[i], _gp_std[i]); 45 329 : _flag_sample[i] = !learningFunction(_outputs_lf_batch[i] + _gp_mean[i], _gp_std[i]); 46 329 : _lf_corrected[i] = _outputs_lf_batch[i] + _gp_mean[i]; 47 : } 48 : 49 272 : for (const auto & fs : _flag_sample) 50 239 : if (!fs) 51 : return false; 52 33 : return true; 53 : } 54 : 55 : void 56 260 : BiFidelityActiveLearningGPDecision::preNeedSample() 57 : { 58 260 : _outputs_lf_batch = _outputs_lf; 59 260 : _local_comm.allgather(_outputs_lf_batch); 60 : // Accumulate inputs and outputs if we previously decided we needed a sample 61 260 : if (_t_step > 1 && _decision) 62 : { 63 121 : std::vector<Real> differences(_outputs_global.size()); 64 342 : for (dof_id_type i = 0; i < _outputs_global.size(); ++i) 65 221 : differences[i] = _outputs_global[i] - _outputs_lf_batch[i]; 66 : 67 : // Accumulate data into _batch members 68 121 : setupData(_inputs, differences); 69 : 70 : // Retrain if we are outside the training phase 71 121 : if (_t_step >= _n_train) 72 43 : _al_gp.reTrain(_inputs_batch, _outputs_batch); 73 121 : } 74 : 75 : // Gather inputs for the current step 76 260 : _inputs = _inputs_global; 77 : 78 : // Evaluate GP and decide if we need more data if outside training phase 79 260 : if (_t_step >= _n_train) 80 169 : _decision = facilitateDecision(); 81 260 : } 82 : 83 : bool 84 260 : BiFidelityActiveLearningGPDecision::needSample(const std::vector<Real> &, 85 : dof_id_type, 86 : dof_id_type global_ind, 87 : Real & val) 88 : { 89 260 : if (!_decision) 90 136 : val = _outputs_lf_batch[global_ind] + _gp_mean[global_ind]; 91 260 : return _decision; 92 : } 93 : 94 : #endif