Commit ca7139c8 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

MonteCarlo* -> MonteCarlo

parent 8229f5a4
......@@ -2,21 +2,15 @@
void MultilevelMonteCarlo::initializeMonteCarloMap() {
clearMapMonteCarlo();
mcMap.clear();
for (unsigned long i = 0; i < initLevels.size(); i++) {
bool onlyFine = (i == 0) || (mcOnly);
int M = initSampleAmount[i];
Level level(initLevels[i], initLevels[i] - 1);
mcMap[level] = new MonteCarlo(level, M, onlyFine);
mcMap.insert(LevelMonteCarloPair(level, MonteCarlo(level, M, onlyFine)));
}
}
void MultilevelMonteCarlo::clearMapMonteCarlo() {
for (auto &[level, mc]: mcMap)
delete mc;
mcMap.clear();
}
void MultilevelMonteCarlo::Method() {
mout.StartBlock("MLMC Method");
......@@ -31,7 +25,7 @@ void MultilevelMonteCarlo::Method() {
void MultilevelMonteCarlo::method() {
for (auto &mc : mcMap)
mc.second->Method();
mc.second.Method();
}
void MultilevelMonteCarlo::adaptiveMethod() {
......@@ -40,8 +34,8 @@ void MultilevelMonteCarlo::adaptiveMethod() {
bool converged = false;
while (!converged) {
for (auto &[level, mc] : mcMap)
if (mc->ctr.dM > 0)
mc->Method();
if (mc.ctr.dM > 0)
mc.Method();
mcMap.UpdateSampleCounter(epsilon);
data.ExtractDataFrom(mcMap);
......@@ -59,8 +53,7 @@ void MultilevelMonteCarlo::adaptiveMethod() {
if (errors.numeric > epsilon / 2) {
Level newLevel(mcMap.rbegin()->first.fine + 1,
mcMap.rbegin()->first.fine);
mcMap.AppendLevel(epsilon, exponents, newLevel,
new MonteCarlo(newLevel, 0, false));
mcMap.AppendLevel(epsilon, exponents, newLevel);
} else {
errors.Estimate(data);
vout(1) << "statErr=" << errors.stochastic << endl;
......
......@@ -40,7 +40,7 @@ public:
}
~MultilevelMonteCarlo() {
clearMapMonteCarlo();
mcMap.clear();
}
void Method();
......@@ -59,8 +59,6 @@ private:
void adaptiveMethod();
void initializeMonteCarloMap();
void clearMapMonteCarlo();
};
#endif //MULTILEVELMONTECARLO_HPP
......@@ -7,25 +7,25 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) {
double factor = 0.0;
for (auto &[level, mc] : *this)
factor += sqrt(mc->vars.Y * mc->avgs.Cost);
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)));
sqrt(mc.vars.Y / mc.avgs.Cost)));
if (optimalM == 1) optimalM++; // Hack
mc->ctr.dM = optimalM - mc->ctr.M;
mc.ctr.dM = optimalM - mc.ctr.M;
}
}
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents,
Level newLevel, MonteCarlo *newMc) {
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, Level newLevel) {
if (newLevel <= maxLevel) {
auto oldLevel = this->rbegin()->first;
(*this)[newLevel] = newMc;
(*this)[newLevel]->vars.Y = (*this)[oldLevel]->vars.Y / pow(2.0, exponents.beta);
(*this)[newLevel]->avgs.Cost =
(*this)[oldLevel]->avgs.Cost * pow(2.0, exponents.gamma);
(*this).UpdateSampleCounter(epsilon);
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);
this->insert(LevelMonteCarloPair(newLevel, MonteCarlo(newLevel, 0, false)));
this->find(newLevel)->second.vars.Y = varsY;
this->find(newLevel)->second.avgs.Cost = avgsCost;
this->UpdateSampleCounter(epsilon);
} else {
Exit ("Maximum level has been reached.")
}
......@@ -33,7 +33,7 @@ void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents,
void SampleCounterMap::Update(const MonteCarloMap &mcMap) {
for (auto &[level, mc]: mcMap)
(*this)[level] = mc->ctr;
(*this)[level] = mc.ctr;
}
bool SampleCounterMap::NoSamplesLeft() {
......@@ -48,18 +48,18 @@ bool SampleCounterMap::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?
(*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;
for (auto &[level, mc]: mcMap)
(*this)[level] = mc.vars;
}
void KurtosisMap::Update(const MonteCarloMap &mcMap) {
for (auto &mc: mcMap)
(*this)[mc.first] = mc.second->kurtosis;
for (auto &[level, mc]: mcMap)
(*this)[level] = mc.kurtosis;
}
......@@ -9,7 +9,9 @@
struct Exponents;
struct MonteCarloMap : public LevelMap<MonteCarlo *> {
typedef std::pair<Level, MonteCarlo> LevelMonteCarloPair;
struct MonteCarloMap : public LevelMap<MonteCarlo> {
int maxLevel = 10;
......@@ -17,22 +19,14 @@ struct MonteCarloMap : public LevelMap<MonteCarlo *> {
config.get("maxLevel", maxLevel);
};
// Todo is this all cleaned up nicely?
// code below only not used bc tests are failing
// ~MonteCarloMap() {
// for (auto &[level, mc] : *this)
// delete mc;
// }
MonteCarloMap(std::initializer_list<std::pair<Level, MonteCarlo *>> mcMap) :
LevelMap<MonteCarlo *>(mcMap) {
MonteCarloMap(std::initializer_list<LevelMonteCarloPair> mcMap) :
LevelMap<MonteCarlo>(mcMap) {
config.get("maxLevel", maxLevel);
};
void UpdateSampleCounter(double epsilon);
void AppendLevel(double epsilon, Exponents exponents,
Level newLevel, MonteCarlo *newMc);
void AppendLevel(double epsilon, Exponents exponents, Level newLevel);
};
struct SampleCounterMap : public LevelMap<SampleCounter> {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment