EmpiricMeasureLevelMaps.cpp 2.06 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
void MonteCarloMap::UpdateSampleCounter(double epsilon) {
    int optimalM;

    double factor = 0.0;
niklas.baumgarten's avatar
niklas.baumgarten committed
9
    for (auto &[level, mc] : _levelMap)
niklas.baumgarten's avatar
niklas.baumgarten committed
10
        factor += sqrt(mc.vars.Y * mc.avgs.Cost);
niklas.baumgarten's avatar
niklas.baumgarten committed
11

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

niklas.baumgarten's avatar
niklas.baumgarten committed
21
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, Level newLevel) {
niklas.baumgarten's avatar
niklas.baumgarten committed
22
    if (newLevel <= maxLevel) {
niklas.baumgarten's avatar
niklas.baumgarten committed
23
24
25
        auto oldLevel = this->rbegin();
        double varsY = oldLevel->second.vars.Y / pow(2.0, exponents.beta);
        double avgsCost = oldLevel->second.avgs.Cost * pow(2.0, exponents.gamma);
niklas.baumgarten's avatar
niklas.baumgarten committed
26
27
28
        _levelMap.insert(LevelMonteCarloPair(newLevel, MonteCarlo(newLevel, 0, false)));
        _levelMap.find(newLevel)->second.vars.Y = varsY;
        _levelMap.find(newLevel)->second.avgs.Cost = avgsCost;
niklas.baumgarten's avatar
niklas.baumgarten committed
29
        this->UpdateSampleCounter(epsilon);
niklas.baumgarten's avatar
niklas.baumgarten committed
30
31
32
33
34
35
36
    } else {
        Exit ("Maximum level has been reached.")
    }
}

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

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

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

void VariancesMap::Update(const MonteCarloMap &mcMap) {
niklas.baumgarten's avatar
niklas.baumgarten committed
59
    for (auto &[level, mc]: mcMap)
niklas.baumgarten's avatar
niklas.baumgarten committed
60
        _levelMap[level] = mc.vars;
niklas.baumgarten's avatar
niklas.baumgarten committed
61
62
63
}

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