MultilevelEstimator.cpp 2.73 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
#include "MultilevelEstimator.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
2

3

niklas.baumgarten's avatar
niklas.baumgarten committed
4
5
6
7
8
void MultilevelEstimator::initializeCollocationMap() {
}

void MultilevelEstimator::initializeMonteCarloMap() {
  estimatorMap.clear();
niklas.baumgarten's avatar
niklas.baumgarten committed
9
  for (unsigned long i = 0; i < initLevels.size(); i++) {
niklas.baumgarten's avatar
niklas.baumgarten committed
10
    bool onlyFine = (i == 0);
niklas.baumgarten's avatar
niklas.baumgarten committed
11
    int M = initSampleAmount[i];
niklas.baumgarten's avatar
niklas.baumgarten committed
12
13
14
    estimatorMap.insert(
      {initLevels[i], new MonteCarlo(initLevels[i], M, onlyFine, true)}
    );
niklas.baumgarten's avatar
niklas.baumgarten committed
15
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
16
17
}

niklas.baumgarten's avatar
niklas.baumgarten committed
18
void MultilevelEstimator::Method() {
niklas.baumgarten's avatar
niklas.baumgarten committed
19
  mout.StartBlock("MLMC Method");
niklas.baumgarten's avatar
niklas.baumgarten committed
20

niklas.baumgarten's avatar
niklas.baumgarten committed
21
22
23
24
25
26
27
  if (epsilon == 0.0) {
    vout(1) << "Non adaptive method" << endl;
    method();
  } else {
    vout(1) << "eps=" << to_string(epsilon) << endl;
    adaptiveMethod();
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
28

niklas.baumgarten's avatar
niklas.baumgarten committed
29
  data.ExtractDataFrom(estimatorMap);
niklas.baumgarten's avatar
niklas.baumgarten committed
30
31
32
  exponents.ComputeExponents(data);

  mout.EndBlock(verbose == 0);
niklas.baumgarten's avatar
niklas.baumgarten committed
33
34
}

niklas.baumgarten's avatar
niklas.baumgarten committed
35
36
37
void MultilevelEstimator::method() {
  for (auto &[level, estimator] : estimatorMap)
    estimator->Method();
38
39
}

niklas.baumgarten's avatar
niklas.baumgarten committed
40
void MultilevelEstimator::adaptiveMethod() {
niklas.baumgarten's avatar
niklas.baumgarten committed
41
42
  bool converged = false;
  while (!converged) {
niklas.baumgarten's avatar
niklas.baumgarten committed
43
44
45
    for (auto &[level, estimator] : estimatorMap)
      if (estimator->aggregate.ctr.dM > 0)
        estimator->Method();
niklas.baumgarten's avatar
niklas.baumgarten committed
46

niklas.baumgarten's avatar
niklas.baumgarten committed
47
48
    estimatorMap.UpdateSampleCounter(epsilon);
    data.ExtractDataFrom(estimatorMap);
niklas.baumgarten's avatar
niklas.baumgarten committed
49
50
51
52
53
54
55
56
57
58
    exponents.ComputeExponents(data);

    vout(1) << exponents;
    vout(1) << "dM=" << vec2str(data.ctrs.GetdMVector()) << endl;

    if (data.ctrs.NoSamplesLeft()) {
      errors.Estimate(data);
      vout(1) << "numErr=" << errors.numeric << endl;

      if (errors.numeric > epsilon / 2) {
niklas.baumgarten's avatar
niklas.baumgarten committed
59
        estimatorMap.AppendLevel(epsilon, exponents, estimatorMap.rbegin()->first + 1);
niklas.baumgarten's avatar
niklas.baumgarten committed
60
61
62
63
64
      } else {
        errors.Estimate(data);
        vout(1) << "statErr=" << errors.stochastic << endl;
        converged = true;
      }
65
    }
niklas.baumgarten's avatar
niklas.baumgarten committed
66
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
67
68
}

niklas.baumgarten's avatar
niklas.baumgarten committed
69
70
void MultilevelEstimator::PrintInfo() const {
  mout.PrintInfo("MultilevelEstimator", verbose,
niklas.baumgarten's avatar
niklas.baumgarten committed
71
72
                 PrintInfoEntry("initLevels", vec2str(initLevels)),
                 PrintInfoEntry("initSample", vec2str(initSampleAmount)));
niklas.baumgarten's avatar
niklas.baumgarten committed
73
74
}

niklas.baumgarten's avatar
niklas.baumgarten committed
75
void MultilevelEstimator::MultilevelResults() const {
niklas.baumgarten's avatar
niklas.baumgarten committed
76
  data.MultilevelResults();
77
78
}

niklas.baumgarten's avatar
niklas.baumgarten committed
79
void MultilevelEstimator::EstimatorResults() const {
niklas.baumgarten's avatar
niklas.baumgarten committed
80
  mout.PrintInfo("MLMC Results", verbose,
niklas.baumgarten's avatar
niklas.baumgarten committed
81
82
                 PrintInfoEntry("Value", data.means.Q),
                 PrintInfoEntry("Cost", data.means.Cost),
niklas.baumgarten's avatar
niklas.baumgarten committed
83
84
85
86
                 PrintInfoEntry("Epsilon", epsilon),
                 PrintInfoEntry("Total Error", errors.total),
                 PrintInfoEntry("Statistical Error", errors.stochastic),
                 PrintInfoEntry("Numerical Error", errors.numeric));
87
88
}

niklas.baumgarten's avatar
niklas.baumgarten committed
89
void MultilevelEstimator::ExponentResults() const {
niklas.baumgarten's avatar
niklas.baumgarten committed
90
91
92
93
  mout.PrintInfo("Exponents", verbose,
                 PrintInfoEntry("Final alpha", exponents.alpha),
                 PrintInfoEntry("Final beta", exponents.beta),
                 PrintInfoEntry("Final gamma", exponents.gamma));
niklas.baumgarten's avatar
niklas.baumgarten committed
94
}