aGrUM  0.16.0
groundedInference_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
37  GUM_DESTRUCTOR(GroundedInference);
38 
39  if (__inf != nullptr) delete __inf;
40 
41  if (!__obs.empty())
42  for (const auto pot : __obs)
43  // We used const ptrs only because of
44  // MarginalTargetedInference::addEvidence()
45  // requires it
46  delete const_cast< Potential< GUM_SCALAR >* >(pot);
47  }
48 
49  template < typename GUM_SCALAR >
51  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
53  // Retrieving the BN's variable
54  std::stringstream var_name;
55  var_name << chain.first->name() << "." << chain.second->safeName();
56  bn_obs->add(__inf->BN().variableFromName(var_name.str()));
57  // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
58  const Potential< GUM_SCALAR >* prm_obs =
59  this->evidence(chain.first)[chain.second->id()];
60  Instantiation i(*bn_obs), j(*prm_obs);
61 
62  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
63  bn_obs->set(i, prm_obs->get(j));
64  }
65 
66  __obs.insert(bn_obs);
67  }
68 
69  template < typename GUM_SCALAR >
71  const typename PRMInference< GUM_SCALAR >::Chain& chain) {
72  std::stringstream var_name;
73  var_name << chain.first->name() << "." << chain.second->safeName();
74  const DiscreteVariable& var = __inf->BN().variableFromName(var_name.str());
75 
76  for (auto iter = __obs.beginSafe(); iter != __obs.endSafe();
77  ++iter) { // safe iterator needed here
78  if ((**iter).contains(var)) {
79  __inf->eraseEvidence(var_name.str());
80  const Potential< GUM_SCALAR >* e = *iter;
81  __obs.erase(iter);
82  delete e;
83  break;
84  }
85  }
86  }
87 
88  template < typename GUM_SCALAR >
90  const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& system) :
91  PRMInference< GUM_SCALAR >(prm, system),
92  __inf(0) {
93  GUM_CONSTRUCTOR(GroundedInference);
94  }
95 
96  template < typename GUM_SCALAR >
98  const GroundedInference< GUM_SCALAR >& source) :
99  PRMInference< GUM_SCALAR >(source),
100  __inf(0) {
101  GUM_CONS_CPY(GroundedInference);
102  GUM_ERROR(FatalError, "illegal to copy constructor");
103  }
104 
105  template < typename GUM_SCALAR >
108  GUM_ERROR(FatalError, "illegal call to copy operator");
109  }
110 
111  template < typename GUM_SCALAR >
114  if (__inf != 0) {
115  return *__inf;
116  } else {
117  GUM_ERROR(NotFound, "the inference engine is not yet defined");
118  }
119  }
120 
121  template < typename GUM_SCALAR >
124  if (__inf != 0) { delete __inf; }
125 
126  __inf = bn_inf;
127  }
128 
129  template < typename GUM_SCALAR >
131  const typename PRMInference< GUM_SCALAR >::Chain& chain,
133  if (__inf == 0) {
134  GUM_ERROR(OperationNotAllowed, "no inference engine defined");
135  }
136 
137  std::stringstream sBuff;
138 
139  if (!__obs.empty()) {
140  for (auto e : __obs) {
141  try {
142  __inf->addEvidence(*e);
143  } catch (InvalidArgument&) { __inf->chgEvidence(*e); }
144  }
145  }
146 
147  sBuff << chain.first->name() << "." << chain.second->safeName();
148  m = __inf->posterior(__inf->BN().idFromName(sBuff.str()));
149  }
150 
151  template < typename GUM_SCALAR >
153  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
155  GUM_ERROR(FatalError, "not yet implemented");
156  }
157 
158  template < typename GUM_SCALAR >
159  INLINE std::string GroundedInference< GUM_SCALAR >::name() const {
160  return "grounded inference";
161  }
162 
163  } /* namespace prm */
164 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
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().
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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>
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:229
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:57
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
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:52
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
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:55
<agrum/PRM/groundedInference.h>