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 234 : ActiveLearningMonteCarloSampler::validParams() 17 : { 18 234 : InputParameters params = Sampler::validParams(); 19 234 : params.addClassDescription("Monte Carlo Sampler for active learning with surrogate model."); 20 468 : params.addRequiredParam<dof_id_type>("num_batch", 21 : "The number of full model evaluations in the batch."); 22 468 : 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 468 : params.addRequiredParam<ReporterName>("flag_sample", 27 : "Flag samples if the surrogate prediction was inadequate."); 28 468 : params.addParam<unsigned int>( 29 : "num_random_seeds", 30 468 : 100000, 31 : "Initialize a certain number of random seeds. Change from the default only if you have to."); 32 468 : 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 234 : return params; 38 0 : } 39 : 40 136 : ActiveLearningMonteCarloSampler::ActiveLearningMonteCarloSampler(const InputParameters & parameters) 41 : : Sampler(parameters), 42 136 : _flag_sample(getReporterValue<std::vector<bool>>("flag_sample")), 43 272 : _step(getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")->timeStep()), 44 272 : _num_batch(getParam<dof_id_type>("num_batch")), 45 136 : _check_step(std::numeric_limits<int>::min()), 46 544 : _num_samples(getParam<int>("num_samples")) 47 : { 48 680 : for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions")) 49 408 : _distributions.push_back(&getDistributionByName(name)); 50 136 : setNumberOfRows(_num_batch); 51 136 : setNumberOfCols(_distributions.size()); 52 136 : _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size())); 53 272 : setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds")); 54 136 : } 55 : 56 : void 57 5480 : ActiveLearningMonteCarloSampler::sampleSetUp(const Sampler::SampleMode /*mode*/) 58 : { 59 : // If we've already done this step, skip 60 5480 : if (_check_step == _step) 61 : return; 62 : 63 1640 : 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 1640 : if (_step > 0) 69 4160 : for (dof_id_type i = 0; i < _num_batch; ++i) 70 2520 : if (_flag_sample[i]) 71 : { 72 340 : _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 340 : ++_retraining_steps; 78 : } 79 : 80 : // If we don't have enough failed inputs, generate new ones 81 1640 : if (_inputs_gp_fails.size() < _num_batch) 82 : { 83 3680 : for (dof_id_type i = 0; i < _num_batch; ++i) 84 8880 : for (dof_id_type j = 0; j < _distributions.size(); ++j) 85 6660 : _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 180 : _inputs_sto.assign(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch); 91 180 : _inputs_gp_fails.erase(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch); 92 : } 93 : 94 1640 : _check_step = _step; 95 : 96 : // check if we have finished the sampling 97 1640 : if (_step >= _num_samples + _retraining_steps) 98 80 : _is_sampling_completed = true; 99 : } 100 : 101 : Real 102 16440 : ActiveLearningMonteCarloSampler::computeSample(dof_id_type row_index, dof_id_type col_index) 103 : { 104 16440 : return _inputs_sto[row_index][col_index]; 105 : }