https://mooseframework.inl.gov
CovarianceFunctionBase.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 
10 #include "CovarianceFunctionBase.h"
11 
14 {
16  params.addParam<std::vector<UserObjectName>>(
17  "covariance_functions", {}, "Covariance functions that this covariance function depends on.");
18  params.addParam<unsigned int>(
19  "num_outputs", 1, "The number of outputs expected for this covariance function.");
20  params.addClassDescription("Base class for covariance functions");
21  params.registerBase("CovarianceFunctionBase");
22  params.registerSystemAttributeName("CovarianceFunction");
23  return params;
24 }
25 
27  : MooseObject(parameters),
28  CovarianceInterface(parameters),
29  _num_outputs(getParam<unsigned int>("num_outputs")),
30  _dependent_covariance_names(getParam<std::vector<UserObjectName>>("covariance_functions"))
31 
32 {
33  // Fetch the dependent covariance functions
34  for (const auto & name : _dependent_covariance_names)
35  {
37  _dependent_covariance_types.push_back(_covariance_functions.back()->type());
38  }
39 }
40 
41 bool
43  const RealEigenMatrix & /*x*/,
44  const std::string & /*hyper_param_name*/,
45  unsigned int /*ind*/) const
46 {
47  mooseError("Hyperparameter tuning not set up for this covariance function. Please define "
48  "computedKdhyper() to compute gradient.");
49 }
50 
51 const Real &
53  const Real value,
54  const bool is_tunable)
55 {
56  const auto prefixed_name = _name + ":" + name;
57  if (is_tunable)
58  _tunable_hp.insert(prefixed_name);
59  return _hp_map_real.emplace(prefixed_name, value).first->second;
60 }
61 
62 const std::vector<Real> &
64  const std::vector<Real> value,
65  const bool is_tunable)
66 {
67  const auto prefixed_name = _name + ":" + name;
68  if (is_tunable)
69  _tunable_hp.insert(prefixed_name);
70  return _hp_map_vector_real.emplace(prefixed_name, value).first->second;
71 }
72 
73 bool
74 CovarianceFunctionBase::isTunable(const std::string & name) const
75 {
76  // First, we check if the dependent covariances have the parameter
77  for (const auto dependent_covar : _covariance_functions)
78  if (dependent_covar->isTunable(name))
79  return true;
80 
81  if (_tunable_hp.find(name) != _tunable_hp.end())
82  return true;
83  else if (_hp_map_real.find(name) != _hp_map_real.end() ||
85  mooseError("We found hyperparameter ", name, " but it was not declared tunable!");
86 
87  return false;
88 }
89 
90 void
92  const std::unordered_map<std::string, Real> & map,
93  const std::unordered_map<std::string, std::vector<Real>> & vec_map)
94 {
95  // First, load the hyperparameters of the dependent covariance functions
96  for (const auto dependent_covar : _covariance_functions)
97  dependent_covar->loadHyperParamMap(map, vec_map);
98 
99  // Then we load the hyperparameters of this object
100  for (auto & iter : _hp_map_real)
101  {
102  const auto & map_iter = map.find(iter.first);
103  if (map_iter != map.end())
104  iter.second = map_iter->second;
105  }
106  for (auto & iter : _hp_map_vector_real)
107  {
108  const auto & map_iter = vec_map.find(iter.first);
109  if (map_iter != vec_map.end())
110  iter.second = map_iter->second;
111  }
112 }
113 
114 void
116  std::unordered_map<std::string, Real> & map,
117  std::unordered_map<std::string, std::vector<Real>> & vec_map) const
118 {
119  // First, add the hyperparameters of the dependent covariance functions
120  for (const auto dependent_covar : _covariance_functions)
121  dependent_covar->buildHyperParamMap(map, vec_map);
122 
123  // At the end we just append the hyperparameters this object owns
124  for (const auto & iter : _hp_map_real)
125  map[iter.first] = iter.second;
126  for (const auto & iter : _hp_map_vector_real)
127  vec_map[iter.first] = iter.second;
128 }
129 
130 bool
132  unsigned int & size,
133  Real & min,
134  Real & max) const
135 {
136  // First, check the dependent covariances
137  for (const auto dependent_covar : _covariance_functions)
138  if (dependent_covar->getTuningData(name, size, min, max))
139  return true;
140 
141  min = 1e-9;
142  max = 1e9;
143 
144  if (_hp_map_real.find(name) != _hp_map_real.end())
145  {
146  size = 1;
147  return true;
148  }
149  else if (_hp_map_vector_real.find(name) != _hp_map_vector_real.end())
150  {
151  const auto & vector_value = _hp_map_vector_real.find(name);
152  size = vector_value->second.size();
153  return true;
154  }
155  else
156  {
157  size = 0;
158  return false;
159  }
160 }
161 
162 void
164  std::map<UserObjectName, std::string> & name_type_map) const
165 {
166  for (const auto dependent_covar : _covariance_functions)
167  {
168  dependent_covar->dependentCovarianceTypes(name_type_map);
169  name_type_map.insert(std::make_pair(dependent_covar->name(), dependent_covar->type()));
170  }
171 }
std::unordered_map< std::string, Real > _hp_map_real
Map of real-valued hyperparameters.
void loadHyperParamMap(const std::unordered_map< std::string, Real > &map, const std::unordered_map< std::string, std::vector< Real >> &vec_map)
Load some hyperparameters into the local maps contained in this object.
std::vector< std::string > _dependent_covariance_types
The types of the dependent covariance functions.
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
void registerSystemAttributeName(const std::string &value)
static InputParameters validParams()
CovarianceFunctionBase(const InputParameters &parameters)
const std::vector< Real > & addVectorRealHyperParameter(const std::string &name, const std::vector< Real > value, const bool is_tunable)
Register a vector hyperparameter to this covariance function.
virtual const std::string & name() const
auto max(const L &left, const R &right)
void registerBase(const std::string &value)
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
const std::string name
Definition: Setup.h:20
void buildHyperParamMap(std::unordered_map< std::string, Real > &map, std::unordered_map< std::string, std::vector< Real >> &vec_map) const
Populates the input maps with the owned hyperparameters.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
const std::string _name
void dependentCovarianceTypes(std::map< UserObjectName, std::string > &name_type_map) const
Populate a map with the names and types of the dependent covariance functions.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::unordered_map< std::string, std::vector< Real > > _hp_map_vector_real
Map of vector-valued hyperparameters.
const std::vector< UserObjectName > _dependent_covariance_names
The names of the dependent covariance functions.
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
static InputParameters validParams()
const Real & addRealHyperParameter(const std::string &name, const Real value, const bool is_tunable)
Register a scalar hyperparameter to this covariance function.
auto min(const L &left, const R &right)
virtual bool computedKdhyper(RealEigenMatrix &dKdhp, const RealEigenMatrix &x, const std::string &hyper_param_name, unsigned int ind) const
Redirect dK/dhp for hyperparameter "hp".
void ErrorVector unsigned int
virtual bool getTuningData(const std::string &name, unsigned int &size, Real &min, Real &max) const
Get the default minimum and maximum and size of a hyperparameter.
virtual bool isTunable(const std::string &name) const
Check if a given parameter is tunable.
CovarianceFunctionBase * getCovarianceFunctionByName(const UserObjectName &name) const
Lookup a CovarianceFunction object by name and return pointer.