MUQ  0.4.3
OneStepCachePiece.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
5 OneStepCachePiece::OneStepCachePiece(std::shared_ptr<ModPiece> baseModPiece, const double& prec)
6  : ModPiece(baseModPiece->inputSizes, baseModPiece->outputSizes),
7  baseModPiece(baseModPiece),
8  prec(prec)
9 {}
10 
12 
13  // Find out whether we have a cache hit, i.e. input equals last call
14  bool cacheHit = true;
15 
16  if (firstEvaluation) {
17  firstEvaluation = false;
18  cacheHit = false;
19  lastInput.resize(input.size());
20  } else {
21  for (int i = 0; i < input.size(); i++) {
22  if (!input[i].get().isApprox(lastInput[i], prec)) {
23  cacheHit = false;
24  break;
25  }
26  }
27  }
28 
29  // Return last output if hit; otherwise, return new evaluation
30  if (cacheHit) {
31  hits++;
33  return;
34  }
35 
36  misses++;
37 
38  outputs = baseModPiece->Evaluate(input);
40  for (int i = 0; i < input.size(); i++) {
41  lastInput[i] = input[i].get();
42  }
43 }
44 
46  return (double)hits / (hits + misses);
47 }
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
std::vector< Eigen::VectorXd > outputs
Definition: ModPiece.h:503
OneStepCachePiece(std::shared_ptr< ModPiece > baseModPiece, const double &prec=Eigen::NumTraits< double >::dummy_precision())
std::shared_ptr< ModPiece > baseModPiece
std::vector< Eigen::VectorXd > lastInput
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &input) override
std::vector< Eigen::VectorXd > lastOutputs
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.h:3956