aGrUM  0.20.2
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

◆ addAllTargets()

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

adds all nodes as targets

Definition at line 137 of file marginalTargetedMNInference_tpl.h.

137  {
138  // check if the node belongs to the Markov network
139  if (this->hasNoModel_())
140  GUM_ERROR(NullElement,
141  "No Markov net has been assigned to the "
142  "inference algorithm");
143 
144 
145  setTargetedMode_(); // does nothing if already in targeted mode
146  for (const auto target: this->MN().graph()) {
147  if (!targets__.contains(target)) {
148  targets__.insert(target);
149  onMarginalTargetAdded_(target);
150  this->setState_(
152  }
153  }
154  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
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:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addEvidence() [1/8]

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

adds a new hard evidence on node id

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

Definition at line 243 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [2/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 250 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [3/8]

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

adds a new hard evidence on node id

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

Definition at line 259 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [4/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 266 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [5/8]

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

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

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

Definition at line 275 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [6/8]

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

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

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

Definition at line 302 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [7/8]

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

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

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

Definition at line 352 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [8/8]

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

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

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

Definition at line 310 of file graphicalModelInference_tpl.h.

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

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

◆ addListOfEvidence()

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

adds a new list of evidence

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

Definition at line 361 of file graphicalModelInference_tpl.h.

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

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

◆ addSetOfEvidence()

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

adds a new set of evidence

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

Definition at line 370 of file graphicalModelInference_tpl.h.

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

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

◆ addTarget() [1/2]

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

112  {
113  // check if the node belongs to the Markov network
114  if (this->hasNoModel_())
115  GUM_ERROR(NullElement,
116  "No Markov net has been assigned to the "
117  "inference algorithm");
118 
119  if (!this->MN().graph().exists(target)) {
120  GUM_ERROR(UndefinedElement,
121  target << " is not a NodeId in the Markov network");
122  }
123 
124  setTargetedMode_(); // does nothing if already in targeted mode
125  // add the new target
126  if (!targets__.contains(target)) {
127  targets__.insert(target);
128  onMarginalTargetAdded_(target);
129  this->setState_(
131  }
132  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
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:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 159 of file marginalTargetedMNInference_tpl.h.

160  {
161  // check if the node belongs to the Markov network
162  if (this->hasNoModel_())
163  GUM_ERROR(NullElement,
164  "No Markov net has been assigned to the "
165  "inference algorithm");
166 
167  addTarget(this->MN().idFromName(nodeName));
168  }
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:54

◆ chgEvidence() [1/7]

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

change the value of an already existing hard evidence

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

Definition at line 432 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [2/7]

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

change the value of an already existing hard evidence

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

Definition at line 439 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [3/7]

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

change the value of an already existing hard evidence

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

Definition at line 448 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [4/7]

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

change the value of an already existing hard evidence

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

Definition at line 455 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [5/7]

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

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

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

Definition at line 464 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [6/7]

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

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

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

Definition at line 492 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [7/7]

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

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

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

Definition at line 501 of file graphicalModelInference_tpl.h.

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

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

◆ clear()

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

clears all the data structures allocated for the last inference

Reimplemented in gum::ShaferShenoyLIMIDInference< GUM_SCALAR >.

Definition at line 147 of file graphicalModelInference_tpl.h.

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

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

◆ domainSizes()

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

get the domain sizes of the random variables of the model

Definition at line 168 of file graphicalModelInference_tpl.h.

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

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

◆ eraseAllEvidence()

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

removes all the evidence entered into the network

Definition at line 597 of file graphicalModelInference_tpl.h.

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

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

◆ eraseAllTargets()

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

Clear all previously defined targets.

Reimplemented in gum::JointTargetedMNInference< GUM_SCALAR >.

Definition at line 99 of file marginalTargetedMNInference_tpl.h.

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

◆ eraseEvidence() [1/2]

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

removed the evidence, if any, corresponding to node id

Definition at line 568 of file graphicalModelInference_tpl.h.

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

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

◆ eraseEvidence() [2/2]

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

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

Definition at line 589 of file graphicalModelInference_tpl.h.

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

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

◆ eraseTarget() [1/2]

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

173  {
174  // check if the node belongs to the Markov network
175  if (this->hasNoModel_())
176  GUM_ERROR(NullElement,
177  "No Markov net has been assigned to the "
178  "inference algorithm");
179 
180  if (!this->MN().graph().exists(target)) {
181  GUM_ERROR(UndefinedElement,
182  target << " is not a NodeId in the Markov network");
183  }
184 
185 
186  if (targets__.contains(target)) {
187  targeted_mode__ = true; // we do not use setTargetedMode_ because we do not
188  // want to clear the targets
189  onMarginalTargetErased_(target);
190  targets__.erase(target);
191  this->setState_(
193  }
194  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
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:675
NodeSet targets__
the set of marginal targets
bool targeted_mode__
whether the actual targets are default
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
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 199 of file marginalTargetedMNInference_tpl.h.

200  {
201  // check if the node belongs to the Markov network
202  if (this->hasNoModel_())
203  GUM_ERROR(NullElement,
204  "No Markov net has been assigned to the "
205  "inference algorithm");
206 
207  eraseTarget(this->MN().idFromName(nodeName));
208  }
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:54

◆ evidence()

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

returns the set of evidence

Definition at line 652 of file graphicalModelInference_tpl.h.

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

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

◆ evidenceImpact() [1/2]

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

289  {
290  const auto& vtarget = this->MN().variable(target);
291 
292  if (evs.contains(target)) {
293  GUM_ERROR(InvalidArgument,
294  "Target <" << vtarget.name() << "> (" << target
295  << ") can not be in evs (" << evs << ").");
296  }
297  auto condset = this->MN().minimalCondSet(target, evs);
298 
299  Potential< GUM_SCALAR > res;
300  this->eraseAllTargets();
301  this->eraseAllEvidence();
302  res.add(this->MN().variable(target));
303  this->addTarget(target);
304  for (const auto& n: condset) {
305  res.add(this->MN().variable(n));
306  this->addEvidence(n, 0);
307  }
308 
309  Instantiation inst(res);
310  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
311  // inferring
312  for (const auto& n: condset)
313  this->chgEvidence(n, inst.val(this->MN().variable(n)));
314  this->makeInference();
315  // populate res
316  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
317  res.set(inst, this->posterior(target)[inst]);
318  }
319  inst.setFirstVar(vtarget); // remove inst.end() flag
320  }
321 
322  return res;
323  }
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:54

◆ 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 328 of file marginalTargetedMNInference_tpl.h.

330  {
331  const auto& mn = this->MN();
332  return evidenceImpact(mn.idFromName(target), mn.nodeset(evs));
333  }
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 271 of file marginalTargetedMNInference_tpl.h.

271  {
272  return posterior(X).entropy();
273  }
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 280 of file marginalTargetedMNInference_tpl.h.

280  {
281  return H(this->MN().idFromName(nodeName));
282  }
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 644 of file graphicalModelInference_tpl.h.

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

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

◆ hardEvidenceNodes()

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

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 668 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [1/3]

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

indicates whether some node(s) have received evidence

Definition at line 379 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [2/3]

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

indicates whether node id has received an evidence

Definition at line 386 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [3/3]

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

indicates whether node id has received an evidence

Definition at line 409 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [1/2]

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

indicates whether node id has received a hard evidence

Definition at line 394 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [2/2]

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

indicates whether node id has received a hard evidence

Definition at line 417 of file graphicalModelInference_tpl.h.

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

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

◆ hasNoModel_()

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

Definition at line 533 of file graphicalModelInference.h.

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

◆ hasSoftEvidence() [1/2]

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

indicates whether node id has received a soft evidence

Definition at line 402 of file graphicalModelInference_tpl.h.

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

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

◆ hasSoftEvidence() [2/2]

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

indicates whether node id has received a soft evidence

Definition at line 425 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceDone()

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

returns whether the inference object is in a InferenceDone state

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

Definition at line 89 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceOutdatedPotentials()

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

returns whether the inference object is in a OutdatedPotential state

Definition at line 82 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceOutdatedStructure()

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

returns whether the inference object is in a OutdatedStructure state

Definition at line 75 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceReady()

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

returns whether the inference object is in a ready state

Definition at line 70 of file graphicalModelInference_tpl.h.

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

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

◆ isTarget() [1/2]

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

return true if variable is a (marginal) target

Definition at line 75 of file marginalTargetedMNInference_tpl.h.

75  {
76  // check that the variable belongs to the mn
77  if (this->hasNoModel_())
78  GUM_ERROR(NullElement,
79  "No Markov net has been assigned to the "
80  "inference algorithm");
81  if (!this->MN().graph().exists(node)) {
82  GUM_ERROR(UndefinedElement,
83  node << " is not a NodeId in the Markov network");
84  }
85 
86  return targets__.contains(node);
87  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
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:54

◆ 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 91 of file marginalTargetedMNInference_tpl.h.

92  {
93  return isTarget(this->MN().idFromName(nodeName));
94  }
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 337 of file marginalTargetedMNInference_tpl.h.

337  {
338  return targeted_mode__;
339  }
bool targeted_mode__
whether the actual targets are default

◆ makeInference()

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

perform the heavy computations needed to compute the targets' posteriors

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

Definition at line 714 of file graphicalModelInference_tpl.h.

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

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

◆ makeInference_()

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

◆ 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 59 of file MarkovNetInference_tpl.h.

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

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

◆ model()

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

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

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

Definition at line 114 of file graphicalModelInference_tpl.h.

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

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

◆ nbrEvidence()

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

returns the number of evidence entered into the Bayesian network

Definition at line 622 of file graphicalModelInference_tpl.h.

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

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

◆ nbrHardEvidence()

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

returns the number of hard evidence entered into the Bayesian network

Definition at line 629 of file graphicalModelInference_tpl.h.

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

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

◆ nbrSoftEvidence()

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

returns the number of soft evidence entered into the Bayesian network

Definition at line 636 of file graphicalModelInference_tpl.h.

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

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

◆ nbrTargets()

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

returns the number of marginal targets

Definition at line 221 of file marginalTargetedMNInference_tpl.h.

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

◆ onAllEvidenceErased_()

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

◆ onAllMarginalTargetsAdded_()

template<typename GUM_SCALAR >
virtual void gum::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.

62  {
63  targeted_mode__ = true;
65  }
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 244 of file marginalTargetedMNInference_tpl.h.

244  {
245  if (this->hardEvidenceNodes().contains(node)) {
246  return *(this->evidence()[node]);
247  }
248 
249  if (!isTarget(node)) {
250  // throws UndefinedElement if var is not a target
251  GUM_ERROR(UndefinedElement, node << " is not a target node");
252  }
253 
254  if (!this->isInferenceDone()) { this->makeInference(); }
255 
256  return posterior_(node);
257  }
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:54

◆ 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 262 of file marginalTargetedMNInference_tpl.h.

263  {
264  return posterior(this->MN().idFromName(nodeName));
265  }
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 695 of file graphicalModelInference_tpl.h.

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

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

◆ setAllMarginalTargets__()

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

sets all the nodes of the Markov net as targets

Definition at line 228 of file marginalTargetedMNInference_tpl.h.

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

◆ 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 67 of file MarkovNetInference_tpl.h.

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

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

◆ setModel_()

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

Definition at line 125 of file graphicalModelInference_tpl.h.

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

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

◆ setModelDuringConstruction_()

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

assigns a model during the inference engine construction

Definition at line 137 of file graphicalModelInference_tpl.h.

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

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

◆ setOutdatedPotentialsState_()

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

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

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

Definition at line 688 of file graphicalModelInference_tpl.h.

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

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

◆ setOutdatedStructureState_()

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

put the inference into an outdated model structure state

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

Definition at line 679 of file graphicalModelInference_tpl.h.

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

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

◆ setState_()

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

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

Definition at line 103 of file graphicalModelInference_tpl.h.

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

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

◆ setTargetedMode_()

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

Definition at line 341 of file marginalTargetedMNInference_tpl.h.

341  {
342  if (!targeted_mode__) {
343  targets__.clear();
344  targeted_mode__ = true;
345  }
346  }
NodeSet targets__
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:374
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 660 of file graphicalModelInference_tpl.h.

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

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

◆ state()

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

returns the state of the inference engine

Definition at line 97 of file graphicalModelInference_tpl.h.

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

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

◆ targets()

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

returns the list of marginal targets

Definition at line 214 of file marginalTargetedMNInference_tpl.h.

214  {
215  return targets__;
216  }
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 240 of file marginalTargetedMNInference.h.

◆ targets__

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

the set of marginal targets

Definition at line 243 of file marginalTargetedMNInference.h.


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