aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
samplingInference_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Implementation of the non pure virtual methods of class
25  * ApproximateInference.
26  *
27  * @author Paul ALAM & Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #include <agrum/BN/BayesNetFragment.h>
31 #include <agrum/BN/algorithms/barrenNodesFinder.h>
32 #include <agrum/BN/algorithms/dSeparation.h>
33 #include <agrum/BN/inference/tools/samplingInference.h>
34 
35 
36 #define DEFAULT_MAXITER 10000000
37 #define DEFAULT_PERIOD_SIZE 100
38 #define DEFAULT_VERBOSITY false
39 #define DEFAULT_TIMEOUT 6000
40 #define DEFAULT_EPSILON 1e-2
41 #define DEFAULT_MIN_EPSILON_RATE 1e-5
42 
43 
44 namespace gum {
45 
46  template < typename GUM_SCALAR >
47  SamplingInference< GUM_SCALAR >::SamplingInference(
48  const IBayesNet< GUM_SCALAR >* bn) :
49  ApproximateInference< GUM_SCALAR >(bn),
50  estimator__(), samplingBN__(nullptr) {
51  this->setEpsilon(DEFAULT_EPSILON);
52  this->setMinEpsilonRate(DEFAULT_MIN_EPSILON_RATE);
53  this->setMaxIter(DEFAULT_MAXITER);
54  this->setVerbosity(DEFAULT_VERBOSITY);
55  this->setPeriodSize(DEFAULT_PERIOD_SIZE);
56  this->setMaxTime(DEFAULT_TIMEOUT);
57  GUM_CONSTRUCTOR(SamplingInference);
58  }
59 
60 
61  template < typename GUM_SCALAR >
64  if (samplingBN__ != nullptr) {
65  if (isContextualized) { // otherwise samplingBN__==&BN()
66  delete samplingBN__;
67  }
68  }
69  }
70 
71  template < typename GUM_SCALAR >
72  INLINE const IBayesNet< GUM_SCALAR >&
74  this->prepareInference();
75  if (samplingBN__ == nullptr)
76  return this->BN();
77  else
78  return *samplingBN__;
79  }
80  template < typename GUM_SCALAR >
83  this->isSetEstimator = true;
84  }
85 
86  template < typename GUM_SCALAR >
91  this->isSetEstimator = true;
92  }
93 
94 
95  template < typename GUM_SCALAR >
96  const Potential< GUM_SCALAR >&
98  return estimator__.posterior(this->BN().variable(id));
99  }
100 
101  template < typename GUM_SCALAR >
102  const Potential< GUM_SCALAR >&
104  return currentPosterior(this->BN().idFromName(name));
105  }
106 
107  template < typename GUM_SCALAR >
108  const Potential< GUM_SCALAR >&
110  return estimator__.posterior(this->BN().variable(id));
111  }
112 
113  template < typename GUM_SCALAR >
115  // Finding Barren nodes
116 
118  barr_nodes.setTargets(&this->targets());
121 
122  // creating BN fragment
123  samplingBN__ = new BayesNetFragment< GUM_SCALAR >(this->BN());
124  for (const auto elmt: this->BN().dag().asNodeSet() - barren)
126 
127  // D-separated nodes
128 
132  this->BN().dag(),
133  this->BN().nodes().asNodeSet(), // no target for approximateInference
134  this->hardEvidenceNodes(),
135  this->softEvidenceNodes(), // should be empty
136  requisite);
137  requisite += this->hardEvidenceNodes();
138 
139  auto nonRequisite = this->BN().dag().asNodeSet() - requisite;
140 
141  for (const auto elmt: nonRequisite)
143  for (const auto hard: this->hardEvidenceNodes()) {
145  I.add(this->BN().variable(hard));
146  I.chgVal(this->BN().variable(hard), this->hardEvidence()[hard]);
147 
148  for (const auto& child: this->BN().children(hard)) {
150  }
151  }
152 
153  this->isContextualized = true;
155  }
156 
157 
158  template < typename GUM_SCALAR >
160  if (!isSetEstimator) this->setEstimatorFromBN_();
162  }
163 
164  template < typename GUM_SCALAR >
166  //@todo This should be in prepareInference__
167  if (!isContextualized) { this->contextualize(); }
168 
169  this->initApproximationScheme();
171  GUM_SCALAR w = .0; //
172 
173  // Burn in
174  Ip = this->burnIn_();
175  do {
176  Ip = this->draw_(&w, Ip);
180 
181  this->isSetEstimator = false;
182  }
183 
184 
185  template < typename GUM_SCALAR >
187  Instantiation* I) {
189 
190  I->add(samplingBN().variable(nod));
193  }
194 
195  template < typename GUM_SCALAR >
198 
199 
200  template < typename GUM_SCALAR >
202  bool isHardEvidence) {
203  if (!isHardEvidence) {
204  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence");
205  }
206  }
207 
208  template < typename GUM_SCALAR >
210  bool isHardEvidence) {}
211 
212  template < typename GUM_SCALAR >
214  bool contains_hard_evidence) {}
215 
216  template < typename GUM_SCALAR >
217  void
219  bool hasChangedSoftHard) {
220  if (hasChangedSoftHard) {
221  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence");
222  }
223  }
224 
225  template < typename GUM_SCALAR >
227  }
228 
229  template < typename GUM_SCALAR >
231 
232  template < typename GUM_SCALAR >
234 
235  template < typename GUM_SCALAR >
237 
238  template < typename GUM_SCALAR >
240 
241  template < typename GUM_SCALAR >
243 
244  template < typename GUM_SCALAR >
246 
247  template < typename GUM_SCALAR >
249  if (this->isInferenceReady()) {
250  estimator__.clear();
251  this->initApproximationScheme();
252  }
253  }
254 } // namespace gum
#define DEFAULT_MAXITER
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
#define DEFAULT_EPSILON
#define DEFAULT_PERIOD_SIZE
#define DEFAULT_TIMEOUT
#define DEFAULT_MIN_EPSILON_RATE
#define DEFAULT_VERBOSITY