aGrUM  0.15.1
marginalTargetedInference_tpl.h
Go to the documentation of this file.
1 
28 #include <iterator>
29 
30 namespace gum {
31 
32 
33  // Default Constructor
34  template < typename GUM_SCALAR >
36  const IBayesNet< GUM_SCALAR >* bn) :
37  BayesNetInference< GUM_SCALAR >(bn) {
38  // assign a BN if this has not been done before (due to virtual inheritance)
39  if (this->__bn == nullptr) {
41  }
42 
43  // sets all the nodes as targets
44  if (bn != nullptr) {
45  __targeted_mode = false;
46  __targets = bn->dag().asNodeSet();
47  }
48 
49  GUM_CONSTRUCTOR(MarginalTargetedInference);
50  }
51 
52 
53  // Destructor
54  template < typename GUM_SCALAR >
56  GUM_DESTRUCTOR(MarginalTargetedInference);
57  }
58 
59 
60  // fired when a new BN is assigned to the inference engine
61  template < typename GUM_SCALAR >
63  const IBayesNet< GUM_SCALAR >* bn) {
64  __targeted_mode = true;
66  }
67 
68 
69  // ##############################################################################
70  // Targets
71  // ##############################################################################
72 
73  // return true if variable is a target
74  template < typename GUM_SCALAR >
75  INLINE bool
77  // check that the variable belongs to the bn
78  if (this->__bn == nullptr)
80  "No Bayes net has been assigned to the "
81  "inference algorithm");
82  if (!this->__bn->dag().exists(node)) {
83  GUM_ERROR(UndefinedElement, node << " is not a NodeId in the bn");
84  }
85 
86  return __targets.contains(node);
87  }
88 
89  // Add a single target to the list of targets
90  template < typename GUM_SCALAR >
92  const std::string& nodeName) const {
93  return isTarget(this->__bn->idFromName(nodeName));
94  }
95 
96 
97  // Clear all previously defined targets (single targets and sets of targets)
98  template < typename GUM_SCALAR >
101 
102  __targets.clear();
103  _setTargetedMode(); // does nothing if already in targeted mode
104 
105  this->__setState(
107  }
108 
109 
110  // Add a single target to the list of targets
111  template < typename GUM_SCALAR >
113  // check if the node belongs to the Bayesian network
114  if (this->__bn == nullptr)
116  "No Bayes net has been assigned to the "
117  "inference algorithm");
118 
119  if (!this->__bn->dag().exists(target)) {
120  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
121  }
122 
123  _setTargetedMode(); // does nothing if already in targeted mode
124  // add the new target
125  if (!__targets.contains(target)) {
126  __targets.insert(target);
127  _onMarginalTargetAdded(target);
128  this->__setState(
130  }
131  }
132 
133 
134  // Add all nodes as targets
135  template < typename GUM_SCALAR >
137  // check if the node belongs to the Bayesian network
138  if (this->__bn == nullptr)
140  "No Bayes net has been assigned to the "
141  "inference algorithm");
142 
143 
144  _setTargetedMode(); // does nothing if already in targeted mode
145  for (const auto target : this->__bn->dag()) {
146  if (!__targets.contains(target)) {
147  __targets.insert(target);
148  _onMarginalTargetAdded(target);
149  this->__setState(
151  }
152  }
153  }
154 
155 
156  // Add a single target to the list of targets
157  template < typename GUM_SCALAR >
159  const std::string& nodeName) {
160  // check if the node belongs to the Bayesian network
161  if (this->__bn == nullptr)
163  "No Bayes net has been assigned to the "
164  "inference algorithm");
165 
166  addTarget(this->__bn->idFromName(nodeName));
167  }
168 
169 
170  // removes an existing target
171  template < typename GUM_SCALAR >
173  // check if the node belongs to the Bayesian network
174  if (this->__bn == nullptr)
176  "No Bayes net has been assigned to the "
177  "inference algorithm");
178 
179  if (!this->__bn->dag().exists(target)) {
180  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
181  }
182 
183 
184  if (__targets.contains(target)) {
185  __targeted_mode = true; // we do not use _setTargetedMode because we do not
186  // want to clear the targets
187  _onMarginalTargetErased(target);
188  __targets.erase(target);
189  this->__setState(
191  }
192  }
193 
194 
195  // Add a single target to the list of targets
196  template < typename GUM_SCALAR >
198  const std::string& nodeName) {
199  // check if the node belongs to the Bayesian network
200  if (this->__bn == nullptr)
202  "No Bayes net has been assigned to the "
203  "inference algorithm");
204 
205  eraseTarget(this->__bn->idFromName(nodeName));
206  }
207 
208 
209  // returns the list of single targets
210  template < typename GUM_SCALAR >
212  noexcept {
213  return __targets;
214  }
215 
216  // returns the list of single targets
217  template < typename GUM_SCALAR >
219  noexcept {
220  return __targets.size();
221  }
222 
223 
225  template < typename GUM_SCALAR >
227  __targets.clear();
228  if (this->__bn != nullptr) {
229  __targets = this->__bn->dag().asNodeSet();
231  }
232  }
233 
234 
235  // ##############################################################################
236  // Inference
237  // ##############################################################################
238 
239  // Compute the posterior of a node.
240  template < typename GUM_SCALAR >
243  if (this->hardEvidenceNodes().contains(node)) {
244  return *(this->evidence()[node]);
245  }
246 
247  if (!isTarget(node)) {
248  // throws UndefinedElement if var is not a target
249  GUM_ERROR(UndefinedElement, node << " is not a target node");
250  }
251 
252  if (!this->isDone()) { this->makeInference(); }
253 
254  return _posterior(node);
255  }
256 
257  // Compute the posterior of a node.
258  template < typename GUM_SCALAR >
261  const std::string& nodeName) {
262  return posterior(this->BN().idFromName(nodeName));
263  }
264 
265  /* Entropy
266  * Compute Shanon's entropy of a node given the observation
267  */
268  template < typename GUM_SCALAR >
270  return posterior(X).entropy();
271  }
272 
273  /* Entropy
274  * Compute Shanon's entropy of a node given the observation
275  */
276  template < typename GUM_SCALAR >
277  INLINE GUM_SCALAR
278  MarginalTargetedInference< GUM_SCALAR >::H(const std::string& nodeName) {
279  return H(this->BN().idFromName(nodeName));
280  }
281 
282 
283  template < typename GUM_SCALAR >
286  const NodeSet& evs) {
287  const auto& vtarget = this->BN().variable(target);
288 
289  if (evs.contains(target)) {
291  "Target <" << vtarget.name() << "> (" << target
292  << ") can not be in evs (" << evs << ").");
293  }
294  auto condset = this->BN().minimalCondSet(target, evs);
295 
297  this->eraseAllTargets();
298  this->eraseAllEvidence();
299  res.add(this->BN().variable(target));
300  this->addTarget(target);
301  for (const auto& n : condset) {
302  res.add(this->BN().variable(n));
303  this->addEvidence(n, 0);
304  }
305 
306  Instantiation inst(res);
307  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
308  // inferring
309  for (const auto& n : condset)
310  this->chgEvidence(n, inst.val(this->BN().variable(n)));
311  this->makeInference();
312  // populate res
313  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
314  res.set(inst, this->posterior(target)[inst]);
315  }
316  inst.setFirstVar(vtarget); // remove inst.end() flag
317  }
318 
319  return res;
320  }
321 
322 
323  template < typename GUM_SCALAR >
325  const std::string& target, const std::vector< std::string >& evs) {
326  const auto& bn = this->BN();
327 
328  gum::NodeSet evsId;
329  for (const auto& evname : evs) {
330  evsId.insert(bn.idFromName(evname));
331  }
332 
333  return evidenceImpact(bn.idFromName(target), evsId);
334  }
335 
336 
337  template < typename GUM_SCALAR >
339  return __targeted_mode;
340  }
341  template < typename GUM_SCALAR >
343  if (!__targeted_mode) {
344  __targets.clear();
345  __targeted_mode = true;
346  }
347  }
348 } /* namespace gum */
void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)
fired after a new Bayes net has been assigned to the engine
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
NodeSet __targets
the set of marginal targets
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
virtual void _onMarginalTargetAdded(const NodeId id)=0
fired after a new marginal target is inserted
virtual const Size nbrTargets() const noexcept final
returns the number of marginal targets
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
<agrum/BN/inference/marginalTargetedInference.h>
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
virtual void _onAllMarginalTargetsErased()=0
fired before a all marginal targets are removed
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
void __setAllMarginalTargets()
sets all the nodes of the Bayes net as targets
<agrum/BN/inference/BayesNetInference.h>
virtual void addAllTargets() final
adds all nodes as targets
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
virtual void _onMarginalTargetErased(const NodeId id)=0
fired before a marginal target is removed
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
Potential< GUM_SCALAR > evidenceImpact(NodeId target, const NodeSet &evs)
Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) ...
virtual void _onAllMarginalTargetsAdded()=0
fired after all the nodes of the BN are added as marginal targets
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 const Potential< GUM_SCALAR > & _posterior(NodeId id)=0
asks derived classes for the posterior of a given variable
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
virtual void eraseAllTargets()
Clear all previously defined targets.
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
bool __targeted_mode
whether the actual targets are default
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:63
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
MarginalTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
bool end() const
Returns true if the Instantiation reached the end.