aGrUM  0.14.2
groundedInference_tpl.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 
29 namespace gum {
30  namespace prm {
31 
32  template < typename GUM_SCALAR >
34  GUM_DESTRUCTOR(GroundedInference);
35 
36  if (__inf != nullptr) delete __inf;
37 
38  if (!__obs.empty())
39  for (const auto pot : __obs)
40  // We used const ptrs only because of
41  // MarginalTargetedInference::addEvidence()
42  // requires it
43  delete const_cast< Potential< GUM_SCALAR >* >(pot);
44  }
45 
46  template < typename GUM_SCALAR >
48  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
50  // Retrieving the BN's variable
51  std::stringstream var_name;
52  var_name << chain.first->name() << "." << chain.second->safeName();
53  bn_obs->add(__inf->BN().variableFromName(var_name.str()));
54  // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
55  const Potential< GUM_SCALAR >* prm_obs =
56  this->evidence(chain.first)[chain.second->id()];
57  Instantiation i(*bn_obs), j(*prm_obs);
58 
59  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
60  bn_obs->set(i, prm_obs->get(j));
61  }
62 
63  __obs.insert(bn_obs);
64  }
65 
66  template < typename GUM_SCALAR >
68  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
69  std::stringstream var_name;
70  var_name << chain.first->name() << "." << chain.second->safeName();
71  const DiscreteVariable& var = __inf->BN().variableFromName(var_name.str());
72 
73  for (auto iter = __obs.beginSafe(); iter != __obs.endSafe();
74  ++iter) { // safe iterator needed here
75  if ((**iter).contains(var)) {
76  __inf->eraseEvidence(var_name.str());
77  const Potential< GUM_SCALAR >* e = *iter;
78  __obs.erase(iter);
79  delete e;
80  break;
81  }
82  }
83  }
84 
85  template < typename GUM_SCALAR >
87  const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& system) :
88  PRMInference< GUM_SCALAR >(prm, system),
89  __inf(0) {
90  GUM_CONSTRUCTOR(GroundedInference);
91  }
92 
93  template < typename GUM_SCALAR >
95  const GroundedInference< GUM_SCALAR >& source) :
96  PRMInference< GUM_SCALAR >(source),
97  __inf(0) {
98  GUM_CONS_CPY(GroundedInference);
99  GUM_ERROR(FatalError, "illegal to copy constructor");
100  }
101 
102  template < typename GUM_SCALAR >
105  GUM_ERROR(FatalError, "illegal call to copy operator");
106  }
107 
108  template < typename GUM_SCALAR >
111  if (__inf != 0) {
112  return *__inf;
113  } else {
114  GUM_ERROR(NotFound, "the inference engine is not yet defined");
115  }
116  }
117 
118  template < typename GUM_SCALAR >
121  if (__inf != 0) { delete __inf; }
122 
123  __inf = bn_inf;
124  }
125 
126  template < typename GUM_SCALAR >
128  const typename PRMInference< GUM_SCALAR >::Chain& chain,
130  if (__inf == 0) {
131  GUM_ERROR(OperationNotAllowed, "no inference engine defined");
132  }
133 
134  std::stringstream sBuff;
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();
145  m = __inf->posterior(__inf->BN().idFromName(sBuff.str()));
146  }
147 
148  template < typename GUM_SCALAR >
150  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
152  GUM_ERROR(FatalError, "not yet implemented");
153  }
154 
155  template < typename GUM_SCALAR >
156  INLINE std::string GroundedInference< GUM_SCALAR >::name() const {
157  return "grounded inference";
158  }
159 
160  } /* namespace prm */
161 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
MarginalTargetedInference< GUM_SCALAR > * __inf
The bayesnet inference engine used by this class.
virtual void _evidenceRemoved(const typename PRMInference< GUM_SCALAR >::Chain &chain)
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
virtual void _joint(const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Potential< GUM_SCALAR > &j)
Generic method to compute the marginal of given element.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
Headers of GroundedInference.
virtual std::string name() const
Returns the bayesnet inference engine used by this class.
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
virtual void _marginal(const typename PRMInference< GUM_SCALAR >::Chain &chain, Potential< GUM_SCALAR > &m)
Generic method to compute the marginal of given element.
Base class for discrete random variable.
<agrum/BN/inference/marginalTargetedInference.h>
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual ~GroundedInference()
Destructor.
void inc()
Operator increment.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:226
MarginalTargetedInference< GUM_SCALAR > & getBNInference()
Returns the bayesnet inference engine used by this class.
GroundedInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:54
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
GroundedInference & operator=(const GroundedInference &source)
Copy operator.
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
Definition: PRMInference.h:49
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:63
virtual void _evidenceAdded(const typename PRMInference< GUM_SCALAR >::Chain &chain)
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference...
List< const Potential< GUM_SCALAR > *> __obs
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
virtual std::string name() const =0
Returns the name of the current inference algorithm.
void setBNInference(MarginalTargetedInference< GUM_SCALAR > *bn_inf)
Defines the bayesnet inference engine used by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
<agrum/PRM/groundedInference.h>