aGrUM  0.16.0
BayesNetInference.h
Go to the documentation of this file.
1 
31 #ifndef GUM_BAYES_NET_INFERENCE_H
32 #define GUM_BAYES_NET_INFERENCE_H
33 
34 
35 #include <agrum/BN/IBayesNet.h>
36 #include <agrum/agrum.h>
37 
38 
39 namespace gum {
40 
41 
53  enum class FindBarrenNodesType {
54  FIND_NO_BARREN_NODES, // do not try to find barren nodes
55  FIND_BARREN_NODES // use a bottom-up algorithm to detect barren nodes
56  };
57 
58 
59  // JointTargetedInference, the class for computing joint posteriors, should
60  // have access to the states of Inference and change them when needed: this
61  // will be a friend of Inference
62  template < typename GUM_SCALAR >
64 
65  // MarginalTargetedInference, the class for computing marginal posteriors,
66  // should have access to the states of Inference and change them when needed:
67  // this should be a friend of Inference
68  template < typename GUM_SCALAR >
70 
71  // EvidenceInference, the class for computing the probability of evidence,
72  // should have access to the states of Inference and change them when needed:
73  // this will be a friend of Inference
74  template < typename GUM_SCALAR >
76 
77 
141  template < typename GUM_SCALAR >
143  public:
183  enum class StateOfInference {
184  OutdatedBNStructure,
185  OutdatedBNPotentials,
186  ReadyForInference,
187  Done
188  };
189 
190 
191  // ############################################################################
193  // ############################################################################
195 
197 
199  explicit BayesNetInference(const IBayesNet< GUM_SCALAR >* bn);
200 
202 
211 
213  virtual ~BayesNetInference();
214 
216 
217 
218  // ############################################################################
220  // ############################################################################
222 
224 
229  virtual void setBN(const IBayesNet< GUM_SCALAR >* bn);
230 
232 
234  virtual const IBayesNet< GUM_SCALAR >& BN() const final;
235 
237  virtual const NodeProperty< Size >& domainSizes() const final;
238 
240  virtual bool isInferenceReady() const noexcept final;
242  virtual bool isInferenceOutdatedBNStructure() const noexcept final;
244  virtual bool isInferenceOutdatedBNPotentials() const noexcept final;
246  virtual bool isInferenceDone() const noexcept final;
247 
249 
254  virtual bool isDone() const noexcept final;
255 
257  virtual void prepareInference() final;
258 
260 
265  virtual void makeInference() final;
266 
268  virtual void clear();
269 
271  virtual StateOfInference state() const noexcept final;
273 
274 
275  // ############################################################################
277  // ############################################################################
279 
281 
286  virtual void addEvidence(NodeId id, const Idx val) final;
287 
289 
294  virtual void addEvidence(const std::string& nodeName, const Idx val) final;
295 
297 
302  virtual void addEvidence(NodeId id, const std::string& label) final;
303 
305 
310  virtual void addEvidence(const std::string& nodeName,
311  const std::string& label) final;
312 
314 
321  virtual void addEvidence(NodeId id,
322  const std::vector< GUM_SCALAR >& vals) final;
323 
325 
332  virtual void addEvidence(const std::string& nodeName,
333  const std::vector< GUM_SCALAR >& vals) final;
334 
336 
344  virtual void addEvidence(const Potential< GUM_SCALAR >& pot) final;
345 
347 
355  virtual void addEvidence(Potential< GUM_SCALAR >&& pot) final;
356 
358 
366  virtual void
367  addSetOfEvidence(const Set< const Potential< GUM_SCALAR >* >& potset) final;
368 
370 
378  virtual void addListOfEvidence(
379  const List< const Potential< GUM_SCALAR >* >& potlist) final;
380 
382 
387  virtual void chgEvidence(NodeId id, const Idx val) final;
388 
390 
395  virtual void chgEvidence(const std::string& nodeName, const Idx val) final;
396 
398 
403  virtual void chgEvidence(NodeId id, const std::string& label) final;
404 
406 
411  virtual void chgEvidence(const std::string& nodeName,
412  const std::string& label) final;
413 
415 
422  virtual void chgEvidence(NodeId id,
423  const std::vector< GUM_SCALAR >& vals) final;
424 
426 
433  virtual void chgEvidence(const std::string& nodeName,
434  const std::vector< GUM_SCALAR >& vals) final;
435 
437 
445  virtual void chgEvidence(const Potential< GUM_SCALAR >& pot) final;
446 
448  virtual void eraseAllEvidence() final;
449 
451  virtual void eraseEvidence(NodeId id) final;
452 
454  virtual void eraseEvidence(const std::string& nodeName) final;
455 
457  virtual bool hasEvidence() const final;
458 
460  virtual bool hasEvidence(NodeId id) const final;
461 
463  virtual bool hasHardEvidence(NodeId id) const final;
464 
466  virtual bool hasSoftEvidence(NodeId id) const final;
468 
469  virtual bool hasEvidence(const std::string& nodeName) const final;
470 
472  virtual bool hasHardEvidence(const std::string& nodeName) const final;
473 
475  virtual bool hasSoftEvidence(const std::string& nodeName) const final;
476 
478  virtual Size nbrEvidence() const final;
479 
481  virtual Size nbrHardEvidence() const final;
482 
484  virtual Size nbrSoftEvidence() const final;
485 
487  const NodeProperty< const Potential< GUM_SCALAR >* >& evidence() const;
488 
490  const NodeSet& softEvidenceNodes() const;
491 
493  const NodeSet& hardEvidenceNodes() const;
494 
496  const NodeProperty< Idx >& hardEvidence() const;
497 
499 
500 
501  protected:
503  virtual void _onStateChanged() = 0;
504 
506  virtual void _onEvidenceAdded(const NodeId id, bool isHardEvidence) = 0;
507 
509  virtual void _onEvidenceErased(const NodeId id, bool isHardEvidence) = 0;
510 
512  virtual void _onAllEvidenceErased(bool contains_hard_evidence) = 0;
513 
522  virtual void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) = 0;
523 
525  virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR >* bn) = 0;
526 
528 
531  virtual void _updateOutdatedBNStructure() = 0;
532 
534 
537  virtual void _updateOutdatedBNPotentials() = 0;
538 
540 
541  virtual void _makeInference() = 0;
542 
544 
564  void _setOutdatedBNStructureState();
565 
575  void _setOutdatedBNPotentialsState();
576 
577 
578  private:
580  StateOfInference __state{StateOfInference::OutdatedBNStructure};
581 
583  const IBayesNet< GUM_SCALAR >* __bn{nullptr};
584 
587 
590 
593 
596 
599 
603  virtual void __setState(const StateOfInference state) final;
604 
606  Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const;
607 
609  bool __isHardEvidence(const Potential< GUM_SCALAR >& pot, Idx& val) const;
610 
612  void __computeDomainSizes();
613 
615  void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR >* bn);
616 
617 
622  };
623 
624 
625 } // namespace gum
626 
627 
629 
630 
631 #endif // GUM_BAYES_NET_INFERENCE_H
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
NodeProperty< Size > __domain_sizes
the domain sizes of the random variables
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
<agrum/BN/inference/marginalTargetedInference.h>
Generic doubly linked lists.
Definition: list.h:372
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
The class for generic Hash Tables.
Definition: hashTable.h:679
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
<agrum/BN/inference/BayesNetInference.h>
FindBarrenNodesType
type of algorithm to determine barren nodes
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
<agrum/BN/inference/jointTargetedInference.h>
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
Size Idx
Type for indexes.
Definition: types.h:53
<agrum/BN/inference/evidenceInference.h>
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
StateOfInference
current state of the inference
Size NodeId
Type for node ids.
Definition: graphElements.h:98