EmpiricMeasureLevelMaps.cpp 1.88 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
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void MonteCarloMap::UpdateSampleCounter(double epsilon) {
    int optimalM;

    double factor = 0.0;
    for (auto &[level, mc] : *this)
        factor += sqrt(mc->vars.Y * mc->avgs.Cost);

    for (auto &[level, mc] : *this) {
        optimalM = (int) (ceil(2 * pow(epsilon, -2) * factor *
            sqrt(mc->vars.Y / mc->avgs.Cost)));
        if (optimalM == 1) optimalM++;  // Hack
        mc->ctr.dM = optimalM - mc->ctr.M;
    }
}

void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents,
                                Level newLevel, MonteCarlo *newMc) {
niklas.baumgarten's avatar
niklas.baumgarten committed
22
23
    if (newLevel <= maxLevel) {
        auto oldLevel = this->rbegin()->first;
niklas.baumgarten's avatar
niklas.baumgarten committed
24
        (*this)[newLevel] = newMc;
niklas.baumgarten's avatar
niklas.baumgarten committed
25
        (*this)[newLevel]->vars.Y = (*this)[oldLevel]->vars.Y / pow(2.0, exponents.beta);
niklas.baumgarten's avatar
niklas.baumgarten committed
26
        (*this)[newLevel]->avgs.Cost =
niklas.baumgarten's avatar
niklas.baumgarten committed
27
            (*this)[oldLevel]->avgs.Cost * pow(2.0, exponents.gamma);
niklas.baumgarten's avatar
niklas.baumgarten committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        (*this).UpdateSampleCounter(epsilon);
    } else {
        Exit ("Maximum level has been reached.")
    }
}

void SampleCounterMap::Update(const MonteCarloMap &mcMap) {
    for (auto &[level, mc]: mcMap)
        (*this)[level] = mc->ctr;
}

bool SampleCounterMap::NoSamplesLeft() {
    bool noSamplesLeft = true;
    for (auto &&[level, entry]: *this)
        if (entry.dM > 0) {
            noSamplesLeft = false;
            break;
        }
    return noSamplesLeft;
}

void AveragesMap::Update(const MonteCarloMap &mcMap) {
    for (auto &&[level, mc]: mcMap) {
        (*this)[level] = mc->avgs;
        Q += mc->avgs.Y;
        Cost += mc->avgs.Cost; // Todo at the right place?
    }
}

void VariancesMap::Update(const MonteCarloMap &mcMap) {
    for (auto &mc: mcMap)
        (*this)[mc.first] = mc.second->vars;
}

void KurtosisMap::Update(const MonteCarloMap &mcMap) {
    for (auto &mc: mcMap)
        (*this)[mc.first] = mc.second->kurtosis;
}