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

refactoring LevelMap

parent e8d48500
......@@ -27,7 +27,7 @@ struct Level {
void Update(int level);
int ActualLevel(bool getCoarse) const {
if(getCoarse) return fine;
if (getCoarse) return fine;
else return coarse;
};
......@@ -64,63 +64,88 @@ struct Level {
*/
template<typename T>
struct LevelMap : public std::map<Level, T> {
struct LevelMap {
protected:
std::map<Level, T> _levelMap;
public:
LevelMap() {};
LevelMap(std::initializer_list<std::pair<Level, T>> levelMap) {
for (std::pair<Level, T> pair : levelMap) {
this->emplace(pair.first, T(pair.second));
_levelMap.emplace(pair.first, T(pair.second));
}
}
std::map<Level, T> SetWithVectors(std::vector<Level> levels,
std::vector<T> entries) {
for (int i = 0; i < levels.size(); i++)
(*this)[levels[i]] = entries[i];
return *this;
}
std::vector<int> GetLevelVector() const {
std::vector<int> levelVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
levelVector.push_back(level.fine);
return levelVector;
}
std::vector<double> GetQVector() const {
std::vector<double> qVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
qVector.push_back(entry.Q);
return qVector;
}
std::vector<double> GetYVector() const {
std::vector<double> yVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
yVector.push_back(entry.Y);
return yVector;
}
std::vector<double> GetCostVector() const {
std::vector<double> costVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
costVector.push_back(entry.Cost);
return costVector;
}
std::vector<int> GetMVector() const {
std::vector<int> mVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
mVector.push_back(entry.M);
return mVector;
}
std::vector<int> GetdMVector() const {
std::vector<int> dMVector;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
dMVector.push_back(entry.dM);
return dMVector;
}
auto size() const { return _levelMap.size(); }
auto clear() { _levelMap.clear(); }
auto insert(std::pair<Level, T> pair) { _levelMap.insert(pair); }
auto begin() { return _levelMap.begin(); }
auto rbegin() { return _levelMap.rbegin(); }
auto end() { return _levelMap.end(); }
auto rend() { return _levelMap.end(); }
auto begin() const { return _levelMap.begin(); }
auto rbegin() const { return _levelMap.rbegin(); }
auto end() const { return _levelMap.end(); }
auto rend() const { return _levelMap.end(); }
auto operator[](Level level) { return _levelMap[level]; }
auto at(Level level) { return _levelMap.at(level); }
auto find(Level level) { return _levelMap.find(level); }
};
#endif //LEVEL_HPP
......@@ -24,8 +24,8 @@ void MultilevelMonteCarlo::Method() {
}
void MultilevelMonteCarlo::method() {
for (auto &mc : mcMap)
mc.second.Method();
for (auto &[level, mc] : mcMap)
mc.Method();
}
void MultilevelMonteCarlo::adaptiveMethod() {
......
......@@ -6,10 +6,10 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) {
int optimalM;
double factor = 0.0;
for (auto &[level, mc] : *this)
for (auto &[level, mc] : _levelMap)
factor += sqrt(mc.vars.Y * mc.avgs.Cost);
for (auto &[level, mc] : *this) {
for (auto &[level, mc] : _levelMap) {
optimalM = (int) (ceil(2 * pow(epsilon, -2) * factor *
sqrt(mc.vars.Y / mc.avgs.Cost)));
if (optimalM == 1) optimalM++; // Hack
......@@ -22,9 +22,9 @@ void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, Level newLe
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;
_levelMap.insert(LevelMonteCarloPair(newLevel, MonteCarlo(newLevel, 0, false)));
_levelMap.find(newLevel)->second.vars.Y = varsY;
_levelMap.find(newLevel)->second.avgs.Cost = avgsCost;
this->UpdateSampleCounter(epsilon);
} else {
Exit ("Maximum level has been reached.")
......@@ -33,12 +33,12 @@ void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, Level newLe
void SampleCounterMap::Update(const MonteCarloMap &mcMap) {
for (auto &[level, mc]: mcMap)
(*this)[level] = mc.ctr;
_levelMap[level] = mc.ctr;
}
bool SampleCounterMap::NoSamplesLeft() {
bool noSamplesLeft = true;
for (auto &&[level, entry]: *this)
for (auto &&[level, entry]: _levelMap)
if (entry.dM > 0) {
noSamplesLeft = false;
break;
......@@ -48,7 +48,7 @@ bool SampleCounterMap::NoSamplesLeft() {
void AveragesMap::Update(const MonteCarloMap &mcMap) {
for (auto &&[level, mc]: mcMap) {
(*this)[level] = mc.avgs;
_levelMap[level] = mc.avgs;
Q += mc.avgs.Y;
Cost += mc.avgs.Cost; // Todo at the right place?
}
......@@ -56,10 +56,10 @@ void AveragesMap::Update(const MonteCarloMap &mcMap) {
void VariancesMap::Update(const MonteCarloMap &mcMap) {
for (auto &[level, mc]: mcMap)
(*this)[level] = mc.vars;
_levelMap[level] = mc.vars;
}
void KurtosisMap::Update(const MonteCarloMap &mcMap) {
for (auto &[level, mc]: mcMap)
(*this)[level] = mc.kurtosis;
_levelMap[level] = mc.kurtosis;
}
Markdown is supported
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