aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
loopyBeliefPropagation.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief This file contains gibbs sampling (for BNs) class definitions.
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_LOOPYBELIEFPROPAGATION_H
28 #define GUM_LOOPYBELIEFPROPAGATION_H
29 
30 #include <agrum/BN/inference/tools/approximateInference.h>
31 
32 namespace gum {
33  /**
34  * @class LoopyBeliefPropagation loopyBeliefPropagation.h
35  *<agrum/BN/inference/loopyBeliefPropagation.h>
36  * @brief class for making Loopy Belief Propagation in Bayesian networks.
37  * @ingroup bn_inference
38  *
39  */
40  template < typename GUM_SCALAR >
42  public:
43  /**
44  * Default constructor
45  */
46  explicit LoopyBeliefPropagation(const IBayesNet< GUM_SCALAR >* bn);
47 
48  /**
49  * Destructor.
50  */
51  virtual ~LoopyBeliefPropagation();
52 
53  protected:
54  virtual void onStateChanged_(){};
55 
56  virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence){};
57 
58  virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence){};
59 
60  virtual void onAllEvidenceErased_(bool contains_hard_evidence){};
61 
62  virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard){};
63 
64  virtual void onModelChanged_(const GraphicalModel* bn){};
65 
66  virtual void updateOutdatedStructure_();
67 
68  virtual void updateOutdatedPotentials_(){};
69 
70  virtual void onMarginalTargetAdded_(const NodeId id){};
71 
72  virtual void onMarginalTargetErased_(const NodeId id){};
73 
74  virtual void onAllMarginalTargetsAdded_(){};
75 
76  virtual void onAllMarginalTargetsErased_(){};
77 
78  /// asks derived classes for the posterior of a given variable
79  /** @param id The variable's id. */
80  virtual const Potential< GUM_SCALAR >& posterior_(NodeId id);
81 
82  virtual void makeInference_();
83 
84  // will be used in both directions :
85  // for x->y, (x,y) and (y,x) will be in _messages_
88 
89  void _initStats_();
90 
91  void _init_messages_();
96 
97  // return the max differential BNdistance for this node
98  GUM_SCALAR _updateNodeMessage_(NodeId X);
99  };
100 
101 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
102 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
103  // extern template class LoopyBeliefPropagation<float>;
104 # endif
105 #endif
106 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
107 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
108  // extern template class LoopyBeliefPropagation<double>;
109 # endif
110 #endif
111 } /* namespace gum */
112 
113 #include <agrum/BN/inference/loopyBeliefPropagation_tpl.h>
114 
115 
116 #endif // GUM_LOOPYBELIEFPROPAGATION_H
virtual void onStateChanged_()
fired when the stage is changed
virtual void makeInference_()
called when the inference has to be performed effectively
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)
fired before an evidence is removed
virtual void onModelChanged_(const GraphicalModel *bn)
fired after a new Bayes net has been assigned to the engine
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Potential< GUM_SCALAR > _computeProdLambda_(NodeId X)
virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard)
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
virtual void updateOutdatedStructure_()
prepares inference when the latter is in OutdatedStructure state
virtual void onAllMarginalTargetsAdded_()
fired after all the nodes of the BN are added as marginal targets
virtual ~LoopyBeliefPropagation()
Destructor.
virtual void onMarginalTargetAdded_(const NodeId id)
fired after a new marginal target is inserted
Potential< GUM_SCALAR > _computeProdLambda_(NodeId X, NodeId except)
virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence)
fired after a new evidence is inserted
virtual void onAllMarginalTargetsErased_()
fired before a all marginal targets are removed
NodeProperty< Potential< GUM_SCALAR > > _posteriors_
LoopyBeliefPropagation(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
virtual void updateOutdatedPotentials_()
prepares inference when the latter is in OutdatedPotentials state
virtual void onAllEvidenceErased_(bool contains_hard_evidence)
fired before all the evidence are erased
virtual void onMarginalTargetErased_(const NodeId id)
fired before a marginal target is removed
Potential< GUM_SCALAR > _computeProdPi_(NodeId X, NodeId except)
Potential< GUM_SCALAR > _computeProdPi_(NodeId X)
virtual const Potential< GUM_SCALAR > & posterior_(NodeId id)
asks derived classes for the posterior of a given variable
GUM_SCALAR _updateNodeMessage_(NodeId X)