MUQ  0.4.3
MIMCMC.cpp
Go to the documentation of this file.
2 
4 
5 using namespace muq::SamplingAlgorithms;
6 
7 MIMCMC::MIMCMC(boost::property_tree::ptree pt,
8  Eigen::VectorXd const& startPt,
9  std::vector<std::shared_ptr<muq::Modeling::ModPiece>> const& models,
10  std::shared_ptr<MultiIndexSet> const& multis) : MIMCMC(pt,startPt, CreateProblems(models),multis)
11 {
12 
13 }
14 
15 MIMCMC::MIMCMC(boost::property_tree::ptree pt,
16  Eigen::VectorXd const& startPt,
17  std::vector<std::shared_ptr<AbstractSamplingProblem>> const& problems,
18  std::shared_ptr<MultiIndexSet> const& multis) : MIMCMC(pt, std::make_shared<DefaultComponentFactory>(pt,startPt,ProcessMultis(multis,problems.size()),problems))
19 {
20 
21 }
22 
23 
24 MIMCMC::MIMCMC (pt::ptree pt, std::shared_ptr<MIComponentFactory> const& componentFactory)
25 : pt(pt),
26  componentFactory(componentFactory)
27 {
28  gridIndices = MultiIndexFactory::CreateFullTensor(componentFactory->FinestIndex()->GetVector());
29 
30  for (int i = 0; i < gridIndices->Size(); i++) {
31  std::shared_ptr<MultiIndex> boxHighestIndex = (*gridIndices)[i];
32  auto box = std::make_shared<MIMCMCBox>(componentFactory, boxHighestIndex);
33  boxes.push_back(box);
34  }
35 }
36 
37 std::shared_ptr<MIMCMCBox> MIMCMC::GetBox(std::shared_ptr<MultiIndex> index) {
38  for (std::shared_ptr<MIMCMCBox> box : boxes) {
39  if (*(box->GetHighestIndex()) == *index)
40  return box;
41  }
42  return nullptr;
43 }
44 
45 std::shared_ptr<MultiIndexEstimator> MIMCMC::GetSamples() const {
46  return std::make_shared<MultiIndexEstimator>(boxes);
47 }
48 std::shared_ptr<MultiIndexEstimator> MIMCMC::GetQOIs() const {
49  return std::make_shared<MultiIndexEstimator>(boxes,true);
50 }
51 
52 std::shared_ptr<MultiIndexEstimator> MIMCMC::Run() {
53  for (auto box : boxes) {
54  assert(box);
55  int numSamples = pt.get<int>("NumSamples" + multiindexToConfigString(box->GetHighestIndex()));
56  for (int samp = 0; samp < numSamples; samp++) {
57  box->Sample();
58  }
59  }
60 
61  return GetSamples();
62 }
63 
64 std::shared_ptr<MIMCMCBox> MIMCMC::GetMIMCMCBox(std::shared_ptr<MultiIndex> index) {
65  for (auto box : boxes) {
66  if (*(box->GetHighestIndex()) == *index)
67  return box;
68  }
69  return nullptr;
70 }
71 
72 void MIMCMC::WriteToFile(std::string filename) {
73  for (auto box : boxes) {
74  box->WriteToFile(filename);
75  }
76 }
77 
78 
79 std::shared_ptr<MultiIndexSet> MIMCMC::GetIndices() {
80  return gridIndices;
81 }
82 
83 
84 std::string MIMCMC::multiindexToConfigString (std::shared_ptr<MultiIndex> index) {
85  std::stringstream strs;
86  for (int i = 0; i < index->GetLength(); i++) {
87  strs << "_" << index->GetValue(i);
88  }
89  return strs.str();
90 }
91 
92 void MIMCMC::Draw(bool drawSamples) {
93  std::ofstream graphfile;
94  graphfile.open ("graph");
95  graphfile << "digraph {" << std::endl;
96  graphfile << "nodesep=1.2;" << std::endl;
97  graphfile << "splines=false;" << std::endl;
98  for (auto box : boxes) {
99  box->Draw(graphfile, drawSamples);
100  }
101  graphfile << "}" << std::endl;
102  graphfile.close();
103 }
104 
105 std::shared_ptr<MultiIndexSet> MIMCMC::ProcessMultis(std::shared_ptr<MultiIndexSet> const& multis,
106  unsigned int numLevels)
107 {
108  if(multis){
109  return multis;
110  }else{
111  return MultiIndexFactory::CreateFullTensor(1, numLevels-1);
112  }
113 }
114 
115 std::vector<std::shared_ptr<AbstractSamplingProblem>> MIMCMC::CreateProblems(std::vector<std::shared_ptr<muq::Modeling::ModPiece>> const& models)
116 {
117  std::vector<std::shared_ptr<AbstractSamplingProblem>> output(models.size());
118  for(unsigned int i=0; i<models.size(); ++i)
119  output.at(i) = std::make_shared<SamplingProblem>(models.at(i));
120 
121  return output;
122 }
Provides a high level interface for the sampling problems on each MIMCMC level.
Multiindex MCMC method.
Definition: MIMCMC.h:21
std::shared_ptr< MIMCMCBox > GetBox(std::shared_ptr< MultiIndex > index)
Access an MIMCMCBox.
Definition: MIMCMC.cpp:37
MIMCMC(boost::property_tree::ptree options, std::shared_ptr< MIComponentFactory > const &componentFactory)
void Draw(bool drawSamples=true)
Draw MI structure (mostly debugging purposes)
Definition: MIMCMC.cpp:92
std::string multiindexToConfigString(std::shared_ptr< MultiIndex > index)
Definition: MIMCMC.cpp:84
std::shared_ptr< MultiIndexSet > GetIndices()
Get set of indices of boxes set up by the method.
Definition: MIMCMC.cpp:79
virtual std::shared_ptr< MultiIndexEstimator > GetQOIs() const
Definition: MIMCMC.cpp:48
std::shared_ptr< MIMCMCBox > GetMIMCMCBox(std::shared_ptr< MultiIndex > index)
Definition: MIMCMC.cpp:64
std::shared_ptr< MultiIndexSet > gridIndices
Definition: MIMCMC.h:69
static std::shared_ptr< MultiIndexSet > ProcessMultis(std::shared_ptr< MultiIndexSet > const &multis, unsigned int numLevels)
Definition: MIMCMC.cpp:105
virtual std::shared_ptr< MultiIndexEstimator > Run()
Definition: MIMCMC.cpp:52
static std::vector< std::shared_ptr< AbstractSamplingProblem > > CreateProblems(std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &models)
Definition: MIMCMC.cpp:115
void WriteToFile(std::string filename)
Write HDF5 output for the entire MIMCMC method.
Definition: MIMCMC.cpp:72
virtual std::shared_ptr< MultiIndexEstimator > GetSamples() const
Definition: MIMCMC.cpp:45
std::vector< std::shared_ptr< MIMCMCBox > > boxes
Definition: MIMCMC.h:71
std::shared_ptr< MIComponentFactory > componentFactory
Definition: MIMCMC.h:70