https://mooseframework.inl.gov
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.");
32  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  return params;
38 }
39 
41  : Sampler(parameters),
42  _flag_sample(getReporterValue<std::vector<bool>>("flag_sample")),
43  _step(getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")->timeStep()),
44  _num_batch(getParam<dof_id_type>("num_batch")),
45  _check_step(std::numeric_limits<int>::min()),
46  _num_samples(getParam<int>("num_samples"))
47 {
48  for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
52  _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size()));
53  setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
54 }
55 
56 void
58 {
59  // If we've already done this step, skip
60  if (_check_step == _step)
61  return;
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] = _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  _inputs_sto.assign(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch);
92  }
93 
95 
96  // check if we have finished the sampling
99 }
100 
101 Real
103 {
104  return _inputs_sto[row_index][col_index];
105 }
void setNumberOfRows(dof_id_type n_rows)
A class used to perform Monte Carlo Sampling with active learning.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const std::vector< bool > & _flag_sample
Flag samples if the surrogate prediction was inadequate.
int _retraining_steps
Number of retraining performed.
Real getRand(unsigned int index=0)
virtual const std::string & name() const
const int & _num_samples
Number of samples requested.
void addRequiredParam(const std::string &name, const std::string &doc_string)
std::vector< std::vector< Real > > _inputs_gp_fails
Store the input params for which the GP fails.
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
const T & getParam(const std::string &name) const
const int & _step
Track the current step of the main App.
std::vector< std::vector< Real > > _inputs_sto
Storage for previously accepted samples by the decision reporter system.
bool _is_sampling_completed
True if the sampling is completed.
const Distribution & getDistributionByName(const DistributionName &name) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
int _check_step
Ensure that the sampler proceeds in a sequential fashion.
const unsigned int _num_batch
The maximum number of GP fails.
auto min(const L &left, const R &right)
registerMooseObject("StochasticToolsApp", ActiveLearningMonteCarloSampler)
void ErrorVector unsigned int
virtual void sampleSetUp(const Sampler::SampleMode mode) override
Gather all the samples.
uint8_t dof_id_type
ActiveLearningMonteCarloSampler(const InputParameters &parameters)
void setNumberOfRandomSeeds(std::size_t number)