aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMInference_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 >
50 
51  for (const auto& elt: source._evidences_) {
53 
54  for (const auto& elt2: *elt.second) {
55  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
57  Instantiation i(*e);
58 
59  for (i.setFirst(); !i.end(); i.inc())
60  e->set(i, elt2.second->get(i));
61 
63  }
64  }
65  }
66 
67  template < typename GUM_SCALAR >
70  clearEvidence();
71  prm_ = source.prm_;
72  sys_ = source.sys_;
73 
74  for (const auto& elt: source._evidences_) {
76 
77  for (const auto& elt2: *elt.second) {
78  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
80  Instantiation i(*e);
81 
82  for (i.setFirst(); !i.end(); i.inc()) {
83  e->set(i, elt2.second->get(i));
84  }
85 
87  }
88  }
89 
90  return *this;
91  }
92 
93  template < typename GUM_SCALAR >
94  typename PRMInference< GUM_SCALAR >::EMap&
96  if (_evidences_.exists(i)) {
97  return *(_evidences_[i]);
98  } else {
100  return *(_evidences_[i]);
101  }
102  }
103 
104  template < typename GUM_SCALAR >
106  const Potential< GUM_SCALAR >& p) {
107  if (chain.first->exists(chain.second->id())) {
108  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
109  GUM_ERROR(OperationNotAllowed, "illegal evidence for the given PRMAttribute.")
110 
111  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
112  e->add(chain.second->type().variable());
113  Instantiation i(*e);
114 
115  for (i.setFirst(); !i.end(); i.inc())
116  e->set(i, p.get(i));
117 
119 
120  if (emap.exists(chain.second->id())) {
121  delete emap[chain.second->id()];
122  emap[chain.second->id()] = e;
123  } else {
124  emap.insert(chain.second->id(), e);
125  }
126 
128  } else {
130  "the given PRMAttribute does not belong to this "
131  "Instance<GUM_SCALAR>.");
132  }
133  }
134 
135  template < typename GUM_SCALAR >
137  const PRMSystem< GUM_SCALAR >& system) :
138  prm_(&prm),
139  sys_(&system) {
141  }
142 
143  template < typename GUM_SCALAR >
146  clearEvidence();
147  }
148 
149  template < typename GUM_SCALAR >
150  INLINE typename PRMInference< GUM_SCALAR >::EMap&
152  try {
153  return *(_evidences_[&i]);
154  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
155  }
156 
157  template < typename GUM_SCALAR >
158  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
160  try {
161  return *(_evidences_[&i]);
162  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
163  }
164 
165  template < typename GUM_SCALAR >
166  INLINE typename PRMInference< GUM_SCALAR >::EMap&
168  try {
169  return *(_evidences_[i]);
170  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
171  }
172 
173  template < typename GUM_SCALAR >
174  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
176  try {
177  return *(_evidences_[i]);
178  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
179  }
180 
181  template < typename GUM_SCALAR >
183  return _evidences_.exists(&i);
184  }
185 
186  template < typename GUM_SCALAR >
188  return _evidences_.exists(i);
189  }
190 
191  template < typename GUM_SCALAR >
193  return (hasEvidence(chain.first)) ? evidence(chain.first).exists(chain.second->id()) : false;
194  }
195 
196  template < typename GUM_SCALAR >
198  return (_evidences_.size() != (Size)0);
199  }
200 
201  template < typename GUM_SCALAR >
203  try {
204  if (_EMap_(chain.first).exists(chain.second->id())) {
206  delete _EMap_(chain.first)[chain.second->id()];
208  }
209  } catch (NotFound&) {
210  // Ok, we are only removing
211  }
212  }
213 
214  template < typename GUM_SCALAR >
216  const typename PRMInference< GUM_SCALAR >::Chain& chain,
217  Potential< GUM_SCALAR >& m) {
218  if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
219 
220  if (hasEvidence(chain)) {
221  m.add(chain.second->type().variable());
222  const Potential< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
223  Instantiation i(m), j(e);
224 
225  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
226  m.set(i, e.get(j));
227  } else {
228  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
233  } else {
234  m.add(chain.second->type().variable());
235  posterior_(chain, m);
236  }
237  }
238  }
239 
240  template < typename GUM_SCALAR >
242  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& chains,
243  Potential< GUM_SCALAR >& j) {
244  if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
245 
246  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
247  j.add(chain->second->type().variable());
248  }
249 
250  joint_(chains, j);
251  }
252 
253  } /* namespace prm */
254 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)