Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://www.mooseframework.org 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 "BayesianActiveLearningSampler.h" 11 : #include "Normal.h" 12 : #include "Uniform.h" 13 : 14 : registerMooseObject("StochasticToolsApp", BayesianActiveLearningSampler); 15 : 16 : InputParameters 17 9 : BayesianActiveLearningSampler::validParams() 18 : { 19 9 : InputParameters params = PMCMCBase::validParams(); 20 9 : params.addClassDescription("Fast Bayesian inference with the parallel active learning (partly " 21 : "inspired from El Gammal et al. 2023)."); 22 18 : params.addRequiredParam<ReporterName>( 23 : "sorted_indices", "The sorted sample indices in order of importance to evaluate the subApp."); 24 18 : params.addRequiredRangeCheckedParam<unsigned int>( 25 : "num_tries", 26 : "num_tries>0", 27 : "Number of samples to propose in each iteration (not all are sent for subApp evals)."); 28 9 : return params; 29 0 : } 30 : 31 5 : BayesianActiveLearningSampler::BayesianActiveLearningSampler(const InputParameters & parameters) 32 : : PMCMCBase(parameters), 33 5 : _sorted_indices(getReporterValue<std::vector<unsigned int>>("sorted_indices")), 34 10 : _num_tries(getParam<unsigned int>("num_tries")), 35 5 : _inputs_test(_num_tries, std::vector<Real>(_priors.size())), 36 10 : _var_test(_num_tries) 37 : { 38 5 : } 39 : 40 : const std::vector<std::vector<Real>> & 41 4 : BayesianActiveLearningSampler::getSampleTries() const 42 : { 43 4 : return _inputs_test; 44 : } 45 : 46 : const std::vector<Real> & 47 4 : BayesianActiveLearningSampler::getVarSampleTries() const 48 : { 49 4 : return _var_test; 50 : } 51 : 52 : void 53 30 : BayesianActiveLearningSampler::proposeSamples() 54 : { 55 3025 : auto fill_vector = [&](std::vector<Real> & vector) 56 : { 57 9075 : for (unsigned int i = 0; i < _priors.size(); ++i) 58 6050 : vector[i] = _priors[i]->quantile(random()); 59 3055 : }; 60 : 61 : /* If step is 1, randomly generate the samples. 62 : Else, generate the samples informed by the GP from the reporter "sorted_indices" */ 63 180 : for (dof_id_type i = 0; i < _num_parallel_proposals; ++i) 64 : { 65 150 : if (_t_step < 1) 66 : { 67 25 : fill_vector(_new_samples[i]); 68 25 : if (_var_prior) 69 0 : _new_var_samples[i] = _var_prior->quantile(random()); 70 : } 71 : else 72 : { 73 125 : _new_samples[i] = _inputs_test[_sorted_indices[i]]; 74 125 : if (_var_prior) 75 0 : _new_var_samples[i] = _var_test[_sorted_indices[i]]; 76 : } 77 : } 78 : 79 : /* Finally, generate several new samples randomly for the GP to try and pass it to the 80 : reporter */ 81 3030 : for (dof_id_type i = 0; i < _num_tries; ++i) 82 : { 83 3000 : fill_vector(_inputs_test[i]); 84 3000 : if (_var_prior) 85 0 : _var_test[i] = _var_prior->quantile(random()); 86 : } 87 30 : }