aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
groundedInference_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 GroundedInference.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  *
28  */
29 #include <agrum/PRM/inference/groundedInference.h>
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
37 
38  if (inf__ != nullptr) delete inf__;
39 
40  if (!obs__.empty())
41  for (const auto pot: obs__)
42  // We used const ptrs only because of
43  // MarginalTargetedInference::addEvidence()
44  // requires it
45  delete const_cast< Potential< GUM_SCALAR >* >(pot);
46  }
47 
48  template < typename GUM_SCALAR >
50  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
52  // Retrieving the BN's variable
54  var_name << chain.first->name() << "." << chain.second->safeName();
56  // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
57  const Potential< GUM_SCALAR >* prm_obs
58  = this->evidence(chain.first)[chain.second->id()];
60 
61  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
62  bn_obs->set(i, prm_obs->get(j));
63  }
64 
66  }
67 
68  template < typename GUM_SCALAR >
70  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
72  var_name << chain.first->name() << "." << chain.second->safeName();
74 
75  for (auto iter = obs__.beginSafe(); iter != obs__.endSafe();
76  ++iter) { // safe iterator needed here
77  if ((**iter).contains(var)) {
79  const Potential< GUM_SCALAR >* e = *iter;
80  obs__.erase(iter);
81  delete e;
82  break;
83  }
84  }
85  }
86 
87  template < typename GUM_SCALAR >
89  const PRM< GUM_SCALAR >& prm,
90  const PRMSystem< GUM_SCALAR >& system) :
92  inf__(0) {
94  }
95 
96  template < typename GUM_SCALAR >
100  inf__(0) {
102  GUM_ERROR(FatalError, "illegal to copy constructor");
103  }
104 
105  template < typename GUM_SCALAR >
108  const GroundedInference< GUM_SCALAR >& source) {
109  GUM_ERROR(FatalError, "illegal call to copy operator");
110  }
111 
112  template < typename GUM_SCALAR >
115  if (inf__ != 0) {
116  return *inf__;
117  } else {
118  GUM_ERROR(NotFound, "the inference engine is not yet defined");
119  }
120  }
121 
122  template < typename GUM_SCALAR >
125  if (inf__ != 0) { delete inf__; }
126 
127  inf__ = bn_inf;
128  }
129 
130  template < typename GUM_SCALAR >
132  const typename PRMInference< GUM_SCALAR >::Chain& chain,
133  Potential< GUM_SCALAR >& m) {
134  if (inf__ == 0) {
135  GUM_ERROR(OperationNotAllowed, "no inference engine defined");
136  }
137 
139 
140  if (!obs__.empty()) {
141  for (auto e: obs__) {
142  try {
143  inf__->addEvidence(*e);
144  } catch (InvalidArgument&) { inf__->chgEvidence(*e); }
145  }
146  }
147 
148  sBuff << chain.first->name() << "." << chain.second->safeName();
150  }
151 
152  template < typename GUM_SCALAR >
154  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
155  Potential< GUM_SCALAR >& j) {
156  GUM_ERROR(FatalError, "not yet implemented");
157  }
158 
159  template < typename GUM_SCALAR >
161  return "grounded inference";
162  }
163 
164  } /* namespace prm */
165 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)