https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
ActiveLearningMonteCarloSampler Class Reference

A class used to perform Monte Carlo Sampling with active learning. More...

#include <ActiveLearningMonteCarloSampler.h>

Inheritance diagram for ActiveLearningMonteCarloSampler:
[legend]

Public Types

enum  SampleMode { SampleMode::GLOBAL, SampleMode::LOCAL }
 
typedef DataFileName DataFileParameterType
 

Public Member Functions

 ActiveLearningMonteCarloSampler (const InputParameters &parameters)
 
virtual bool isAdaptiveSamplingCompleted () const override
 Returns true if the adaptive sampling is completed. More...
 
std::vector< RealgetNextLocalRow ()
 
dof_id_type getNumberOfRows () const
 
dof_id_type getNumberOfCols () const
 
dof_id_type getNumberOfLocalRows () const
 
const LocalRankConfiggetRankConfig (bool batch_mode) const
 
libMesh::Parallel::CommunicatorgetLocalComm ()
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T * queryParam (const std::string &name) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
virtual void initialSetup ()
 
virtual void timestepSetup ()
 
virtual void jacobianSetup ()
 
virtual void residualSetup ()
 
virtual void subdomainSetup ()
 
virtual void customSetup (const ExecFlagType &)
 
const ExecFlagEnumgetExecuteOnEnum () const
 
PerfGraphperfGraph ()
 
T & getSampler (const std::string &name)
 
SamplergetSampler (const std::string &name)
 
T & getSamplerByName (const SamplerName &name)
 
SamplergetSamplerByName (const SamplerName &name)
 
const VectorPostprocessorValuegetVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name, bool needs_broadcast) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
bool hasVectorPostprocessor (const std::string &param_name, const std::string &vector_name) const
 
bool hasVectorPostprocessor (const std::string &param_name) const
 
bool hasVectorPostprocessorByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
bool hasVectorPostprocessorByName (const VectorPostprocessorName &name) const
 
const VectorPostprocessorName & getVectorPostprocessorName (const std::string &param_name) const
 
DenseMatrix< RealgetGlobalSamples ()
 
DenseMatrix< RealgetGlobalSamples ()
 
DenseMatrix< RealgetLocalSamples ()
 
DenseMatrix< RealgetLocalSamples ()
 
dof_id_type getLocalRowBegin () const
 
dof_id_type getLocalRowBegin () const
 
dof_id_type getLocalRowEnd () const
 
dof_id_type getLocalRowEnd () const
 
const DistributiongetDistribution (const std::string &name) const
 
const T & getDistribution (const std::string &name) const
 
const DistributiongetDistribution (const std::string &name) const
 
const T & getDistribution (const std::string &name) const
 
const DistributiongetDistributionByName (const DistributionName &name) const
 
const T & getDistributionByName (const std::string &name) const
 
const DistributiongetDistributionByName (const DistributionName &name) const
 
const T & getDistributionByName (const std::string &name) const
 
bool isVectorPostprocessorDistributed (const std::string &param_name) const
 
bool isVectorPostprocessorDistributed (const std::string &param_name) const
 
bool isVectorPostprocessorDistributedByName (const VectorPostprocessorName &name) const
 
bool isVectorPostprocessorDistributedByName (const VectorPostprocessorName &name) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Public Attributes

const ConsoleStream _console
 

Protected Types

enum  CommMethod
 

Protected Member Functions

virtual void sampleSetUp (const Sampler::SampleMode mode) override
 Gather all the samples. More...
 
virtual Real computeSample (dof_id_type row_index, dof_id_type col_index) override
 Return the sample for the given row and column. More...
 
void setNumberOfRandomSeeds (std::size_t number)
 
Real getRand (unsigned int index=0)
 
uint32_t getRandl (unsigned int index, uint32_t lower, uint32_t upper)
 
virtual LocalRankConfig constructRankConfig (bool batch_mode) const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level) const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level, const std::string &live_message, const bool print_dots=true) const
 
std::string timedSectionName (const std::string &section_name) const
 
virtual void addVectorPostprocessorDependencyHelper (const VectorPostprocessorName &) const
 
const ReporterNamegetReporterName (const std::string &param_name) const
 
virtual void addReporterDependencyHelper (const ReporterName &)
 
void setNumberOfRows (dof_id_type n_rows)
 
void setNumberOfRows (dof_id_type n_rows)
 
void setNumberOfCols (dof_id_type n_cols)
 
void setNumberOfCols (dof_id_type n_cols)
 
virtual void sampleTearDown (const SampleMode)
 
virtual void sampleTearDown (const SampleMode)
 
virtual void computeSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeLocalSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeLocalSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeSampleRow (dof_id_type i, std::vector< Real > &data)
 
virtual void computeSampleRow (dof_id_type i, std::vector< Real > &data)
 
virtual void advanceGenerators (const dof_id_type count)
 
virtual void advanceGenerators (const dof_id_type count)
 
virtual void advanceGenerator (const unsigned int seed_index, const dof_id_type count)
 
virtual void advanceGenerator (const unsigned int seed_index, const dof_id_type count)
 
void setAutoAdvanceGenerators (const bool state)
 
void setAutoAdvanceGenerators (const bool state)
 
void shuffle (std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
 
void shuffle (std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
 
virtual void executeSetUp ()
 
virtual void executeSetUp ()
 
virtual void executeTearDown ()
 
virtual void executeTearDown ()
 
void saveGeneratorState ()
 
void saveGeneratorState ()
 
void restoreGeneratorState ()
 
void restoreGeneratorState ()
 
const T & getReporterValue (const std::string &param_name, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, ReporterMode mode, const std::size_t time_index=0)
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 

Protected Attributes

std::vector< Distribution const * > _distributions
 Storage for distribution objects to be utilized. More...
 
const std::vector< bool > & _flag_sample
 Flag samples if the surrogate prediction was inadequate. More...
 
bool _is_sampling_completed = false
 True if the sampling is completed. More...
 
 NONE
 
 LOCAL
 
 SEMI_LOCAL
 
const dof_id_type _min_procs_per_row
 
const dof_id_type _max_procs_per_row
 
libMesh::Parallel::Communicator _local_comm
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
const ExecFlagEnum_execute_enum
 
const ExecFlagType_current_execute_flag
 
MooseApp_pg_moose_app
 
const std::string _prefix
 
const Parallel::Communicator & _communicator
 

Private Attributes

const int_step
 Track the current step of the main App. More...
 
const unsigned int _num_batch
 The maximum number of GP fails. More...
 
int _check_step
 Ensure that the sampler proceeds in a sequential fashion. More...
 
const int_num_samples
 Number of samples requested. More...
 
int _retraining_steps = 0
 Number of retraining performed. More...
 
std::vector< std::vector< Real > > _inputs_sto
 Storage for previously accepted samples by the decision reporter system. More...
 
std::vector< std::vector< Real > > _inputs_gp_fails
 Store the input params for which the GP fails. More...
 

Detailed Description

A class used to perform Monte Carlo Sampling with active learning.

Definition at line 17 of file ActiveLearningMonteCarloSampler.h.

Constructor & Destructor Documentation

◆ ActiveLearningMonteCarloSampler()

ActiveLearningMonteCarloSampler::ActiveLearningMonteCarloSampler ( const InputParameters parameters)

Definition at line 40 of file ActiveLearningMonteCarloSampler.C.

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 }
void setNumberOfRows(dof_id_type n_rows)
const std::vector< bool > & _flag_sample
Flag samples if the surrogate prediction was inadequate.
virtual const std::string & name() const
const int & _num_samples
Number of samples requested.
Sampler(const InputParameters &parameters)
const T & getReporterValue(const std::string &param_name, const std::size_t time_index=0)
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.
const Distribution & getDistributionByName(const DistributionName &name) const
void setNumberOfCols(dof_id_type n_cols)
const InputParameters & parameters() const
int _check_step
Ensure that the sampler proceeds in a sequential fashion.
const unsigned int _num_batch
The maximum number of GP fails.
void setNumberOfRandomSeeds(std::size_t number)

Member Function Documentation

◆ computeSample()

Real ActiveLearningMonteCarloSampler::computeSample ( dof_id_type  row_index,
dof_id_type  col_index 
)
overrideprotectedvirtual

Return the sample for the given row and column.

Implements Sampler.

Definition at line 102 of file ActiveLearningMonteCarloSampler.C.

103 {
104  return _inputs_sto[row_index][col_index];
105 }
std::vector< std::vector< Real > > _inputs_sto
Storage for previously accepted samples by the decision reporter system.

◆ isAdaptiveSamplingCompleted()

virtual bool ActiveLearningMonteCarloSampler::isAdaptiveSamplingCompleted ( ) const
inlineoverridevirtual

Returns true if the adaptive sampling is completed.

Reimplemented from Sampler.

Definition at line 27 of file ActiveLearningMonteCarloSampler.h.

27 { return _is_sampling_completed; }
bool _is_sampling_completed
True if the sampling is completed.

◆ sampleSetUp()

void ActiveLearningMonteCarloSampler::sampleSetUp ( const Sampler::SampleMode  mode)
overrideprotectedvirtual

Gather all the samples.

Reimplemented from Sampler.

Definition at line 57 of file ActiveLearningMonteCarloSampler.C.

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 }
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)
const int & _num_samples
Number of samples requested.
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 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.
void mooseError(Args &&... args) const
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.
uint8_t dof_id_type

◆ validParams()

InputParameters ActiveLearningMonteCarloSampler::validParams ( )
static

Definition at line 16 of file ActiveLearningMonteCarloSampler.C.

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 }
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
uint8_t dof_id_type

Member Data Documentation

◆ _check_step

int ActiveLearningMonteCarloSampler::_check_step
private

Ensure that the sampler proceeds in a sequential fashion.

Definition at line 52 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().

◆ _distributions

std::vector<Distribution const *> ActiveLearningMonteCarloSampler::_distributions
protected

Storage for distribution objects to be utilized.

Definition at line 36 of file ActiveLearningMonteCarloSampler.h.

Referenced by ActiveLearningMonteCarloSampler(), and sampleSetUp().

◆ _flag_sample

const std::vector<bool>& ActiveLearningMonteCarloSampler::_flag_sample
protected

Flag samples if the surrogate prediction was inadequate.

Definition at line 39 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().

◆ _inputs_gp_fails

std::vector<std::vector<Real> > ActiveLearningMonteCarloSampler::_inputs_gp_fails
private

Store the input params for which the GP fails.

Definition at line 64 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().

◆ _inputs_sto

std::vector<std::vector<Real> > ActiveLearningMonteCarloSampler::_inputs_sto
private

Storage for previously accepted samples by the decision reporter system.

Definition at line 61 of file ActiveLearningMonteCarloSampler.h.

Referenced by ActiveLearningMonteCarloSampler(), computeSample(), and sampleSetUp().

◆ _is_sampling_completed

bool ActiveLearningMonteCarloSampler::_is_sampling_completed = false
protected

True if the sampling is completed.

Definition at line 42 of file ActiveLearningMonteCarloSampler.h.

Referenced by isAdaptiveSamplingCompleted(), and sampleSetUp().

◆ _num_batch

const unsigned int ActiveLearningMonteCarloSampler::_num_batch
private

The maximum number of GP fails.

Definition at line 49 of file ActiveLearningMonteCarloSampler.h.

Referenced by ActiveLearningMonteCarloSampler(), and sampleSetUp().

◆ _num_samples

const int& ActiveLearningMonteCarloSampler::_num_samples
private

Number of samples requested.

Definition at line 55 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().

◆ _retraining_steps

int ActiveLearningMonteCarloSampler::_retraining_steps = 0
private

Number of retraining performed.

Definition at line 58 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().

◆ _step

const int& ActiveLearningMonteCarloSampler::_step
private

Track the current step of the main App.

Definition at line 46 of file ActiveLearningMonteCarloSampler.h.

Referenced by sampleSetUp().


The documentation for this class was generated from the following files: