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

refactoring LevelMap

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