aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMInference_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 Inline implementation of PRMInference.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  *
28  */
29 #include <agrum/PRM/inference/PRMInference.h>
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
35  void PRMInference< GUM_SCALAR >::clearEvidence() {
36  for (const auto& elt: evidences__) {
37  for (const auto& elt2: *elt.second)
38  delete elt2.second;
39 
40  delete elt.second;
41  }
42 
44  }
45 
46  template < typename GUM_SCALAR >
48  const PRMInference< GUM_SCALAR >& source) :
49  prm_(source.prm_),
50  sys_(source.sys_) {
52 
53  for (const auto& elt: source.evidences__) {
55 
56  for (const auto& elt2: *elt.second) {
57  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
59  Instantiation i(*e);
60 
61  for (i.setFirst(); !i.end(); i.inc())
62  e->set(i, elt2.second->get(i));
63 
65  }
66  }
67  }
68 
69  template < typename GUM_SCALAR >
71  const PRMInference< GUM_SCALAR >& source) {
72  clearEvidence();
73  prm_ = source.prm_;
74  sys_ = source.sys_;
75 
76  for (const auto& elt: source.evidences__) {
78 
79  for (const auto& elt2: *elt.second) {
80  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
82  Instantiation i(*e);
83 
84  for (i.setFirst(); !i.end(); i.inc()) {
85  e->set(i, elt2.second->get(i));
86  }
87 
89  }
90  }
91 
92  return *this;
93  }
94 
95  template < typename GUM_SCALAR >
96  typename PRMInference< GUM_SCALAR >::EMap&
98  if (evidences__.exists(i)) {
99  return *(evidences__[i]);
100  } else {
102  return *(evidences__[i]);
103  }
104  }
105 
106  template < typename GUM_SCALAR >
107  void
109  const Potential< GUM_SCALAR >& p) {
110  if (chain.first->exists(chain.second->id())) {
111  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
113  "illegal evidence for the given PRMAttribute.");
114 
115  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
116  e->add(chain.second->type().variable());
117  Instantiation i(*e);
118 
119  for (i.setFirst(); !i.end(); i.inc())
120  e->set(i, p.get(i));
121 
123 
124  if (emap.exists(chain.second->id())) {
125  delete emap[chain.second->id()];
126  emap[chain.second->id()] = e;
127  } else {
128  emap.insert(chain.second->id(), e);
129  }
130 
132  } else {
134  "the given PRMAttribute does not belong to this "
135  "Instance<GUM_SCALAR>.");
136  }
137  }
138 
139  template < typename GUM_SCALAR >
141  const PRM< GUM_SCALAR >& prm,
142  const PRMSystem< GUM_SCALAR >& system) :
143  prm_(&prm),
144  sys_(&system) {
146  }
147 
148  template < typename GUM_SCALAR >
151  clearEvidence();
152  }
153 
154  template < typename GUM_SCALAR >
155  INLINE typename PRMInference< GUM_SCALAR >::EMap&
157  try {
158  return *(evidences__[&i]);
159  } catch (NotFound&) {
160  GUM_ERROR(NotFound, "this instance has no evidence.");
161  }
162  }
163 
164  template < typename GUM_SCALAR >
165  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
167  const PRMInstance< GUM_SCALAR >& i) const {
168  try {
169  return *(evidences__[&i]);
170  } catch (NotFound&) {
171  GUM_ERROR(NotFound, "this instance has no evidence.");
172  }
173  }
174 
175  template < typename GUM_SCALAR >
176  INLINE typename PRMInference< GUM_SCALAR >::EMap&
178  try {
179  return *(evidences__[i]);
180  } catch (NotFound&) {
181  GUM_ERROR(NotFound, "this instance has no evidence.");
182  }
183  }
184 
185  template < typename GUM_SCALAR >
186  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
188  const PRMInstance< GUM_SCALAR >* i) const {
189  try {
190  return *(evidences__[i]);
191  } catch (NotFound&) {
192  GUM_ERROR(NotFound, "this instance has no evidence.");
193  }
194  }
195 
196  template < typename GUM_SCALAR >
198  const PRMInstance< GUM_SCALAR >& i) const {
199  return evidences__.exists(&i);
200  }
201 
202  template < typename GUM_SCALAR >
204  const PRMInstance< GUM_SCALAR >* i) const {
205  return evidences__.exists(i);
206  }
207 
208  template < typename GUM_SCALAR >
210  return (hasEvidence(chain.first))
212  : false;
213  }
214 
215  template < typename GUM_SCALAR >
217  return (evidences__.size() != (Size)0);
218  }
219 
220  template < typename GUM_SCALAR >
222  try {
223  if (EMap__(chain.first).exists(chain.second->id())) {
225  delete EMap__(chain.first)[chain.second->id()];
227  }
228  } catch (NotFound&) {
229  // Ok, we are only removing
230  }
231  }
232 
233  template < typename GUM_SCALAR >
235  const typename PRMInference< GUM_SCALAR >::Chain& chain,
236  Potential< GUM_SCALAR >& m) {
237  if (m.nbrDim() > 0) {
238  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
239  }
240 
241  if (hasEvidence(chain)) {
242  m.add(chain.second->type().variable());
243  const Potential< GUM_SCALAR >& e
244  = *(evidence(chain.first)[chain.second->id()]);
245  Instantiation i(m), j(e);
246 
247  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
248  m.set(i, e.get(j));
249  } else {
250  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
253  &(chain.first->get(chain.second->safeName())));
256  } else {
257  m.add(chain.second->type().variable());
258  posterior_(chain, m);
259  }
260  }
261  }
262 
263  template < typename GUM_SCALAR >
265  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& chains,
266  Potential< GUM_SCALAR >& j) {
267  if (j.nbrDim() > 0) {
268  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
269  }
270 
271  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
272  j.add(chain->second->type().variable());
273  }
274 
275  joint_(chains, j);
276  }
277 
278  } /* namespace prm */
279 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)