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