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  _num_samples(getParam<int>("num_samples"))
46 {
47  for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
51  _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size()));
52  setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
54 }
55 
56 void
58 {
60  mooseError("Internal bug: the adaptive sampling is supposed to be completed but another sample "
61  "has been requested.");
62 
63  // Keep data where the GP failed
64  if (_step > 0)
65  for (dof_id_type i = 0; i < _num_batch; ++i)
66  if (_flag_sample[i])
67  {
68  _inputs_gp_fails.push_back(_inputs_sto[i]);
69 
70  // When the GP fails, the current time step is 'wasted' and the retraining step doesn't
71  // happen until the next time step. Therefore, keep track of the number of retraining steps
72  // to increase the total number of steps taken.
74  }
75 
76  // If we don't have enough failed inputs, generate new ones
77  if (_inputs_gp_fails.size() < _num_batch)
78  {
79  for (dof_id_type i = 0; i < _num_batch; ++i)
80  for (dof_id_type j = 0; j < _distributions.size(); ++j)
81  _inputs_sto[i][j] =
82  _distributions[j]->quantile(getRand(i * _distributions.size() + j, _step));
83  }
84  // If we do have enough failed inputs, assign them and clear the tracked ones
85  else
86  {
87  _inputs_sto.assign(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch);
89  }
90 
91  // check if we have finished the sampling
94 }
95 
96 Real
98 {
99  return _inputs_sto[row_index][col_index];
100 }
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 void executeSetUp() override
Gather all the samples once per timestep.
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()
const T & getParam(const std::string &name) const
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.
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.
const std::string & name() const
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
Real getRand(std::size_t n, unsigned int index=0) 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
void setAutoAdvanceGenerators(const bool state)
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")
const unsigned int _num_batch
The maximum number of GP fails.
registerMooseObject("StochasticToolsApp", ActiveLearningMonteCarloSampler)
void ErrorVector unsigned int
uint8_t dof_id_type
ActiveLearningMonteCarloSampler(const InputParameters &parameters)
void setNumberOfRandomSeeds(std::size_t number)