aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
groundedInference_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 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 = this->evidence(chain.first)[chain.second->id()];
59 
60  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
61  bn_obs->set(i, prm_obs->get(j));
62  }
63 
65  }
66 
67  template < typename GUM_SCALAR >
69  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
71  var_name << chain.first->name() << "." << chain.second->safeName();
73 
74  for (auto iter = _obs_.beginSafe(); iter != _obs_.endSafe();
75  ++iter) { // safe iterator needed here
76  if ((**iter).contains(var)) {
78  const Potential< GUM_SCALAR >* e = *iter;
79  _obs_.erase(iter);
80  delete e;
81  break;
82  }
83  }
84  }
85 
86  template < typename GUM_SCALAR >
87  INLINE
89  const PRMSystem< GUM_SCALAR >& system) :
91  _inf_(0) {
93  }
94 
95  template < typename GUM_SCALAR >
99  _inf_(0) {
101  GUM_ERROR(FatalError, "illegal to copy constructor")
102  }
103 
104  template < typename GUM_SCALAR >
107  GUM_ERROR(FatalError, "illegal call to copy operator")
108  }
109 
110  template < typename GUM_SCALAR >
113  if (_inf_ != 0) {
114  return *_inf_;
115  } else {
116  GUM_ERROR(NotFound, "the inference engine is not yet defined")
117  }
118  }
119 
120  template < typename GUM_SCALAR >
123  if (_inf_ != 0) { delete _inf_; }
124 
125  _inf_ = bn_inf;
126  }
127 
128  template < typename GUM_SCALAR >
130  const typename PRMInference< GUM_SCALAR >::Chain& chain,
131  Potential< GUM_SCALAR >& m) {
132  if (_inf_ == 0) { GUM_ERROR(OperationNotAllowed, "no inference engine defined") }
133 
135 
136  if (!_obs_.empty()) {
137  for (auto e: _obs_) {
138  try {
139  _inf_->addEvidence(*e);
140  } catch (InvalidArgument&) { _inf_->chgEvidence(*e); }
141  }
142  }
143 
144  sBuff << chain.first->name() << "." << chain.second->safeName();
146  }
147 
148  template < typename GUM_SCALAR >
150  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
151  Potential< GUM_SCALAR >& j) {
152  GUM_ERROR(FatalError, "not yet implemented")
153  }
154 
155  template < typename GUM_SCALAR >
157  return "grounded inference";
158  }
159 
160  } /* namespace prm */
161 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)