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

<agrum/MN/inference/marginalTargetedMNInference.h> More...

#include <marginalTargetedMNInference.h>

+ Inheritance diagram for gum::MarginalTargetedMNInference< GUM_SCALAR >:
+ Collaboration diagram for gum::MarginalTargetedMNInference< 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
 MarginalTargetedMNInference (const IMarkovNet< GUM_SCALAR > *mn)
 default constructor More...
 
virtual ~MarginalTargetedMNInference ()
 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 setMN (const IMarkovNet< GUM_SCALAR > *mn)
 assigns a new MN to the inference engine More...
 
virtual const IMarkovNet< GUM_SCALAR > & MN () const final
 Returns a constant reference over the IMarkovNet 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 MN are added as marginal targets More...
 
virtual void onAllMarginalTargetsErased_ ()=0
 fired before a all marginal targets are removed More...
 
virtual void onModelChanged_ (const GraphicalModel *mn)
 fired after a new Markov 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::MarginalTargetedMNInference< GUM_SCALAR >

<agrum/MN/inference/marginalTargetedMNInference.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 MarginalTargetedMNInference class inherits from Inference that takes care of handling both evidence and the current state of the inference. Note that the MarginalTargetedMNInference is designed to be used in incremental inference engines.

Definition at line 56 of file marginalTargetedMNInference.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

◆ MarginalTargetedMNInference()

template<typename GUM_SCALAR >
gum::MarginalTargetedMNInference< GUM_SCALAR >::MarginalTargetedMNInference ( const IMarkovNet< GUM_SCALAR > *  mn)
explicit

default constructor

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

Definition at line 34 of file marginalTargetedMNInference_tpl.h.

35  :
36  MarkovNetInference< GUM_SCALAR >(mn) {
37  // assign a MN 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 (mn != nullptr) {
44  _targeted_mode_ = false;
45  _targets_ = mn->graph().asNodeSet();
46  }
47 
48  GUM_CONSTRUCTOR(MarginalTargetedMNInference);
49  }
void _setMarkovNetDuringConstruction_(const IMarkovNet< GUM_SCALAR > *mn)
assigns a MN during the inference engine construction
NodeSet _targets_
the set of marginal targets
bool _targeted_mode_
whether the actual targets are default
MarginalTargetedMNInference(const IMarkovNet< GUM_SCALAR > *mn)
default constructor

◆ ~MarginalTargetedMNInference()

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

destructor

Definition at line 54 of file marginalTargetedMNInference_tpl.h.

54  {
55  GUM_DESTRUCTOR(MarginalTargetedMNInference);
56  }
MarginalTargetedMNInference(const IMarkovNet< GUM_SCALAR > *mn)
default constructor

Member Function Documentation

◆ _setAllMarginalTargets_()

template<typename GUM_SCALAR >
void gum::MarginalTargetedMNInference< GUM_SCALAR >::_setAllMarginalTargets_ ( )
private

sets all the nodes of the Markov net as targets

Definition at line 215 of file marginalTargetedMNInference_tpl.h.

215  {
216  _targets_.clear();
217  if (!this->hasNoModel_()) {
218  _targets_ = this->MN().graph().asNodeSet();
220  }
221  }
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
NodeSet _targets_
the set of marginal targets
virtual void onAllMarginalTargetsAdded_()=0
fired after all the nodes of the MN are added as marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:361

◆ addAllTargets()

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

adds all nodes as targets

Definition at line 131 of file marginalTargetedMNInference_tpl.h.

131  {
132  // check if the node belongs to the Markov network
133  if (this->hasNoModel_())
134  GUM_ERROR(NullElement,
135  "No Markov net has been assigned to the "
136  "inference algorithm");
137 
138 
139  setTargetedMode_(); // does nothing if already in targeted mode
140  for (const auto target: this->MN().graph()) {
141  if (!_targets_.contains(target)) {
142  _targets_.insert(target);
143  onMarginalTargetAdded_(target);
145  }
146  }
147  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
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
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

◆ 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:

◆ addTarget() [1/2]

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

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Markov net

Definition at line 108 of file marginalTargetedMNInference_tpl.h.

108  {
109  // check if the node belongs to the Markov network
110  if (this->hasNoModel_())
111  GUM_ERROR(NullElement,
112  "No Markov net has been assigned to the "
113  "inference algorithm");
114 
115  if (!this->MN().graph().exists(target)) {
116  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the Markov network")
117  }
118 
119  setTargetedMode_(); // does nothing if already in targeted mode
120  // add the new target
121  if (!_targets_.contains(target)) {
122  _targets_.insert(target);
123  onMarginalTargetAdded_(target);
125  }
126  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
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
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

◆ addTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedMNInference< 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 Markov net

Definition at line 152 of file marginalTargetedMNInference_tpl.h.

152  {
153  // check if the node belongs to the Markov network
154  if (this->hasNoModel_())
155  GUM_ERROR(NullElement,
156  "No Markov net has been assigned to the "
157  "inference algorithm");
158 
159  addTarget(this->MN().idFromName(nodeName));
160  }
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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:

◆ eraseAllTargets()

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

Clear all previously defined targets.

Reimplemented in gum::JointTargetedMNInference< GUM_SCALAR >.

Definition at line 96 of file marginalTargetedMNInference_tpl.h.

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

◆ 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:

◆ eraseTarget() [1/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedMNInference< 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 165 of file marginalTargetedMNInference_tpl.h.

165  {
166  // check if the node belongs to the Markov network
167  if (this->hasNoModel_())
168  GUM_ERROR(NullElement,
169  "No Markov net has been assigned to the "
170  "inference algorithm");
171 
172  if (!this->MN().graph().exists(target)) {
173  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the Markov network")
174  }
175 
176 
177  if (_targets_.contains(target)) {
178  _targeted_mode_ = true; // we do not use setTargetedMode_ because we do not
179  // want to clear the targets
180  onMarginalTargetErased_(target);
181  _targets_.erase(target);
183  }
184  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
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:649
NodeSet _targets_
the set of marginal targets
bool _targeted_mode_
whether the actual targets are default
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
virtual void onMarginalTargetErased_(const NodeId id)=0
fired before a marginal target is removed

◆ eraseTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedMNInference< 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 189 of file marginalTargetedMNInference_tpl.h.

189  {
190  // check if the node belongs to the Markov network
191  if (this->hasNoModel_())
192  GUM_ERROR(NullElement,
193  "No Markov net has been assigned to the "
194  "inference algorithm");
195 
196  eraseTarget(this->MN().idFromName(nodeName));
197  }
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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:

◆ evidenceImpact() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedMNInference< 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
mnthe MarkovNet
targetthe nodeId of the targetted variable
evsthe vector of nodeId of the observed variables
Returns
a Potential

Definition at line 269 of file marginalTargetedMNInference_tpl.h.

269  {
270  const auto& vtarget = this->MN().variable(target);
271 
272  if (evs.contains(target)) {
273  GUM_ERROR(InvalidArgument,
274  "Target <" << vtarget.name() << "> (" << target << ") can not be in evs (" << evs
275  << ").");
276  }
277  auto condset = this->MN().minimalCondSet(target, evs);
278 
279  Potential< GUM_SCALAR > res;
280  this->eraseAllTargets();
281  this->eraseAllEvidence();
282  res.add(this->MN().variable(target));
283  this->addTarget(target);
284  for (const auto& n: condset) {
285  res.add(this->MN().variable(n));
286  this->addEvidence(n, 0);
287  }
288 
289  Instantiation inst(res);
290  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
291  // inferring
292  for (const auto& n: condset)
293  this->chgEvidence(n, inst.val(this->MN().variable(n)));
294  this->makeInference();
295  // populate res
296  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
297  res.set(inst, this->posterior(target)[inst]);
298  }
299  inst.setFirstVar(vtarget); // remove inst.end() flag
300  }
301 
302  return res;
303  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
virtual void eraseAllTargets()
Clear all previously defined targets.
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 makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ evidenceImpact() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedMNInference< 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 307 of file marginalTargetedMNInference_tpl.h.

309  {
310  const auto& mn = this->MN();
311  return evidenceImpact(mn.idFromName(target), mn.nodeset(evs));
312  }
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 IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.

◆ H() [1/2]

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MarginalTargetedMNInference< 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 254 of file marginalTargetedMNInference_tpl.h.

254  {
255  return posterior(X).entropy();
256  }
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::MarginalTargetedMNInference< 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 262 of file marginalTargetedMNInference_tpl.h.

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

◆ 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:

◆ isTarget() [1/2]

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

return true if variable is a (marginal) target

Definition at line 73 of file marginalTargetedMNInference_tpl.h.

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

◆ isTarget() [2/2]

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

return true if variable is a (marginal) target

Definition at line 89 of file marginalTargetedMNInference_tpl.h.

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

◆ isTargetedMode_()

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

Definition at line 316 of file marginalTargetedMNInference_tpl.h.

316  {
317  return _targeted_mode_;
318  }
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 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

◆ MN()

template<typename GUM_SCALAR >
INLINE const IMarkovNet< GUM_SCALAR > & gum::MarkovNetInference< GUM_SCALAR >::MN ( ) const
finalvirtualinherited

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

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

Definition at line 57 of file MarkovNetInference_tpl.h.

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

57  {
58  return static_cast< const IMarkovNet< 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:

◆ 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:

◆ nbrTargets()

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

returns the number of marginal targets

Definition at line 208 of file marginalTargetedMNInference_tpl.h.

208  {
209  return _targets_.size();
210  }
NodeSet _targets_
the set of marginal targets
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:694

◆ 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::MarginalTargetedMNInference< GUM_SCALAR >::onAllMarginalTargetsAdded_ ( )
protectedpure virtual

fired after all the nodes of the MN are added as marginal targets

Implemented in gum::ShaferShenoyMNInference< GUM_SCALAR >.

◆ onAllMarginalTargetsErased_()

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

fired before a all marginal targets are removed

Implemented in gum::ShaferShenoyMNInference< GUM_SCALAR >.

◆ 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::MarginalTargetedMNInference< GUM_SCALAR >::onMarginalTargetAdded_ ( const NodeId  id)
protectedpure virtual

fired after a new marginal target is inserted

Parameters
idThe target variable's id.

Implemented in gum::ShaferShenoyMNInference< GUM_SCALAR >.

◆ onMarginalTargetErased_()

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

fired before a marginal target is removed

Parameters
idThe target variable's id.

Implemented in gum::ShaferShenoyMNInference< GUM_SCALAR >.

◆ onModelChanged_()

template<typename GUM_SCALAR >
void gum::MarginalTargetedMNInference< GUM_SCALAR >::onModelChanged_ ( const GraphicalModel mn)
protectedvirtual

fired after a new Markov net has been assigned to the engine

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Reimplemented in gum::JointTargetedMNInference< GUM_SCALAR >.

Definition at line 61 of file marginalTargetedMNInference_tpl.h.

61  {
62  _targeted_mode_ = true;
64  }
void _setAllMarginalTargets_()
sets all the nodes of the Markov net as targets
bool _targeted_mode_
whether the actual targets are default

◆ onStateChanged_()

◆ posterior() [1/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::MarginalTargetedMNInference< 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::JointTargetedMNInference< GUM_SCALAR >.

Definition at line 230 of file marginalTargetedMNInference_tpl.h.

230  {
231  if (this->hardEvidenceNodes().contains(node)) { return *(this->evidence()[node]); }
232 
233  if (!isTarget(node)) {
234  // throws UndefinedElement if var is not a target
235  GUM_ERROR(UndefinedElement, node << " is not a target node")
236  }
237 
238  if (!this->isInferenceDone()) { this->makeInference(); }
239 
240  return posterior_(node);
241  }
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
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:51

◆ posterior() [2/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::MarginalTargetedMNInference< 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::JointTargetedMNInference< GUM_SCALAR >.

Definition at line 246 of file marginalTargetedMNInference_tpl.h.

246  {
247  return posterior(this->MN().idFromName(nodeName));
248  }
virtual const IMarkovNet< GUM_SCALAR > & MN() const final
Returns a constant reference over the IMarkovNet referenced by this class.
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.

◆ posterior_()

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

asks derived classes for the posterior of a given variable

Parameters
idThe variable's id.

Implemented in gum::ShaferShenoyMNInference< GUM_SCALAR >.

◆ 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:

◆ setMN()

template<typename GUM_SCALAR >
void gum::MarkovNetInference< GUM_SCALAR >::setMN ( const IMarkovNet< GUM_SCALAR > *  mn)
virtualinherited

assigns a new MN to the inference engine

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

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

Definition at line 64 of file MarkovNetInference_tpl.h.

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

64  {
65  this->setModel_(mn);
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:

◆ setTargetedMode_()

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

Definition at line 320 of file marginalTargetedMNInference_tpl.h.

320  {
321  if (!_targeted_mode_) {
322  _targets_.clear();
323  _targeted_mode_ = true;
324  }
325  }
NodeSet _targets_
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:361
bool _targeted_mode_
whether the actual targets are default

◆ 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:

◆ targets()

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

returns the list of marginal targets

Definition at line 202 of file marginalTargetedMNInference_tpl.h.

202  {
203  return _targets_;
204  }
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::MarginalTargetedMNInference< GUM_SCALAR >::_targeted_mode_
private

whether the actual targets are default

Definition at line 239 of file marginalTargetedMNInference.h.

◆ _targets_

template<typename GUM_SCALAR >
NodeSet gum::MarginalTargetedMNInference< GUM_SCALAR >::_targets_
private

the set of marginal targets

Definition at line 242 of file marginalTargetedMNInference.h.


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