aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::MarginalTargetedInference< GUM_SCALAR > Class Template Referenceabstract

<agrum/BN/inference/marginalTargetedInference.h> More...

#include <BayesNetInference.h>

+ Inheritance diagram for gum::MarginalTargetedInference< GUM_SCALAR >:
+ Collaboration diagram for gum::MarginalTargetedInference< GUM_SCALAR >:

Public Member Functions

Potential< GUM_SCALAR > evidenceImpact (NodeId target, const NodeSet &evs)
 Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) More...
 
Potential< GUM_SCALAR > evidenceImpact (const std::string &target, const std::vector< std::string > &evs)
 Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) More...
 
Constructors / Destructors
 MarginalTargetedInference (const IBayesNet< GUM_SCALAR > *bn)
 default constructor More...
 
virtual ~MarginalTargetedInference ()
 destructor More...
 
Probability computations
virtual const Potential< GUM_SCALAR > & posterior (NodeId node)
 Computes and returns the posterior of a node. More...
 
virtual const Potential< GUM_SCALAR > & posterior (const std::string &nodeName)
 Computes and returns the posterior of a node. More...
 
Targets
virtual void eraseAllTargets ()
 Clear all previously defined targets. More...
 
virtual void addAllTargets () final
 adds all nodes as targets More...
 
virtual void addTarget (NodeId target) final
 Add a marginal target to the list of targets. More...
 
virtual void addTarget (const std::string &nodeName) final
 Add a marginal target to the list of targets. More...
 
virtual void eraseTarget (NodeId target) final
 removes an existing (marginal) target More...
 
virtual void eraseTarget (const std::string &nodeName) final
 removes an existing (marginal) target More...
 
virtual bool isTarget (NodeId node) const final
 return true if variable is a (marginal) target More...
 
virtual bool isTarget (const std::string &nodeName) const final
 return true if variable is a (marginal) target More...
 
virtual const Size nbrTargets () const noexcept final
 returns the number of marginal targets More...
 
virtual const NodeSettargets () const noexcept final
 returns the list of marginal targets More...
 
Information Theory related functions
virtual GUM_SCALAR H (NodeId X) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
virtual GUM_SCALAR H (const std::string &nodeName) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
Accessors / Modifiers
virtual void setBN (const IBayesNet< GUM_SCALAR > *bn)
 assigns a new BN to the inference engine More...
 
virtual const IBayesNet< GUM_SCALAR > & BN () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
Accessors / Modifiers
virtual const GraphicalModelmodel () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
virtual const NodeProperty< Size > & domainSizes () const final
 get the domain sizes of the random variables of the model More...
 
virtual bool isInferenceReady () const noexcept final
 returns whether the inference object is in a ready state More...
 
virtual bool isInferenceOutdatedStructure () const noexcept final
 returns whether the inference object is in a OutdatedStructure state More...
 
virtual bool isInferenceOutdatedPotentials () const noexcept final
 returns whether the inference object is in a OutdatedPotential state More...
 
virtual bool isInferenceDone () const noexcept final
 returns whether the inference object is in a InferenceDone state More...
 
virtual void prepareInference () final
 prepare the internal inference structures for the next inference More...
 
virtual void makeInference () final
 perform the heavy computations needed to compute the targets' posteriors More...
 
virtual void clear ()
 clears all the data structures allocated for the last inference More...
 
virtual StateOfInference state () const noexcept final
 returns the state of the inference engine More...
 
Evidence
virtual void addEvidence (NodeId id, const Idx val) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const Idx val) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::string &label) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const std::string &label) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node named nodeName (might be soft or hard) More...
 
virtual void addEvidence (const Potential< GUM_SCALAR > &pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (Potential< GUM_SCALAR > &&pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addSetOfEvidence (const Set< const Potential< GUM_SCALAR > * > &potset) final
 adds a new set of evidence More...
 
virtual void addListOfEvidence (const List< const Potential< GUM_SCALAR > * > &potlist) final
 adds a new list of evidence More...
 
virtual void chgEvidence (NodeId id, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const Potential< GUM_SCALAR > &pot) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void eraseAllEvidence () final
 removes all the evidence entered into the network More...
 
virtual void eraseEvidence (NodeId id) final
 removed the evidence, if any, corresponding to node id More...
 
virtual void eraseEvidence (const std::string &nodeName) final
 removed the evidence, if any, corresponding to node of name nodeName More...
 
virtual bool hasEvidence () const final
 indicates whether some node(s) have received evidence More...
 
virtual bool hasEvidence (NodeId id) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasEvidence (const std::string &nodeName) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasHardEvidence (NodeId id) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasHardEvidence (const std::string &nodeName) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasSoftEvidence (NodeId id) const final
 indicates whether node id has received a soft evidence More...
 
virtual bool hasSoftEvidence (const std::string &nodeName) const final
 indicates whether node id has received a soft evidence More...
 
virtual Size nbrEvidence () const final
 returns the number of evidence entered into the Bayesian network More...
 
virtual Size nbrHardEvidence () const final
 returns the number of hard evidence entered into the Bayesian network More...
 
virtual Size nbrSoftEvidence () const final
 returns the number of soft evidence entered into the Bayesian network More...
 
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence () const
 returns the set of evidence More...
 
const NodeSetsoftEvidenceNodes () const
 returns the set of nodes with soft evidence More...
 
const NodeSethardEvidenceNodes () const
 returns the set of nodes with hard evidence More...
 
const NodeProperty< Idx > & hardEvidence () const
 indicate for each node with hard evidence which value it took More...
 

Public Types

enum  StateOfInference { StateOfInference::OutdatedStructure, StateOfInference::OutdatedPotentials, StateOfInference::ReadyForInference, StateOfInference::Done }
 current state of the inference More...
 

Protected Member Functions

virtual void onMarginalTargetAdded_ (const NodeId id)=0
 fired after a new marginal target is inserted More...
 
virtual void onMarginalTargetErased_ (const NodeId id)=0
 fired before a marginal target is removed More...
 
virtual void onAllMarginalTargetsAdded_ ()=0
 fired after all the nodes of the BN are added as marginal targets More...
 
virtual void onAllMarginalTargetsErased_ ()=0
 fired before a all marginal targets are removed More...
 
virtual void onModelChanged_ (const GraphicalModel *bn)
 fired after a new Bayes net has been assigned to the engine More...
 
virtual const Potential< GUM_SCALAR > & posterior_ (NodeId id)=0
 asks derived classes for the posterior of a given variable More...
 
void setTargetedMode_ ()
 
bool isTargetedMode_ () const
 
virtual void onStateChanged_ ()=0
 fired when the stage is changed More...
 
virtual void onEvidenceAdded_ (const NodeId id, bool isHardEvidence)=0
 fired after a new evidence is inserted More...
 
virtual void onEvidenceErased_ (const NodeId id, bool isHardEvidence)=0
 fired before an evidence is removed More...
 
virtual void onAllEvidenceErased_ (bool contains_hard_evidence)=0
 fired before all the evidence are erased More...
 
virtual void onEvidenceChanged_ (const NodeId id, bool hasChangedSoftHard)=0
 fired after an evidence is changed, in particular when its status (soft/hard) changes More...
 
virtual void updateOutdatedStructure_ ()=0
 prepares inference when the latter is in OutdatedStructure state More...
 
virtual void updateOutdatedPotentials_ ()=0
 prepares inference when the latter is in OutdatedPotentials state More...
 
virtual void makeInference_ ()=0
 called when the inference has to be performed effectively More...
 
void setOutdatedStructureState_ ()
 put the inference into an outdated model structure state More...
 
void setOutdatedPotentialsState_ ()
 puts the inference into an OutdatedPotentials state if it is not already in an OutdatedStructure state More...
 
virtual void setState_ (const StateOfInference state) final
 set the state of the inference engine and call the notification onStateChanged_ when necessary (i.e. when the state has effectively changed). More...
 
void setModel_ (const GraphicalModel *model)
 
void setModelDuringConstruction_ (const GraphicalModel *model)
 assigns a model during the inference engine construction More...
 
bool hasNoModel_ () const
 

Detailed Description

template<typename GUM_SCALAR>
class gum::MarginalTargetedInference< GUM_SCALAR >

<agrum/BN/inference/marginalTargetedInference.h>

A generic class for the computation of (possibly incrementally) marginal posteriors

The goal of this class is to take care of the marginal targets used for computing marginal posteriors. The MarginalTargetedInference class inherits from Inference that takes care of handling both evidence and the current state of the inference. Note that the MarginalTargetedInference is designed to be used in incremental inference engines.

Definition at line 70 of file BayesNetInference.h.

Member Enumeration Documentation

◆ StateOfInference

template<typename GUM_SCALAR >
enum gum::GraphicalModelInference::StateOfInference
stronginherited

current state of the inference

graphicalModelInference can be in one of 4 different states:

  • OutdatedStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the model: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the model, hence involving a structural change in the model.
  • OutdatedPotentials: in this state, the structure of the model remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.
  • Ready4Inference: in this state, all the data structures are ready for inference. There just remains to perform the inference computations.
  • Done: the heavy computations of inference have been done. There might still remain a few light computations to perform to get the posterior potentials we need.
Enumerator
OutdatedStructure 
OutdatedPotentials 
ReadyForInference 
Done 

Definition at line 106 of file graphicalModelInference.h.

107  {
108  OutdatedStructure,
109  OutdatedPotentials,
110  ReadyForInference,
111  Done
112  };

Constructor & Destructor Documentation

◆ MarginalTargetedInference()

template<typename GUM_SCALAR >
gum::MarginalTargetedInference< GUM_SCALAR >::MarginalTargetedInference ( const IBayesNet< GUM_SCALAR > *  bn)
explicit

default constructor

Warning
By default, all the nodes of the Bayes net are targets.
note that, by aGrUM's rule, the BN is not copied but only referenced by the inference algorithm.

Definition at line 34 of file marginalTargetedInference_tpl.h.

35  :
36  BayesNetInference< GUM_SCALAR >(bn) {
37  // assign a BN if this has not been done before (due to virtual inheritance)
38  if (this->hasNoModel_()) {
40  }
41 
42  // sets all the nodes as targets
43  if (bn != nullptr) {
44  targeted_mode__ = false;
45  targets__ = bn->dag().asNodeSet();
46  }
47 
48  GUM_CONSTRUCTOR(MarginalTargetedInference);
49  }
bool targeted_mode__
whether the actual targets are default
void setBayesNetDuringConstruction__(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
NodeSet targets__
the set of marginal targets
MarginalTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor

◆ ~MarginalTargetedInference()

template<typename GUM_SCALAR >
gum::MarginalTargetedInference< GUM_SCALAR >::~MarginalTargetedInference ( )
virtual

destructor

Definition at line 54 of file marginalTargetedInference_tpl.h.

54  {
55  GUM_DESTRUCTOR(MarginalTargetedInference);
56  }
MarginalTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor

Member Function Documentation

◆ addAllTargets()

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addAllTargets ( )
finalvirtual

adds all nodes as targets

Definition at line 135 of file marginalTargetedInference_tpl.h.

135  {
136  // check if the node belongs to the Bayesian network
137  if (this->hasNoModel_())
138  GUM_ERROR(NullElement,
139  "No Bayes net has been assigned to the "
140  "inference algorithm");
141 
142 
143  setTargetedMode_(); // does nothing if already in targeted mode
144  for (const auto target: this->BN().dag()) {
145  if (!targets__.contains(target)) {
146  targets__.insert(target);
147  onMarginalTargetAdded_(target);
150  }
151  }
152  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
GraphicalModelInference()
default constructor with a null model (useful for virtual inheritance)
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void onMarginalTargetAdded_(const NodeId id)=0
fired after a new marginal target is inserted
NodeSet targets__
the set of marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addEvidence() [1/8]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const Idx  val 
)
finalvirtualinherited

adds a new hard evidence on node id

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id already has an evidence

Definition at line 243 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

244  {
246  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
Potential< GUM_SCALAR > createHardEvidence__(NodeId id, Idx val) const
create the internal structure for a hard evidence
+ Here is the call graph for this function:

◆ addEvidence() [2/8]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const Idx  val 
)
finalvirtualinherited

adds a new hard evidence on node named nodeName

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif nodeName already has an evidence

Definition at line 250 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

252  {
253  addEvidence(this->model().idFromName(nodeName), val);
254  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [3/8]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const std::string &  label 
)
finalvirtualinherited

adds a new hard evidence on node id

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id already has an evidence

Definition at line 259 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

260  {
261  addEvidence(id, this->model().variable(id)[label]);
262  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [4/8]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const std::string &  label 
)
finalvirtualinherited

adds a new hard evidence on node named nodeName

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif nodeName already has an evidence

Definition at line 266 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

268  {
269  NodeId id = this->model().idFromName(nodeName);
270  addEvidence(id, this->model().variable(id)[label]);
271  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ addEvidence() [5/8]

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif id already has an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 275 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

277  {
278  // checks that the evidence is meaningful
279  if (model__ == nullptr)
280  GUM_ERROR(NullElement,
281  "No Bayes net has been assigned to the "
282  "inference algorithm");
283 
284  if (!model__->exists(id)) {
285  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the model");
286  }
287 
288  if (model__->variable(id).domainSize() != vals.size()) {
289  GUM_ERROR(InvalidArgument,
290  "node " << model__->variable(id)
291  << " and its evidence vector have different sizes.");
292  }
293 
294  Potential< GUM_SCALAR > pot;
295  pot.add(model__->variable(id));
296  pot.fillWith(vals);
297  addEvidence(std::move(pot));
298  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual Size domainSize() const =0
virtual bool exists(NodeId node) const =0
Return true if this node exists in this graphical model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const GraphicalModel * model__
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ addEvidence() [6/8]

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

adds a new evidence on node named nodeName (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif nodeName already has an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node nodeName

Definition at line 302 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

304  {
305  addEvidence(this->model().idFromName(nodeName), vals);
306  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [7/8]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( const Potential< GUM_SCALAR > &  pot)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 352 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

353  {
354  Potential< GUM_SCALAR > new_pot(pot);
355  addEvidence(std::move(new_pot));
356  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addEvidence() [8/8]

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::addEvidence ( Potential< GUM_SCALAR > &&  pot)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 310 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

311  {
312  // check if the potential corresponds to an evidence
313  if (pot.nbrDim() != 1) {
314  GUM_ERROR(InvalidArgument, pot << " is not mono-dimensional.");
315  }
316  if (model__ == nullptr)
317  GUM_ERROR(NullElement,
318  "No Bayes net has been assigned to the "
319  "inference algorithm");
320 
321  NodeId id = model__->nodeId(pot.variable(0));
322 
323  if (hasEvidence(id)) {
324  GUM_ERROR(InvalidArgument,
325  " node " << id
326  << " already has an evidence. Please use chgEvidence().");
327  }
328 
329  // check whether we have a hard evidence (and also check whether the
330  // potential only contains 0 (in this case, this will automatically raise
331  // an exception) )
332  Idx val;
333  bool is_hard_evidence = isHardEvidence__(pot, val);
334 
335  // insert the evidence
336  evidence__.insert(
337  id,
338  new Potential< GUM_SCALAR >(std::forward< Potential< GUM_SCALAR > >(pot)));
339  if (is_hard_evidence) { // pot is deterministic
340  hard_evidence__.insert(id, val);
342  } else {
344  }
346  onEvidenceAdded_(id, is_hard_evidence);
347  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
bool isHardEvidence__(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
const GraphicalModel * model__
the Bayes net on which we perform inferences
NodeProperty< Idx > hard_evidence__
assign to each node with a hard evidence the index of its observed value
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ addListOfEvidence()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addListOfEvidence ( const List< const Potential< GUM_SCALAR > * > &  potlist)
finalvirtualinherited

adds a new list of evidence

Exceptions
UndefinedElementif some potential is defined over several nodes
UndefinedElementif the node on which some potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of some potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 361 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

362  {
363  for (const auto pot: potlist)
364  addEvidence(*pot);
365  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addSetOfEvidence()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::addSetOfEvidence ( const Set< const Potential< GUM_SCALAR > * > &  potset)
finalvirtualinherited

adds a new set of evidence

Exceptions
UndefinedElementif some potential is defined over several nodes
UndefinedElementif the node on which some potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of some potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 370 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

371  {
372  for (const auto pot: potset)
373  addEvidence(*pot);
374  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addTarget() [1/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addTarget ( NodeId  target)
finalvirtual

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 111 of file marginalTargetedInference_tpl.h.

111  {
112  // check if the node belongs to the Bayesian network
113  if (this->hasNoModel_())
114  GUM_ERROR(NullElement,
115  "No Bayes net has been assigned to the "
116  "inference algorithm");
117 
118  if (!this->BN().dag().exists(target)) {
119  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
120  }
121 
122  setTargetedMode_(); // does nothing if already in targeted mode
123  // add the new target
124  if (!targets__.contains(target)) {
125  targets__.insert(target);
126  onMarginalTargetAdded_(target);
129  }
130  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
GraphicalModelInference()
default constructor with a null model (useful for virtual inheritance)
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void onMarginalTargetAdded_(const NodeId id)=0
fired after a new marginal target is inserted
NodeSet targets__
the set of marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addTarget ( const std::string &  nodeName)
finalvirtual

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 157 of file marginalTargetedInference_tpl.h.

158  {
159  // check if the node belongs to the Bayesian network
160  if (this->hasNoModel_())
161  GUM_ERROR(NullElement,
162  "No Bayes net has been assigned to the "
163  "inference algorithm");
164 
165  addTarget(this->BN().idFromName(nodeName));
166  }
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ BN()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::BayesNetInference< GUM_SCALAR >::BN ( ) const
finalvirtualinherited

Returns a constant reference over the IBayesNet referenced by this class.

Exceptions
UndefinedElementis raised if no Bayes net has been assigned to the inference.

Definition at line 59 of file BayesNetInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

59  {
60  return static_cast< const IBayesNet< GUM_SCALAR >& >(this->model());
61  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ chgEvidence() [1/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const Idx  val 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 432 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

433  {
435  }
Potential< GUM_SCALAR > createHardEvidence__(NodeId id, Idx val) const
create the internal structure for a hard evidence
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
+ Here is the call graph for this function:

◆ chgEvidence() [2/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const Idx  val 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 439 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

441  {
442  chgEvidence(this->model().idFromName(nodeName), val);
443  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
+ Here is the call graph for this function:

◆ chgEvidence() [3/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const std::string &  label 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 448 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

449  {
450  chgEvidence(id, this->model().variable(id)[label]);
451  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
+ Here is the call graph for this function:

◆ chgEvidence() [4/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const std::string &  label 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 455 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

457  {
458  NodeId id = this->model().idFromName(nodeName);
459  chgEvidence(id, this->model().variable(id)[label]);
460  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ chgEvidence() [5/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif the node does not already have an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 464 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

466  {
467  // check whether this corresponds to an evidence
468  if (model__ == nullptr)
469  GUM_ERROR(NullElement,
470  "No Bayes net has been assigned to the "
471  "inference algorithm");
472 
473  if (!model__->exists(id)) {
474  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the model");
475  }
476 
477  if (model__->variable(id).domainSize() != vals.size()) {
478  GUM_ERROR(InvalidArgument,
479  "node " << model__->variable(id)
480  << " and its evidence have different sizes.");
481  }
482 
483  // create the potential corresponding to vals
484  Potential< GUM_SCALAR > pot;
485  pot.add(model__->variable(id));
486  pot.fillWith(vals);
487  chgEvidence(pot);
488  }
virtual Size domainSize() const =0
virtual bool exists(NodeId node) const =0
Return true if this node exists in this graphical model.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const GraphicalModel * model__
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ chgEvidence() [6/7]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif the node does not already have an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 492 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

494  {
495  chgEvidence(this->model().idFromName(nodeName), vals);
496  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
+ Here is the call graph for this function:

◆ chgEvidence() [7/7]

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::chgEvidence ( const Potential< GUM_SCALAR > &  pot)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential does not already have an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 501 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

502  {
503  // check if the potential corresponds to an evidence
504  if (pot.nbrDim() != 1) {
505  GUM_ERROR(InvalidArgument, pot << " is not a mono-dimensional potential.");
506  }
507  if (model__ == nullptr)
508  GUM_ERROR(NullElement,
509  "No Bayes net has been assigned to the "
510  "inference algorithm");
511 
512  NodeId id = model__->nodeId(pot.variable(0));
513 
514  if (!hasEvidence(id)) {
515  GUM_ERROR(InvalidArgument,
516  id << " has no evidence. Please use addEvidence().");
517  }
518 
519  // check whether we have a hard evidence (and also check whether the
520  // potential only contains 0 (in this case, this will automatically raise
521  // an exception) )
522  Idx val;
523  bool is_hard_evidence = isHardEvidence__(pot, val);
524 
525  // modify the evidence already stored
526  const Potential< GUM_SCALAR >* localPot = evidence__[id];
527  Instantiation I(pot);
528  for (I.setFirst(); !I.end(); I.inc()) {
529  localPot->set(I, pot[I]);
530  }
531 
532  // the inference state will be different
533  // whether evidence change from Hard to Soft or not.
534  bool hasChangedSoftHard = false;
535 
536  if (is_hard_evidence) {
537  if (!hasHardEvidence(id)) {
538  hasChangedSoftHard = true;
539  hard_evidence__.insert(id, val);
542  } else {
543  hard_evidence__[id] = val;
544  }
545  } else {
546  if (hasHardEvidence(id)) { // evidence was hard
547  hard_evidence__.erase(id);
550  hasChangedSoftHard = true;
551  }
552  }
553 
554  if (hasChangedSoftHard) {
556  } else {
559  }
560  }
561 
562  onEvidenceChanged_(id, hasChangedSoftHard);
563  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:675
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
bool isHardEvidence__(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard)=0
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
const GraphicalModel * model__
the Bayes net on which we perform inferences
NodeProperty< Idx > hard_evidence__
assign to each node with a hard evidence the index of its observed value
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ clear()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::clear ( )
virtualinherited

clears all the data structures allocated for the last inference

Reimplemented in gum::ShaferShenoyLIMIDInference< GUM_SCALAR >.

Definition at line 147 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

147  {
150  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
+ Here is the call graph for this function:

◆ domainSizes()

template<typename GUM_SCALAR >
INLINE const NodeProperty< Size > & gum::GraphicalModelInference< GUM_SCALAR >::domainSizes ( ) const
finalvirtualinherited

get the domain sizes of the random variables of the model

Definition at line 168 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

168  {
169  return domain_sizes__;
170  }
NodeProperty< Size > domain_sizes__
the domain sizes of the random variables
+ Here is the call graph for this function:

◆ eraseAllEvidence()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::eraseAllEvidence ( )
finalvirtualinherited

removes all the evidence entered into the network

Definition at line 597 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

597  {
598  bool has_hard_evidence = !hard_evidence__.empty();
599  this->onAllEvidenceErased_(has_hard_evidence);
600 
601  for (const auto& pair: evidence__) {
602  if (pair.second != nullptr) { delete (pair.second); }
603  }
604 
605  evidence__.clear();
606  hard_evidence__.clear();
609 
610  if (has_hard_evidence) {
612  } else {
615  }
616  }
617  }
virtual void onAllEvidenceErased_(bool contains_hard_evidence)=0
fired before all the evidence are erased
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:374
NodeProperty< Idx > hard_evidence__
assign to each node with a hard evidence the index of its observed value
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
+ Here is the call graph for this function:

◆ eraseAllTargets()

template<typename GUM_SCALAR >
INLINE void gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets ( )
virtual

Clear all previously defined targets.

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 98 of file marginalTargetedInference_tpl.h.

98  {
100 
101  targets__.clear();
102  setTargetedMode_(); // does nothing if already in targeted mode
103 
104  this->setState_(
106  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void onAllMarginalTargetsErased_()=0
fired before a all marginal targets are removed
NodeSet targets__
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:374

◆ eraseEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::eraseEvidence ( NodeId  id)
finalvirtualinherited

removed the evidence, if any, corresponding to node id

Definition at line 568 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

568  {
569  if (hasEvidence(id)) {
570  if (hasHardEvidence(id)) {
571  onEvidenceErased_(id, true);
572  hard_evidence__.erase(id);
575  } else {
576  onEvidenceErased_(id, false);
580  }
581  }
582 
583  delete (evidence__[id]);
584  evidence__.erase(id);
585  }
586  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:675
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
NodeProperty< Idx > hard_evidence__
assign to each node with a hard evidence the index of its observed value
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
+ Here is the call graph for this function:

◆ eraseEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::eraseEvidence ( const std::string &  nodeName)
finalvirtualinherited

removed the evidence, if any, corresponding to node of name nodeName

Definition at line 589 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

590  {
591  eraseEvidence(this->model().idFromName(nodeName));
592  }
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void eraseEvidence(NodeId id) final
removed the evidence, if any, corresponding to node id
+ Here is the call graph for this function:

◆ eraseTarget() [1/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget ( NodeId  target)
finalvirtual

removes an existing (marginal) target

Warning
If the target does not already exist, the method does nothing. In particular, it does not raise any exception.

Definition at line 171 of file marginalTargetedInference_tpl.h.

171  {
172  // check if the node belongs to the Bayesian network
173  if (this->hasNoModel_())
174  GUM_ERROR(NullElement,
175  "No Bayes net has been assigned to the "
176  "inference algorithm");
177 
178  if (!this->BN().dag().exists(target)) {
179  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
180  }
181 
182 
183  if (targets__.contains(target)) {
184  targeted_mode__ = true; // we do not use setTargetedMode_ because we do not
185  // want to clear the targets
186  onMarginalTargetErased_(target);
187  targets__.erase(target);
190  }
191  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
GraphicalModelInference()
default constructor with a null model (useful for virtual inheritance)
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:675
bool targeted_mode__
whether the actual targets are default
NodeSet targets__
the set of marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void onMarginalTargetErased_(const NodeId id)=0
fired before a marginal target is removed
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ eraseTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget ( const std::string &  nodeName)
finalvirtual

removes an existing (marginal) target

Warning
If the target does not already exist, the method does nothing. In particular, it does not raise any exception.

Definition at line 196 of file marginalTargetedInference_tpl.h.

197  {
198  // check if the node belongs to the Bayesian network
199  if (this->hasNoModel_())
200  GUM_ERROR(NullElement,
201  "No Bayes net has been assigned to the "
202  "inference algorithm");
203 
204  eraseTarget(this->BN().idFromName(nodeName));
205  }
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ evidence()

template<typename GUM_SCALAR >
INLINE const NodeProperty< const Potential< GUM_SCALAR > *> & gum::GraphicalModelInference< GUM_SCALAR >::evidence ( ) const
inherited

returns the set of evidence

Definition at line 652 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

652  {
653  return evidence__;
654  }
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ evidenceImpact() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact ( NodeId  target,
const NodeSet evs 
)

Create a gum::Potential for P(target|evs) (for all instanciation of target and evs)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
bnthe BayesNet
targetthe nodeId of the targetted variable
evsthe vector of nodeId of the observed variables
Returns
a Potential

Definition at line 284 of file marginalTargetedInference_tpl.h.

285  {
286  const auto& vtarget = this->BN().variable(target);
287 
288  if (evs.contains(target)) {
289  GUM_ERROR(InvalidArgument,
290  "Target <" << vtarget.name() << "> (" << target
291  << ") can not be in evs (" << evs << ").");
292  }
293  auto condset = this->BN().minimalCondSet(target, evs);
294 
295  Potential< GUM_SCALAR > res;
296  this->eraseAllTargets();
297  this->eraseAllEvidence();
298  res.add(this->BN().variable(target));
299  this->addTarget(target);
300  for (const auto& n: condset) {
301  res.add(this->BN().variable(n));
302  this->addEvidence(n, 0);
303  }
304 
305  Instantiation inst(res);
306  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
307  // inferring
308  for (const auto& n: condset)
309  this->chgEvidence(n, inst.val(this->BN().variable(n)));
310  this->makeInference();
311  // populate res
312  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
313  res.set(inst, this->posterior(target)[inst]);
314  }
315  inst.setFirstVar(vtarget); // remove inst.end() flag
316  }
317 
318  return res;
319  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
virtual void eraseAllTargets()
Clear all previously defined targets.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ evidenceImpact() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact ( const std::string &  target,
const std::vector< std::string > &  evs 
)

Create a gum::Potential for P(target|evs) (for all instanciation of target and evs)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
targetthe nodeId of the target variable
evsthe nodeId of the observed variable
Returns
a Potential

Definition at line 323 of file marginalTargetedInference_tpl.h.

325  {
326  const auto& bn = this->BN();
327  return evidenceImpact(bn.idFromName(target), bn.nodeset(evs));
328  }
Potential< GUM_SCALAR > evidenceImpact(NodeId target, const NodeSet &evs)
Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) ...
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

◆ H() [1/2]

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MarginalTargetedInference< GUM_SCALAR >::H ( NodeId  X)
finalvirtual

Entropy Compute Shanon's entropy of a node given the observation.

See also
http://en.wikipedia.org/wiki/Information_entropy

Definition at line 268 of file marginalTargetedInference_tpl.h.

268  {
269  return posterior(X).entropy();
270  }
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.

◆ H() [2/2]

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MarginalTargetedInference< GUM_SCALAR >::H ( const std::string &  nodeName)
finalvirtual

Entropy Compute Shanon's entropy of a node given the observation.

See also
http://en.wikipedia.org/wiki/Information_entropy

Definition at line 277 of file marginalTargetedInference_tpl.h.

277  {
278  return H(this->BN().idFromName(nodeName));
279  }
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

◆ hardEvidence()

template<typename GUM_SCALAR >
INLINE const NodeProperty< Idx > & gum::GraphicalModelInference< GUM_SCALAR >::hardEvidence ( ) const
inherited

indicate for each node with hard evidence which value it took

Definition at line 644 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

644  {
645  return hard_evidence__;
646  }
NodeProperty< Idx > hard_evidence__
assign to each node with a hard evidence the index of its observed value
+ Here is the call graph for this function:

◆ hardEvidenceNodes()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::GraphicalModelInference< GUM_SCALAR >::hardEvidenceNodes ( ) const
inherited

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 668 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

668  {
669  return hard_evidence_nodes__;
670  }
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
+ Here is the call graph for this function:

◆ hasEvidence() [1/3]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasEvidence ( ) const
finalvirtualinherited

indicates whether some node(s) have received evidence

Definition at line 379 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

379  {
380  return !evidence__.empty();
381  }
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ hasEvidence() [2/3]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received an evidence

Definition at line 386 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

386  {
387  return evidence__.exists(id);
388  }
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ hasEvidence() [3/3]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received an evidence

Definition at line 409 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

410  {
411  return hasEvidence(this->model().idFromName(nodeName));
412  }
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ hasHardEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasHardEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received a hard evidence

Definition at line 394 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

394  {
395  return hard_evidence_nodes__.exists(id);
396  }
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
+ Here is the call graph for this function:

◆ hasHardEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasHardEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received a hard evidence

Definition at line 417 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

418  {
419  return hasHardEvidence(this->model().idFromName(nodeName));
420  }
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ hasNoModel_()

template<typename GUM_SCALAR >
bool gum::GraphicalModelInference< GUM_SCALAR >::hasNoModel_ ( ) const
inlineprotectedinherited

Definition at line 533 of file graphicalModelInference.h.

533 { return model__ == nullptr; };
const GraphicalModel * model__
the Bayes net on which we perform inferences

◆ hasSoftEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasSoftEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received a soft evidence

Definition at line 402 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

402  {
403  return soft_evidence_nodes__.exists(id);
404  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
+ Here is the call graph for this function:

◆ hasSoftEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::hasSoftEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received a soft evidence

Definition at line 425 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

426  {
427  return hasSoftEvidence(this->model().idFromName(nodeName));
428  }
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ isInferenceDone()

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::isInferenceDone ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a InferenceDone state

The inference object is in a done state when the posteriors can be retrieved without performing a new inference, i.e., all the heavy computations have already been performed. Typically, in a junction tree algorithm, this corresponds to a situation in which all the messages needed in the JT have been computed and sent.

Definition at line 89 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

89  {
90  return (state__ == StateOfInference::Done);
91  }
StateOfInference state__
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ isInferenceOutdatedPotentials()

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::isInferenceOutdatedPotentials ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a OutdatedPotential state

Definition at line 82 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

83  {
85  }
StateOfInference state__
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ isInferenceOutdatedStructure()

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::isInferenceOutdatedStructure ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a OutdatedStructure state

Definition at line 75 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

76  {
78  }
StateOfInference state__
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ isInferenceReady()

template<typename GUM_SCALAR >
INLINE bool gum::GraphicalModelInference< GUM_SCALAR >::isInferenceReady ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a ready state

Definition at line 70 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

70  {
72  }
StateOfInference state__
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ isTarget() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::isTarget ( NodeId  node) const
finalvirtual

return true if variable is a (marginal) target

Definition at line 75 of file marginalTargetedInference_tpl.h.

75  {
76  // check that the variable belongs to the bn
77  if (this->hasNoModel_())
78  GUM_ERROR(NullElement,
79  "No Bayes net has been assigned to the "
80  "inference algorithm");
81  if (!this->BN().dag().exists(node)) {
82  GUM_ERROR(UndefinedElement, node << " is not a NodeId in the bn");
83  }
84 
85  return targets__.contains(node);
86  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
NodeSet targets__
the set of marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ isTarget() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::isTarget ( const std::string &  nodeName) const
finalvirtual

return true if variable is a (marginal) target

Definition at line 90 of file marginalTargetedInference_tpl.h.

91  {
92  return isTarget(this->BN().idFromName(nodeName));
93  }
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

◆ isTargetedMode_()

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::isTargetedMode_ ( ) const
protected

Definition at line 332 of file marginalTargetedInference_tpl.h.

332  {
333  return targeted_mode__;
334  }
bool targeted_mode__
whether the actual targets are default

◆ makeInference()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::makeInference ( )
finalvirtualinherited

perform the heavy computations needed to compute the targets' posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done "lightly" by multiplying and projecting those messages.

Definition at line 714 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

714  {
715  if (isInferenceDone()) { return; }
716 
717  if (!isInferenceReady()) { prepareInference(); }
718 
719  makeInference_();
720 
722  }
virtual void prepareInference() final
prepare the internal inference structures for the next inference
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual void makeInference_()=0
called when the inference has to be performed effectively
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
+ Here is the call graph for this function:

◆ makeInference_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::makeInference_ ( )
protectedpure virtualinherited

◆ model()

template<typename GUM_SCALAR >
INLINE const GraphicalModel & gum::GraphicalModelInference< GUM_SCALAR >::model ( ) const
finalvirtualinherited

Returns a constant reference over the IBayesNet referenced by this class.

Exceptions
UndefinedElementis raised if no Bayes net has been assigned to the inference.

Definition at line 114 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

114  {
115  if (model__ == nullptr)
116  GUM_ERROR(UndefinedElement,
117  "No Bayes net has been assigned to "
118  "the inference algorithm.");
119  return *model__;
120  }
const GraphicalModel * model__
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ nbrEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::GraphicalModelInference< GUM_SCALAR >::nbrEvidence ( ) const
finalvirtualinherited

returns the number of evidence entered into the Bayesian network

Definition at line 622 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

622  {
623  return evidence__.size();
624  }
NodeProperty< const Potential< GUM_SCALAR > *> evidence__
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ nbrHardEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::GraphicalModelInference< GUM_SCALAR >::nbrHardEvidence ( ) const
finalvirtualinherited

returns the number of hard evidence entered into the Bayesian network

Definition at line 629 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

629  {
630  return hard_evidence_nodes__.size();
631  }
NodeSet hard_evidence_nodes__
the set of nodes that received hard evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:720
+ Here is the call graph for this function:

◆ nbrSoftEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::GraphicalModelInference< GUM_SCALAR >::nbrSoftEvidence ( ) const
finalvirtualinherited

returns the number of soft evidence entered into the Bayesian network

Definition at line 636 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

636  {
637  return soft_evidence_nodes__.size();
638  }
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:720
+ Here is the call graph for this function:

◆ nbrTargets()

template<typename GUM_SCALAR >
INLINE const Size gum::MarginalTargetedInference< GUM_SCALAR >::nbrTargets ( ) const
finalvirtualnoexcept

returns the number of marginal targets

Definition at line 218 of file marginalTargetedInference_tpl.h.

218  {
219  return targets__.size();
220  }
NodeSet targets__
the set of marginal targets
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:720

◆ onAllEvidenceErased_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::onAllEvidenceErased_ ( bool  contains_hard_evidence)
protectedpure virtualinherited

◆ onAllMarginalTargetsAdded_()

template<typename GUM_SCALAR >
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::onAllMarginalTargetsAdded_ ( )
protectedpure virtual

◆ onAllMarginalTargetsErased_()

template<typename GUM_SCALAR >
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::onAllMarginalTargetsErased_ ( )
protectedpure virtual

◆ onEvidenceAdded_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::onEvidenceAdded_ ( const NodeId  id,
bool  isHardEvidence 
)
protectedpure virtualinherited

◆ onEvidenceChanged_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::onEvidenceChanged_ ( const NodeId  id,
bool  hasChangedSoftHard 
)
protectedpure virtualinherited

fired after an evidence is changed, in particular when its status (soft/hard) changes

Parameters
nodeIdthe node of the changed evidence
hasChangedSoftHardtrue if the evidence has changed from Soft to Hard or from Hard to Soft

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::ShaferShenoyLIMIDInference< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, gum::ShaferShenoyMNInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

◆ onEvidenceErased_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::onEvidenceErased_ ( const NodeId  id,
bool  isHardEvidence 
)
protectedpure virtualinherited

◆ onMarginalTargetAdded_()

template<typename GUM_SCALAR >
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::onMarginalTargetAdded_ ( const NodeId  id)
protectedpure virtual

◆ onMarginalTargetErased_()

template<typename GUM_SCALAR >
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::onMarginalTargetErased_ ( const NodeId  id)
protectedpure virtual

◆ onModelChanged_()

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::onModelChanged_ ( const GraphicalModel bn)
protectedvirtual

◆ onStateChanged_()

◆ posterior() [1/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::MarginalTargetedInference< GUM_SCALAR >::posterior ( NodeId  node)
virtual

Computes and returns the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
nodethe node for which we need a posterior probability
Warning
for efficiency reasons, the potential is stored into the inference engine and is returned by reference. In order to ensure that the potential may still exist even if the Inference object is destroyed, the user has to copy it explicitly.
prepareInference and makeInference may be applied if needed by the posterior method.
Exceptions
UndefinedElementif node is not in the set of targets

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 241 of file marginalTargetedInference_tpl.h.

241  {
242  if (this->hardEvidenceNodes().contains(node)) {
243  return *(this->evidence()[node]);
244  }
245 
246  if (!isTarget(node)) {
247  // throws UndefinedElement if var is not a target
248  GUM_ERROR(UndefinedElement, node << " is not a target node");
249  }
250 
251  if (!this->isInferenceDone()) { this->makeInference(); }
252 
253  return posterior_(node);
254  }
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual const Potential< GUM_SCALAR > & posterior_(NodeId id)=0
asks derived classes for the posterior of a given variable
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ posterior() [2/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::MarginalTargetedInference< GUM_SCALAR >::posterior ( const std::string &  nodeName)
virtual

Computes and returns the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
nodeNamethe anme of the node for which we need a posterior probability
Warning
for efficiency reasons, the potential is stored into the inference engine and is returned by reference. In order to ensure that the potential may still exist even if the Inference object is destroyed, the user has to copy it explicitly.
prepareInference and makeInference may be applied if needed by the posterior method.
Exceptions
UndefinedElementif node is not in the set of targets

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 259 of file marginalTargetedInference_tpl.h.

260  {
261  return posterior(this->BN().idFromName(nodeName));
262  }
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

◆ posterior_()

template<typename GUM_SCALAR >
virtual const Potential< GUM_SCALAR >& gum::MarginalTargetedInference< GUM_SCALAR >::posterior_ ( NodeId  id)
protectedpure virtual

◆ prepareInference()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::prepareInference ( )
finalvirtualinherited

prepare the internal inference structures for the next inference

Definition at line 695 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

695  {
696  if (isInferenceReady() || isInferenceDone()) { return; }
697 
698  if (model__ == nullptr)
699  GUM_ERROR(NullElement,
700  "No model been assigned to the "
701  "inference algorithm");
702 
705  else
707 
709  }
virtual void updateOutdatedPotentials_()=0
prepares inference when the latter is in OutdatedPotentials state
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void updateOutdatedStructure_()=0
prepares inference when the latter is in OutdatedStructure state
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
StateOfInference state__
the current state of the inference (outdated/ready/done)
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
const GraphicalModel * model__
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setAllMarginalTargets__()

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::setAllMarginalTargets__ ( )
private

sets all the nodes of the Bayes net as targets

Definition at line 225 of file marginalTargetedInference_tpl.h.

225  {
226  targets__.clear();
227  if (!this->hasNoModel_()) {
228  targets__ = this->BN().dag().asNodeSet();
230  }
231  }
NodeSet targets__
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:374
virtual void onAllMarginalTargetsAdded_()=0
fired after all the nodes of the BN are added as marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

◆ setBN()

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::setBN ( const IBayesNet< GUM_SCALAR > *  bn)
virtualinherited

assigns a new BN to the inference engine

Assigns a new BN to the BayesNetInference engine and sends messages to the descendants of BayesNetInference to inform them that the BN has changed.

Warning
By default, all the nodes of the Bayes net are targets.
note that, by aGrUM's rule, the bn is not copied into the inference engine but only referenced.

Definition at line 66 of file BayesNetInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

66  {
67  this->setModel_(bn);
68  }
void setModel_(const GraphicalModel *model)
+ Here is the call graph for this function:

◆ setModel_()

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::setModel_ ( const GraphicalModel model)
protectedinherited

Definition at line 125 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

126  {
127  clear();
128  model__ = model;
132  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
void computeDomainSizes__()
computes the domain sizes of the random variables
virtual void onModelChanged_(const GraphicalModel *model)=0
fired after a new Bayes net has been assigned to the engine
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void clear()
clears all the data structures allocated for the last inference
const GraphicalModel * model__
the Bayes net on which we perform inferences
+ Here is the call graph for this function:

◆ setModelDuringConstruction_()

template<typename GUM_SCALAR >
void gum::GraphicalModelInference< GUM_SCALAR >::setModelDuringConstruction_ ( const GraphicalModel model)
protectedinherited

assigns a model during the inference engine construction

Definition at line 137 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

138  {
139  model__ = model;
142  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
void computeDomainSizes__()
computes the domain sizes of the random variables
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
const GraphicalModel * model__
the Bayes net on which we perform inferences
+ Here is the call graph for this function:

◆ setOutdatedPotentialsState_()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::setOutdatedPotentialsState_ ( )
protectedinherited

puts the inference into an OutdatedPotentials state if it is not already in an OutdatedStructure state

OutdatedPotentials: in this state, the structure of the model remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.

Definition at line 688 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

688  {
690  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
+ Here is the call graph for this function:

◆ setOutdatedStructureState_()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::setOutdatedStructureState_ ( )
protectedinherited

put the inference into an outdated model structure state

OutdatedStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the model: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the model, hence involving a structural change in the model. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from graphicalModelInference may be smarter than graphicalModelInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedPotentials state. As an example, consider a LazyPropagation inference in Bayes Net A->B->C->D->E in which C has received hard evidence e_C and E is the only target. In this case, A and B are not needed for inference, the only potentials that matter are P(D|e_C) and P(E|D). So the smallest join tree needed for inference contains only one clique DE. Now, adding new evidence e_A on A has no impact on E given hard evidence e_C. In this case, LazyPropagation can be smart and not update its join tree.

Definition at line 679 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

679  {
681  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
+ Here is the call graph for this function:

◆ setState_()

template<typename GUM_SCALAR >
INLINE void gum::GraphicalModelInference< GUM_SCALAR >::setState_ ( const StateOfInference  state)
finalprotectedvirtualinherited

set the state of the inference engine and call the notification onStateChanged_ when necessary (i.e. when the state has effectively changed).

Definition at line 103 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

104  {
105  if (state__ != state) {
106  state__ = state;
107  onStateChanged_();
108  }
109  }
virtual StateOfInference state() const noexcept final
returns the state of the inference engine
StateOfInference state__
the current state of the inference (outdated/ready/done)
virtual void onStateChanged_()=0
fired when the stage is changed
+ Here is the call graph for this function:

◆ setTargetedMode_()

template<typename GUM_SCALAR >
INLINE void gum::MarginalTargetedInference< GUM_SCALAR >::setTargetedMode_ ( )
protected

Definition at line 336 of file marginalTargetedInference_tpl.h.

336  {
337  if (!targeted_mode__) {
338  targets__.clear();
339  targeted_mode__ = true;
340  }
341  }
bool targeted_mode__
whether the actual targets are default
NodeSet targets__
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:374

◆ softEvidenceNodes()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::GraphicalModelInference< GUM_SCALAR >::softEvidenceNodes ( ) const
inherited

returns the set of nodes with soft evidence

the set of nodes that received soft evidence

Definition at line 660 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

660  {
661  return soft_evidence_nodes__;
662  }
NodeSet soft_evidence_nodes__
the set of nodes that received soft evidence
+ Here is the call graph for this function:

◆ state()

template<typename GUM_SCALAR >
INLINE GraphicalModelInference< GUM_SCALAR >::StateOfInference gum::GraphicalModelInference< GUM_SCALAR >::state ( ) const
finalvirtualnoexceptinherited

returns the state of the inference engine

Definition at line 97 of file graphicalModelInference_tpl.h.

References gum::Set< Key, Alloc >::emplace().

97  {
98  return state__;
99  }
StateOfInference state__
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ targets()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::MarginalTargetedInference< GUM_SCALAR >::targets ( ) const
finalvirtualnoexcept

returns the list of marginal targets

Definition at line 211 of file marginalTargetedInference_tpl.h.

211  {
212  return targets__;
213  }
NodeSet targets__
the set of marginal targets

◆ updateOutdatedPotentials_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::updateOutdatedPotentials_ ( )
protectedpure virtualinherited

prepares inference when the latter is in OutdatedPotentials state

Note that the values of evidence are not necessarily known and can be changed between updateOutdatedPotentials_ and makeInference_.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, gum::ShaferShenoyMNInference< GUM_SCALAR >, gum::ShaferShenoyLIMIDInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

◆ updateOutdatedStructure_()

template<typename GUM_SCALAR >
virtual void gum::GraphicalModelInference< GUM_SCALAR >::updateOutdatedStructure_ ( )
protectedpure virtualinherited

prepares inference when the latter is in OutdatedStructure state

Note that the values of evidence are not necessarily known and can be changed between updateOutdatedStructure_ and makeInference_.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, gum::ShaferShenoyMNInference< GUM_SCALAR >, gum::ShaferShenoyLIMIDInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Member Data Documentation

◆ targeted_mode__

template<typename GUM_SCALAR >
bool gum::MarginalTargetedInference< GUM_SCALAR >::targeted_mode__
private

whether the actual targets are default

Definition at line 239 of file marginalTargetedInference.h.

◆ targets__

template<typename GUM_SCALAR >
NodeSet gum::MarginalTargetedInference< GUM_SCALAR >::targets__
private

the set of marginal targets

Definition at line 242 of file marginalTargetedInference.h.


The documentation for this class was generated from the following files: