Commit 52204fda authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

implemented EstimatorCreator

parent d2d33343
......@@ -5,23 +5,24 @@
#include "Estimator.hpp"
Estimator *CreateEstimator(const std::string &estimatorName) {
if (estimatorName == "StochasticCollocation")
Estimator *EstimatorCreator::Create() {
if (_estimatorName == "StochasticCollocation")
return new StochasticCollocation();
if (estimatorName == "MultilevelMonteCarlo")
return new MultilevelEstimator();
if (estimatorName == "MonteCarlo")
return new MonteCarlo();
else Exit("Estimator is not implemented")
}
if (_estimatorName == "MultilevelMonteCarlo")
return new MultilevelEstimator(
_levelVec, _samplesVec, _subEstName, _parallel, _meshesCreator, _pdeSolverCreator
);
if (_estimatorName == "MonteCarlo")
return new MonteCarlo(
_level, _initSamples, _onlyFine, _parallel, _meshesCreator, _pdeSolverCreator
);
std::unique_ptr<Estimator> CreateEstimatorUniquePtr(const std::string &estimatorName) {
if (estimatorName == "StochasticCollocation")
return std::make_unique<StochasticCollocation>();
if (estimatorName == "MultilevelMonteCarlo")
return std::make_unique<MultilevelEstimator>();
if (estimatorName == "MonteCarlo")
return std::make_unique<MonteCarlo>();
else Exit("Estimator is not implemented")
Exit("Estimator is not implemented")
}
std::unique_ptr<Estimator> EstimatorCreator::CreateUnique() {
return std::unique_ptr<Estimator>(Create());
}
......@@ -2,6 +2,9 @@
#define ESTIMATOR_HPP
#include "WelfordAggregate.hpp"
#include "PDESolverCreator.hpp"
#include "Exponents.hpp"
#include "LevelMaps.hpp"
#include "Errors.hpp"
#include "MeshesCreator.hpp"
......@@ -13,6 +16,8 @@ protected:
double epsilon = 0.0;
double initSamples;
double value;
double cost;
......@@ -23,7 +28,7 @@ public:
WelfordAggregate aggregate;
Estimator(int initSamples = 0) :
aggregate(WelfordAggregate(initSamples)) {
initSamples(initSamples), aggregate(WelfordAggregate(initSamples)) {
config.get("epsilon", epsilon);
}
......@@ -55,78 +60,137 @@ public:
virtual void ExponentResults() const {};
};
Estimator *CreateEstimator(const std::string &estimatorName);
std::unique_ptr<Estimator> CreateEstimatorUniquePtr(const std::string &estimatorName);
//class EstimatorCreator {
//private:
// int _level = 0;
//
// bool _onlyFine = false;
//
// bool _parallel = true;
//
// std::string _estimatorName = "MonteCarlo";
//
// MeshesCreator *meshesCreator;
//
// PDESolverCreator *pdeSolverCreator;
//
//public:
// EstimatorCreator(const std::string &estimatorName) : _estimatorName(estimatorName) {}
//
// EstimatorCreator WithInitLevel(int level) {
// _level = level;
// return *this;
// }
//
// MeshesCreator WithNeumannAt(std::vector<int> faceIndex) {
// return *this;
// }
//
// MeshesCreator WithProcIds(std::vector<int> procIds) {
// return *this;
// }
//
// MeshesCreator WithLevel(int level) {
// _level = level;
// return *this;
// }
//
// MeshesCreator WithPLevel(int pLevel) {
// _pLevel = pLevel;
// return *this;
// }
//
// MeshesCreator WithDistribute(const std::string &distName) {
// _distName = distName;
// return *this;
// }
//
// MeshesCreator WithoutDistribute() {
// _distName = "NoDistribution";
// return *this;
// }
//
// MeshesCreator WithOverlap(const std::string &olapName) {
// _olapName = olapName;
// return *this;
// }
//
// MeshesCreator WithoutOverlap() {
// _olapName = "NoOverlap";
// return *this;
// }
//
// MeshesCreator WithCommSplit(int commSplit) {
// _commSplit = commSplit;
// return *this;
// }
//
// Meshes *Create();
//
// std::unique_ptr<Meshes> CreateUnique();
//};
typedef std::vector<int> Levels;
typedef std::vector<int> Samples;
class EstimatorCreator {
private:
int _level;
int _initSamples;
double _epsilon;
Levels _levelVec;
Samples _samplesVec;
bool _parallel = true;
bool _onlyFine = false;
std::string _subEstName = "MonteCarlo";
std::string _estimatorName = "MonteCarlo";
MeshesCreator _meshesCreator;
PDESolverCreator _pdeSolverCreator;
public:
EstimatorCreator(const std::string &estimatorName) : _estimatorName(estimatorName) {
config.get("Level", _level);
config.get("Samples", _initSamples);
config.get("OnlyFine", _onlyFine);
config.get("MCParallel", _parallel);
config.get("initLevels", _levelVec);
config.get("initSamples", _samplesVec);
}
EstimatorCreator WithInitLevel(int level) {
_level = level;
return *this;
}
EstimatorCreator WithInitLevel(const std::vector<int> &levels) {
_levelVec = levels;
return *this;
}
EstimatorCreator WithEpsilon(double epsilon) {
_epsilon = epsilon;
return *this;
}
EstimatorCreator WithInitSamples(int initSamples) {
_initSamples = initSamples;
return *this;
}
EstimatorCreator WithOnlyFine(bool onlyFine) {
_onlyFine = onlyFine;
return *this;
}
EstimatorCreator WithParallel(bool parallel) {
_parallel = parallel;
return *this;
}
EstimatorCreator WithInitSamples(const std::vector<int> &initSamples) {
_samplesVec = initSamples;
return *this;
}
EstimatorCreator WithMeshesCreator(MeshesCreator meshesCreator) {
_meshesCreator = meshesCreator;
return *this;
}
EstimatorCreator WithPDESolverCreator(PDESolverCreator pdeSolverCreator) {
_pdeSolverCreator = pdeSolverCreator;
return *this;
}
Estimator *Create();
std::unique_ptr<Estimator> CreateUnique();
};
struct EstimatorMap : public LevelMap<Estimator *> {
int maxLevel = 10;
EstimatorMap() {
config.get("maxLevel", maxLevel);
};
EstimatorMap(std::initializer_list<std::pair<int, Estimator *>> estimatorMap) :
LevelMap<Estimator *>(estimatorMap) {
config.get("maxLevel", maxLevel);
};
void UpdateSampleCounter(double epsilon);
void AppendLevel(double epsilon, Exponents exponents, int newLevel);
void SetEstimatorMapData(const MultilevelData &_data) {
for (auto &[level, estimator] : _levelMap) {
estimator->aggregate.ctr = _data.ctrs.at(level);
estimator->aggregate.mean = _data.means.at(level);
estimator->aggregate.sVar = _data.sVars.at(level);
estimator->aggregate.kurtosis = _data.kurtosis.at(level);
}
}
void Fill(Levels initLevels, Samples initSamples,
const std::string &estimatorName, bool parallel,
MeshesCreator meshesCreator = MeshesCreator(),
PDESolverCreator pdeCreator = PDESolverCreator()) {
for (auto i = 0; i < initLevels.size(); i++)
this->insert({initLevels[i],
EstimatorCreator(estimatorName).
WithPDESolverCreator(pdeCreator).
WithMeshesCreator(meshesCreator).
WithInitSamples(initSamples[i]).
WithInitLevel(initLevels[i]).
WithOnlyFine((i == 0)).
WithParallel(parallel).
Create()});
}
};
#endif //ESTIMATOR_HPP
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