https://mooseframework.inl.gov
GenericActiveLearningSampler.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "Normal.h"
12 #include "Uniform.h"
13 
15 
18 {
20  params.addClassDescription("A generic sampler to support parallel active learning.");
21  params.addRequiredParam<unsigned int>(
22  "num_parallel_proposals",
23  "Number of proposals to make and corresponding subApps executed in "
24  "parallel.");
25  params.addRequiredParam<std::vector<DistributionName>>(
26  "distributions",
27  "The distribution names to be sampled, the number of distributions provided defines the "
28  "number of columns per matrix.");
30  "sorted_indices", "The sorted sample indices in order of importance to evaluate the subApp.");
31  params.addRequiredRangeCheckedParam<unsigned int>(
32  "num_tries",
33  "num_tries>0",
34  "Number of samples to propose in each iteration (not all are sent for subApp evals).");
35  params.addRequiredParam<std::vector<Real>>("initial_values",
36  "The starting values of the inputs to be calibrated.");
37  params.addParam<unsigned int>(
38  "num_random_seeds",
39  100000,
40  "Initialize a certain number of random seeds. Change from the default only if you have to.");
41  return params;
42 }
43 
45  : Sampler(parameters),
46  TransientInterface(this),
47  _num_parallel_proposals(getParam<unsigned int>("num_parallel_proposals")),
48  _sorted_indices(getReporterValue<std::vector<unsigned int>>("sorted_indices")),
49  _initial_values(getParam<std::vector<Real>>("initial_values")),
50  _num_tries(getParam<unsigned int>("num_tries"))
51 {
52  // Filling the `distributions` vector with the user-provided distributions.
53  for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
55 
56  // Setting the number of sampler rows to be equal to the number of parallel proposals
58 
59  // Setting the number of columns in the sampler matrix (equal to the number of distributions)
61 
62  // Setting the sizes for the different vectors enabling sampling and selection
63  _inputs_all.resize(_num_tries, std::vector<Real>(_distributions.size(), 0.0));
65 
66  setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
68 }
69 
70 const std::vector<std::vector<Real>> &
72 {
73  return _inputs_all;
74 }
75 
76 void
78 {
79  std::size_t rand_index = 0;
80  auto fill_vector = [&](std::vector<Real> & vector)
81  {
82  vector.resize(getNumberOfCols());
83  for (const auto j : make_range(getNumberOfCols()))
84  vector[j] = _distributions[j]->quantile(getRand(rand_index++, _t_step + 1));
85  };
86 
87  /* If step is 1, randomly generate the samples.
88  Else, generate the samples informed by the GP from the reporter "sorted_indices" */
89  for (const auto i : make_range(getNumberOfRows()))
90  {
91  if (_t_step <= 0)
92  fill_vector(_new_samples[i]);
93  else
95  }
96 
97  /* Finally, generate several new samples randomly for the GP to try and pass it to the
98  reporter */
99  for (const auto i : make_range(_num_tries))
100  fill_vector(_inputs_all[i]);
101 }
102 
103 Real
105 {
106  if (_t_step < 1)
107  for (unsigned int i = 0; i < _num_parallel_proposals; ++i)
109 
110  return _new_samples[row_index][col_index];
111 }
void setNumberOfRows(dof_id_type n_rows)
const std::vector< unsigned int > & _sorted_indices
The selected sample indices to evaluate the subApp.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
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< std::vector< Real > > & getSampleTries() const
Return random samples for the GP to try in the reporter class.
void addRequiredParam(const std::string &name, const std::string &doc_string)
const std::string & name() const
Real getRand(std::size_t n, unsigned int index=0) const
virtual void executeSetUp() override
Gather all the samples.
const std::vector< Real > & _initial_values
Initial values of the input params to get the MCMC scheme started.
GenericActiveLearningSampler(const InputParameters &parameters)
dof_id_type getNumberOfRows() const
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)
const unsigned int _num_parallel_proposals
Number of parallel proposals to be made and subApps to be executed.
A generic sampler to support parallel active learning.
IntRange< T > make_range(T beg, T end)
void addClassDescription(const std::string &doc_string)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
std::vector< std::vector< Real > > _new_samples
Vectors of new proposed samples.
void ErrorVector unsigned int
registerMooseObject("StochasticToolsApp", GenericActiveLearningSampler)
dof_id_type getNumberOfCols() const
std::vector< std::vector< Real > > _inputs_all
Storage for all the proposed samples.
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
const unsigned int _num_tries
Number of samples to propose in each iteration (not all are sent for subApp evals) ...
uint8_t dof_id_type
void setNumberOfRandomSeeds(std::size_t number)
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.