MUQ  0.4.3
ParallelProblem.h
Go to the documentation of this file.
2 
3 #include "MCSampleProposal.h"
4 
5 class MySamplingProblem : public AbstractSamplingProblem {
6 public:
7  MySamplingProblem(std::shared_ptr<muq::Modeling::ModPiece> targetIn)
8  : AbstractSamplingProblem(Eigen::VectorXi::Constant(1,2), Eigen::VectorXi::Constant(1,2)),
9  target(targetIn){}
10 
11  virtual ~MySamplingProblem() = default;
12 
13 
14  virtual double LogDensity(std::shared_ptr<SamplingState> const& state) override {
15  lastState = state;
16  return target->Evaluate(state->state).at(0)(0);
17  };
18 
19  virtual std::shared_ptr<SamplingState> QOI() override {
20  assert (lastState != nullptr);
21  return std::make_shared<SamplingState>(lastState->state[0] * 2, 1.0);
22  }
23 
24 private:
25  std::shared_ptr<SamplingState> lastState = nullptr;
26 
27  std::shared_ptr<muq::Modeling::ModPiece> target;
28 
29 };
30 
31 
32 class MyInterpolation : public MIInterpolation {
33 public:
34  std::shared_ptr<SamplingState> Interpolate (std::shared_ptr<SamplingState> const& coarseProposal, std::shared_ptr<SamplingState> const& fineProposal) {
35  return std::make_shared<SamplingState>(fineProposal->state);
36  }
37 };
38 
39 class MyMIComponentFactory : public ParallelizableMIComponentFactory {
40 public:
42  : pt(pt)
43  { }
44 
45  virtual bool IsInverseProblem() override {
46  return false;
47  }
48 
49  void SetComm(std::shared_ptr<parcer::Communicator> const& comm) override {
50 
51  }
52 
53  virtual std::shared_ptr<MCMCProposal> Proposal (std::shared_ptr<MultiIndex> const& index, std::shared_ptr<AbstractSamplingProblem> const& samplingProblem) override {
54  Eigen::VectorXd mu(2);
55  mu << 1.0, 2.0;
56 
57  Eigen::MatrixXd cov(2,2);
58  cov << 1.0, 0.8,
59  0.8, 1.5;
60 
61  if (index->GetValue(0) == 0) {
62  mu *= 0.8;
63  cov *= 2.0;
64  } else if (index->GetValue(0) == 1) {
65  mu *= 0.9;
66  cov *= 1.5;
67  } else if (index->GetValue(0) == 2) {
68  mu *= 0.99;
69  cov *= 1.1;
70  } else if (index->GetValue(0) == 3) {
71  mu *= 1.0;
72  cov *= 1.0;
73  } else {
74  std::cerr << "Sampling problem not defined!" << std::endl;
75  assert (false);
76  }
77 
78 
79 
80  auto proposalDensity = std::make_shared<Gaussian>(mu, cov);
81 
82  return std::make_shared<MCSampleProposal>(pt, samplingProblem, proposalDensity);
83  }
84 
85  virtual std::shared_ptr<MultiIndex> FinestIndex() override {
86  auto index = std::make_shared<MultiIndex>(1);
87  index->SetValue(0, 3);
88  return index;
89  }
90 
91  virtual std::shared_ptr<MCMCProposal> CoarseProposal (std::shared_ptr<MultiIndex> const& fineIndex,
92  std::shared_ptr<MultiIndex> const& coarseIndex,
93  std::shared_ptr<AbstractSamplingProblem> const& coarseProblem,
94  std::shared_ptr<SingleChainMCMC> const& coarseChain) override {
95  Eigen::VectorXd mu(2);
96  mu << 0.0, 0.0;
97 
98  Eigen::MatrixXd cov_prop(2,2);
99  cov_prop << 1.0, 0.8,
100  0.8, 1.5;
101  auto proposalDensity = std::make_shared<Gaussian>(mu, cov_prop);
102 
103  return std::make_shared<MCSampleProposal>(pt, coarseProblem, proposalDensity);
104  }
105 
106  virtual std::shared_ptr<AbstractSamplingProblem> SamplingProblem (std::shared_ptr<MultiIndex> const& index) override {
107  Eigen::VectorXd mu(2);
108  mu << 1.0, 2.0;
109  Eigen::MatrixXd cov(2,2);
110  cov << 0.7, 0.6,
111  0.6, 1.0;
112 
113  if (index->GetValue(0) == 0) {
114  mu *= 0.8;
115  cov *= 2.0;
116  } else if (index->GetValue(0) == 1) {
117  mu *= 0.9;
118  cov *= 1.5;
119  } else if (index->GetValue(0) == 2) {
120  mu *= 0.99;
121  cov *= 1.1;
122  } else if (index->GetValue(0) == 3) {
123  mu *= 1.0;
124  cov *= 1.0;
125  } else {
126  std::cerr << "Sampling problem not defined!" << std::endl;
127  assert (false);
128  }
129 
130  auto coarseTargetDensity = std::make_shared<Gaussian>(mu, cov)->AsDensity();
131  return std::make_shared<MySamplingProblem>(coarseTargetDensity);
132  }
133 
134  virtual std::shared_ptr<MIInterpolation> Interpolation (std::shared_ptr<MultiIndex> const& index) override {
135  return std::make_shared<MyInterpolation>();
136  }
137 
138  virtual Eigen::VectorXd StartingPoint (std::shared_ptr<MultiIndex> const& index) override {
139  Eigen::VectorXd mu(2);
140  mu << 1.0, 2.0;
141  return mu;
142  }
143 
144 private:
145  pt::ptree pt;
146 };
147 
148 
std::shared_ptr< SamplingState > Interpolate(std::shared_ptr< SamplingState > const &coarseProposal, std::shared_ptr< SamplingState > const &fineProposal)
virtual Eigen::VectorXd StartingPoint(std::shared_ptr< MultiIndex > const &index) override
virtual std::shared_ptr< MCMCProposal > CoarseProposal(std::shared_ptr< MultiIndex > const &fineIndex, std::shared_ptr< MultiIndex > const &coarseIndex, std::shared_ptr< AbstractSamplingProblem > const &coarseProblem, std::shared_ptr< SingleChainMCMC > const &coarseChain) override
void SetComm(std::shared_ptr< parcer::Communicator > const &comm) override
virtual std::shared_ptr< AbstractSamplingProblem > SamplingProblem(std::shared_ptr< MultiIndex > const &index) override
virtual std::shared_ptr< MultiIndex > FinestIndex() override
virtual bool IsInverseProblem() override
virtual std::shared_ptr< MCMCProposal > Proposal(std::shared_ptr< MultiIndex > const &index, std::shared_ptr< AbstractSamplingProblem > const &samplingProblem) override
MyMIComponentFactory(pt::ptree pt)
virtual std::shared_ptr< MIInterpolation > Interpolation(std::shared_ptr< MultiIndex > const &index) override