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

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

#include <BayesNetInference.h>

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

Public Member Functions

Constructors / Destructors

current state of the inference

BayesNetInference 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 BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. 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 BayesNetInference may be smarter than BayesNetInference 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.
  • OutdatedPotentials: in this state, the structure of the BN 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. Typically, in Lazy Propagation, all the messages in the join tree have been computed but, to get the potentials, we still need to perform the combinations of the potentials in the cliques with the messages sent to the cliques. In some inference algorithms, this step may even be empty.
 BayesNetInference (const IBayesNet< GUM_SCALAR > *bn)
 default constructor More...
 
 BayesNetInference ()
 default constructor with a null BN (useful for virtual inheritance) More...
 
virtual ~BayesNetInference ()
 destructor 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 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 onModelChanged_ (const GraphicalModel *model)=0
 fired after a new Bayes net has been assigned to the engine 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
 

Accessors / Modifiers

friend MarginalTargetedInference< GUM_SCALAR >
 allow JointInference to access the single targets and inference states More...
 
friend JointTargetedInference< GUM_SCALAR >
 allow JointInference to access the single targets and inference states More...
 
friend EvidenceInference< GUM_SCALAR >
 allow JointInference to access the single targets and inference states More...
 
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...
 
void _setBayesNetDuringConstruction_ (const IBayesNet< GUM_SCALAR > *bn)
 assigns a BN during the inference engine construction More...
 

Detailed Description

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

<agrum/BN/inference/BayesNetInference.h>

A generic class for Bayes net inference: handles evidence and the current state of the (incremental) inference

The goal of the BayesNetInference class is twofold: i) handling the common resources of BN inference (bn, soft/hard evidence); ii) propose a general high-level scheme for all the inference methods.

A specialized inference just has to specify how to prepare inference, how to make inference and how to get the posteriors for nodes and set of nodes. The scheme for every inference derived from BayesNetInference will be the same:

1- ie=SpecificInference(bn); // state <- OutdatedStructure 2- set targets and evidence in ie 3- ie.prepareInference(); // state <- Ready4Inference 4.a- change values of evidence in ie // state <- OutdatedPotentials 4.b- change some hard evidence or targets // state <- OutdatedStructure 5- ie.makeInference(); // state <- Done 6- get posteriors 7- goto 2 or 4

BayesNetInference 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 BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. 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 BayesNetInference may be smarter than BayesNetInference 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.
  • OutdatedPotentials: in this state, the structure of the BN 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. Typically, in Lazy Propagation, all the messages in the join tree have been computed but, to get the potentials, we still need to perform the combinations of the potentials in the cliques with the messages sent to the cliques. In some inference algorithms, this step may even be empty.

Definition at line 143 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

◆ BayesNetInference() [1/2]

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

default constructor

Warning
note that, by aGrUM's rule, the BN is not copied but only referenced by the inference algorithm.

Definition at line 35 of file BayesNetInference_tpl.h.

35  :
36  GraphicalModelInference< GUM_SCALAR >(bn) {
37  GUM_CONSTRUCTOR(BayesNetInference);
38  }
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)

◆ BayesNetInference() [2/2]

template<typename GUM_SCALAR >
gum::BayesNetInference< GUM_SCALAR >::BayesNetInference ( )

default constructor with a null BN (useful for virtual inheritance)

Warning
BayesNetInference is virtually inherited by MarginalTargetedInference. As a result, the lowest descendant of BayesNetInference will create the latter. To avoid requiring developers to add in the constructors of their inference algorithms a call to BayesNetInference( bn ), we added constructor BayesNetInference(), which will be called automatically by the lowest descendant. Then, MarginalTargetedInference and JointTargetedInference will take care of setting the appropriate bn into BayesNetInference.

Definition at line 43 of file BayesNetInference_tpl.h.

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

43  {
44  GUM_CONSTRUCTOR(BayesNetInference);
45  }
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)
+ Here is the call graph for this function:

◆ ~BayesNetInference()

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

destructor

Definition at line 50 of file BayesNetInference_tpl.h.

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

50  {
51  GUM_DESTRUCTOR(BayesNetInference);
52  }
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)
+ Here is the call graph for this function:

Member Function Documentation

◆ _setBayesNetDuringConstruction_()

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::_setBayesNetDuringConstruction_ ( const IBayesNet< GUM_SCALAR > *  bn)
private

assigns a BN during the inference engine construction

Definition at line 71 of file BayesNetInference_tpl.h.

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

72  {
74  }
void setModelDuringConstruction_(const GraphicalModel *model)
assigns a model during the inference engine construction
+ Here is the call graph for this function:

◆ 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 228 of file graphicalModelInference_tpl.h.

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

228  {
230  }
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 234 of file graphicalModelInference_tpl.h.

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

235  {
236  addEvidence(this->model().idFromName(nodeName), val);
237  }
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 241 of file graphicalModelInference_tpl.h.

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

242  {
243  addEvidence(id, this->model().variable(id)[label]);
244  }
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 248 of file graphicalModelInference_tpl.h.

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

249  {
250  NodeId id = this->model().idFromName(nodeName);
251  addEvidence(id, this->model().variable(id)[label]);
252  }
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 256 of file graphicalModelInference_tpl.h.

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

257  {
258  // checks that the evidence is meaningful
259  if (_model_ == nullptr)
260  GUM_ERROR(NullElement,
261  "No Bayes net has been assigned to the "
262  "inference algorithm");
263 
264  if (!_model_->exists(id)) { GUM_ERROR(UndefinedElement, id << " is not a NodeId in the model") }
265 
266  if (_model_->variable(id).domainSize() != vals.size()) {
267  GUM_ERROR(InvalidArgument,
268  "node " << _model_->variable(id)
269  << " and its evidence vector have different sizes.");
270  }
271 
272  Potential< GUM_SCALAR > pot;
273  pot.add(_model_->variable(id));
274  pot.fillWith(vals);
275  addEvidence(std::move(pot));
276  }
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.
const GraphicalModel * _model_
the Bayes net on which we perform inferences
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 280 of file graphicalModelInference_tpl.h.

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

281  {
282  addEvidence(this->model().idFromName(nodeName), vals);
283  }
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 325 of file graphicalModelInference_tpl.h.

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

325  {
326  Potential< GUM_SCALAR > new_pot(pot);
327  addEvidence(std::move(new_pot));
328  }
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 287 of file graphicalModelInference_tpl.h.

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

287  {
288  // check if the potential corresponds to an evidence
289  if (pot.nbrDim() != 1) { GUM_ERROR(InvalidArgument, pot << " is not mono-dimensional.") }
290  if (_model_ == nullptr)
291  GUM_ERROR(NullElement,
292  "No Bayes net has been assigned to the "
293  "inference algorithm");
294 
295  NodeId id = _model_->nodeId(pot.variable(0));
296 
297  if (hasEvidence(id)) {
298  GUM_ERROR(InvalidArgument,
299  " node " << id << " already has an evidence. Please use chgEvidence().");
300  }
301 
302  // check whether we have a hard evidence (and also check whether the
303  // potential only contains 0 (in this case, this will automatically raise
304  // an exception) )
305  Idx val;
306  bool is_hard_evidence = _isHardEvidence_(pot, val);
307 
308  // insert the evidence
309  _evidence_.insert(id,
310  new Potential< GUM_SCALAR >(std::forward< Potential< GUM_SCALAR > >(pot)));
311  if (is_hard_evidence) { // pot is deterministic
312  _hard_evidence_.insert(id, val);
314  } else {
316  }
318  onEvidenceAdded_(id, is_hard_evidence);
319  }
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
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
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
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
const GraphicalModel * _model_
the Bayes net on which we perform inferences
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:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 333 of file graphicalModelInference_tpl.h.

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

334  {
335  for (const auto pot: potlist)
336  addEvidence(*pot);
337  }
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 342 of file graphicalModelInference_tpl.h.

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

343  {
344  for (const auto pot: potset)
345  addEvidence(*pot);
346  }
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:

◆ BN()

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

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 57 of file BayesNetInference_tpl.h.

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

57  {
58  return static_cast< const IBayesNet< GUM_SCALAR >& >(this->model());
59  }
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 402 of file graphicalModelInference_tpl.h.

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

402  {
404  }
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 408 of file graphicalModelInference_tpl.h.

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

409  {
410  chgEvidence(this->model().idFromName(nodeName), val);
411  }
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 415 of file graphicalModelInference_tpl.h.

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

416  {
417  chgEvidence(id, this->model().variable(id)[label]);
418  }
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 422 of file graphicalModelInference_tpl.h.

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

423  {
424  NodeId id = this->model().idFromName(nodeName);
425  chgEvidence(id, this->model().variable(id)[label]);
426  }
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 431 of file graphicalModelInference_tpl.h.

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

432  {
433  // check whether this corresponds to an evidence
434  if (_model_ == nullptr)
435  GUM_ERROR(NullElement,
436  "No Bayes net has been assigned to the "
437  "inference algorithm");
438 
439  if (!_model_->exists(id)) { GUM_ERROR(UndefinedElement, id << " is not a NodeId in the model") }
440 
441  if (_model_->variable(id).domainSize() != vals.size()) {
442  GUM_ERROR(InvalidArgument,
443  "node " << _model_->variable(id) << " and its evidence have different sizes.");
444  }
445 
446  // create the potential corresponding to vals
447  Potential< GUM_SCALAR > pot;
448  pot.add(_model_->variable(id));
449  pot.fillWith(vals);
450  chgEvidence(pot);
451  }
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
const GraphicalModel * _model_
the Bayes net on which we perform inferences
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 456 of file graphicalModelInference_tpl.h.

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

457  {
458  chgEvidence(this->model().idFromName(nodeName), vals);
459  }
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 464 of file graphicalModelInference_tpl.h.

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

464  {
465  // check if the potential corresponds to an evidence
466  if (pot.nbrDim() != 1) {
467  GUM_ERROR(InvalidArgument, pot << " is not a mono-dimensional potential.")
468  }
469  if (_model_ == nullptr)
470  GUM_ERROR(NullElement,
471  "No Bayes net has been assigned to the "
472  "inference algorithm");
473 
474  NodeId id = _model_->nodeId(pot.variable(0));
475 
476  if (!hasEvidence(id)) {
477  GUM_ERROR(InvalidArgument, id << " has no evidence. Please use addEvidence().")
478  }
479 
480  // check whether we have a hard evidence (and also check whether the
481  // potential only contains 0 (in this case, this will automatically raise
482  // an exception) )
483  Idx val;
484  bool is_hard_evidence = _isHardEvidence_(pot, val);
485 
486  // modify the evidence already stored
487  const Potential< GUM_SCALAR >* localPot = _evidence_[id];
488  Instantiation I(pot);
489  for (I.setFirst(); !I.end(); I.inc()) {
490  localPot->set(I, pot[I]);
491  }
492 
493  // the inference state will be different
494  // whether evidence change from Hard to Soft or not.
495  bool hasChangedSoftHard = false;
496 
497  if (is_hard_evidence) {
498  if (!hasHardEvidence(id)) {
499  hasChangedSoftHard = true;
500  _hard_evidence_.insert(id, val);
503  } else {
504  _hard_evidence_[id] = val;
505  }
506  } else {
507  if (hasHardEvidence(id)) { // evidence was hard
508  _hard_evidence_.erase(id);
511  hasChangedSoftHard = true;
512  }
513  }
514 
515  if (hasChangedSoftHard) {
517  } else {
519  }
520 
521  onEvidenceChanged_(id, hasChangedSoftHard);
522  }
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
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:649
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
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
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
const GraphicalModel * _model_
the Bayes net on which we perform inferences
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:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 139 of file graphicalModelInference_tpl.h.

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

139  {
142  }
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 159 of file graphicalModelInference_tpl.h.

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

159  {
160  return _domain_sizes_;
161  }
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 553 of file graphicalModelInference_tpl.h.

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

553  {
554  bool has_hard_evidence = !_hard_evidence_.empty();
555  this->onAllEvidenceErased_(has_hard_evidence);
556 
557  for (const auto& pair: _evidence_) {
558  if (pair.second != nullptr) { delete (pair.second); }
559  }
560 
561  _evidence_.clear();
562  _hard_evidence_.clear();
565 
566  if (has_hard_evidence) {
568  } else {
570  }
571  }
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...
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
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:361
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() [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 527 of file graphicalModelInference_tpl.h.

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

527  {
528  if (hasEvidence(id)) {
529  if (hasHardEvidence(id)) {
530  onEvidenceErased_(id, true);
531  _hard_evidence_.erase(id);
534  } else {
535  onEvidenceErased_(id, false);
538  }
539 
540  delete (_evidence_[id]);
541  _evidence_.erase(id);
542  }
543  }
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
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:649
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
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 546 of file graphicalModelInference_tpl.h.

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

546  {
547  eraseEvidence(this->model().idFromName(nodeName));
548  }
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:

◆ 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 605 of file graphicalModelInference_tpl.h.

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

605  {
606  return _evidence_;
607  }
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ 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 597 of file graphicalModelInference_tpl.h.

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

597  {
598  return _hard_evidence_;
599  }
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 619 of file graphicalModelInference_tpl.h.

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

619  {
620  return _hard_evidence_nodes_;
621  }
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 351 of file graphicalModelInference_tpl.h.

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

351  {
352  return !_evidence_.empty();
353  }
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 358 of file graphicalModelInference_tpl.h.

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

358  {
359  return _evidence_.exists(id);
360  }
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 380 of file graphicalModelInference_tpl.h.

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

380  {
381  return hasEvidence(this->model().idFromName(nodeName));
382  }
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 365 of file graphicalModelInference_tpl.h.

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

365  {
366  return _hard_evidence_nodes_.exists(id);
367  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
+ 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 388 of file graphicalModelInference_tpl.h.

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

388  {
389  return hasHardEvidence(this->model().idFromName(nodeName));
390  }
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 527 of file graphicalModelInference.h.

527 { 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 372 of file graphicalModelInference_tpl.h.

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

372  {
373  return _soft_evidence_nodes_.exists(id);
374  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
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 396 of file graphicalModelInference_tpl.h.

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

396  {
397  return hasSoftEvidence(this->model().idFromName(nodeName));
398  }
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 84 of file graphicalModelInference_tpl.h.

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

84  {
85  return (_state_ == StateOfInference::Done);
86  }
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 79 of file graphicalModelInference_tpl.h.

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

79  {
81  }
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 73 of file graphicalModelInference_tpl.h.

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

73  {
75  }
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 68 of file graphicalModelInference_tpl.h.

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

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

◆ 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 664 of file graphicalModelInference_tpl.h.

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

664  {
665  if (isInferenceDone()) { return; }
666 
667  if (!isInferenceReady()) { prepareInference(); }
668 
669  makeInference_();
670 
672  }
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 107 of file graphicalModelInference_tpl.h.

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

107  {
108  if (_model_ == nullptr)
109  GUM_ERROR(UndefinedElement,
110  "No Bayes net has been assigned to "
111  "the inference algorithm.");
112  return *_model_;
113  }
const GraphicalModel * _model_
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 576 of file graphicalModelInference_tpl.h.

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

576  {
577  return _evidence_.size();
578  }
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 583 of file graphicalModelInference_tpl.h.

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

583  {
584  return _hard_evidence_nodes_.size();
585  }
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:694
+ 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 590 of file graphicalModelInference_tpl.h.

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

590  {
591  return _soft_evidence_nodes_.size();
592  }
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:694
+ Here is the call graph for this function:

◆ onAllEvidenceErased_()

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

◆ 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

◆ onModelChanged_()

◆ onStateChanged_()

◆ 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 645 of file graphicalModelInference_tpl.h.

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

645  {
646  if (isInferenceReady() || isInferenceDone()) { return; }
647 
648  if (_model_ == nullptr)
649  GUM_ERROR(NullElement,
650  "No model been assigned to the "
651  "inference algorithm");
652 
655  else
657 
659  }
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:51
+ Here is the call graph for this function:

◆ setBN()

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

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 64 of file BayesNetInference_tpl.h.

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

64  {
65  this->setModel_(bn);
66  }
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 118 of file graphicalModelInference_tpl.h.

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

118  {
119  clear();
120  _model_ = model;
124  }
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 129 of file graphicalModelInference_tpl.h.

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

130  {
131  _model_ = model;
134  }
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 638 of file graphicalModelInference_tpl.h.

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

638  {
640  }
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 630 of file graphicalModelInference_tpl.h.

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

630  {
632  }
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 98 of file graphicalModelInference_tpl.h.

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

98  {
99  if (_state_ != state) {
100  _state_ = state;
101  onStateChanged_();
102  }
103  }
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:

◆ 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 612 of file graphicalModelInference_tpl.h.

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

612  {
613  return _soft_evidence_nodes_;
614  }
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 92 of file graphicalModelInference_tpl.h.

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

92  {
93  return _state_;
94  }
StateOfInference _state_
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ 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

◆ EvidenceInference< GUM_SCALAR >

template<typename GUM_SCALAR >
friend gum::BayesNetInference< GUM_SCALAR >::EvidenceInference< GUM_SCALAR >
private

allow JointInference to access the single targets and inference states

Definition at line 239 of file BayesNetInference.h.

◆ JointTargetedInference< GUM_SCALAR >

template<typename GUM_SCALAR >
friend gum::BayesNetInference< GUM_SCALAR >::JointTargetedInference< GUM_SCALAR >
private

allow JointInference to access the single targets and inference states

Definition at line 238 of file BayesNetInference.h.

◆ MarginalTargetedInference< GUM_SCALAR >

template<typename GUM_SCALAR >
friend gum::BayesNetInference< GUM_SCALAR >::MarginalTargetedInference< GUM_SCALAR >
private

allow JointInference to access the single targets and inference states

Definition at line 237 of file BayesNetInference.h.


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