aGrUM  0.13.2
gum::BayesNetInference< GUM_SCALAR > Class Template Referenceabstract

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

#include <BayesNetInference.h>

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

Public Member Functions

Constructors / Destructors
 BayesNetInference (const IBayesNet< GUM_SCALAR > *bn)
 default constructor More...
 
 BayesNetInference ()
 default constructor with a null BN (useful for virtual inheritance) More...
 
virtual ~BayesNetInference ()
 destructor More...
 
Accessors / Modifiers
virtual void setBN (const IBayesNet< GUM_SCALAR > *bn)
 assigns a new BN to the inference engine More...
 
virtual const IBayesNet< GUM_SCALAR > & BN () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
virtual const NodeProperty< Size > & domainSizes () const final
 get the domain sizes of the random variables of the BN More...
 
virtual bool isInferenceReady () const noexceptfinal
 returns whether the inference object is in a ready state More...
 
virtual bool isInferenceOutdatedBNStructure () const noexceptfinal
 returns whether the inference object is in a OutdatedBNStructure state More...
 
virtual bool isInferenceOutdatedBNPotentials () const noexceptfinal
 returns whether the inference object is in a OutdatedBNPotential state More...
 
virtual bool isInferenceDone () const noexceptfinal
 returns whether the inference object is in a InferenceDone state More...
 
virtual bool isDone () const noexceptfinal
 returns whether the inference object is in a done 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 noexceptfinal
 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 hasHardEvidence (NodeId id) 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 hasEvidence (const std::string &nodeName) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasHardEvidence (const std::string &nodeName) const final
 indicates whether node id has received a hard 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::OutdatedBNStructure, StateOfInference::OutdatedBNPotentials, StateOfInference::ReadyForInference, StateOfInference::Done }
 current state of the inference More...
 

Protected Member Functions

virtual void _onStateChanged ()=0
 fired when the stage is changed More...
 
virtual void _onEvidenceAdded (NodeId id, bool isHardEvidence)=0
 fired after a new evidence is inserted More...
 
virtual void _onEvidenceErased (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 (NodeId id, bool hasChangedSoftHard)=0
 fired after an evidence is changed, in particular when its status (soft/hard) changes More...
 
virtual void _onBayesNetChanged (const IBayesNet< GUM_SCALAR > *bn)=0
 fired after a new Bayes net has been assigned to the engine More...
 
virtual void _updateOutdatedBNStructure ()=0
 prepares inference when the latter is in OutdatedBNStructure state More...
 
virtual void _updateOutdatedBNPotentials ()=0
 prepares inference when the latter is in OutdatedBNPotentials state More...
 
virtual void _makeInference ()=0
 called when the inference has to be performed effectively More...
 
void _setOutdatedBNStructureState ()
 put the inference into an outdated BN structure state More...
 
void _setOutdatedBNPotentialsState ()
 puts the inference into an OutdatedBNPotentials state if it is not already in an OutdatedBNStructure state More...
 

Detailed Description

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

<agrum/BN/inference/BayesNetInference.h>

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

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

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

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

BayesNetInference can be in one of 4 different states:

  • OutdatedBNStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from BayesNetInference may be smarter than BayesNetInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedBNPotentials 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.
  • OutdatedBNPotentials: in this state, the structure of the BN remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.
  • Ready4Inference: in this state, all the data structures are ready for inference. There just remains to perform the inference computations.
  • Done: the heavy computations of inference have been done. There might still remain a few light computations to perform to get the posterior potentials we need. Typically, in Lazy Propagation, all the messages in the join tree have been computed but, to get the potentials, we still need to perform the combinations of the potentials in the cliques with the messages sent to the cliques. In some inference algorithms, this step may even be empty.

Definition at line 139 of file BayesNetInference.h.

Member Enumeration Documentation

template<typename GUM_SCALAR >
enum gum::BayesNetInference::StateOfInference
strong

current state of the inference

BayesNetInference can be in one of 4 different states:

  • OutdatedBNStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from BayesNetInference may be smarter than BayesNetInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedBNPotentials 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.
  • OutdatedBNPotentials: in this state, the structure of the BN remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.
  • Ready4Inference: in this state, all the data structures are ready for inference. There just remains to perform the inference computations.
  • Done: the heavy computations of inference have been done. There might still remain a few light computations to perform to get the posterior potentials we need. Typically, in Lazy Propagation, all the messages in the join tree have been computed but, to get the potentials, we still need to perform the combinations of the potentials in the cliques with the messages sent to the cliques. In some inference algorithms, this step may even be empty.
Enumerator
OutdatedBNStructure 
OutdatedBNPotentials 
ReadyForInference 
Done 

Definition at line 180 of file BayesNetInference.h.

180  {
181  OutdatedBNStructure,
182  OutdatedBNPotentials,
183  ReadyForInference,
184  Done
185  };

Constructor & Destructor Documentation

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

default constructor

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

Definition at line 33 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes().

34  :
35  __bn(bn) {
37 
38  GUM_CONSTRUCTOR(BayesNetInference);
39  }
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void __computeDomainSizes()
computes the domain sizes of the random variables

+ Here is the call graph for this function:

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

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

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

Definition at line 44 of file BayesNetInference_tpl.h.

44  {
45  GUM_CONSTRUCTOR(BayesNetInference);
46  }
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)
template<typename GUM_SCALAR >
gum::BayesNetInference< GUM_SCALAR >::~BayesNetInference ( )
virtual

destructor

Definition at line 51 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

51  {
52  // clear all evidence.
53  // Warning: Do not use method eraseAllEvidence () because it contains a call
54  // to pure virtual method _onAllEvidenceErased which belongs to an inherited
55  // instance and, therefore, does not exist anymore when ~BayesNetInference ()
56  // is called
57  for (const auto& pair : __evidence) {
58  if (pair.second != nullptr) { delete (pair.second); }
59  }
60 
61  GUM_DESTRUCTOR(BayesNetInference);
62  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)

Member Function Documentation

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes ( )
private

computes the domain sizes of the random variables

Definition at line 158 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, and gum::BayesNetInference< GUM_SCALAR >::__domain_sizes.

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setBayesNetDuringConstruction(), gum::BayesNetInference< GUM_SCALAR >::BayesNetInference(), and gum::BayesNetInference< GUM_SCALAR >::setBN().

158  {
159  __domain_sizes.clear();
160  if (__bn != nullptr) {
161  for (const auto node : __bn->dag()) {
162  __domain_sizes.insert(node, __bn->variable(node).domainSize());
163  }
164  }
165  }
NodeProperty< Size > __domain_sizes
the domain sizes of the random variables
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::BayesNetInference< GUM_SCALAR >::__createHardEvidence ( NodeId  id,
Idx  val 
) const
private

create the internal structure for a hard evidence

Definition at line 183 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MultiDimDecorator< GUM_SCALAR >::beginMultipleChanges(), gum::Instantiation::chgVal(), and GUM_ERROR.

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

184  {
185  // check that it is possible to create the evidence
186  if (__bn == nullptr)
187  GUM_ERROR(NullElement,
188  "No Bayes net has been assigned to the "
189  "inference algorithm");
190 
191  if (!__bn->dag().exists(id)) {
192  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the bn");
193  }
194 
195  if (__bn->variable(id).domainSize() <= val) {
196  GUM_ERROR(InvalidArgument,
197  "node " << __bn->variable(id) << " has fewer possible values than "
198  << val);
199  }
200 
201  // create the deterministic potential
202  Potential< GUM_SCALAR > pot;
203  pot.beginMultipleChanges();
204  pot << __bn->variable(id);
205  pot.endMultipleChanges(0.0);
206 
207  Instantiation I(pot);
208  I.chgVal(__bn->variable(id), val);
209  pot.set(I, 1.0);
210 
211  return pot;
212  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
bool gum::BayesNetInference< GUM_SCALAR >::__isHardEvidence ( const Potential< GUM_SCALAR > &  pot,
Idx val 
) const
private

checks whether a potential corresponds to a hard evidence or not

Definition at line 217 of file BayesNetInference_tpl.h.

References gum::Instantiation::end(), GUM_ERROR, gum::Instantiation::inc(), gum::Instantiation::setFirst(), and gum::Instantiation::val().

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

218  {
219  // checking if pot is determininstic
220  bool notZero = false;
221  Instantiation I(pot);
222 
223  for (I.setFirst(); !I.end(); I.inc()) {
224  if (pot[I] != 0.0) {
225  if (notZero) { // we already met a non-zero value
226  return false;
227  } else {
228  val = I.val(0);
229  notZero = true; // this is the first met non-zero value
230  }
231  }
232  }
233 
234  if (!notZero) { // we met no non-zero value
235  GUM_ERROR(FatalError, "Evidence of impossibility (vector of 0s)");
236  }
237 
238  return true; // pot is deterministic
239  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

assigns a BN during the inference engine construction

Definition at line 140 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes(), gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::EvidenceInference< GUM_SCALAR >::EvidenceInference(), gum::JointTargetedInference< GUM_SCALAR >::JointTargetedInference(), and gum::MarginalTargetedInference< GUM_SCALAR >::MarginalTargetedInference().

141  {
142  __bn = bn;
145  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void __computeDomainSizes()
computes the domain sizes of the random variables

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::__setState ( const StateOfInference  state)
finalprivatevirtual

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

References gum::BayesNetInference< GUM_SCALAR >::__state, gum::BayesNetInference< GUM_SCALAR >::_onStateChanged(), and gum::BayesNetInference< GUM_SCALAR >::state().

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setBayesNetDuringConstruction(), gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNPotentialsState(), gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNStructureState(), gum::MarginalTargetedInference< GUM_SCALAR >::addAllTargets(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::clear(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), gum::JointTargetedInference< GUM_SCALAR >::eraseAllJointTargets(), gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), gum::JointTargetedInference< GUM_SCALAR >::eraseJointTarget(), gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::BayesNetInference< GUM_SCALAR >::prepareInference(), and gum::BayesNetInference< GUM_SCALAR >::setBN().

108  {
109  if (__state != state) {
110  __state = state;
111  _onStateChanged();
112  }
113  }
virtual void _onStateChanged()=0
fired when the stage is changed
StateOfInference __state
the current state of the inference (outdated/ready/done)
virtual StateOfInference state() const noexceptfinal
returns the state of the inference engine

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_makeInference ( )
protectedpure virtual

called when the inference has to be performed effectively

Once the inference is done, _fillPosterior can be called.

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::makeInference().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onAllEvidenceErased ( bool  contains_hard_evidence)
protectedpure virtual

fired before all the evidence are erased

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence().

+ Here is the caller graph for this function:

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

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

Implemented in gum::JointTargetedInference< GUM_SCALAR >, gum::SamplingInference< GUM_SCALAR >, gum::MarginalTargetedInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::setBN().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceAdded ( NodeId  id,
bool  isHardEvidence 
)
protectedpure virtual

fired after a new evidence is inserted

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceChanged ( NodeId  id,
bool  hasChangedSoftHard 
)
protectedpure virtual

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::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceErased ( NodeId  id,
bool  isHardEvidence 
)
protectedpure virtual

fired before an evidence is removed

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onStateChanged ( )
protectedpure virtual

fired when the stage is changed

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setState().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNPotentialsState ( )
protected

puts the inference into an OutdatedBNPotentials state if it is not already in an OutdatedBNStructure state

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

Definition at line 682 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials.

682  {
684  }
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:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNStructureState ( )
protected

put the inference into an outdated BN structure state

OutdatedBNStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from BayesNetInference may be smarter than BayesNetInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedBNPotentials 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 674 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

674  {
676  }
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:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNPotentials ( )
protectedpure virtual

prepares inference when the latter is in OutdatedBNPotentials state

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

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::prepareInference().

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNStructure ( )
protectedpure virtual

prepares inference when the latter is in OutdatedBNStructure state

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

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

Referenced by gum::BayesNetInference< GUM_SCALAR >::prepareInference().

+ Here is the caller graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::__createHardEvidence().

Referenced by gum::LoopySamplingInference< GUM_SCALAR, APPROX >::_makeInference(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::addListOfEvidence(), gum::BayesNetInference< GUM_SCALAR >::addSetOfEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

245  {
247  }
Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const
create the internal structure for a hard evidence
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:

+ Here is the caller graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

253  {
254  addEvidence(this->BN().idFromName(nodeName), val);
255  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

261  {
262  addEvidence(id, this->BN().variable(id)[label]);
263  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

269  {
270  NodeId id = this->BN().idFromName(nodeName);
271  addEvidence(id, this->BN().variable(id)[label]);
272  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and GUM_ERROR.

277  {
278  // checks that the evidence is meaningful
279  if (__bn == nullptr)
280  GUM_ERROR(NullElement,
281  "No Bayes net has been assigned to the "
282  "inference algorithm");
283 
284  if (!__bn->dag().exists(id)) {
285  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the bn");
286  }
287 
288  if (__bn->variable(id).domainSize() != vals.size()) {
289  GUM_ERROR(InvalidArgument,
290  "node " << __bn->variable(id)
291  << " and its evidence vector have different sizes.");
292  }
293 
294  Potential< GUM_SCALAR > pot;
295  pot.add(__bn->variable(id));
296  pot.fillWith(vals);
297  addEvidence(std::move(pot));
298  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

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

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 BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

303  {
304  addEvidence(this->BN().idFromName(nodeName), vals);
305  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

352  {
353  Potential< GUM_SCALAR > new_pot(pot);
354  addEvidence(std::move(new_pot));
355  }
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:

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( Potential< GUM_SCALAR > &&  pot)
finalvirtual

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 BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__isHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceAdded(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::Set< Key, Alloc >::insert(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

310  {
311  // check if the potential corresponds to an evidence
312  if (pot.nbrDim() != 1) {
313  GUM_ERROR(InvalidArgument, pot << " is not mono-dimensional.");
314  }
315  if (__bn == nullptr)
316  GUM_ERROR(NullElement,
317  "No Bayes net has been assigned to the "
318  "inference algorithm");
319 
320  NodeId id = __bn->nodeId(pot.variable(0));
321 
322  if (hasEvidence(id)) {
323  GUM_ERROR(InvalidArgument,
324  " node " << id
325  << " already has an evidence. Please use chgEvidence().");
326  }
327 
328  // check whether we have a hard evidence (and also check whether the
329  // potential only contains 0 (in this case, this will automatically raise
330  // an exception) )
331  Idx val;
332  bool is_hard_evidence = __isHardEvidence(pot, val);
333 
334  // insert the evidence
335  __evidence.insert(
336  id,
337  new Potential< GUM_SCALAR >(std::forward< Potential< GUM_SCALAR > >(pot)));
338  if (is_hard_evidence) { // pot is deterministic
339  __hard_evidence.insert(id, val);
341  } else {
343  }
345  _onEvidenceAdded(id, is_hard_evidence);
346  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual void _onEvidenceAdded(NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
bool __isHardEvidence(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
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
unsigned long Idx
Type for indexes.
Definition: types.h:43
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

361  {
362  for (const auto pot : potlist)
363  addEvidence(*pot);
364  }
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:

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

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

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

370  {
371  for (const auto pot : potset)
372  addEvidence(*pot);
373  }
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:

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

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

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

Definition at line 118 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, and GUM_ERROR.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::SamplingInference< GUM_SCALAR >::_posterior(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::SamplingInference< GUM_SCALAR >::contextualize(), gum::SamplingInference< GUM_SCALAR >::currentPosterior(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::MarginalTargetedInference< GUM_SCALAR >::H(), gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence(), gum::JointTargetedInference< GUM_SCALAR >::I(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), gum::JointTargetedInference< GUM_SCALAR >::posterior(), gum::SamplingInference< GUM_SCALAR >::samplingBN(), and gum::Estimator< GUM_SCALAR >::setFromLBP().

118  {
119  if (__bn == nullptr)
120  GUM_ERROR(UndefinedElement,
121  "No Bayes net has been assigned to "
122  "the inference algorithm.");
123  return *__bn;
124  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the caller graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::__createHardEvidence().

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

430  {
432  }
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:

+ Here is the caller graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

438  {
439  chgEvidence(this->BN().idFromName(nodeName), val);
440  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

446  {
447  chgEvidence(id, this->BN().variable(id)[label]);
448  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

454  {
455  NodeId id = this->BN().idFromName(nodeName);
456  chgEvidence(id, this->BN().variable(id)[label]);
457  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), and GUM_ERROR.

462  {
463  // check whether this corresponds to an evidence
464  if (__bn == nullptr)
465  GUM_ERROR(NullElement,
466  "No Bayes net has been assigned to the "
467  "inference algorithm");
468 
469  if (!__bn->dag().exists(id)) {
470  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the bn");
471  }
472 
473  if (__bn->variable(id).domainSize() != vals.size()) {
474  GUM_ERROR(InvalidArgument,
475  "node " << __bn->variable(id)
476  << " and its evidence have different sizes.");
477  }
478 
479  // create the potential corresponding to vals
480  Potential< GUM_SCALAR > pot;
481  pot.add(__bn->variable(id));
482  pot.fillWith(vals);
483  chgEvidence(pot);
484  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

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

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

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

489  {
490  chgEvidence(this->BN().idFromName(nodeName), vals);
491  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( const Potential< GUM_SCALAR > &  pot)
finalvirtual

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

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__isHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceChanged(), gum::Instantiation::end(), gum::Set< Key, Alloc >::erase(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::Instantiation::inc(), gum::Set< Key, Alloc >::insert(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure, gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

497  {
498  // check if the potential corresponds to an evidence
499  if (pot.nbrDim() != 1) {
500  GUM_ERROR(InvalidArgument, pot << " is not a mono-dimensional potential.");
501  }
502  if (__bn == nullptr)
503  GUM_ERROR(NullElement,
504  "No Bayes net has been assigned to the "
505  "inference algorithm");
506 
507  NodeId id = __bn->nodeId(pot.variable(0));
508 
509  if (!hasEvidence(id)) {
510  GUM_ERROR(InvalidArgument,
511  id << " has no evidence. Please use addEvidence().");
512  }
513 
514  // check whether we have a hard evidence (and also check whether the
515  // potential only contains 0 (in this case, this will automatically raise
516  // an exception) )
517  Idx val;
518  bool is_hard_evidence = __isHardEvidence(pot, val);
519 
520  // modify the evidence already stored
521  const Potential< GUM_SCALAR >* localPot = __evidence[id];
522  Instantiation I(pot);
523  for (I.setFirst(); !I.end(); I.inc()) {
524  localPot->set(I, pot[I]);
525  }
526 
527  // the inference state will be different
528  // whether evidence change from Hard to Soft or not.
529  bool hasChangedSoftHard = false;
530 
531  if (is_hard_evidence) {
532  if (!hasHardEvidence(id)) {
533  hasChangedSoftHard = true;
534  __hard_evidence.insert(id, val);
537  } else {
538  __hard_evidence[id] = val;
539  }
540  } else {
541  if (hasHardEvidence(id)) { // evidence was hard
542  __hard_evidence.erase(id);
545  hasChangedSoftHard = true;
546  }
547  }
548 
549  if (hasChangedSoftHard) {
551  } else {
554  }
555  }
556 
557  _onEvidenceChanged(id, hasChangedSoftHard);
558  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard)=0
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
bool __isHardEvidence(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
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 bool isInferenceOutdatedBNStructure() const noexceptfinal
returns whether the inference object is in a OutdatedBNStructure state
unsigned long Idx
Type for indexes.
Definition: types.h:43
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::clear ( )
virtual

clears all the data structures allocated for the last inference

Definition at line 150 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::setBN().

150  {
153  }
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:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE const NodeProperty< Size > & gum::BayesNetInference< GUM_SCALAR >::domainSizes ( ) const
finalvirtual

get the domain sizes of the random variables of the BN

Definition at line 171 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__domain_sizes.

171  {
172  return __domain_sizes;
173  }
NodeProperty< Size > __domain_sizes
the domain sizes of the random variables
template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence ( )
finalvirtual

removes all the evidence entered into the network

Definition at line 592 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onAllEvidenceErased(), gum::Set< Key, Alloc >::clear(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::clear(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

592  {
593  bool has_hard_evidence = !__hard_evidence.empty();
594  this->_onAllEvidenceErased(has_hard_evidence);
595 
596  for (const auto& pair : __evidence) {
597  if (pair.second != nullptr) { delete (pair.second); }
598  }
599 
600  __evidence.clear();
601  __hard_evidence.clear();
604 
605  if (has_hard_evidence) {
607  } else {
610  }
611  }
612  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual void _onAllEvidenceErased(bool contains_hard_evidence)=0
fired before all the evidence are erased
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
virtual bool isInferenceOutdatedBNStructure() const noexceptfinal
returns whether the inference object is in a OutdatedBNStructure state
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::eraseEvidence ( NodeId  id)
finalvirtual

removed the evidence, if any, corresponding to node id

Definition at line 563 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceErased(), gum::Set< Key, Alloc >::erase(), gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

563  {
564  if (hasEvidence(id)) {
565  if (hasHardEvidence(id)) {
566  _onEvidenceErased(id, true);
567  __hard_evidence.erase(id);
570  } else {
571  _onEvidenceErased(id, false);
575  }
576  }
577 
578  delete (__evidence[id]);
579  __evidence.erase(id);
580  }
581  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
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:656
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
virtual void _onEvidenceErased(NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
virtual bool isInferenceOutdatedBNStructure() const noexceptfinal
returns whether the inference object is in a OutdatedBNStructure state

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

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

Definition at line 585 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

585  {
586  eraseEvidence(this->BN().idFromName(nodeName));
587  }
virtual const IBayesNet< GUM_SCALAR > & BN() 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:

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

returns the set of evidence

Definition at line 647 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

647  {
648  return __evidence;
649  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network

+ Here is the caller graph for this function:

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

indicate for each node with hard evidence which value it took

Definition at line 639 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), and gum::SamplingInference< GUM_SCALAR >::contextualize().

639  {
640  return __hard_evidence;
641  }
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value

+ Here is the caller graph for this function:

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

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 663 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), gum::SamplingInference< GUM_SCALAR >::_setEstimatorFromBN(), gum::SamplingInference< GUM_SCALAR >::_setEstimatorFromLBP(), gum::SamplingInference< GUM_SCALAR >::contextualize(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

663  {
664  return __hard_evidence_nodes;
665  }
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( ) const
finalvirtual

indicates whether some node(s) have received evidence

Definition at line 378 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), and gum::BayesNetInference< GUM_SCALAR >::hasEvidence().

378  {
379  return !__evidence.empty();
380  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( NodeId  id) const
finalvirtual

indicates whether node id has received an evidence

Definition at line 385 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

385  {
386  return __evidence.exists(id);
387  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( const std::string &  nodeName) const
finalvirtual

indicates whether node id has received an evidence

Definition at line 406 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasEvidence().

407  {
408  return hasEvidence(this->BN().idFromName(nodeName));
409  }
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence ( NodeId  id) const
finalvirtual

indicates whether node id has received a hard evidence

Definition at line 392 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, and gum::Set< Key, Alloc >::exists().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), and gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence().

392  {
393  return __hard_evidence_nodes.exists(id);
394  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

indicates whether node id has received a hard evidence

Definition at line 414 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence().

415  {
416  return hasHardEvidence(this->BN().idFromName(nodeName));
417  }
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence ( NodeId  id) const
finalvirtual

indicates whether node id has received a soft evidence

Definition at line 399 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, and gum::Set< Key, Alloc >::exists().

Referenced by gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence().

399  {
400  return __soft_evidence_nodes.exists(id);
401  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

indicates whether node id has received a soft evidence

Definition at line 422 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence().

423  {
424  return hasSoftEvidence(this->BN().idFromName(nodeName));
425  }
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isDone ( ) const
finalvirtualnoexcept

returns whether the inference object is in a done 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 93 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::Done.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

93  {
94  return (__state == StateOfInference::Done);
95  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceDone ( ) const
finalvirtualnoexcept

returns whether the inference object is in a InferenceDone state

Definition at line 86 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::Done.

86  {
87  return (__state == StateOfInference::Done);
88  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNPotentials ( ) const
finalvirtualnoexcept

returns whether the inference object is in a OutdatedBNPotential state

Definition at line 80 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials.

81  {
83  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure ( ) const
finalvirtualnoexcept

returns whether the inference object is in a OutdatedBNStructure state

Definition at line 73 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), and gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

74  {
76  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceReady ( ) const
finalvirtualnoexcept

returns whether the inference object is in a ready state

Definition at line 67 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::ReadyForInference.

Referenced by gum::SamplingInference< GUM_SCALAR >::_onStateChanged(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

67  {
69  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::makeInference ( )
finalvirtual

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

References gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::_makeInference(), gum::BayesNetInference< GUM_SCALAR >::Done, gum::BayesNetInference< GUM_SCALAR >::isDone(), gum::BayesNetInference< GUM_SCALAR >::isInferenceReady(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

Referenced by gum::LoopySamplingInference< GUM_SCALAR, APPROX >::_makeInference(), gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

708  {
709  if (isDone()) { return; }
710 
711  if (!isInferenceReady()) { prepareInference(); }
712 
713  _makeInference();
714 
716  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual bool isInferenceReady() const noexceptfinal
returns whether the inference object is in a ready state
virtual void _makeInference()=0
called when the inference has to be performed effectively
virtual void prepareInference() final
prepare the internal inference structures for the next inference
virtual bool isDone() const noexceptfinal
returns whether the inference object is in a done state

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrEvidence ( ) const
finalvirtual

returns the number of evidence entered into the Bayesian network

Definition at line 617 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

617  {
618  return __evidence.size();
619  }
NodeProperty< const Potential< GUM_SCALAR > * > __evidence
the set of evidence entered into the network
template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrHardEvidence ( ) const
finalvirtual

returns the number of hard evidence entered into the Bayesian network

Definition at line 624 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, and gum::Set< Key, Alloc >::size().

624  {
625  return __hard_evidence_nodes.size();
626  }
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:701

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrSoftEvidence ( ) const
finalvirtual

returns the number of soft evidence entered into the Bayesian network

Definition at line 631 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, and gum::Set< Key, Alloc >::size().

631  {
632  return __soft_evidence_nodes.size();
633  }
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:701

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::prepareInference ( )
finalvirtual

prepare the internal inference structures for the next inference

Definition at line 689 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__state, gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNPotentials(), gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNStructure(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::isDone(), gum::BayesNetInference< GUM_SCALAR >::isInferenceReady(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure, and gum::BayesNetInference< GUM_SCALAR >::ReadyForInference.

Referenced by gum::BayesNetInference< GUM_SCALAR >::makeInference(), and gum::SamplingInference< GUM_SCALAR >::samplingBN().

689  {
690  if (isInferenceReady() || isDone()) { return; }
691 
692  if (__bn == nullptr)
693  GUM_ERROR(NullElement,
694  "No Bayes net has been assigned to the "
695  "inference algorithm");
696 
699  else
701 
703  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
StateOfInference __state
the current state of the inference (outdated/ready/done)
virtual bool isInferenceReady() const noexceptfinal
returns whether the inference object is in a ready state
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void _updateOutdatedBNPotentials()=0
prepares inference when the latter is in OutdatedBNPotentials state
virtual void _updateOutdatedBNStructure()=0
prepares inference when the latter is in OutdatedBNStructure state
virtual bool isDone() const noexceptfinal
returns whether the inference object is in a done state
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

assigns a new BN to the inference engine

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

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

Definition at line 129 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::_onBayesNetChanged(), gum::BayesNetInference< GUM_SCALAR >::clear(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

129  {
130  clear();
131  __bn = bn;
133  _onBayesNetChanged(bn);
135  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)=0
fired after a new Bayes net has been assigned to the engine
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void clear()
clears all the data structures allocated for the last inference
void __computeDomainSizes()
computes the domain sizes of the random variables

+ Here is the call graph for this function:

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

returns the set of nodes with soft evidence

the set of nodes that received soft evidence

Definition at line 655 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes.

Referenced by gum::SamplingInference< GUM_SCALAR >::contextualize().

655  {
656  return __soft_evidence_nodes;
657  }
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE BayesNetInference< GUM_SCALAR >::StateOfInference gum::BayesNetInference< GUM_SCALAR >::state ( ) const
finalvirtualnoexcept

returns the state of the inference engine

Definition at line 101 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state.

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setState().

101  {
102  return __state;
103  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

+ Here is the caller graph for this function:

Member Data Documentation

template<typename GUM_SCALAR >
NodeProperty< Size > gum::BayesNetInference< GUM_SCALAR >::__domain_sizes
private

the domain sizes of the random variables

Definition at line 583 of file BayesNetInference.h.

Referenced by gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes(), and gum::BayesNetInference< GUM_SCALAR >::domainSizes().

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

Definition at line 618 of file BayesNetInference.h.

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

Definition at line 617 of file BayesNetInference.h.

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

allow JointInference to access the single targets and inference states

Definition at line 616 of file BayesNetInference.h.


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