aGrUM  0.13.2
ShaferShenoyInference.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  ***************************************************************************/
25 #ifndef GUM_SHAFER_SHENOY_INFERENCE_H
26 #define GUM_SHAFER_SHENOY_INFERENCE_H
27 
28 #include <cmath>
29 #include <utility>
30 
34 #include <agrum/agrum.h>
36 
37 namespace gum {
38 
39 
40  // the function used to combine two tables
41  template < typename GUM_SCALAR >
42  INLINE static Potential< GUM_SCALAR >*
44  const Potential< GUM_SCALAR >& t2) {
45  return new Potential< GUM_SCALAR >(t1 * t2);
46  }
47 
48  // the function used to combine two tables
49  template < typename GUM_SCALAR >
50  INLINE static Potential< GUM_SCALAR >*
52  const Set< const DiscreteVariable* >& del_vars) {
53  return new Potential< GUM_SCALAR >(t1.margSumOut(del_vars));
54  }
55 
56 
64  template < typename GUM_SCALAR >
66  : public JointTargetedInference< GUM_SCALAR >
67  , public EvidenceInference< GUM_SCALAR > {
68  public:
69  // ############################################################################
71  // ############################################################################
73 
75  explicit ShaferShenoyInference(
78  bool use_binary_join_tree = true);
79 
81  ~ShaferShenoyInference() final;
82 
84 
85 
86  // ############################################################################
88  // ############################################################################
90 
92  void setTriangulation(const Triangulation& new_triangulation);
93 
95 
101 
103 
106  const JoinTree* joinTree();
107 
109 
113  const JunctionTree* junctionTree();
114 
116  GUM_SCALAR evidenceProbability();
117 
119 
120 
121  protected:
123  void _onStateChanged() final{};
124 
126  void _onEvidenceAdded(NodeId id, bool isHardEvidence) final;
127 
129  void _onEvidenceErased(NodeId id, bool isHardEvidence) final;
130 
132  void _onAllEvidenceErased(bool contains_hard_evidence) final;
133 
141  void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) final;
142 
144 
145  void _onMarginalTargetAdded(NodeId id) final;
146 
148 
149  void _onMarginalTargetErased(NodeId id) final;
150 
152 
153  void _onJointTargetAdded(const NodeSet& set) final;
154 
156 
157  void _onJointTargetErased(const NodeSet& set) final;
158 
160  void _onAllMarginalTargetsAdded() final;
161 
163  void _onAllMarginalTargetsErased() final;
164 
166  void _onAllJointTargetsErased() final;
167 
169  void _onAllTargetsErased() final;
170 
172 
175  void _updateOutdatedBNStructure() final;
176 
178 
181  void _updateOutdatedBNPotentials() final;
182 
184 
185  void _makeInference() final;
186 
187 
189 
190  const Potential< GUM_SCALAR >& _posterior(NodeId id) final;
191 
193 
195  const Potential< GUM_SCALAR >& _jointPosterior(const NodeSet& set) final;
196 
205  _jointPosterior(const NodeSet& wanted_target,
206  const NodeSet& declared_target) final;
207 
210 
213 
214 
215  private:
219 
220 
223 
225  Potential< GUM_SCALAR >* (*__projection_op)(
228 
230  Potential< GUM_SCALAR >* (*__combination_op)(const Potential< GUM_SCALAR >&,
231  const Potential< GUM_SCALAR >&){
233 
236 
240 
242 
248 
250  JoinTree* __JT{nullptr};
251 
254 
256 
259  bool __is_new_jt_needed{true};
260 
262 
269 
272 
275 
277 
285 
287 
292 
294 
297 
299 
305 
307 
309 
311 
313 
319 
322 
326 
328 
335 
337 
342 
345 
348 
352 
354  const GUM_SCALAR __1_minus_epsilon{GUM_SCALAR(1.0 - 1e-6)};
355 
356 
358  bool __isNewJTNeeded() const;
359 
361  void __createNewJT();
363  void __setProjectionFunction(Potential< GUM_SCALAR >* (*proj)(
364  const Potential< GUM_SCALAR >&, const Set< const DiscreteVariable* >&));
365 
367  void __setCombinationFunction(Potential< GUM_SCALAR >* (*comb)(
368  const Potential< GUM_SCALAR >&, const Potential< GUM_SCALAR >&));
369 
372  NodeId to,
373  NodeSet& cliques_invalidated);
374 
377 
379  void __computeJoinTreeRoots();
380 
381  // remove barren variables and return the newly created projected potentials
382  __PotentialSet
383  __removeBarrenVariables(__PotentialSet& pot_list,
385 
388  __PotentialSet __marginalizeOut(__PotentialSet pot_list,
390  Set< const DiscreteVariable* >& kept_vars);
391 
393  void __produceMessage(NodeId from_id, NodeId to_id);
394 
396  void __collectMessage(NodeId id, NodeId from);
397 
400 
404  };
405 
406 
407  extern template class ShaferShenoyInference< float >;
408  extern template class ShaferShenoyInference< double >;
409 
410 
411 } /* namespace gum */
412 
414 
415 #endif /* SHAFER_SHENOY_INFERENCE_H */
NodeProperty< const Potential< GUM_SCALAR > * > __clique_ss_potential
the potentials stored into the cliques by Shafer-Shenoy
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
void _onAllJointTargetsErased() final
fired before a all the joint targets are removed
FindBarrenNodesType __barren_nodes_type
the type of barren nodes computation we wish
void __setProjectionFunction(Potential< GUM_SCALAR > *(*proj)(const Potential< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
sets the operator for performing the projections
void _onEvidenceErased(NodeId id, bool isHardEvidence) final
fired before an evidence is removed
NodeProperty< GUM_SCALAR > __constants
the constants resulting from the projections of CPTs defined over only hard evidence nodes remove th...
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
JunctionTree * __junctionTree
the junction tree to answer the last inference query
void __collectMessage(NodeId id, NodeId from)
actually perform the collect phase
static INLINE Potential< GUM_SCALAR > * SSNewmultiPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)
__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 ...
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:808
This file contains the abstract inference class definition for computing (incrementally) joint poster...
ArcProperty< bool > __messages_computed
indicates whether a message (from one clique to another) has been computed
Set< const Potential< GUM_SCALAR > * > __PotentialSet
void __setCombinationFunction(Potential< GUM_SCALAR > *(*comb)(const Potential< GUM_SCALAR > &, const Potential< GUM_SCALAR > &))
sets the operator for performing the combinations
const GUM_SCALAR __1_minus_epsilon
for comparisons with 1 - epsilon
Potential< GUM_SCALAR > * _unnormalizedJointPosterior(NodeId id) final
returns a fresh potential equal to P(argument,evidence)
void _onAllMarginalTargetsAdded() final
fired after all the nodes of the BN are added as single targets
NodeSet __roots
a clique node used as a root in each connected component of __JT
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
GUM_SCALAR evidenceProbability()
returns the probability of evidence
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
void _makeInference() final
called when the inference has to be performed effectively
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
This file contains the abstract class definition for computing the probability of evidence entered in...
bool __isNewJTNeeded() const
check whether a new join tree is really needed for the next inference
HashTable< NodeSet, const Potential< GUM_SCALAR > * > __joint_target_posteriors
the set of set target posteriors computed during the last inference
void _onMarginalTargetAdded(NodeId id) final
fired after a new single target is inserted
void _updateOutdatedBNPotentials() final
prepares inference when the latter is in OutdatedBNPotentials state
UndiGraph __graph
the undigraph extracted from the BN and used to construct the join tree
void _onStateChanged() final
fired when the stage is changed
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
ShaferShenoyInference(const IBayesNet< GUM_SCALAR > *BN, FindBarrenNodesType barren_type=FindBarrenNodesType::FIND_BARREN_NODES, bool use_binary_join_tree=true)
default constructor
NodeProperty< EvidenceChangeType > __evidence_changes
indicates which nodes of the BN have evidence that changed since the last inference ...
FindBarrenNodesType
type of algorithm to determine barren nodes
NodeProperty< const Potential< GUM_SCALAR > * > __node_to_soft_evidence
the soft evidence stored in the cliques per their assigned node in the BN
HashTable< NodeId, NodeId > __node_to_clique
for each node of __graph (~ in the Bayes net), associate an ID in the JT
NodeProperty< const Potential< GUM_SCALAR > * > __target_posteriors
the set of single posteriors computed during the last inference
NodeProperty< const Potential< GUM_SCALAR > * > __hard_ev_projected_CPTs
the CPTs that were projected due to hard evidence nodes
void _onJointTargetAdded(const NodeSet &set) final
fired after a new joint target is inserted
void _updateOutdatedBNStructure() final
prepares inference when the latter is in OutdatedBNStructure state
void setTriangulation(const Triangulation &new_triangulation)
use a new triangulation algorithm
const Potential< GUM_SCALAR > & _jointPosterior(const NodeSet &set) final
returns the posterior of a declared target set
<agrum/BN/inference/jointTargetedInference.h>
void _onAllTargetsErased() final
fired before a all single and joint_targets are removed
ShaferShenoyInference< GUM_SCALAR > & operator=(const ShaferShenoyInference< GUM_SCALAR > &)
avoid copy operators
bool __is_new_jt_needed
indicates whether a new join tree is needed for the next inference
static INLINE Potential< GUM_SCALAR > * SSNewprojPotential(const Potential< GUM_SCALAR > &t1, const Set< const DiscreteVariable * > &del_vars)
SetIteratorSafe< const Potential< GUM_SCALAR > * > __PotentialSetIterator
void setFindBarrenNodesType(FindBarrenNodesType type)
sets how we determine barren nodes
void _onJointTargetErased(const NodeSet &set) final
fired before a joint target is removed
HashTable< NodeSet, NodeId > __joint_target_to_clique
for each set target, assign a clique in the JT that contains it
~ShaferShenoyInference() final
destructor
void _onEvidenceAdded(NodeId id, bool isHardEvidence) final
fired after a new evidence is inserted
ArcProperty< __PotentialSet > __created_potentials
the set of potentials created for the last inference messages
Triangulation * __triangulation
the triangulation class creating the junction tree used for inference
Basic graph of cliques.
Definition: cliqueGraph.h:55
void __createNewJT()
create a new junction tree as well as its related data structures
Detect barren nodes for inference in Bayesian networks.
<agrum/BN/inference/ShaferShenoyInference.h>
void __diffuseMessageInvalidations(NodeId from, NodeId to, NodeSet &cliques_invalidated)
invalidate all the messages sent from a given clique
__PotentialSet __removeBarrenVariables(__PotentialSet &pot_list, Set< const DiscreteVariable * > &del_vars)
JoinTree * __JT
the join (or junction) tree used to answer the last inference query
void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) final
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
Class for computing default triangulations of graphs.
Base class for undirected graphs.
Definition: undiGraph.h:106
void __computeJoinTreeRoots()
compute a root for each connected component of __JT
<agrum/BN/inference/evidenceInference.h>
NodeSet __hard_ev_nodes
the hard evidence nodes which were projected in CPTs
bool __use_binary_join_tree
indicates whether we should transform junction trees into binary join trees
void _onMarginalTargetErased(NodeId id) final
fired before a single target is removed
Implementation of Shafer-Shenoy&#39;s propagation for inference in Bayesian Networks. ...
NodeProperty< __PotentialSet > __clique_potentials
the list of all potentials stored in the cliques
const Potential< GUM_SCALAR > & _posterior(NodeId id) final
returns the posterior of a given variable
Interface for all the triangulation methods.
Definition: triangulation.h:44
const JoinTree * joinTree()
returns the current join tree used
EvidenceChangeType
the possible types of evidence changes
void _onAllEvidenceErased(bool contains_hard_evidence) final
fired before all the evidence are erased
void __invalidateAllMessages()
invalidate all messages, posteriors and created potentials
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void _onAllMarginalTargetsErased() final
fired before a all the single targets are removed
void __produceMessage(NodeId from_id, NodeId to_id)
creates the message sent by clique from_id to clique to_id
ArcProperty< __PotentialSet > __separator_potentials
the list of all potentials stored in the separators after inferences
const JunctionTree * junctionTree()
returns the current junction tree