aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
CNLoopyPropagation.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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
48
49#ifndef GUM_CN_LOOPY_PROPAGATION_H
50#define GUM_CN_LOOPY_PROPAGATION_H
51
52#include <cstdlib>
53#include <ctime>
54#include <limits>
55
57
58#include <string_view>
59
60#define INF_ std::numeric_limits< GUM_SCALAR >::infinity()
61
62namespace gum {
63 namespace credal {
64
74 template < GUM_Numeric GUM_SCALAR >
75 class CNLoopyPropagation: public InferenceEngine< GUM_SCALAR > {
76 public:
77 using msg = std::vector< Tensor< GUM_SCALAR >* >;
78 using cArcP = const class gum::Arc*; // 'class' disambiguates from GDI Arc on Windows
79
100
103
105 void makeInference() override;
106
107 void insertEvidenceFile(std::string_view path) override;
108
110
113
118 void inferenceType(InferenceType inft);
119
124 InferenceType inferenceType();
125
127
130
138 void eraseAllEvidence() override;
139
146 void saveInference(std::string_view path);
147
149
152
158 ~CNLoopyPropagation() override;
160
161 protected:
164
167 void initialize_();
168
170
173
179
181 void updateMarginals_();
182
190 void msgL_(const NodeId X,
191 const NodeId demanding_parent); // allways sent from X to demanding_X
192
213 void compute_ext_(GUM_SCALAR& msg_l_min,
214 GUM_SCALAR& msg_l_max,
215 std::vector< GUM_SCALAR >& lx,
216 GUM_SCALAR& num_min,
217 GUM_SCALAR& num_max,
218 GUM_SCALAR& den_min,
219 GUM_SCALAR& den_max);
220
238 void compute_ext_(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
239 const NodeId& id,
240 GUM_SCALAR& msg_l_min,
241 GUM_SCALAR& msg_l_max,
242 std::vector< GUM_SCALAR >& lx,
243 const Idx& pos);
244
261 void enum_combi_(std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
262 const NodeId& id,
263 GUM_SCALAR& msg_l_min,
264 GUM_SCALAR& msg_l_max,
265 std::vector< GUM_SCALAR >& lx,
266 const Idx& pos);
267
275 void msgP_(const NodeId X, const NodeId demanding_child);
276
289 void enum_combi_(std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
290 const NodeId& id,
291 GUM_SCALAR& msg_p_min,
292 GUM_SCALAR& msg_p_max);
293
305 void compute_ext_(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
306 const NodeId& id,
307 GUM_SCALAR& msg_p_min,
308 GUM_SCALAR& msg_p_max);
309
311 void refreshLMsPIs_(bool refreshIndic = false);
312
317 GUM_SCALAR calculateEpsilon_();
318
320
323
328
331 void updateIndicatrices_();
332
334
343
350
353
365
377
381
382 private:
385
388
391
394
395 // cnfunc getCN = &infE::getCN;
396 };
397
398
399#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
400 extern template class CNLoopyPropagation< double >;
401#endif
402 } // namespace credal
403} // namespace gum
404
406
407#endif
The base class for all directed edges.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
<agrum/CN/CNLoopyPropagation.h>
void insertEvidenceFile(std::string_view path) override
Starts the inference.
void makeInference() override
Starts the inference.
NodeProperty< GUM_SCALAR > NodesL_min_
"Lower" node information obtained by combinaison of children messages.
void eraseAllEvidence() override
Erase all inference related data to perform another one.
NodeProperty< GUM_SCALAR > NodesP_min_
"Lower" node information obtained by combinaison of parent's messages.
NodeProperty< GUM_SCALAR > NodesL_max_
"Upper" node information obtained by combinaison of children messages.
void msgL_(const NodeId X, const NodeId demanding_parent)
Sends a message to one's parent, i.e.
NodeProperty< NodeSet * > msg_l_sent_
Used to keep track of one's messages sent to it's parents.
InferenceType _inferenceType_
The chosen inference type.
void compute_ext_(GUM_SCALAR &msg_l_min, GUM_SCALAR &msg_l_max, std::vector< GUM_SCALAR > &lx, GUM_SCALAR &num_min, GUM_SCALAR &num_max, GUM_SCALAR &den_min, GUM_SCALAR &den_max)
Used by msgL_.
NodeProperty< bool > update_p_
Used to keep track of which node needs to update it's information coming from it's parents.
void refreshLMsPIs_(bool refreshIndic=false)
Get the last messages from one's parents and children.
NodeProperty< bool > update_l_
Used to keep track of which node needs to update it's information coming from it's children.
void makeInferenceNodeToNeighbours_()
Starts the inference with this inference type.
InferenceEngine< GUM_SCALAR > _infE_
To easily access InferenceEngine< GUM_SCALAR > methods.
void initialize_()
Topological forward propagation to initialize old marginals & messages.
std::vector< Tensor< GUM_SCALAR > * > msg
GUM_SCALAR calculateEpsilon_()
Compute epsilon.
void saveInference(std::string_view path)
void makeInferenceByRandomOrder_()
Starts the inference with this inference type.
const IBayesNet< GUM_SCALAR > * _bnet_
A pointer to it's IBayesNet used as a DAG.
ArcProperty< GUM_SCALAR > ArcsP_min_
"Lower" information coming from one's parent.
InferenceType
Inference type to be used by the algorithm.
@ nodeToNeighbours
Uses a node-set so we don't iterate on nodes that can't send a new message.
@ randomOrder
Chooses a random arc ordering and sends messages accordingly.
@ ordered
Chooses an arc ordering and sends messages accordingly at all steps.
void msgP_(const NodeId X, const NodeId demanding_child)
Sends a message to one's child, i.e.
ArcProperty< GUM_SCALAR > ArcsL_max_
"Upper" information coming from one's children.
void updateMarginals_()
Compute marginals from up-to-date messages.
const CredalNet< GUM_SCALAR > * _cn_
A pointer to the CredalNet to be used.
void computeExpectations_()
Since the network is binary, expectations can be computed from the final marginals which give us the ...
NodeProperty< GUM_SCALAR > NodesP_max_
"Upper" node information obtained by combinaison of parent's messages.
void enum_combi_(std::vector< std::vector< std::vector< GUM_SCALAR > > > &msgs_p, const NodeId &id, GUM_SCALAR &msg_l_min, GUM_SCALAR &msg_l_max, std::vector< GUM_SCALAR > &lx, const Idx &pos)
Used by msgL_.
InferenceType inferenceType()
Get the inference type.
void makeInferenceByOrderedArcs_()
Starts the inference with this inference type.
NodeSet active_nodes_set_
The current node-set to iterate through at this current step.
void updateIndicatrices_()
Only update indicatrices variables at the end of computations ( calls msgP_ ).
NodeSet next_active_nodes_set_
The next node-set, i.e.
CNLoopyPropagation(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
bool inference_up_to_date_
TRUE if inference has already been performed, FALSE otherwise.
ArcProperty< GUM_SCALAR > ArcsL_min_
"Lower" information coming from one's children.
ArcProperty< GUM_SCALAR > ArcsP_max_
"Upper" information coming from one's parent.
Class template representing a Credal Network.
Definition credalNet.h:97
InferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Construtor.
const CredalNet< GUM_SCALAR > & credalNet() const
Get this credal network.
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
HashTable< Arc, VAL > ArcProperty
Property on graph elements.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Abstract class representing CredalNet inference engines.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46