aGrUM  0.14.2
CNLoopyPropagation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
28 #ifndef __CN_LOOPY_PROPAGATION__H__
29 #define __CN_LOOPY_PROPAGATION__H__
30 
31 #include <cstdlib>
32 #include <ctime>
33 #include <limits>
34 
35 #include <agrum/CN/credalNet.h>
38 #include <agrum/core/math/pow.h>
39 #include <agrum/core/sequence.h>
40 
41 #define _INF std::numeric_limits< GUM_SCALAR >::infinity()
42 
43 namespace gum {
44  namespace credal {
45 
55  template < typename GUM_SCALAR >
56  class CNLoopyPropagation : public InferenceEngine< GUM_SCALAR > {
57  public:
58  using msg = std::vector< Potential< GUM_SCALAR >* >;
59  using cArcP = const Arc*;
60 
64  enum class InferenceType : char {
70  ordered,
80  };
81 
84 
86  void makeInference();
87 
89 
92 
97  void inferenceType(InferenceType inft);
98 
104 
106 
109 
117  void eraseAllEvidence();
118 
125  void saveInference(const std::string& path);
126 
128 
131 
135  explicit CNLoopyPropagation(const CredalNet< GUM_SCALAR >& cnet);
137  virtual ~CNLoopyPropagation();
139 
140  protected:
143 
146  void _initialize();
147 
149 
152 
158 
160  void _updateMarginals();
161 
169  void _msgL(
170  const NodeId X,
171  const NodeId demanding_parent); // allways sent from X to demanding_X
172 
193  void _compute_ext(GUM_SCALAR& msg_l_min,
194  GUM_SCALAR& msg_l_max,
195  std::vector< GUM_SCALAR >& lx,
196  GUM_SCALAR& num_min,
197  GUM_SCALAR& num_max,
198  GUM_SCALAR& den_min,
199  GUM_SCALAR& den_max);
200 
218  void _compute_ext(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
219  const NodeId& id,
220  GUM_SCALAR& msg_l_min,
221  GUM_SCALAR& msg_l_max,
222  std::vector< GUM_SCALAR >& lx,
223  const Idx& pos);
224 
241  void _enum_combi(
242  std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
243  const NodeId& id,
244  GUM_SCALAR& msg_l_min,
245  GUM_SCALAR& msg_l_max,
246  std::vector< GUM_SCALAR >& lx,
247  const Idx& pos);
248 
256  void _msgP(const NodeId X, const NodeId demanding_child);
257 
270  void _enum_combi(
271  std::vector< std::vector< std::vector< GUM_SCALAR > > >& msgs_p,
272  const NodeId& id,
273  GUM_SCALAR& msg_p_min,
274  GUM_SCALAR& msg_p_max);
275 
287  void _compute_ext(std::vector< std::vector< GUM_SCALAR > >& combi_msg_p,
288  const NodeId& id,
289  GUM_SCALAR& msg_p_min,
290  GUM_SCALAR& msg_p_max);
291 
293  void _refreshLMsPIs(bool refreshIndic = false);
294 
299  GUM_SCALAR _calculateEpsilon();
300 
302 
305 
309  void _computeExpectations();
310 
313  void _updateIndicatrices();
314 
316 
325 
332 
335 
347 
359 
363 
364  private:
367 
370 
373 
376 
377  // typedef const CredalNet< GUM_SCALAR > * (infE::*cnfunc) ();
378  // cnfunc getCN = &infE::getCN;
379  public:
380  virtual void insertEvidenceFile(const std::string& path) {
382  };
383  };
384 
385 
386 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
387  extern template class CNLoopyPropagation< double >;
388 #endif
389  } // namespace credal
390 } // namespace gum
391 
393 
394 #endif
NodeProperty< bool > _update_p
Used to keep track of which node needs to update it&#39;s information coming from it&#39;s parents...
This file contains general scheme for iteratively convergent algorithms.
NodeProperty< GUM_SCALAR > _NodesP_min
"Lower" node information obtained by combinaison of parent&#39;s messages.
Header file of gum::Sequence, a class for storing (ordered) sequences of objects. ...
void _makeInferenceByOrderedArcs()
Starts the inference with this inference type.
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
void eraseAllEvidence()
Erase all inference related data to perform another one.
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
InferenceType __inferenceType
The choosen inference type.
ArcProperty< GUM_SCALAR > _ArcsP_min
"Lower" information coming from one&#39;s parent.
NodeSet active_nodes_set
The current node-set to iterate through at this current step.
void _initialize()
Topological forward propagation to initialize old marginals & messages.
Implementation of pow functions with integers, faster than stl implementation.
const CredalNet< GUM_SCALAR > * __cn
A pointer to the CredalNet to be used.
<agrum/CN/CNLoopyPropagation.h>
ArcProperty< GUM_SCALAR > _ArcsP_max
"Upper" information coming from one&#39;s parent.
ArcProperty< GUM_SCALAR > _ArcsL_max
"Upper" information coming from one&#39;s children.
Chooses an arc ordering and sends messages accordingly at all steps.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void _computeExpectations()
Since the network is binary, expectations can be computed from the final marginals which give us the ...
Abstract class representing CredalNet inference engines.
The class for generic Hash Tables.
Definition: hashTable.h:676
void _makeInferenceByRandomOrder()
Starts the inference with this inference type.
const IBayesNet< GUM_SCALAR > * __bnet
A pointer to it&#39;s IBayesNet used as a DAG.
void _updateMarginals()
Compute marginals from up-to-date messages.
Class template representing a Credal Network.
Definition: credalNet.h:87
InferenceType inferenceType()
Get the inference type.
bool _InferenceUpToDate
TRUE if inference has already been performed, FALSE otherwise.
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
CNLoopyPropagation(const CredalNet< GUM_SCALAR > &cnet)
Constructor.
Uses a node-set so we don&#39;t iterate on nodes that can&#39;t send a new message.
ArcProperty< GUM_SCALAR > _ArcsL_min
"Lower" information coming from one&#39;s children.
GUM_SCALAR _calculateEpsilon()
Compute epsilon.
void saveInference(const std::string &path)
void _msgL(const NodeId X, const NodeId demanding_parent)
Sends a message to one&#39;s parent, i.e.
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.
NodeProperty< NodeSet *> _msg_l_sent
Used to keep track of one&#39;s messages sent to it&#39;s parents.
Abstract class template representing a CredalNet inference engine.
void _refreshLMsPIs(bool refreshIndic=false)
Get the last messages from one&#39;s parents and children.
Class representing Credal Networks.
NodeProperty< GUM_SCALAR > _NodesL_min
"Lower" node information obtained by combinaison of children messages.
InferenceType
Inference type to be used by the algorithm.
Size Idx
Type for indexes.
Definition: types.h:50
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.
void _updateIndicatrices()
Only update indicatrices variables at the end of computations ( calls _msgP ).
void _msgP(const NodeId X, const NodeId demanding_child)
Sends a message to one&#39;s child, i.e.
Chooses a random arc ordering and sends messages accordingly.
NodeSet next_active_nodes_set
The next node-set, i.e.
NodeProperty< GUM_SCALAR > _NodesP_max
"Upper" node information obtained by combinaison of parent&#39;s messages.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
NodeProperty< bool > _update_l
Used to keep track of which node needs to update it&#39;s information coming from it&#39;s children...
NodeProperty< GUM_SCALAR > _NodesL_max
"Upper" node information obtained by combinaison of children messages.
void _makeInferenceNodeToNeighbours()
Starts the inference with this inference type.
std::vector< Potential< GUM_SCALAR > *> msg