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

added constructor to WelfordAggregate and changed signature of Estimator constructor

parent ef83024a
Pipeline #149334 failed with stages
in 11 minutes and 52 seconds
......@@ -23,7 +23,10 @@ protected:
public:
WelfordAggregate aggregate;
Estimator() { config.get("epsilon", epsilon); }
Estimator(int initSamples = 0) :
aggregate(WelfordAggregate(initSamples)) {
config.get("epsilon", epsilon);
}
double TotalError() const { return errors.total; }
......
......@@ -18,7 +18,7 @@ protected:
bool parallel;
// Todo make unique pointer, there are issues with cleaning up this object!
// Todo make unique pointer, there are issues with cleaning up these objects
Meshes *meshes;
PDESolver *pdeSolver;
......@@ -34,20 +34,20 @@ public:
MonteCarlo(int level, int dM, bool onlyFine, bool parallel) :
level(level), onlyFine(onlyFine), parallel(parallel),
pdeSolverCreator(PDESolverCreator()),
Estimator(dM), pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName())) {
Init(dM);
}
MonteCarlo(int level, int dM, bool onlyFine, bool parallel,
MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
level(level), onlyFine(onlyFine), parallel(parallel),
level(level), onlyFine(onlyFine), parallel(parallel), Estimator(dM),
pdeSolverCreator(pdeCreator), meshesCreator(meshesCreator) {
Init(dM);
}
MonteCarlo() :
pdeSolverCreator(PDESolverCreator()),
Estimator(), pdeSolverCreator(PDESolverCreator()),
meshesCreator(MeshesCreator(pdeSolverCreator.GetMeshName())) {
int dM = 0;
config.get("Level", level);
......@@ -58,7 +58,7 @@ public:
}
MonteCarlo(MeshesCreator meshesCreator, PDESolverCreator pdeCreator) :
pdeSolverCreator(pdeCreator), meshesCreator(meshesCreator) {
Estimator(), pdeSolverCreator(pdeCreator), meshesCreator(meshesCreator) {
int dM = 0;
config.get("Level", level);
config.get("Samples", dM);
......@@ -71,16 +71,6 @@ public:
return "MonteCarlo";
}
void EstimatorResults() const override {
mout.PrintInfo("MLMC Results", verbose,
PrintInfoEntry("Value", aggregate.mean.Q),
PrintInfoEntry("Cost", aggregate.mean.C * aggregate.ctr.M),
PrintInfoEntry("Epsilon", epsilon));
// PrintInfoEntry("Total Error", errors.total),
// PrintInfoEntry("Stochastical Error", errors.stochastic),
// PrintInfoEntry("Numerical Error", errors.numeric));
}
void Init(int dM) {
config.get("MCVerbose", verbose);
config.get("MCParallel", parallel);
......
......@@ -74,7 +74,8 @@ public:
CreateMonteCarloMap(estimatorMap, initLevels, initSamples);
}
MultilevelEstimator(const EstimatorMap &mcMap) : Estimator(), estimatorMap(mcMap) {
MultilevelEstimator(const EstimatorMap &estimatorMap) :
Estimator(), estimatorMap(estimatorMap) {
config.get("MLMCVerbose", verbose);
}
......
......@@ -8,6 +8,8 @@ class StochasticCollocation : public Estimator {
private:
public:
StochasticCollocation() {}
std::string Name() const override {
return "StochasticCollocation";
}
......
......@@ -60,7 +60,7 @@ struct Mean {
friend Logging &operator<<(Logging &s, const Mean &mean) {
return s << "MeanQ=" << mean.Q
<< " MeanY=" << mean.Y
// << " MeanC=" << mean.C
// << " MeanC=" << mean.C
<< " MeanQcomm=" << mean.Qcomm
<< " MeanYcomm=" << mean.Ycomm << endl;
// << " MeanCcomm=" << mean.Ccomm << endl;
......@@ -92,7 +92,7 @@ struct SVar {
friend Logging &operator<<(Logging &s, const SVar &sVar) {
return s << "SVarQ=" << sVar.Q
<< " SVarY=" << sVar.Y
// << " SVarC=" << sVar.C
// << " SVarC=" << sVar.C
<< " SVarQcomm=" << sVar.Qcomm
<< " SVarYcomm=" << sVar.Ycomm << endl;
// << " SVarCcomm=" << sVar.Ccomm << endl;
......@@ -194,6 +194,10 @@ public:
bool parallel = true;
public:
WelfordAggregate(int dM) {
UpdateSampleCounter(dM);
}
void Update(const SampleSolution &fineSolution, const SampleSolution &coarseSolution) {
double newC = fineSolution.Cost;
double newQ = fineSolution.Q;
......
#include "TestMonteCarlo.hpp"
/*
* Todo: More tests with other pde's.
* Maybe hand 1D problems to Chinmay.
* Test Parallel MonteCarlo against Seriell one
* MonteCarlo tests with epsilon, level such that total error can be met
* Idea: Point mesh for ode's -> Enables parallelized ode solving
*
* Extend this test idea to multilevel test
* Then Extend idea to Collocation
*
* Test of Estimators against each other
*/
INSTANTIATE_TEST_SUITE_P(TestMonteCarlo, TestMonteCarloSeriellInterval, Values(
TestStruct{"StochasticDummyScalarGeneratorProblem", "GeneratorValue", "DummyPDESolver",
0.0, 0.0, 1.0, 1.0},
......
......@@ -19,6 +19,7 @@ protected:
NormalDistributionReal normalDist;
TestWelfordAggregate(int numSamples) :
aggregate(WelfordAggregate(numSamples)),
meshes(MeshesCreator("Interval").CreateUnique()),
normalDist(NormalDistributionReal(*meshes)), numSamples(numSamples) {
}
......@@ -63,9 +64,7 @@ protected:
class TestWelfordAggregate4Samples : public TestWelfordAggregate {
public:
TestWelfordAggregate4Samples() : TestWelfordAggregate(4) {
aggregate.UpdateSampleCounter(numSamples);
}
TestWelfordAggregate4Samples() : TestWelfordAggregate(4) {}
};
TEST_F(TestWelfordAggregate4Samples, TestSampleCounter) {
......@@ -102,9 +101,7 @@ TEST_F(TestWelfordAggregate4Samples, TestSVar) { TestSVarParallel(); }
class TestWelfordAggregate8Samples : public TestWelfordAggregate {
public:
TestWelfordAggregate8Samples() : TestWelfordAggregate(8) {
aggregate.UpdateSampleCounter(numSamples);
}
TestWelfordAggregate8Samples() : TestWelfordAggregate(8) {}
};
TEST_F(TestWelfordAggregate8Samples, TestSampleCounter) {
......@@ -139,9 +136,7 @@ TEST_F(TestWelfordAggregate8Samples, TestMean) { TestMeanParallel(); }
class TestWelfordAggregate1e6Samples : public TestWelfordAggregate {
public:
TestWelfordAggregate1e6Samples() : TestWelfordAggregate(1e6) {
aggregate.UpdateSampleCounter(numSamples);
}
TestWelfordAggregate1e6Samples() : TestWelfordAggregate(1e6) {}
};
TEST_F(TestWelfordAggregate1e6Samples, TestSampleCounter) {
......@@ -166,21 +161,4 @@ TEST_F(TestWelfordAggregate1e6Samples, TestSampleCounter) {
TEST_F(TestWelfordAggregate1e6Samples, TestMean) { TestMeanParallel(); }
//while (aggregate.ctr.dMcomm != 0)
//aggregate.Update(
//1.0, normalDist.DrawAndEvalSample(SampleID(0, aggregate.index(), false)), 0.0
//);
//EXPECT_NEAR(aggregate.MeanQ, 0.0, sqrt(1.0 / numSamples));
//EXPECT_NEAR(aggregate.MeanY, 0.0, sqrt(1.0 / numSamples));
//// EXPECT_NEAR(aggregate.SVarQ, 1.0, sqrt(10.0 / numSamples));
//// EXPECT_NEAR(aggregate.SVarY, 1.0, sqrt(10.0 / numSamples));
#endif //TESTWELFORDAGGREGATE_HPP
......@@ -612,7 +612,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"scrolled": false
},
......@@ -657,6 +657,20 @@
" MonteCarlo l=3: MeanQ=0 MeanY=0 MeanQcomm=0 MeanYcomm=0\n",
" MonteCarlo l=3: SVarQ=0 SVarY=0 SVarQcomm=0 SVarYcomm=0\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-812128e7e28b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mmpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"mc_elliptic\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mmpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse_log\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/CLionProjects/mlmc/mpp/python/mppy/mppy.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, kernels, config, kwargs, return_log)\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\n================ running mpp ================\\n'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[0mrun_parameters\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchain_run_parameters\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkernels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 72\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_subprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_parameters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcwd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmute\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_log\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 73\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mchain_run_parameters\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkernels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexecutable\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/CLionProjects/mlmc/mpp/python/mppy/mppy.py\u001b[0m in \u001b[0;36mrun_subprocess\u001b[0;34m(self, args, cwd, mute, return_log)\u001b[0m\n\u001b[1;32m 94\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun_subprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcwd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmute\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_log\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 96\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_subprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcwd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmute\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_log\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 97\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mreset_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/CLionProjects/mlmc/mpp/python/mppy/utilities/subprocess_utilities.py\u001b[0m in \u001b[0;36mrun_subprocess\u001b[0;34m(self, args, cwd, mute, return_log)\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mprocess\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcwd\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcwd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstdout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstderr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0mstdout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mmute\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mreturn_log\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
......
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