49#ifndef DOXYGEN_SHOULD_SKIP_THIS
61 template < GUM_Numeric GUM_SCALAR >
65 GUM_CONSTRUCTOR(LoopyBeliefPropagation)
69 this->setFromConfiguration_(
"lbp",
false);
75 template < GUM_Numeric GUM_SCALAR >
76 LoopyBeliefPropagation< GUM_SCALAR >::~LoopyBeliefPropagation() {
77 GUM_DESTRUCTOR(LoopyBeliefPropagation)
80 template < GUM_Numeric GUM_SCALAR >
81 void LoopyBeliefPropagation< GUM_SCALAR >::_init_messages_() {
83 for (
const auto& tail: this->BN().nodes()) {
84 Tensor< GUM_SCALAR > p;
85 p.add(this->BN().variable(tail));
86 p.fill(
static_cast< GUM_SCALAR
>(1));
88 for (
const auto& head: this->BN().children(tail)) {
89 _messages_.insert(
Arc(head, tail), p);
90 _messages_.insert(
Arc(tail, head), p);
95 template < GUM_Numeric GUM_SCALAR >
96 void LoopyBeliefPropagation< GUM_SCALAR >::updateOutdatedStructure_() {
100 template < GUM_Numeric GUM_SCALAR >
101 Tensor< GUM_SCALAR > LoopyBeliefPropagation< GUM_SCALAR >::_computeProdPi_(NodeId X) {
102 const auto& varX = this->BN().variable(X);
104 auto piX = this->BN().cpt(X);
105 for (
const auto& U: this->BN().parents(X)) {
106 piX *= _messages_[
Arc(U, X)];
108 piX = piX.sumIn({&varX});
113 template < GUM_Numeric GUM_SCALAR >
114 Tensor< GUM_SCALAR > LoopyBeliefPropagation< GUM_SCALAR >::_computeProdPi_(NodeId X,
116 const auto& varX = this->BN().variable(X);
117 const auto& varExcept = this->BN().variable(except);
118 auto piXexcept = this->BN().cpt(X);
119 for (
const auto& U: this->BN().parents(X)) {
120 if (U != except) { piXexcept *= _messages_[
Arc(U, X)]; }
122 piXexcept = piXexcept.sumIn({&varX, &varExcept});
126 template < GUM_Numeric GUM_SCALAR >
127 Tensor< GUM_SCALAR > LoopyBeliefPropagation< GUM_SCALAR >::_computeProdLambda_(NodeId X) {
128 Tensor< GUM_SCALAR > lamX;
129 if (this->hasEvidence(X)) {
130 lamX = *(this->evidence()[X]);
132 lamX.add(this->BN().variable(X));
135 for (
const auto& Y: this->BN().children(X)) {
136 lamX *= _messages_[
Arc(Y, X)];
142 template < GUM_Numeric GUM_SCALAR >
143 Tensor< GUM_SCALAR > LoopyBeliefPropagation< GUM_SCALAR >::_computeProdLambda_(NodeId X,
145 Tensor< GUM_SCALAR > lamXexcept;
146 if (this->hasEvidence(X)) {
147 lamXexcept = *this->evidence()[X];
149 lamXexcept.add(this->BN().variable(X));
152 for (
const auto& Y: this->BN().children(X)) {
153 if (Y != except) { lamXexcept *= _messages_[
Arc(Y, X)]; }
159 template < GUM_Numeric GUM_SCALAR >
160 GUM_SCALAR LoopyBeliefPropagation< GUM_SCALAR >::_updateNodeMessage_(NodeId X) {
161 auto piX = _computeProdPi_(X);
162 auto lamX = _computeProdLambda_(X);
168 for (
const auto& U: this->BN().parents(X)) {
169 auto newLambda = (_computeProdPi_(X, U) * lamX).sumIn({&this->BN().variable(U)});
170 newLambda.normalize();
171 auto ekl =
static_cast< GUM_SCALAR
>(0);
173 ekl = _messages_[
Arc(X, U)].KL(newLambda);
177 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
183 _messages_.set(
Arc(X, U), newLambda);
187 for (
const auto& Y: this->BN().children(X)) {
188 auto newPi = (piX * _computeProdLambda_(X, Y));
192 ekl = _messages_[
Arc(X, Y)].KL(newPi);
196 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
202 _messages_.set(
Arc(X, Y), newPi);
208 template < GUM_Numeric GUM_SCALAR >
209 void LoopyBeliefPropagation< GUM_SCALAR >::_initStats_() {
212 _updateNodeMessage_(node);
217 template < GUM_Numeric GUM_SCALAR >
218 void LoopyBeliefPropagation< GUM_SCALAR >::makeInference_() {
220 this->initApproximationScheme();
222 std::vector< NodeId > shuffleIds;
223 for (
const auto& node: this->BN().nodes())
224 shuffleIds.push_back(node);
226 auto engine = std::default_random_engine{};
228 GUM_SCALAR error = 0.0;
231 std::shuffle(std::begin(shuffleIds), std::end(shuffleIds), engine);
232 this->updateApproximationScheme();
233 for (
const auto& node: shuffleIds) {
234 GUM_SCALAR e = _updateNodeMessage_(node);
235 if (e > error) error = e;
237 }
while (this->continueApproximationScheme(error));
241 template < GUM_Numeric GUM_SCALAR >
242 const Tensor< GUM_SCALAR >& LoopyBeliefPropagation< GUM_SCALAR >::posterior_(NodeId
id) {
243 auto p = _computeProdPi_(
id) * _computeProdLambda_(
id);
245 _posteriors_.set(
id, p);
247 return _posteriors_[id];
250 template < GUM_Numeric GUM_SCALAR >
251 void LoopyBeliefPropagation< GUM_SCALAR >::onStateChanged_() {}
253 template < GUM_Numeric GUM_SCALAR >
254 void LoopyBeliefPropagation< GUM_SCALAR >::onEvidenceAdded_(
const NodeId
id,
255 bool isHardEvidence) {}
257 template < GUM_Numeric GUM_SCALAR >
258 void LoopyBeliefPropagation< GUM_SCALAR >::onEvidenceErased_(
const NodeId
id,
259 bool isHardEvidence) {}
261 template < GUM_Numeric GUM_SCALAR >
262 void LoopyBeliefPropagation< GUM_SCALAR >::onAllEvidenceErased_(
bool contains_hard_evidence) {}
264 template < GUM_Numeric GUM_SCALAR >
265 void LoopyBeliefPropagation< GUM_SCALAR >::onEvidenceChanged_(
const NodeId
id,
266 bool hasChangedSoftHard) {}
268 template < GUM_Numeric GUM_SCALAR >
269 void LoopyBeliefPropagation< GUM_SCALAR >::onModelChanged_(
const GraphicalModel* bn) {}
271 template < GUM_Numeric GUM_SCALAR >
272 void LoopyBeliefPropagation< GUM_SCALAR >::updateOutdatedTensors_() {}
274 template < GUM_Numeric GUM_SCALAR >
275 void LoopyBeliefPropagation< GUM_SCALAR >::onMarginalTargetAdded_(
const NodeId
id) {}
277 template < GUM_Numeric GUM_SCALAR >
278 void LoopyBeliefPropagation< GUM_SCALAR >::onMarginalTargetErased_(
const NodeId
id) {}
280 template < GUM_Numeric GUM_SCALAR >
281 void LoopyBeliefPropagation< GUM_SCALAR >::onAllMarginalTargetsAdded_() {}
283 template < GUM_Numeric GUM_SCALAR >
284 void LoopyBeliefPropagation< GUM_SCALAR >::onAllMarginalTargetsErased_() {}
KL is the base class for KL computation betweens 2 BNs.
Exception : fatal (unknown ?) error.
Class representing the minimal interface for Bayesian network with no numerical data.
Exception: at least one argument passed to a function is not what was expected.
LoopyBeliefPropagation(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
#define GUM_ERROR(type, msg)
This file contains gibbs sampling (for BNs) class definitions.
Sequence< NodeId > topologicalOrder(const G &g)
Returns a topological ordering of the nodes of g (Kahn's algorithm).
gum is the global namespace for all aGrUM entities