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 "ActiveLearningMonteCarloSampler.h" 11 : #include "Distribution.h" 12 : 13 : registerMooseObject("StochasticToolsApp", ActiveLearningMonteCarloSampler); 14 : 15 : InputParameters 16 250 : ActiveLearningMonteCarloSampler::validParams() 17 : { 18 250 : InputParameters params = Sampler::validParams(); 19 250 : params.addClassDescription("Monte Carlo Sampler for active learning with surrogate model."); 20 500 : params.addRequiredParam<dof_id_type>("num_batch", 21 : "The number of full model evaluations in the batch."); 22 500 : params.addRequiredParam<std::vector<DistributionName>>( 23 : "distributions", 24 : "The distribution names to be sampled, the number of distributions provided defines the " 25 : "number of columns per matrix."); 26 500 : params.addRequiredParam<ReporterName>("flag_sample", 27 : "Flag samples if the surrogate prediction was inadequate."); 28 500 : params.addParam<unsigned int>( 29 : "num_random_seeds", 30 500 : 100000, 31 : "Initialize a certain number of random seeds. Change from the default only if you have to."); 32 500 : params.addRequiredRangeCheckedParam<int>( 33 : "num_samples", 34 : "num_samples>0", 35 : "Number of samples to use (the total number of steps taken will be equal to this number + " 36 : "the number of re-training steps)."); 37 250 : return params; 38 0 : } 39 : 40 144 : ActiveLearningMonteCarloSampler::ActiveLearningMonteCarloSampler(const InputParameters & parameters) 41 : : Sampler(parameters), 42 144 : _flag_sample(getReporterValue<std::vector<bool>>("flag_sample")), 43 288 : _step(getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")->timeStep()), 44 288 : _num_batch(getParam<dof_id_type>("num_batch")), 45 144 : _check_step(std::numeric_limits<int>::min()), 46 576 : _num_samples(getParam<int>("num_samples")) 47 : { 48 720 : for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions")) 49 432 : _distributions.push_back(&getDistributionByName(name)); 50 144 : setNumberOfRows(_num_batch); 51 144 : setNumberOfCols(_distributions.size()); 52 144 : _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size())); 53 288 : setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds")); 54 144 : } 55 : 56 : void 57 6028 : ActiveLearningMonteCarloSampler::sampleSetUp(const Sampler::SampleMode /*mode*/) 58 : { 59 : // If we've already done this step, skip 60 6028 : if (_check_step == _step) 61 : return; 62 : 63 1804 : if (_is_sampling_completed) 64 0 : mooseError("Internal bug: the adaptive sampling is supposed to be completed but another sample " 65 : "has been requested."); 66 : 67 : // Keep data where the GP failed 68 1804 : if (_step > 0) 69 4576 : for (dof_id_type i = 0; i < _num_batch; ++i) 70 2772 : if (_flag_sample[i]) 71 : { 72 374 : _inputs_gp_fails.push_back(_inputs_sto[i]); 73 : 74 : // When the GP fails, the current time step is 'wasted' and the retraining step doesn't 75 : // happen until the next time step. Therefore, keep track of the number of retraining steps 76 : // to increase the total number of steps taken. 77 374 : ++_retraining_steps; 78 : } 79 : 80 : // If we don't have enough failed inputs, generate new ones 81 1804 : if (_inputs_gp_fails.size() < _num_batch) 82 : { 83 4048 : for (dof_id_type i = 0; i < _num_batch; ++i) 84 9768 : for (dof_id_type j = 0; j < _distributions.size(); ++j) 85 7326 : _inputs_sto[i][j] = _distributions[j]->quantile(getRand(_step)); 86 : } 87 : // If we do have enough failed inputs, assign them and clear the tracked ones 88 : else 89 : { 90 198 : _inputs_sto.assign(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch); 91 198 : _inputs_gp_fails.erase(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch); 92 : } 93 : 94 1804 : _check_step = _step; 95 : 96 : // check if we have finished the sampling 97 1804 : if (_step >= _num_samples + _retraining_steps) 98 88 : _is_sampling_completed = true; 99 : } 100 : 101 : Real 102 18084 : ActiveLearningMonteCarloSampler::computeSample(dof_id_type row_index, dof_id_type col_index) 103 : { 104 18084 : return _inputs_sto[row_index][col_index]; 105 : }