Commit 66dbb6ed authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactoring

parent 185349ef
Pipeline #149212 failed with stages
in 4 minutes and 9 seconds
......@@ -6,7 +6,7 @@
#include "MultilevelMonteCarlo.hpp"
#include "MonteCarlo.hpp"
#include "IEstimator.hpp"
#include "Estimator.hpp"
bool PowerOfTwo(int n) {
......@@ -14,7 +14,7 @@ bool PowerOfTwo(int n) {
return (ceil(log2(n)) == floor(log2(n)));
}
std::unique_ptr<IEstimator> CreateEstimator(const std::string &estimator) {
std::unique_ptr<Estimator> CreateEstimator(const std::string &estimator) {
if (estimator == "MultilevelStochasticCollocation")
return std::make_unique<MultilevelStochasticCollocation>();
if (estimator == "StochasticCollocation")
......@@ -33,7 +33,7 @@ private:
std::string estimatorName = "MultilevelMonteCarlo";
public:
std::unique_ptr<IEstimator> estimator;
std::unique_ptr<Estimator> estimator;
MainProgram() {
config.get("MainVerbose", verbose);
......
add_library(MONTECARLO STATIC
MonteCarlo.cpp
MultilevelMonteCarlo.cpp
datastructure/Errors.cpp
datastructure/MultilevelErrors.cpp
datastructure/Exponents.cpp
datastructure/WelfordAggregate.cpp
datastructure/EmpiricMeasureLevelMaps.cpp
......
#ifndef IESTIMATOR_HPP
#define IESTIMATOR_HPP
#ifndef ESTIMATOR_HPP
#define ESTIMATOR_HPP
#include "Config.hpp"
#include "WelfordAggregate.hpp"
//#include "Errors.hpp"
#include "Errors.hpp"
struct Exponents;
class IEstimator {
class Estimator {
protected:
double epsilon = 0.0;
// Errors errors;
Errors errors;
public:
IEstimator() {
WelfordAggregate aggregate;
Estimator() {
config.get("epsilon", epsilon);
}
......@@ -24,11 +26,11 @@ public:
double Epsilon() const { return epsilon; }
virtual void Method() = 0;
virtual void Method() {};
virtual std::string Name() const = 0;
virtual std::string Name() const { return "Estimator"; };
virtual void EstimatorResults() const = 0;
virtual void EstimatorResults() const {};
virtual void MultilevelResults() const {};
......@@ -36,4 +38,4 @@ public:
};
#endif //IESTIMATOR_HPP
#endif //ESTIMATOR_HPP
......@@ -3,12 +3,12 @@
#include "WelfordAggregate.hpp"
#include "PDESolverCreator.hpp"
#include "IEstimator.hpp"
#include "Estimator.hpp"
#include "MeshesCreator.hpp"
class MonteCarlo : public IEstimator {
class MonteCarlo : public Estimator {
protected:
int verbose = 1;
......
......@@ -4,17 +4,15 @@
#include "EmpiricMeasureLevelMaps.hpp"
#include "MonteCarlo.hpp"
#include "Exponents.hpp"
#include "Errors.hpp"
#include "MultilevelErrors.hpp"
#include "Utilities.hpp"
#include "IEstimator.hpp"
#include "Estimator.hpp"
class MultilevelMonteCarlo : public IEstimator {
class MultilevelMonteCarlo : public Estimator {
private:
int verbose = 1;
bool mcOnly = false;
// Levels init{3, 4, 5};
std::vector<int> initLevels{3, 4, 5};
......@@ -22,20 +20,16 @@ private:
std::vector<int> initSampleAmount{12, 6, 3};
public:
double epsilon = 0.0;
MonteCarloMap mcMap;
// Here Could be EstimatorMap
EstimatorMap mcMap;
MultilevelData data;
Exponents exponents;
Errors errors;
MultilevelErrors errors;
MultilevelMonteCarlo() : IEstimator() {
MultilevelMonteCarlo() : Estimator() {
config.get("MLMCVerbose", verbose);
config.get("mcOnly", mcOnly);
config.get("initLevels", initLevels);
config.get("initSampleAmount", initSampleAmount);
......
#ifndef MULTILEVELSTOCHASTICCOLLOCATION_HPP
#define MULTILEVELSTOCHASTICCOLLOCATION_HPP
#include "IEstimator.hpp"
#include "Estimator.hpp"
class MultilevelStochasticCollocation : public IEstimator {
class MultilevelStochasticCollocation : public Estimator {
private:
public:
......
#ifndef STOCHASTICCOLLOCATION_HPP
#define STOCHASTICCOLLOCATION_HPP
#include "IEstimator.hpp"
#include "Estimator.hpp"
class StochasticCollocation : public IEstimator {
class StochasticCollocation : public Estimator {
private:
public:
......
......@@ -2,7 +2,7 @@
#include "Exponents.hpp"
void MonteCarloMap::UpdateSampleCounter(double epsilon) {
void EstimatorMap::UpdateSampleCounter(double epsilon) {
int optimalM;
double factor = 0.0;
......@@ -17,7 +17,7 @@ void MonteCarloMap::UpdateSampleCounter(double epsilon) {
}
}
void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, int newLevel) {
void EstimatorMap::AppendLevel(double epsilon, Exponents exponents, int newLevel) {
if (newLevel <= maxLevel) {
auto oldLevel = this->rbegin();
double sVarY = oldLevel->second.aggregate.sVar.Y / pow(2.0, exponents.beta);
......@@ -29,13 +29,14 @@ void MonteCarloMap::AppendLevel(double epsilon, Exponents exponents, int newLeve
_levelMap.find(newLevel)->second.aggregate.sVar.Y = sVarY;
_levelMap.find(newLevel)->second.aggregate.mean.C = avgsCost;
this->UpdateSampleCounter(epsilon);
} else {
Exit ("Maximum level has been reached.")
}
}
void SampleCounterMap::Update(const MonteCarloMap &mcMap) {
void SampleCounterMap::Update(const EstimatorMap &mcMap) {
for (auto &[level, mc]: mcMap)
_levelMap[level] = SampleCounter(mc.aggregate.ctr);
}
......@@ -50,7 +51,7 @@ bool SampleCounterMap::NoSamplesLeft() {
return noSamplesLeft;
}
void MeanMap::Update(const MonteCarloMap &mcMap) {
void MeanMap::Update(const EstimatorMap &mcMap) {
for (auto &&[level, mc]: mcMap) {
_levelMap[level] = mc.aggregate.mean;
Q += mc.aggregate.mean.Y;
......@@ -58,12 +59,12 @@ void MeanMap::Update(const MonteCarloMap &mcMap) {
}
}
void SVarMap::Update(const MonteCarloMap &mcMap) {
void SVarMap::Update(const EstimatorMap &mcMap) {
for (auto &[level, mc]: mcMap)
_levelMap[level] = mc.aggregate.sVar;
}
void KurtosisMap::Update(const MonteCarloMap &mcMap) {
void KurtosisMap::Update(const EstimatorMap &mcMap) {
for (auto &[level, mc]: mcMap)
_levelMap[level] = mc.aggregate.kurtosis;
}
......@@ -10,16 +10,16 @@
struct Exponents;
struct MonteCarloMap : public LevelMap<MonteCarlo> {
struct EstimatorMap : public LevelMap<Estimator> {
int maxLevel = 10;
MonteCarloMap() {
EstimatorMap() {
config.get("maxLevel", maxLevel);
};
MonteCarloMap(std::initializer_list<std::pair<int, MonteCarlo>> mcMap) :
LevelMap<MonteCarlo>(mcMap) {
EstimatorMap(std::initializer_list<std::pair<int, Estimator>> mcMap) :
LevelMap<Estimator>(mcMap) {
config.get("maxLevel", maxLevel);
};
......@@ -35,7 +35,7 @@ struct SampleCounterMap : public LevelMap<SampleCounter> {
SampleCounterMap(std::initializer_list<std::pair<int, SampleCounter>> ctrs) :
LevelMap<SampleCounter>(ctrs) {};
void Update(const MonteCarloMap &mcMap);
void Update(const EstimatorMap &mcMap);
bool NoSamplesLeft();
};
......@@ -49,11 +49,11 @@ struct MeanMap : public LevelMap<Mean> {
MeanMap(std::initializer_list<std::pair<int, Mean>> avgs) :
LevelMap<Mean>(avgs) {};
MeanMap(const MonteCarloMap &mcMap) {
MeanMap(const EstimatorMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
void Update(const EstimatorMap &mcMap);
};
struct SVarMap : public LevelMap<SVar> {
......@@ -63,11 +63,11 @@ struct SVarMap : public LevelMap<SVar> {
SVarMap(std::initializer_list<std::pair<int, SVar>> vars) :
LevelMap<SVar>(vars) {};
SVarMap(const MonteCarloMap &mcMap) {
SVarMap(const EstimatorMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
void Update(const EstimatorMap &mcMap);
};
struct KurtosisMap : LevelMap<Kurtosis> {
......@@ -77,11 +77,11 @@ struct KurtosisMap : LevelMap<Kurtosis> {
KurtosisMap(std::initializer_list<std::pair<int, Kurtosis>> kurtosis) :
LevelMap<Kurtosis>(kurtosis) {};
KurtosisMap(const MonteCarloMap &mcMap) {
KurtosisMap(const EstimatorMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
void Update(const EstimatorMap &mcMap);
};
struct MultilevelData {
......@@ -95,7 +95,7 @@ struct MultilevelData {
MultilevelData() {};
MultilevelData(const MonteCarloMap &mcMap) {
MultilevelData(const EstimatorMap &mcMap) {
ExtractDataFrom(mcMap);
}
......@@ -103,7 +103,7 @@ struct MultilevelData {
const SVarMap &vars, const KurtosisMap &kurtosis) :
ctrs(ctrs), means(avgs), sVars(vars), kurtosis(kurtosis) {};
void ExtractDataFrom(const MonteCarloMap &mcMap) {
void ExtractDataFrom(const EstimatorMap &mcMap) {
ctrs.Update(mcMap);
means.Update(mcMap);
sVars.Update(mcMap);
......@@ -130,7 +130,7 @@ struct MultilevelData {
<< "kurtosis=" << data.kurtosis.GetYVector() << endl
<< "E(cost)=" << data.means.GetCostVector() << endl
<< "Used Levels=" << data.means.GetLevelVector() << endl
<< "Used Samples" << data.ctrs.GetMVector() << endl;
<< "Used Samples=" << data.ctrs.GetMVector() << endl;
}
};
......
#include "Exponents.hpp"
void Exponents::ComputeExponents(const MonteCarloMap &mcMap) {
void Exponents::ComputeExponents(const EstimatorMap &mcMap) {
ComputeExponents(MultilevelData(mcMap));
}
......
......@@ -11,7 +11,7 @@ struct Exponents {
Exponents() {};
Exponents(const MonteCarloMap &mcMap) {
Exponents(const EstimatorMap &mcMap) {
ComputeExponents(mcMap);
};
......@@ -21,7 +21,7 @@ struct Exponents {
void ComputeExponents(const MultilevelData &data);
void ComputeExponents(const MonteCarloMap &mcMap);
void ComputeExponents(const EstimatorMap &mcMap);
void ComputeExponents(const MeanMap &mean, const SVarMap &sVar);
......
#include "Errors.hpp"
#include "MultilevelErrors.hpp"
void Errors::Estimate(const MonteCarloMap &mcMap) {
void MultilevelErrors::Estimate(const EstimatorMap &mcMap) {
Estimate(MultilevelData(mcMap));
}
void Errors::Estimate(const MultilevelData &data) {
void MultilevelErrors::Estimate(const MultilevelData &data) {
exponents.ComputeExponents(data);
numeric = EstimateNumeric(data);
stochastic = EstimateStochastic(data);
total = numeric + stochastic;
}
double Errors::EstimateNumeric(const MultilevelData &data) {
double MultilevelErrors::EstimateNumeric(const MultilevelData &data) {
exponents.ComputeExponents(data);
return EstimateNumeric(data.means);
}
double Errors::EstimateNumeric(const MeanMap &avgs) {
auto _levels = avgs.GetLevelVector();
auto _avgs = avgs.GetYVector();
double MultilevelErrors::EstimateNumeric(const MeanMap &means) {
auto _levels = means.GetLevelVector();
auto _avgs = means.GetYVector();
double alpha = exponents.alpha;
if (alpha == 0.0) alpha = exponents.ComputeAlpha(avgs);
if (alpha == 0.0) alpha = exponents.ComputeAlpha(means);
double err = 0.0;
int integer = avgs.size() - 2;
int integer = means.size() - 2;
for (int i = 1; i < _levels.size(); i++) {
err = max(_avgs[i] / (pow(2.0, alpha) - 1) / (pow(2.0, integer)), err);
integer--;
......@@ -34,13 +34,13 @@ double Errors::EstimateNumeric(const MeanMap &avgs) {
return err;
}
double Errors::EstimateStochastic(const MultilevelData &data) {
double MultilevelErrors::EstimateStochastic(const MultilevelData &data) {
return EstimateStochastic(data.ctrs, data.sVars);
}
double Errors::EstimateStochastic(const SampleCounterMap &ctrs, const SVarMap &vars) {
double MultilevelErrors::EstimateStochastic(const SampleCounterMap &ctrs, const SVarMap &sVars) {
auto _ctrs = ctrs.GetMVector();
auto _vars = vars.GetYVector();
auto _vars = sVars.GetYVector();
double err = 0.0;
for (int i = 0; i < _ctrs.size(); i++)
......
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