aGrUM  0.14.2
BayesNetInference.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
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 #ifndef GUM_BAYES_NET_INFERENCE_H
29 #define GUM_BAYES_NET_INFERENCE_H
30 
31 
32 #include <agrum/BN/IBayesNet.h>
33 #include <agrum/agrum.h>
34 
35 
36 namespace gum {
37 
38 
50  enum class FindBarrenNodesType {
51  FIND_NO_BARREN_NODES, // do not try to find barren nodes
52  FIND_BARREN_NODES // use a bottom-up algorithm to detect barren nodes
53  };
54 
55 
56  // JointTargetedInference, the class for computing joint posteriors, should
57  // have access to the states of Inference and change them when needed: this
58  // will be a friend of Inference
59  template < typename GUM_SCALAR >
61 
62  // MarginalTargetedInference, the class for computing marginal posteriors,
63  // should have access to the states of Inference and change them when needed:
64  // this should be a friend of Inference
65  template < typename GUM_SCALAR >
67 
68  // EvidenceInference, the class for computing the probability of evidence,
69  // should have access to the states of Inference and change them when needed:
70  // this will be a friend of Inference
71  template < typename GUM_SCALAR >
73 
74 
138  template < typename GUM_SCALAR >
140  public:
180  enum class StateOfInference {
181  OutdatedBNStructure,
182  OutdatedBNPotentials,
183  ReadyForInference,
184  Done
185  };
186 
187 
188  // ############################################################################
190  // ############################################################################
192 
194 
196  explicit BayesNetInference(const IBayesNet< GUM_SCALAR >* bn);
197 
199 
208 
210  virtual ~BayesNetInference();
211 
213 
214 
215  // ############################################################################
217  // ############################################################################
219 
221 
226  virtual void setBN(const IBayesNet< GUM_SCALAR >* bn);
227 
229 
231  virtual const IBayesNet< GUM_SCALAR >& BN() const final;
232 
234  virtual const NodeProperty< Size >& domainSizes() const final;
235 
237  virtual bool isInferenceReady() const noexcept final;
239  virtual bool isInferenceOutdatedBNStructure() const noexcept final;
241  virtual bool isInferenceOutdatedBNPotentials() const noexcept final;
243  virtual bool isInferenceDone() const noexcept final;
244 
246 
251  virtual bool isDone() const noexcept final;
252 
254  virtual void prepareInference() final;
255 
257 
262  virtual void makeInference() final;
263 
265  virtual void clear();
266 
268  virtual StateOfInference state() const noexcept final;
270 
271 
272  // ############################################################################
274  // ############################################################################
276 
278 
283  virtual void addEvidence(NodeId id, const Idx val) final;
284 
286 
291  virtual void addEvidence(const std::string& nodeName, const Idx val) final;
292 
294 
299  virtual void addEvidence(NodeId id, const std::string& label) final;
300 
302 
307  virtual void addEvidence(const std::string& nodeName,
308  const std::string& label) final;
309 
311 
318  virtual void addEvidence(NodeId id,
319  const std::vector< GUM_SCALAR >& vals) final;
320 
322 
329  virtual void addEvidence(const std::string& nodeName,
330  const std::vector< GUM_SCALAR >& vals) final;
331 
333 
341  virtual void addEvidence(const Potential< GUM_SCALAR >& pot) final;
342 
344 
352  virtual void addEvidence(Potential< GUM_SCALAR >&& pot) final;
353 
355 
363  virtual void
364  addSetOfEvidence(const Set< const Potential< GUM_SCALAR >* >& potset) final;
365 
367 
375  virtual void addListOfEvidence(
376  const List< const Potential< GUM_SCALAR >* >& potlist) final;
377 
379 
384  virtual void chgEvidence(NodeId id, const Idx val) final;
385 
387 
392  virtual void chgEvidence(const std::string& nodeName, const Idx val) final;
393 
395 
400  virtual void chgEvidence(NodeId id, const std::string& label) final;
401 
403 
408  virtual void chgEvidence(const std::string& nodeName,
409  const std::string& label) final;
410 
412 
419  virtual void chgEvidence(NodeId id,
420  const std::vector< GUM_SCALAR >& vals) final;
421 
423 
430  virtual void chgEvidence(const std::string& nodeName,
431  const std::vector< GUM_SCALAR >& vals) final;
432 
434 
442  virtual void chgEvidence(const Potential< GUM_SCALAR >& pot) final;
443 
445  virtual void eraseAllEvidence() final;
446 
448  virtual void eraseEvidence(NodeId id) final;
449 
451  virtual void eraseEvidence(const std::string& nodeName) final;
452 
454  virtual bool hasEvidence() const final;
455 
457  virtual bool hasEvidence(NodeId id) const final;
458 
460  virtual bool hasHardEvidence(NodeId id) const final;
461 
463  virtual bool hasSoftEvidence(NodeId id) const final;
465 
466  virtual bool hasEvidence(const std::string& nodeName) const final;
467 
469  virtual bool hasHardEvidence(const std::string& nodeName) const final;
470 
472  virtual bool hasSoftEvidence(const std::string& nodeName) const final;
473 
475  virtual Size nbrEvidence() const final;
476 
478  virtual Size nbrHardEvidence() const final;
479 
481  virtual Size nbrSoftEvidence() const final;
482 
484  const NodeProperty< const Potential< GUM_SCALAR >* >& evidence() const;
485 
487  const NodeSet& softEvidenceNodes() const;
488 
490  const NodeSet& hardEvidenceNodes() const;
491 
493  const NodeProperty< Idx >& hardEvidence() const;
494 
496 
497 
498  protected:
500  virtual void _onStateChanged() = 0;
501 
503  virtual void _onEvidenceAdded(const NodeId id, bool isHardEvidence) = 0;
504 
506  virtual void _onEvidenceErased(const NodeId id, bool isHardEvidence) = 0;
507 
509  virtual void _onAllEvidenceErased(bool contains_hard_evidence) = 0;
510 
519  virtual void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) = 0;
520 
522  virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR >* bn) = 0;
523 
525 
528  virtual void _updateOutdatedBNStructure() = 0;
529 
531 
534  virtual void _updateOutdatedBNPotentials() = 0;
535 
537 
538  virtual void _makeInference() = 0;
539 
541 
561  void _setOutdatedBNStructureState();
562 
572  void _setOutdatedBNPotentialsState();
573 
574 
575  private:
577  StateOfInference __state{StateOfInference::OutdatedBNStructure};
578 
580  const IBayesNet< GUM_SCALAR >* __bn{nullptr};
581 
584 
587 
590 
593 
596 
600  virtual void __setState(const StateOfInference state) final;
601 
603  Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const;
604 
606  bool __isHardEvidence(const Potential< GUM_SCALAR >& pot, Idx& val) const;
607 
609  void __computeDomainSizes();
610 
612  void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR >* bn);
613 
614 
619  };
620 
621 
622 } // namespace gum
623 
624 
626 
627 
628 #endif // GUM_BAYES_NET_INFERENCE_H
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
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
Implementation of the non pure virtual methods of class BayesNetInference.
Class representing Bayesian networks.
<agrum/BN/inference/marginalTargetedInference.h>
Generic doubly linked lists.
Definition: list.h:369
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
<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:50
<agrum/BN/inference/evidenceInference.h>
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
StateOfInference
current state of the inference
Size NodeId
Type for node ids.
Definition: graphElements.h:97