aGrUM 2.3.2
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-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
48
49#ifndef __CN_LOOPY_PROPAGATION__H__
50#define __CN_LOOPY_PROPAGATION__H__
51
52#include <cstdlib>
53#include <ctime>
54#include <limits>
55
57
58#define INF_ std::numeric_limits< GUM_SCALAR >::infinity()
59
60namespace gum {
61 namespace credal {
62
72 template < typename GUM_SCALAR >
73 class CNLoopyPropagation: public InferenceEngine< GUM_SCALAR > {
74 public:
75 using msg = std::vector< Tensor< GUM_SCALAR >* >;
76 using cArcP = const Arc*;
77
98
101
103 void makeInference();
104
106
109
114 void inferenceType(InferenceType inft);
115
120 InferenceType inferenceType();
121
123
126
134 void eraseAllEvidence();
135
142 void saveInference(const std::string& path);
143
145
148
154 virtual ~CNLoopyPropagation();
156
157 protected:
160
163 void initialize_();
164
166
169
175
177 void updateMarginals_();
178
186 void msgL_(const NodeId X,
187 const NodeId demanding_parent); // allways sent from X to demanding_X
188
209 void compute_ext_(GUM_SCALAR& msg_l_min,
210 GUM_SCALAR& msg_l_max,
211 std::vector< GUM_SCALAR >& lx,
212 GUM_SCALAR& num_min,
213 GUM_SCALAR& num_max,
214 GUM_SCALAR& den_min,
215 GUM_SCALAR& den_max);
216
234 void compute_ext_(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
235 const NodeId& id,
236 GUM_SCALAR& msg_l_min,
237 GUM_SCALAR& msg_l_max,
238 std::vector< GUM_SCALAR >& lx,
239 const Idx& pos);
240
257 void enum_combi_(std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
258 const NodeId& id,
259 GUM_SCALAR& msg_l_min,
260 GUM_SCALAR& msg_l_max,
261 std::vector< GUM_SCALAR >& lx,
262 const Idx& pos);
263
271 void msgP_(const NodeId X, const NodeId demanding_child);
272
285 void enum_combi_(std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
286 const NodeId& id,
287 GUM_SCALAR& msg_p_min,
288 GUM_SCALAR& msg_p_max);
289
301 void compute_ext_(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
302 const NodeId& id,
303 GUM_SCALAR& msg_p_min,
304 GUM_SCALAR& msg_p_max);
305
307 void refreshLMsPIs_(bool refreshIndic = false);
308
313 GUM_SCALAR calculateEpsilon_();
314
316
319
324
327 void updateIndicatrices_();
328
330
339
346
349
361
373
377
378 private:
381
384
387
390
391 // cnfunc getCN = &infE::getCN;
392
393 public:
394 virtual void insertEvidenceFile(const std::string& path) {
396 };
397 };
398
399
400#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
401 extern template class CNLoopyPropagation< double >;
402#endif
403 } // namespace credal
404} // namespace gum
405
407
408#endif
The base class for all directed edges.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
NodeProperty< GUM_SCALAR > NodesL_min_
"Lower" node information obtained by combinaison of children messages.
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 choosen 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 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.
bool InferenceUpToDate_
TRUE if inference has already been performed, FALSE otherwise.
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_.
void makeInference()
Starts the inference.
InferenceType inferenceType()
Get the inference type.
void saveInference(const std::string &path)
void makeInferenceByOrderedArcs_()
Starts the inference with this inference type.
void eraseAllEvidence()
Erase all inference related data to perform another one.
void updateIndicatrices_()
Only update indicatrices variables at the end of computations ( calls msgP_ ).
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
NodeSet active_nodes_set
The current node-set to iterate through at this current step.
NodeSet next_active_nodes_set
The next node-set, i.e.
CNLoopyPropagation(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
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
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
InferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Construtor.
const CredalNet< GUM_SCALAR > & credalNet() const
Get this creadal 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