aGrUM  0.13.2
lazyPropagation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES et Pierre-Henri WUILLEMIN *
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  ***************************************************************************/
27 #ifndef GUM_LAZY_PROPAGATION_H
28 #define GUM_LAZY_PROPAGATION_H
29 
30 #include <cmath>
31 #include <utility>
32 
37 #include <agrum/agrum.h>
39 
40 namespace gum {
41 
42 
43  // the function used to combine two tables
44  template < typename GUM_SCALAR >
45  INLINE static Potential< GUM_SCALAR >*
47  const Potential< GUM_SCALAR >& t2) {
48  return new Potential< GUM_SCALAR >(t1 * t2);
49  }
50 
51  // the function used to combine two tables
52  template < typename GUM_SCALAR >
53  INLINE static Potential< GUM_SCALAR >*
55  const Set< const DiscreteVariable* >& del_vars) {
56  return new Potential< GUM_SCALAR >(t1.margSumOut(del_vars));
57  }
58 
59 
67  template < typename GUM_SCALAR >
69  : public JointTargetedInference< GUM_SCALAR >
70  , public EvidenceInference< GUM_SCALAR > {
71  public:
72  // ############################################################################
74  // ############################################################################
76 
78  explicit LazyPropagation(
83  bool use_binary_join_tree = true);
84 
87 
91 
93  ~LazyPropagation() final;
94 
96 
97 
98  // ############################################################################
100  // ############################################################################
102 
104  void setTriangulation(const Triangulation& new_triangulation);
105 
107 
116 
118 
124 
125 
127 
130  const JoinTree* joinTree();
131 
133 
137  const JunctionTree* junctionTree();
139  GUM_SCALAR evidenceProbability() final;
140 
142 
143 
144  protected:
146  void _onEvidenceAdded(NodeId id, bool isHardEvidence) final;
147 
149  void _onEvidenceErased(NodeId id, bool isHardEvidence) final;
150 
152  void _onAllEvidenceErased(bool has_hard_evidence) final;
153 
161  void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) final;
162 
164 
165  void _onMarginalTargetAdded(NodeId id) final;
166 
168 
169  void _onMarginalTargetErased(NodeId id) final;
170 
172 
173  void _onJointTargetAdded(const NodeSet& set) final;
174 
176 
177  void _onJointTargetErased(const NodeSet& set) final;
178 
180  void _onAllMarginalTargetsAdded() final;
181 
183  void _onAllMarginalTargetsErased() final;
184 
186  void _onAllJointTargetsErased() final;
187 
189  void _onAllTargetsErased() final;
190 
192  void _onStateChanged() final{};
193 
195 
198  void _updateOutdatedBNStructure() final;
199 
201 
204  void _updateOutdatedBNPotentials() final;
205 
207 
208  void _makeInference() final;
209 
210 
212 
213  const Potential< GUM_SCALAR >& _posterior(NodeId id) final;
214 
216 
218  const Potential< GUM_SCALAR >& _jointPosterior(const NodeSet& set) final;
219 
228  _jointPosterior(const NodeSet& wanted_target,
229  const NodeSet& declared_target) final;
230 
233 
236 
237 
238  private:
242 
243 
246 
251  Set< const DiscreteVariable* >& kept_vars);
252 
255 
257  Potential< GUM_SCALAR >* (*__projection_op)(
260 
262  Potential< GUM_SCALAR >* (*__combination_op)(const Potential< GUM_SCALAR >&,
263  const Potential< GUM_SCALAR >&){
265 
268 
272 
274 
280 
282  JoinTree* __JT{nullptr};
283 
286 
288 
291  bool __is_new_jt_needed{true};
292 
294 
301 
304 
307 
309 
315 
317 
320 
322 
328 
330 
332 
334 
336 
342 
345 
349 
351 
358 
360 
365 
368 
371 
375 
377  const GUM_SCALAR __1_minus_epsilon{GUM_SCALAR(1.0 - 1e-6)};
378 
379 
381  bool __isNewJTNeeded() const;
382 
384  void __createNewJT();
386  void __setProjectionFunction(Potential< GUM_SCALAR >* (*proj)(
387  const Potential< GUM_SCALAR >&, const Set< const DiscreteVariable* >&));
388 
390  void __setCombinationFunction(Potential< GUM_SCALAR >* (*comb)(
391  const Potential< GUM_SCALAR >&, const Potential< GUM_SCALAR >&));
392 
395  NodeId to_id,
396  NodeSet& invalidated_cliques);
397 
400 
402  void __computeJoinTreeRoots();
403 
408  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
409 
414  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
415 
420  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
421 
425  void __findRelevantPotentialsGetAll(__PotentialSet& pot_list,
426  Set< const DiscreteVariable* >& kept_vars);
427 
431  void __findRelevantPotentialsXX(__PotentialSet& pot_list,
432  Set< const DiscreteVariable* >& kept_vars);
433 
434  // remove barren variables and return the newly created projected potentials
435  __PotentialSet
436  __removeBarrenVariables(__PotentialSet& pot_list,
438 
441  __PotentialSet __marginalizeOut(__PotentialSet pot_list,
443  Set< const DiscreteVariable* >& kept_vars);
444 
446  void __produceMessage(NodeId from_id, NodeId to_id);
447 
449  void __collectMessage(NodeId id, NodeId from);
450  };
451 
452 
453  extern template class LazyPropagation< float >;
454  extern template class LazyPropagation< double >;
455 
456 
457 } /* namespace gum */
458 
460 
461 #endif /* GUM_LAZY_PROPAGATION_H */
~LazyPropagation() final
destructor
NodeProperty< const Potential< GUM_SCALAR > * > __node_to_soft_evidence
the soft evidence stored in the cliques per their assigned node in the BN
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
HashTable< NodeSet, const Potential< GUM_SCALAR > * > __joint_target_posteriors
the set of set target posteriors computed during the last inference
ArcProperty< bool > __messages_computed
indicates whether a message (from one clique to another) has been computed
void setFindBarrenNodesType(FindBarrenNodesType type)
sets how we determine barren nodes
NodeProperty< const Potential< GUM_SCALAR > * > __hard_ev_projected_CPTs
the CPTs that were projected due to hard evidence nodes
Set< const Potential< GUM_SCALAR > * > __PotentialSet
void _updateOutdatedBNStructure() final
prepares inference when the latter is in OutdatedBNStructure state
LazyPropagation< GUM_SCALAR > & operator=(const LazyPropagation< GUM_SCALAR > &)=delete
avoid copy operators
void setRelevantPotentialsFinderType(RelevantPotentialsFinderType type)
sets how we determine the relevant potentials to combine
JunctionTree * __junctionTree
the junction tree to answer the last inference query
Triangulation * __triangulation
the triangulation class creating the junction tree used for inference
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
void _onAllEvidenceErased(bool has_hard_evidence) final
fired before all the evidence are erased
void __findRelevantPotentialsWithdSeparation3(__PotentialSet &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...
bool __is_new_jt_needed
indicates whether a new join tree is needed for the next inference
Potential< GUM_SCALAR > * _unnormalizedJointPosterior(NodeId id) final
returns a fresh potential equal to P(argument,evidence)
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:808
ArcProperty< __PotentialSet > __created_potentials
the set of potentials created for the last inference messages
bool __isNewJTNeeded() const
check whether a new join tree is really needed for the next inference
This file contains the abstract inference class definition for computing (incrementally) joint poster...
void setTriangulation(const Triangulation &new_triangulation)
use a new triangulation algorithm
void __findRelevantPotentialsXX(__PotentialSet &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...
void _onEvidenceErased(NodeId id, bool isHardEvidence) final
fired before an evidence is removed
void __findRelevantPotentialsWithdSeparation2(__PotentialSet &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...
the type of algorithm to use to perform relevant reasoning in Bayes net inference ...
NodeSet __hard_ev_nodes
the hard evidence nodes which were projected in CPTs
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
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
GUM_SCALAR evidenceProbability() final
returns the probability of evidence
This file contains the abstract class definition for computing the probability of evidence entered in...
void _onJointTargetAdded(const NodeSet &set) final
fired after a new joint target is inserted
void _onAllJointTargetsErased() final
fired before a all the joint targets are removed
const Potential< GUM_SCALAR > & _jointPosterior(const NodeSet &set) final
returns the posterior of a declared target set
RelevantPotentialsFinderType
type of algorithm for determining the relevant potentials for combinations using some d-separation an...
static INLINE Potential< GUM_SCALAR > * LPNewmultiPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)
FindBarrenNodesType __barren_nodes_type
the type of barren nodes computation we wish
EvidenceChangeType
the possible types of evidence changes
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
NodeProperty< const Potential< GUM_SCALAR > * > __target_posteriors
the set of single posteriors computed during the last inference
void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) final
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
const GUM_SCALAR __1_minus_epsilon
for comparisons with 1 - epsilon
void _onAllTargetsErased() final
fired before a all single and joint_targets are removed
NodeSet __roots
a clique node used as a root in each connected component of __JT
void _onMarginalTargetAdded(NodeId id) final
fired after a new single target is inserted
void _onStateChanged() final
fired when the stage is changed
FindBarrenNodesType
type of algorithm to determine barren nodes
const Potential< GUM_SCALAR > & _posterior(NodeId id) final
returns the posterior of a given variable
void _onMarginalTargetErased(NodeId id) final
fired before a single target is removed
void __collectMessage(NodeId id, NodeId from)
actually perform the collect phase
ArcProperty< __PotentialSet > __separator_potentials
the list of all potentials stored in the separators after inferences
void _onEvidenceAdded(NodeId id, bool isHardEvidence) final
fired after a new evidence is inserted
const JoinTree * joinTree()
returns the current join tree used
NodeProperty< GUM_SCALAR > __constants
the constants resulting from the projections of CPTs defined over only hard evidence nodes remove th...
JoinTree * __JT
the join (or junction) tree used to answer the last inference query
void _updateOutdatedBNPotentials() final
prepares inference when the latter is in OutdatedBNPotentials state
<agrum/BN/inference/jointTargetedInference.h>
void _makeInference() final
called when the inference has to be performed effectively
void __diffuseMessageInvalidations(NodeId from_id, NodeId to_id, NodeSet &invalidated_cliques)
invalidate all the messages sent from a given clique
Implementation of lazy propagation for inference in Bayesian Networks.
void _onAllMarginalTargetsAdded() final
fired after all the nodes of the BN are added as single targets
void __setProjectionFunction(Potential< GUM_SCALAR > *(*proj)(const Potential< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
sets the operator for performing the projections
void(LazyPropagation< GUM_SCALAR >::* __findRelevantPotentials)(Set< const Potential< GUM_SCALAR > * > &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...
SetIteratorSafe< const Potential< GUM_SCALAR > * > __PotentialSetIterator
Basic graph of cliques.
Definition: cliqueGraph.h:55
NodeProperty< __PotentialSet > __clique_potentials
the list of all potentials stored in the cliques
void __computeJoinTreeRoots()
compute a root for each connected component of __JT
HashTable< NodeSet, NodeId > __joint_target_to_clique
for each set target, assign a clique in the JT that contains it
__PotentialSet __removeBarrenVariables(__PotentialSet &pot_list, Set< const DiscreteVariable * > &del_vars)
void __createNewJT()
create a new junction tree as well as its related data structures
Detect barren nodes for inference in Bayesian networks.
static INLINE Potential< GUM_SCALAR > * LPNewprojPotential(const Potential< GUM_SCALAR > &t1, const Set< const DiscreteVariable * > &del_vars)
bool __use_binary_join_tree
indicates whether we should transform junction trees into binary join trees
void __invalidateAllMessages()
invalidate all messages, posteriors and created potentials
__PotentialSet __marginalizeOut(__PotentialSet pot_list, Set< const DiscreteVariable * > &del_vars, Set< const DiscreteVariable * > &kept_vars)
removes variables del_vars from a list of potentials and returns the resulting list ...
LazyPropagation(const IBayesNet< GUM_SCALAR > *BN, RelevantPotentialsFinderType=RelevantPotentialsFinderType::DSEP_BAYESBALL_POTENTIALS, FindBarrenNodesType=FindBarrenNodesType::FIND_BARREN_NODES, bool use_binary_join_tree=true)
default constructor
const JunctionTree * junctionTree()
returns the current junction tree
void _onJointTargetErased(const NodeSet &set) final
fired before a joint target is removed
Class for computing default triangulations of graphs.
<agrum/BN/inference/lazyPropagation.h>
Base class for undirected graphs.
Definition: undiGraph.h:106
void _onAllMarginalTargetsErased() final
fired before a all the single targets are removed
HashTable< NodeId, NodeId > __node_to_clique
for each node of __graph (~ in the Bayes net), associate an ID in the JT
<agrum/BN/inference/evidenceInference.h>
void __produceMessage(NodeId from_id, NodeId to_id)
creates the message sent by clique from_id to clique to_id
RelevantPotentialsFinderType __find_relevant_potential_type
the type of relevant potential finding algorithm to be used
Interface for all the triangulation methods.
Definition: triangulation.h:44
void __findRelevantPotentialsGetAll(__PotentialSet &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
UndiGraph __graph
the undigraph extracted from the BN and used to construct the join tree
void __setCombinationFunction(Potential< GUM_SCALAR > *(*comb)(const Potential< GUM_SCALAR > &, const Potential< GUM_SCALAR > &))
sets the operator for performing the combinations
NodeProperty< EvidenceChangeType > __evidence_changes
indicates which nodes of the BN have evidence that changed since the last inference ...
void __findRelevantPotentialsWithdSeparation(__PotentialSet &pot_list, Set< const DiscreteVariable * > &kept_vars)
update a set of potentials: the remaining are those to be combined to produce a message on a separato...