https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ActiveLearningMonteCarloSampler.C
Go to the documentation of this file.
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
11#include "Distribution.h"
12
14
17{
19 params.addClassDescription("Monte Carlo Sampler for active learning with surrogate model.");
20 params.addRequiredParam<dof_id_type>("num_batch",
21 "The number of full model evaluations in the batch.");
22 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 params.addRequiredParam<ReporterName>("flag_sample",
27 "Flag samples if the surrogate prediction was inadequate.");
28 params.addParam<unsigned int>(
29 "num_random_seeds",
30 100000,
31 "Initialize a certain number of random seeds. Change from the default only if you have to.");
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 return params;
38}
39
41 : Sampler(parameters),
42 _flag_sample(getReporterValue<std::vector<bool>>("flag_sample")),
43 _is_sampling_completed(declareRecoverableData<bool>("is_sampling_completed", false)),
44 _step(getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")->timeStep()),
45 _num_batch(getParam<dof_id_type>("num_batch")),
46 _num_samples(getParam<int>("num_samples")),
47 _retraining_steps(declareRecoverableData<int>("retraining_steps", 0)),
48 _inputs_sto(declareRecoverableData<std::vector<std::vector<Real>>>("inputs_sto")),
49 _inputs_gp_fails(declareRecoverableData<std::vector<std::vector<Real>>>("inputs_gp_fails"))
50{
51 for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
55 _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size()));
56 setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
58}
59
60void
62{
64 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 if (_step > 0)
69 for (dof_id_type i = 0; i < _num_batch; ++i)
70 if (_flag_sample[i])
71 {
72 _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.
78 }
79
80 // If we don't have enough failed inputs, generate new ones
81 if (_inputs_gp_fails.size() < _num_batch)
82 {
83 for (dof_id_type i = 0; i < _num_batch; ++i)
84 for (dof_id_type j = 0; j < _distributions.size(); ++j)
85 _inputs_sto[i][j] =
86 _distributions[j]->quantile(getRand(i * _distributions.size() + j, _step));
87 }
88 // If we do have enough failed inputs, assign them and clear the tracked ones
89 else
90 {
93 }
94
95 // check if we have finished the sampling
98}
99
100Real
101ActiveLearningMonteCarloSampler::computeSample(dof_id_type row_index, dof_id_type col_index) const
102{
103 return _inputs_sto[row_index][col_index];
104}
registerMooseObject("StochasticToolsApp", ActiveLearningMonteCarloSampler)
void ErrorVector unsigned int
A class used to perform Monte Carlo Sampling with active learning.
bool & _is_sampling_completed
True if the sampling is completed.
std::vector< std::vector< Real > > & _inputs_sto
Storage for previously accepted samples by the decision reporter system.
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
const int & _step
Track the current step of the main App.
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) const override
Return the sample for the given row and column.
const unsigned int _num_batch
The maximum number of GP fails.
std::vector< std::vector< Real > > & _inputs_gp_fails
Store the input params for which the GP fails.
const int & _num_samples
Number of samples requested.
virtual void executeSetUp() override
Gather all the samples once per timestep.
ActiveLearningMonteCarloSampler(const InputParameters &parameters)
int & _retraining_steps
Number of retraining performed.
const std::vector< bool > & _flag_sample
Flag samples if the surrogate prediction was inadequate.
const Distribution & getDistributionByName(const DistributionName &name) const
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
const std::string & name() const
void mooseError(Args &&... args) const
void setNumberOfCols(dof_id_type n_cols)
Real getRand(std::size_t n, unsigned int index=0) const
void setAutoAdvanceGenerators(const bool state)
static InputParameters validParams()
void setNumberOfRows(dof_id_type n_rows)
void setNumberOfRandomSeeds(std::size_t number)