MUQ  0.4.3
ParallelMultilevelMonteCarlo.cpp
Go to the documentation of this file.
4 
7 
15 
16 #include <boost/property_tree/ptree.hpp>
17 
18 namespace pt = boost::property_tree;
19 using namespace muq::Modeling;
20 using namespace muq::SamplingAlgorithms;
21 using namespace muq::Utilities;
22 
23 #include "ParallelProblem.h"
24 
25 int main(int argc, char **argv){
26 
27  MPI_Init(&argc, &argv);
28 
29  pt::ptree pt;
30 
31  pt.put("NumSamples_0", 1e4);
32  pt.put("NumSamples_1", 5e3);
33  pt.put("NumSamples_2", 1e3);
34  pt.put("NumSamples_3", 5e2);
35  pt.put("MLMCMC.Subsampling", 1);
36  pt.put("MCMC.BurnIn", 10); // number of samples for single level
37  pt.put("verbosity", 1); // show some output
38 
39 /*{
40  std::cout << std::endl << "*************** multilevel" << std::endl << std::endl;
41  auto componentFactory = std::make_shared<MyMIComponentFactory>(pt);
42 
43  MIMCMC mimcmc(pt, componentFactory);
44  mimcmc.Run();
45  std::cout << "mean QOI: " << mimcmc.MeanQOI().transpose() << std::endl;
46 
47  auto index_zero = std::make_shared<MultiIndex>(1);
48  index_zero->SetValue(0, 0);
49  std::cout << "coarsest level mean QOI: " << mimcmc.GetBox(index_zero)->FinestChain()->GetQOIs()->Mean().transpose() << std::endl;
50 }
51 
52 
53 {
54  std::cout << std::endl << "*************** single level reference" << std::endl << std::endl;
55  auto index = componentFactory->FinestIndex();
56 
57  auto problem = componentFactory->SamplingProblem(index);
58  auto proposal = componentFactory->Proposal(index, problem);
59 
60  std::vector<std::shared_ptr<TransitionKernel>> kernels(1);
61  kernels[0] = std::make_shared<DummyKernel>(pt,problem,proposal);
62 
63  Eigen::VectorXd startingPoint = componentFactory->StartingPoint(index);
64 
65  auto mcmc = std::make_shared<SingleChainMCMC>(pt,kernels);
66  mcmc->Run(startingPoint);
67  std::cout << "mean QOI: " << mcmc->GetQOIs()->Mean().transpose() << std::endl;
68 }*/
69 
70 {
71  auto comm = std::make_shared<parcer::Communicator>();
72 
73  auto componentFactory = std::make_shared<MyMIComponentFactory>(pt);
74  StaticLoadBalancingMIMCMC parallelMIMCMC (pt, componentFactory);
75 
76  if (comm->GetRank() == 0) {
77  parallelMIMCMC.Run();
78  Eigen::VectorXd meanQOI = parallelMIMCMC.MeanQOI();
79  std::cout << "mean QOI: " << meanQOI.transpose() << std::endl;
80  parallelMIMCMC.WriteToFile("samples.h5");
81  }
82  parallelMIMCMC.Finalize();
83 
84 }
85 
86  MPI_Finalize();
87 
88  return 0;
89 }
int main(int argc, char **argv)
virtual std::shared_ptr< SampleCollection > Run(std::vector< Eigen::VectorXd > const &x0=std::vector< Eigen::VectorXd >())
void Finalize()
Cleanup parallel method, wait for all ranks to finish.
Eigen::VectorXd MeanQOI()
Get mean quantity of interest estimate computed via telescoping sum.