EmpiricMeasureLevelMaps.cpp 1.98 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
2
3
#include "EmpiricMeasureLevelMaps.hpp"
#include "Exponents.hpp"

4

niklas.baumgarten's avatar
niklas.baumgarten committed
5
void MonteCarloMap::UpdateSampleCounter(double epsilon) {
niklas.baumgarten's avatar
niklas.baumgarten committed
6
  int optimalM;
niklas.baumgarten's avatar
niklas.baumgarten committed
7

niklas.baumgarten's avatar
niklas.baumgarten committed
8
9
10
  double factor = 0.0;
  for (auto &[level, mc] : _levelMap)
    factor += sqrt(mc.aggregate.sVar.Y * mc.aggregate.mean.C);
niklas.baumgarten's avatar
niklas.baumgarten committed
11

niklas.baumgarten's avatar
niklas.baumgarten committed
12
13
14
15
16
17
  for (auto &[level, mc] : _levelMap) {
    optimalM = (int) (ceil(2 * pow(epsilon, -2) * factor *
      sqrt(mc.aggregate.sVar.Y / mc.aggregate.mean.C)));
    if (optimalM == 1) optimalM++;  // Hack
    mc.aggregate.UpdateSampleCounter(optimalM - mc.aggregate.ctr.M);
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
18
19
}

niklas.baumgarten's avatar
niklas.baumgarten committed
20
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, int newLevel) {
niklas.baumgarten's avatar
niklas.baumgarten committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  if (newLevel <= maxLevel) {
    auto oldLevel = this->rbegin();
    double sVarY = oldLevel->second.aggregate.sVar.Y / pow(2.0, exponents.beta);
    double avgsCost = oldLevel->second.aggregate.mean.C * pow(2.0, exponents.gamma);

    _levelMap.insert(
      {newLevel, MonteCarlo(newLevel, 0, false, true)}
    );

    _levelMap.find(newLevel)->second.aggregate.sVar.Y = sVarY;
    _levelMap.find(newLevel)->second.aggregate.mean.C = avgsCost;
    this->UpdateSampleCounter(epsilon);
  } else {
    Exit ("Maximum level has been reached.")
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
36
37
38
}

void SampleCounterMap::Update(const MonteCarloMap &mcMap) {
niklas.baumgarten's avatar
niklas.baumgarten committed
39
40
  for (auto &[level, mc]: mcMap)
    _levelMap[level] = SampleCounter(mc.aggregate.ctr);
niklas.baumgarten's avatar
niklas.baumgarten committed
41
42
43
}

bool SampleCounterMap::NoSamplesLeft() {
niklas.baumgarten's avatar
niklas.baumgarten committed
44
45
46
47
48
49
50
  bool noSamplesLeft = true;
  for (auto &&[level, entry]: _levelMap)
    if (entry.dM > 0) {
      noSamplesLeft = false;
      break;
    }
  return noSamplesLeft;
niklas.baumgarten's avatar
niklas.baumgarten committed
51
52
}

niklas.baumgarten's avatar
niklas.baumgarten committed
53
54
55
56
57
58
void MeanMap::Update(const MonteCarloMap &mcMap) {
  for (auto &&[level, mc]: mcMap) {
    _levelMap[level] = mc.aggregate.mean;
    Q += mc.aggregate.mean.Y;
    Cost += mc.aggregate.mean.C; // Todo at the right place?
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
59
60
}

niklas.baumgarten's avatar
niklas.baumgarten committed
61
62
63
void SVarMap::Update(const MonteCarloMap &mcMap) {
  for (auto &[level, mc]: mcMap)
    _levelMap[level] = mc.aggregate.sVar;
niklas.baumgarten's avatar
niklas.baumgarten committed
64
65
66
}

void KurtosisMap::Update(const MonteCarloMap &mcMap) {
niklas.baumgarten's avatar
niklas.baumgarten committed
67
68
  for (auto &[level, mc]: mcMap)
    _levelMap[level] = mc.aggregate.kurtosis;
niklas.baumgarten's avatar
niklas.baumgarten committed
69
}