aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
loopyBeliefPropagation.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
47#ifndef GUM_LOOPYBELIEFPROPAGATION_H
48#define GUM_LOOPYBELIEFPROPAGATION_H
49
51
52namespace gum {
60 template < typename GUM_SCALAR >
61 class LoopyBeliefPropagation: public ApproximateInference< GUM_SCALAR > {
62 public:
67
72
73 protected:
74 virtual void onStateChanged_() {};
75
76 virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence) {};
77
78 virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence) {};
79
80 virtual void onAllEvidenceErased_(bool contains_hard_evidence) {};
81
82 virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard) {};
83
84 virtual void onModelChanged_(const GraphicalModel* bn) {};
85
87
88 virtual void updateOutdatedTensors_() {};
89
90 virtual void onMarginalTargetAdded_(const NodeId id) {};
91
92 virtual void onMarginalTargetErased_(const NodeId id) {};
93
94 virtual void onAllMarginalTargetsAdded_() {};
95
96 virtual void onAllMarginalTargetsErased_() {};
97
99
100 virtual const Tensor< GUM_SCALAR >& posterior_(NodeId id);
101
102 virtual void makeInference_();
103
104 // will be used in both directions :
105 // for x->y, (x,y) and (y,x) will be in _messages_
108
110
112 Tensor< GUM_SCALAR > _computeProdPi_(NodeId X);
113 Tensor< GUM_SCALAR > _computeProdPi_(NodeId X, NodeId except);
114 Tensor< GUM_SCALAR > _computeProdLambda_(NodeId X);
115 Tensor< GUM_SCALAR > _computeProdLambda_(NodeId X, NodeId except);
116
117 // return the max differential BNdistance for this node
119 };
120
121#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
122# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
123 // extern template class LoopyBeliefPropagation<float>;
124# endif
125#endif
126#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
127# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
128 // extern template class LoopyBeliefPropagation<double>;
129# endif
130#endif
131} /* namespace gum */
132
134
135
136#endif // GUM_LOOPYBELIEFPROPAGATION_H
This file contains general methods for approximate inference.
ApproximateInference(const IBayesNet< GUM_SCALAR > *bn)
Virtual base class for probabilistic graphical models.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
ArcProperty< Tensor< GUM_SCALAR > > _messages_
virtual void onModelChanged_(const GraphicalModel *bn)
fired after a new Bayes net has been assigned to the engine
virtual void onAllEvidenceErased_(bool contains_hard_evidence)
fired before all the evidence are erased
virtual void onMarginalTargetAdded_(const NodeId id)
fired after a new marginal target is inserted
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)
fired before an evidence is removed
virtual void onMarginalTargetErased_(const NodeId id)
fired before a marginal target is removed
GUM_SCALAR _updateNodeMessage_(NodeId X)
virtual void onStateChanged_()
fired when the stage is changed
Tensor< GUM_SCALAR > _computeProdLambda_(NodeId X)
virtual void updateOutdatedTensors_()
prepares inference when the latter is in OutdatedTensors state
virtual ~LoopyBeliefPropagation()
Destructor.
virtual void onAllMarginalTargetsAdded_()
fired after all the nodes of the BN are added as marginal targets
Tensor< GUM_SCALAR > _computeProdLambda_(NodeId X, NodeId except)
virtual void onAllMarginalTargetsErased_()
fired before a all marginal targets are removed
NodeProperty< Tensor< GUM_SCALAR > > _posteriors_
virtual const Tensor< GUM_SCALAR > & posterior_(NodeId id)
asks derived classes for the posterior of a given variable
Tensor< GUM_SCALAR > _computeProdPi_(NodeId X, NodeId except)
virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard)
fired after an evidence is changed, in particular when its status (soft/hard) changes
Tensor< GUM_SCALAR > _computeProdPi_(NodeId X)
LoopyBeliefPropagation(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
virtual void updateOutdatedStructure_()
prepares inference when the latter is in OutdatedStructure state
virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence)
fired after a new evidence is inserted
virtual void makeInference_()
called when the inference has to be performed effectively
Size NodeId
Type for node ids.
HashTable< Arc, VAL > ArcProperty
Property on graph elements.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Implementation of Loopy Belief Propagation in Bayesian networks.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46