aGrUM  0.15.0
variableElimination.h
Go to the documentation of this file.
1 
30 #ifndef GUM_VARIABLE_ELIMINATION_H
31 #define GUM_VARIABLE_ELIMINATION_H
32 
33 #include <utility>
34 
35 #include <agrum/core/math/math.h>
39 #include <agrum/agrum.h>
41 
42 namespace gum {
43 
44 
45  // the function used to combine two tables
46  template < typename GUM_SCALAR >
47  INLINE static Potential< GUM_SCALAR >*
49  const Potential< GUM_SCALAR >& t2) {
50  return new Potential< GUM_SCALAR >(t1 * t2);
51  }
52 
53  // the function used to combine two tables
54  template < typename GUM_SCALAR >
55  INLINE static Potential< GUM_SCALAR >*
57  const Set< const DiscreteVariable* >& del_vars) {
58  return new Potential< GUM_SCALAR >(t1.margSumOut(del_vars));
59  }
60 
61 
69  template < typename GUM_SCALAR >
70  class VariableElimination : public JointTargetedInference< GUM_SCALAR > {
71  public:
72  // ############################################################################
74  // ############################################################################
76 
78  explicit VariableElimination(
80  RelevantPotentialsFinderType relevant_type =
83 
86 
90 
92  ~VariableElimination() final;
93 
95 
96 
97  // ############################################################################
99  // ############################################################################
101 
103  void setTriangulation(const Triangulation& new_triangulation);
104 
106 
115 
117 
123 
125  const JunctionTree* junctionTree(NodeId id);
126 
128 
129 
130  protected:
132  void _onStateChanged() final{};
133 
135  void _onEvidenceAdded(const NodeId id, bool isHardEvidence) final;
136 
138  void _onEvidenceErased(const NodeId id, bool isHardEvidence) final;
139 
141  void _onAllEvidenceErased(bool contains_hard_evidence) final;
142 
150  void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) final;
151 
153 
154  void _onMarginalTargetAdded(const NodeId id) final;
155 
157 
158  void _onMarginalTargetErased(const NodeId id) final;
159 
161  virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR >* bn) final;
162 
164 
165  void _onJointTargetAdded(const NodeSet& set) final;
166 
168 
169  void _onJointTargetErased(const NodeSet& set) final;
170 
172  void _onAllMarginalTargetsAdded() final;
173 
175  void _onAllMarginalTargetsErased() final;
176 
178  void _onAllJointTargetsErased() final;
179 
181  void _onAllTargetsErased() final;
182 
184 
187  void _updateOutdatedBNStructure() final;
188 
190 
193  void _updateOutdatedBNPotentials() final;
194 
196 
197  void _makeInference() final;
198 
199 
201 
202  const Potential< GUM_SCALAR >& _posterior(NodeId id) final;
203 
205 
207  const Potential< GUM_SCALAR >& _jointPosterior(const NodeSet& set) final;
208 
217  _jointPosterior(const NodeSet& wanted_target,
218  const NodeSet& declared_target) final;
219 
222 
225 
226 
227  private:
231 
232 
235 
240  Set< const DiscreteVariable* >& kept_vars);
241 
244 
246  Potential< GUM_SCALAR >* (*__projection_op)(
249 
251  Potential< GUM_SCALAR >* (*__combination_op)(const Potential< GUM_SCALAR >&,
252  const Potential< GUM_SCALAR >&){
254 
257 
259 
265 
267  JunctionTree* __JT{nullptr};
268 
271 
274 
277 
279 
280  Potential< GUM_SCALAR >* __target_posterior{nullptr};
281 
283  const GUM_SCALAR __1_minus_epsilon{GUM_SCALAR(1.0 - 1e-6)};
284 
285 
287  void __createNewJT(const NodeSet& targets);
288 
290  void __setProjectionFunction(Potential< GUM_SCALAR >* (*proj)(
291  const Potential< GUM_SCALAR >&, const Set< const DiscreteVariable* >&));
292 
294  void __setCombinationFunction(Potential< GUM_SCALAR >* (*comb)(
295  const Potential< GUM_SCALAR >&, const Potential< GUM_SCALAR >&));
296 
301  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
302 
307  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
308 
313  __PotentialSet& pot_list, Set< const DiscreteVariable* >& kept_vars);
314 
318  void __findRelevantPotentialsGetAll(__PotentialSet& pot_list,
319  Set< const DiscreteVariable* >& kept_vars);
320 
324  void __findRelevantPotentialsXX(__PotentialSet& pot_list,
325  Set< const DiscreteVariable* >& kept_vars);
326 
327  // remove barren variables and return the newly created projected potentials
328  __PotentialSet
329  __removeBarrenVariables(__PotentialSet& pot_list,
331 
333  std::pair< __PotentialSet, __PotentialSet > __collectMessage(NodeId id,
334  NodeId from);
335 
337  std::pair< __PotentialSet, __PotentialSet > __NodePotentials(NodeId node);
338 
340  std::pair< __PotentialSet, __PotentialSet > __produceMessage(
341  NodeId from_id,
342  NodeId to_id,
343  std::pair< __PotentialSet, __PotentialSet >&& incoming_messages);
344 
347  __PotentialSet __marginalizeOut(__PotentialSet pot_list,
349  Set< const DiscreteVariable* >& kept_vars);
350  };
351 
352 
353 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
354  extern template class VariableElimination< double >;
355 #endif
356 
357 
358 } /* namespace gum */
359 
360 
362 
363 
364 #endif /* GUM_VARIABLE_ELIMINATION_ */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __setProjectionFunction(Potential< GUM_SCALAR > *(*proj)(const Potential< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
sets the operator for performing the projections
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
NodeId __targets2clique
indicate a clique that contains all the nodes of the target
VariableElimination(const IBayesNet< GUM_SCALAR > *BN, RelevantPotentialsFinderType relevant_type=RelevantPotentialsFinderType::DSEP_BAYESBALL_POTENTIALS, FindBarrenNodesType=FindBarrenNodesType::FIND_BARREN_NODES)
default constructor
void _onAllTargetsErased() final
fired before a all single and joint_targets are removed
__PotentialSet __removeBarrenVariables(__PotentialSet &pot_list, Set< const DiscreteVariable * > &del_vars)
static INLINE Potential< GUM_SCALAR > * VENewprojPotential(const Potential< GUM_SCALAR > &t1, const Set< const DiscreteVariable * > &del_vars)
void __setCombinationFunction(Potential< GUM_SCALAR > *(*comb)(const Potential< GUM_SCALAR > &, const Potential< GUM_SCALAR > &))
sets the operator for performing the combinations
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:811
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const Potential< GUM_SCALAR > & _posterior(NodeId id) final
returns the posterior of a given variable
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
JunctionTree * __JT
the junction tree used to answer the last inference query
RelevantPotentialsFinderType __find_relevant_potential_type
the type of relevant potential finding algorithm to be used
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...
static INLINE Potential< GUM_SCALAR > * VENewmultiPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)
void setFindBarrenNodesType(FindBarrenNodesType type)
sets how we determine barren nodes
void _onEvidenceErased(const NodeId id, bool isHardEvidence) final
fired before an evidence is removed
std::pair< __PotentialSet, __PotentialSet > __collectMessage(NodeId id, NodeId from)
actually perform the collect phase
Set< const Potential< GUM_SCALAR > *> __PotentialSet
void _makeInference() final
called when the inference has to be performed effectively
SetIteratorSafe< const Potential< GUM_SCALAR > *> __PotentialSetIterator
Potential< GUM_SCALAR > * __target_posterior
the posterior computed during the last inference
Triangulation * __triangulation
the triangulation class creating the junction tree used for inference
<agrum/BN/inference/variableElimination.h>
void _updateOutdatedBNPotentials() final
prepares inference when the latter is in OutdatedBNPotentials state
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const Potential< GUM_SCALAR > & _jointPosterior(const NodeSet &set) final
returns the posterior of a declared target set
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...
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...
RelevantPotentialsFinderType
type of algorithm for determining the relevant potentials for combinations using some d-separation an...
const JunctionTree * junctionTree(NodeId id)
returns the join tree used for compute the posterior of node id
void _onMarginalTargetErased(const NodeId id) final
fired before a single target is removed
void _onAllMarginalTargetsErased() final
fired before a all the single targets are removed
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...
VariableElimination< GUM_SCALAR > & operator=(const VariableElimination< GUM_SCALAR > &)=delete
avoid copy operators
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
FindBarrenNodesType
type of algorithm to determine barren nodes
UndiGraph __graph
the undigraph extracted from the BN and used to construct the join tree
void _onAllEvidenceErased(bool contains_hard_evidence) final
fired before all the evidence are erased
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...
<agrum/BN/inference/jointTargetedInference.h>
HashTable< NodeId, NodeSet > __clique_potentials
for each BN node, indicate in which clique its CPT will be stored
void _onJointTargetErased(const NodeSet &set) final
fired before a joint target is removed
void _onJointTargetAdded(const NodeSet &set) final
fired after a new joint target is inserted
FindBarrenNodesType __barren_nodes_type
the type of barren nodes computation we wish
void _onAllJointTargetsErased() final
fired before a all the joint targets are removed
void setTriangulation(const Triangulation &new_triangulation)
use a new triangulation algorithm
void setRelevantPotentialsFinderType(RelevantPotentialsFinderType type)
sets how we determine the relevant potentials to combine
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 _onStateChanged() final
fired when the stage is changed
HashTable< NodeId, NodeId > __node_to_clique
for each node of __graph (~ in the Bayes net), associate an ID in the JT
Potential< GUM_SCALAR > * _unnormalizedJointPosterior(NodeId id) final
returns a fresh potential equal to P(argument,evidence)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
__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 ...
std::pair< __PotentialSet, __PotentialSet > __NodePotentials(NodeId node)
returns the CPT + evidence of a node projected w.r.t. hard evidence
void _updateOutdatedBNStructure() final
prepares inference when the latter is in OutdatedBNStructure state
void _onAllMarginalTargetsAdded() final
fired after all the nodes of the BN are added as single targets
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::pair< __PotentialSet, __PotentialSet > __produceMessage(NodeId from_id, NodeId to_id, std::pair< __PotentialSet, __PotentialSet > &&incoming_messages)
creates the message sent by clique from_id to clique to_id
Base class for undirected graphs.
Definition: undiGraph.h:109
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
void __createNewJT(const NodeSet &targets)
create a new junction tree as well as its related data structures
~VariableElimination() final
destructor
Interface for all the triangulation methods.
Definition: triangulation.h:47
const GUM_SCALAR __1_minus_epsilon
for comparisons with 1 - epsilon
void _onMarginalTargetAdded(const NodeId id) final
fired after a new single target is inserted
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) final
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
void(VariableElimination< 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...
void _onEvidenceAdded(const NodeId id, bool isHardEvidence) final
fired after a new evidence is inserted
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn) final
fired after a new Bayes net has been assigned to the engine