Commit 3475b933 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactored MultilevelEstimator

parent b3bb27c4
Pipeline #159469 failed with stages
in 12 minutes and 24 seconds
......@@ -172,7 +172,7 @@ struct EstimatorMap : public LevelMap<Estimator *> {
void UpdateSampleCounter(double epsilon);
void AppendLevel(double epsilon, Exponents exponents, int newLevel);
void UpdateNewLevel(double epsilon, Exponents exponents, int newLevel);
void SetEstimatorMapData(const MultilevelData &_data) {
for (auto &[level, estimator] : _levelMap) {
......@@ -185,19 +185,30 @@ struct EstimatorMap : public LevelMap<Estimator *> {
void Fill(Levels initLevels, Samples initSamples,
const std::string &estimatorName, bool parallel,
MeshesCreator meshesCreator = MeshesCreator(),
PDESolverCreator pdeCreator = PDESolverCreator()) {
const MeshesCreator& meshesCreator = MeshesCreator(),
const PDESolverCreator& pdeCreator = PDESolverCreator()) {
for (auto i = 0; i < initLevels.size(); i++)
this->insert({initLevels[i],
for (auto level_index = 0; level_index < initLevels.size(); level_index++)
this->insert({initLevels[level_index],
EstimatorCreator(estimatorName).
WithInitSamples(initSamples[level_index]).
WithInitLevel(initLevels[level_index]).
WithPDESolverCreator(pdeCreator).
WithMeshesCreator(meshesCreator).
WithInitSamples(initSamples[i]).
WithInitLevel(initLevels[i]).
WithOnlyFine((i == 0)).
WithOnlyFine((level_index == 0)).
WithParallel(parallel).
Create()});
for (auto level = initLevels.back(); level <= maxLevel; level++)
this->insert({level,
EstimatorCreator(estimatorName).
WithPDESolverCreator(pdeCreator).
WithMeshesCreator(meshesCreator).
WithParallel(parallel).
WithInitLevel(level).
WithOnlyFine(false).
WithInitSamples(0).
Create()});
}
};
......
......@@ -24,7 +24,8 @@ void MultilevelEstimator::Method() {
void MultilevelEstimator::method() {
for (auto &[level, estimator] : estMap)
estimator->Method();
if (estimator->aggregate.ctr.dM > 0)
estimator->Method();
}
void MultilevelEstimator::adaptiveMethod() {
......@@ -46,7 +47,7 @@ void MultilevelEstimator::adaptiveMethod() {
vout(1) << "numErr=" << mlErrors.numeric << endl;
if (mlErrors.numeric > epsilon / 2) {
estMap.AppendLevel(epsilon, exponents, estMap.rbegin()->first + 1);
estMap.UpdateNewLevel(epsilon, exponents, estMap.rbegin()->first + 1);
} else {
mlErrors.Estimate(data);
vout(1) << "statErr=" << mlErrors.stochastic << endl;
......
#include "MultilevelEstimator.hpp"
#include "LevelMaps.hpp"
#include "MonteCarlo.hpp" // todo remove as soon as new MonteCarlo disappears
template<>
......@@ -54,32 +53,15 @@ void EstimatorMap::UpdateSampleCounter(double epsilon) {
}
}
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);
double avgsCost = oldLevel->second->aggregate.mean.C * pow(2.0, exponents.gamma);
// this->insert({newLevel,
// EstimatorCreator(this->at(0)->Name()).
// WithPDESolverCreator(this->at(0)->pdeSolverCreator).
// WithMeshesCreator(meshesCreator).
// WithInitSamples(initSamples[i]).
// WithInitLevel(initLevels[i]).
// WithOnlyFine((i == 0)).
// WithParallel(parallel).
// Create()});
this->insert(
{newLevel, new MonteCarlo(0.0, newLevel, 0, false, true)}
);
_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 EstimatorMap::UpdateNewLevel(double epsilon, Exponents exponents, int newLevel) {
auto oldLevel = newLevel - 1;
double sVarY = _levelMap[oldLevel]->aggregate.sVar.Y / pow(2.0, exponents.beta);
double avgsCost = _levelMap[oldLevel]->aggregate.mean.C * pow(2.0, exponents.gamma);
_levelMap.find(newLevel)->second->aggregate.sVar.Y = sVarY;
_levelMap.find(newLevel)->second->aggregate.mean.C = avgsCost;
this->UpdateSampleCounter(epsilon);
}
void SampleCounterMap::Update(const EstimatorMap &estimatorMap) {
......
......@@ -28,7 +28,7 @@ TEST_F(TestLevelMaps, TestMonteCarloMapUpdateSampleCounter) {
TEST_F(TestLevelMaps, TestMonteCarloMapAppendLevel) {
int newLevel = 7;
estimatorMap.AppendLevel(0.1, Exponents(_data), 7);
estimatorMap.UpdateNewLevel(0.1, Exponents(_data), 7);
EXPECT_FLOAT_EQ(estimatorMap.find(newLevel)->second->aggregate.sVar.Y, 0.25e-02);
EXPECT_FLOAT_EQ(estimatorMap.find(newLevel)->second->aggregate.mean.C, 6400.0);
// floor(25 /4)
......
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