MUQ  0.4.3
SmolyakEstimator.h
Go to the documentation of this file.
1 #ifndef SMOLYAKESTIMATOR_H
2 #define SMOLYAKESTIMATOR_H
3 
6 
8 
9 #include <vector>
10 #include <boost/property_tree/ptree.hpp>
11 
12 namespace muq {
13 namespace Approximation {
14 
15  template<typename EstimateType>
17  public:
18 
19  SmolyakEstimator(std::shared_ptr<muq::Modeling::ModPiece> const& modelIn);
20 
21  virtual ~SmolyakEstimator() = default;
22 
26  virtual EstimateType Compute(std::shared_ptr<muq::Utilities::MultiIndexSet> const& fixedSet,
27  boost::property_tree::ptree options = boost::property_tree::ptree());
28 
29 
33  virtual EstimateType Adapt(boost::property_tree::ptree options);
34 
38  virtual double Error() const{return globalError;};
39 
41  virtual unsigned int NumEvals() const{return numEvals;};
42 
44  virtual std::vector<double> ErrorHistory() const{return errorHistory;};
45 
52  virtual std::vector<int> EvalHistory() const{return evalHistory;};
53 
55  virtual std::vector<double> TimeHistory() const{return timeHistory;};
56 
58  virtual std::vector<std::vector<Eigen::VectorXd>> PointHistory() const;
59 
61  virtual std::vector<std::vector<std::shared_ptr<muq::Utilities::MultiIndex>>> TermHistory() const{return termHistory;};
62 
63  protected:
64 
65  virtual void Reset();
66 
67  virtual void AddTerms(std::shared_ptr<muq::Utilities::MultiIndexSet> const& fixedSet);
68 
69  virtual void AddTerms(std::vector<std::shared_ptr<muq::Utilities::MultiIndex>> const& fixedSet);
70 
72  virtual void UpdateErrors();
73 
77  virtual bool Refine();
78 
83  virtual void EvaluatePoints(std::set<unsigned int> const& ptsToEval);
84 
95  virtual std::vector<Eigen::VectorXd> OneTermPoints(std::shared_ptr<muq::Utilities::MultiIndex> const& multi) = 0;
96 
104  virtual EstimateType ComputeOneTerm(std::shared_ptr<muq::Utilities::MultiIndex> const& multi,
105  std::vector<std::reference_wrapper<const Eigen::VectorXd>> const& modEvals) = 0;
106 
108  virtual EstimateType ComputeWeightedSum(Eigen::VectorXd const& weights) const;
109  virtual EstimateType ComputeWeightedSum() const;
110 
111 
112  virtual EstimateType AddEstimates(double w1, EstimateType const& part1, double w2, EstimateType const& part2) const = 0;
113 
114  virtual double ComputeMagnitude(EstimateType const& estimate) const = 0;
115 
117  std::shared_ptr<muq::Modeling::ModPiece> model;
118 
120  std::shared_ptr<muq::Utilities::MultiIndexSet> termMultis;
121 
123  std::vector<double> errorHistory;
124 
126  std::vector<int> evalHistory;
127 
129  std::vector<double> timeHistory;
130 
132  std::vector<std::set<unsigned int>> pointHistory;
133 
135  std::vector<std::vector<std::shared_ptr<muq::Utilities::MultiIndex>>> termHistory;
136 
139  std::vector<Eigen::VectorXd> evalCache;
140 
141  int InCache(Eigen::VectorXd const& input) const;
142  Eigen::VectorXd const& GetFromCache(unsigned int index) const{return pointCache.m_data.at(index);};
143  int AddToCache(Eigen::VectorXd const& newPt);
144  int CacheSize() const{return pointCache.m_data.size();};
145 
146  const double cacheTol = 10.0*std::numeric_limits<double>::epsilon(); // <- points are considered equal if they are closer than this
147 
149  double timeTol = std::numeric_limits<double>::infinity();
150 
152  double errorTol = 0.0;
153 
155  unsigned int maxNumEvals = std::numeric_limits<unsigned int>::max();
156 
158  unsigned int numEvals=0;
159 
160  struct SmolyTerm {
161 
162  // Value of the tensor product approximation for one term in the Smolyak expansion
163  EstimateType val;
164 
165  /* Weight on this tensor product approximation -- will change as terms are
166  added to the Smolyak rule.
167  */
168  double weight = 0.0;
169 
170  /* Has val been computed for this term yet? */
171  bool isComputed = false;
172 
173  /* Whether or not this term is part of the "old" set as defined by G&G */
174  bool isOld = false;
175 
176  /* Is this term needed for the estimate or an error indicator? */
177  bool isNeeded = false;
178 
179  // A local error indicator. See eq (5.1) of Conrad and Marzouk
180  double localError = -1.0;
181 
182  /* A vector containing the indices of points in the evaluation cache
183  (see evalCache and pointCache variables) that are needed to compute the
184  value for this term. This is used for lazy evaluation and helps avoid
185  reevaluation of the model.
186  */
187  std::vector<unsigned int> evalInds;
188 
189  Eigen::VectorXd diffWeights;
190 
191  };
192 
193  std::vector<SmolyTerm> terms;
194 
195  double globalError;
196 
197  const double nzTol = 10.0*std::numeric_limits<double>::epsilon();
198 
199  }; // class SmolyakEstimator
200 
201 } // namespace muq
202 } // namespace Approximation
203 
204 
205 #endif
std::vector< double > timeHistory
Holds the history of how. Each component corresponds to an adaptation iteration.
virtual void AddTerms(std::vector< std::shared_ptr< muq::Utilities::MultiIndex >> const &fixedSet)
unsigned int numEvals
The number of model evaluations that have been performed.
virtual std::vector< std::vector< Eigen::VectorXd > > PointHistory() const
int AddToCache(Eigen::VectorXd const &newPt)
SmolyakEstimator(std::shared_ptr< muq::Modeling::ModPiece > const &modelIn)
virtual std::vector< int > EvalHistory() const
virtual unsigned int NumEvals() const
std::vector< int > evalHistory
Holds the history of how many model evaluations have occured. Each component corresponds to an adapta...
virtual EstimateType AddEstimates(double w1, EstimateType const &part1, double w2, EstimateType const &part2) const =0
virtual void EvaluatePoints(std::set< unsigned int > const &ptsToEval)
unsigned int maxNumEvals
Tolerance on the maximum number of evaluations.
std::vector< double > errorHistory
Holds the history of the error. Each component corresponds to an iteration.
std::shared_ptr< muq::Utilities::MultiIndexSet > termMultis
Multiindices defining each tensor product term in the Smolyak approximation.
virtual EstimateType ComputeOneTerm(std::shared_ptr< muq::Utilities::MultiIndex > const &multi, std::vector< std::reference_wrapper< const Eigen::VectorXd >> const &modEvals)=0
double errorTol
Tolerance on the global error indicator to continue adapting.
std::vector< std::set< unsigned int > > pointHistory
Indices in the cache for the points that were evaluated during each adaptation iteration.
std::shared_ptr< muq::Modeling::ModPiece > model
The model used to construct the approximations.
double timeTol
Tolerance on the time (in seconds) allowed to continue adapting.
virtual std::vector< Eigen::VectorXd > OneTermPoints(std::shared_ptr< muq::Utilities::MultiIndex > const &multi)=0
virtual EstimateType Compute(std::shared_ptr< muq::Utilities::MultiIndexSet > const &fixedSet, boost::property_tree::ptree options=boost::property_tree::ptree())
virtual void AddTerms(std::shared_ptr< muq::Utilities::MultiIndexSet > const &fixedSet)
virtual EstimateType ComputeWeightedSum() const
int InCache(Eigen::VectorXd const &input) const
virtual std::vector< double > ErrorHistory() const
virtual std::vector< double > TimeHistory() const
Eigen::VectorXd const & GetFromCache(unsigned int index) const
virtual std::vector< std::vector< std::shared_ptr< muq::Utilities::MultiIndex > > > TermHistory() const
virtual double ComputeMagnitude(EstimateType const &estimate) const =0
std::vector< std::vector< std::shared_ptr< muq::Utilities::MultiIndex > > > termHistory
The terms that are added during each refinement.
std::vector< Eigen::VectorXd > evalCache
virtual EstimateType Adapt(boost::property_tree::ptree options)
muq::Modeling::DynamicKDTreeAdaptor pointCache
A cache of model evaluations.
std::deque< Eigen::VectorXd > m_data
The kd-tree index for the user to call its methods as usual with any other FLANN index.
Definition: FlannCache.h:29