aGrUM  0.16.0
ShaferShenoyInference.h
Go to the documentation of this file.
1 
28 #ifndef GUM_SHAFER_SHENOY_INFERENCE_H
29 #define GUM_SHAFER_SHENOY_INFERENCE_H
30 
31 #include <utility>
32 
33 #include <agrum/core/math/math.h>
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 ShaferShenoyInference(
81  bool use_binary_join_tree = true);
82 
84  ~ShaferShenoyInference() final;
85 
87 
88 
89  // ############################################################################
91  // ############################################################################
93 
95  void setTriangulation(const Triangulation& new_triangulation);
96 
98 
104 
106 
109  const JoinTree* joinTree();
110 
112 
116  const JunctionTree* junctionTree();
117 
119  GUM_SCALAR evidenceProbability();
120 
122 
123 
124  protected:
126  void _onStateChanged() final{};
127 
129  void _onEvidenceAdded(const NodeId id, bool isHardEvidence) final;
130 
132  void _onEvidenceErased(const NodeId id, bool isHardEvidence) final;
133 
135  void _onAllEvidenceErased(bool contains_hard_evidence) final;
136 
144  void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) final;
145 
147 
148  void _onMarginalTargetAdded(const NodeId id) final;
149 
151 
152  void _onMarginalTargetErased(const NodeId id) final;
153 
155  virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR >* bn) final;
156 
158 
159  void _onJointTargetAdded(const NodeSet& set) final;
160 
162 
163  void _onJointTargetErased(const NodeSet& set) final;
164 
166  void _onAllMarginalTargetsAdded() final;
167 
169  void _onAllMarginalTargetsErased() final;
170 
172  void _onAllJointTargetsErased() final;
173 
175  void _onAllTargetsErased() final;
176 
178 
181  void _updateOutdatedBNStructure() final;
182 
184 
187  void _updateOutdatedBNPotentials() final;
188 
190 
191  void _makeInference() final;
192 
193 
195 
196  const Potential< GUM_SCALAR >& _posterior(NodeId id) final;
197 
199 
201  const Potential< GUM_SCALAR >& _jointPosterior(const NodeSet& set) final;
202 
211  _jointPosterior(const NodeSet& wanted_target,
212  const NodeSet& declared_target) final;
213 
216 
219 
220 
221  private:
225 
226 
229 
231  Potential< GUM_SCALAR >* (*__projection_op)(
234 
236  Potential< GUM_SCALAR >* (*__combination_op)(const Potential< GUM_SCALAR >&,
237  const Potential< GUM_SCALAR >&){
239 
242 
246 
248 
254 
256  JoinTree* __JT{nullptr};
257 
260 
262 
265  bool __is_new_jt_needed{true};
266 
268 
275 
278 
281 
283 
291 
293 
298 
300 
303 
305 
311 
313 
315 
317 
319 
325 
328 
332 
334 
341 
343 
348 
351 
354 
358 
360  const GUM_SCALAR __1_minus_epsilon{GUM_SCALAR(1.0 - 1e-6)};
361 
362 
364  bool __isNewJTNeeded() const;
365 
367  void __createNewJT();
369  void __setProjectionFunction(Potential< GUM_SCALAR >* (*proj)(
370  const Potential< GUM_SCALAR >&, const Set< const DiscreteVariable* >&));
371 
373  void __setCombinationFunction(Potential< GUM_SCALAR >* (*comb)(
374  const Potential< GUM_SCALAR >&, const Potential< GUM_SCALAR >&));
375 
378  NodeId to,
379  NodeSet& cliques_invalidated);
380 
383 
385  void __computeJoinTreeRoots();
386 
387  // remove barren variables and return the newly created projected potentials
388  __PotentialSet
389  __removeBarrenVariables(__PotentialSet& pot_list,
391 
394  __PotentialSet __marginalizeOut(__PotentialSet pot_list,
396  Set< const DiscreteVariable* >& kept_vars);
397 
399  void __produceMessage(NodeId from_id, NodeId to_id);
400 
402  void __collectMessage(NodeId id, NodeId from);
403 
406 
410  };
411 
412 
413 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
414  extern template class ShaferShenoyInference< double >;
415 #endif
416 
417 
418 } /* namespace gum */
419 
421 
422 #endif /* SHAFER_SHENOY_INFERENCE_H */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:60
void _onAllJointTargetsErased() final
fired before a all the joint targets are removed
void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) final
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
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
NodeProperty< GUM_SCALAR > __constants
the constants resulting from the projections of CPTs defined over only hard evidence nodes remove th...
JunctionTree * __junctionTree
the junction tree to answer the last inference query
void __collectMessage(NodeId id, NodeId from)
actually perform the collect phase
void _onEvidenceErased(const NodeId id, bool isHardEvidence) final
fired before an evidence is removed
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 ...
void _onMarginalTargetErased(const NodeId id) final
fired before a single target is removed
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:811
void _onMarginalTargetAdded(const NodeId id) final
fired after a new single target is inserted
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
GUM_SCALAR evidenceProbability()
returns the probability of evidence
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
void _makeInference() final
called when the inference has to be performed effectively
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< NodeSet, const Potential< GUM_SCALAR > *> __joint_target_posteriors
the set of set target posteriors computed during the last inference
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:165
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
bool __isNewJTNeeded() const
check whether a new join tree is really needed for the next inference
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
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn) final
fired after a new Bayes net has been assigned to the engine
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
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:58
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
void __createNewJT()
create a new junction tree as well as its related data structures
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
<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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for undirected graphs.
Definition: undiGraph.h:109
void __computeJoinTreeRoots()
compute a root for each connected component of __JT
void _onEvidenceAdded(const NodeId id, bool isHardEvidence) final
fired after a new evidence is inserted
<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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:47
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
Size NodeId
Type for node ids.
Definition: graphElements.h:98
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