aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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  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,
134  if (__inf == 0) {
135  GUM_ERROR(OperationNotAllowed, "no inference engine defined");
136  }
137 
138  std::stringstream sBuff;
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();
149  m = __inf->posterior(__inf->BN().idFromName(sBuff.str()));
150  }
151 
152  template < typename GUM_SCALAR >
154  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
156  GUM_ERROR(FatalError, "not yet implemented");
157  }
158 
159  template < typename GUM_SCALAR >
160  INLINE std::string GroundedInference< GUM_SCALAR >::name() const {
161  return "grounded inference";
162  }
163 
164  } /* namespace prm */
165 } /* 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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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>