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

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

#include <samplingInference.h>

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

Public Attributes

Signaler3< Size, double, doubleonProgress
 Progression, error and time. More...
 
Signaler1< std::string > onStop
 Criteria messageApproximationScheme. More...
 

Public Member Functions

virtual void contextualize ()
 Simplifying the Bayesian network with relevance reasonning to lighten the computational charge. More...
 
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
 SamplingInference (const IBayesNet< GUM_SCALAR > *bn)
 default constructor More...
 
 ~SamplingInference () override
 destructor More...
 
const Potential< GUM_SCALAR > & currentPosterior (NodeId id)
 Computes and returns the actual estimation of the posterior of a node. More...
 
const Potential< GUM_SCALAR > & currentPosterior (const std::string &name)
 Computes and returns the actual estimation of the posterior of a node by its name. More...
 
Probability computations
const IBayesNet< GUM_SCALAR > & samplingBN ()
 get the BayesNet which is used to really perform the sampling More...
 
const Potential< GUM_SCALAR > & posterior_ (NodeId id) override
 Computes and returns the posterior of a node. More...
 
Estimator objects initializing
virtual void setEstimatorFromBN_ ()
 Initializes the estimators object linked to the simulation. More...
 
virtual void setEstimatorFromLBP_ (LoopyBeliefPropagation< GUM_SCALAR > *lbp, GUM_SCALAR virtualLBPSize)
 Initializes the estimators object linked to the simulation. More...
 
Probability computations
virtual const Potential< GUM_SCALAR > & posterior (NodeId node)
 Computes and returns the posterior of a node. More...
 
virtual const Potential< GUM_SCALAR > & posterior (const std::string &nodeName)
 Computes and returns the posterior of a node. More...
 
Targets
virtual void eraseAllTargets ()
 Clear all previously defined targets. More...
 
virtual void addAllTargets () final
 adds all nodes as targets More...
 
virtual void addTarget (NodeId target) final
 Add a marginal target to the list of targets. More...
 
virtual void addTarget (const std::string &nodeName) final
 Add a marginal target to the list of targets. More...
 
virtual void eraseTarget (NodeId target) final
 removes an existing (marginal) target More...
 
virtual void eraseTarget (const std::string &nodeName) final
 removes an existing (marginal) target More...
 
virtual bool isTarget (NodeId node) const final
 return true if variable is a (marginal) target More...
 
virtual bool isTarget (const std::string &nodeName) const final
 return true if variable is a (marginal) target More...
 
virtual const Size nbrTargets () const noexcept final
 returns the number of marginal targets More...
 
virtual const NodeSettargets () const noexcept final
 returns the list of marginal targets More...
 
Information Theory related functions
virtual GUM_SCALAR H (NodeId X) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
virtual GUM_SCALAR H (const std::string &nodeName) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
Accessors / Modifiers
virtual void setBN (const IBayesNet< GUM_SCALAR > *bn)
 assigns a new BN to the inference engine More...
 
virtual const IBayesNet< GUM_SCALAR > & BN () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
Accessors / Modifiers
virtual const GraphicalModelmodel () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
virtual const NodeProperty< Size > & domainSizes () const final
 get the domain sizes of the random variables of the model More...
 
virtual bool isInferenceReady () const noexcept final
 returns whether the inference object is in a ready state More...
 
virtual bool isInferenceOutdatedStructure () const noexcept final
 returns whether the inference object is in a OutdatedStructure state More...
 
virtual bool isInferenceOutdatedPotentials () const noexcept final
 returns whether the inference object is in a OutdatedPotential state More...
 
virtual bool isInferenceDone () const noexcept final
 returns whether the inference object is in a InferenceDone state More...
 
virtual void prepareInference () final
 prepare the internal inference structures for the next inference More...
 
virtual void makeInference () final
 perform the heavy computations needed to compute the targets' posteriors More...
 
virtual void clear ()
 clears all the data structures allocated for the last inference More...
 
virtual StateOfInference state () const noexcept final
 returns the state of the inference engine More...
 
Evidence
virtual void addEvidence (NodeId id, const Idx val) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const Idx val) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::string &label) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const std::string &label) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node named nodeName (might be soft or hard) More...
 
virtual void addEvidence (const Potential< GUM_SCALAR > &pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (Potential< GUM_SCALAR > &&pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addSetOfEvidence (const Set< const Potential< GUM_SCALAR > * > &potset) final
 adds a new set of evidence More...
 
virtual void addListOfEvidence (const List< const Potential< GUM_SCALAR > * > &potlist) final
 adds a new list of evidence More...
 
virtual void chgEvidence (NodeId id, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const Potential< GUM_SCALAR > &pot) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void eraseAllEvidence () final
 removes all the evidence entered into the network More...
 
virtual void eraseEvidence (NodeId id) final
 removed the evidence, if any, corresponding to node id More...
 
virtual void eraseEvidence (const std::string &nodeName) final
 removed the evidence, if any, corresponding to node of name nodeName More...
 
virtual bool hasEvidence () const final
 indicates whether some node(s) have received evidence More...
 
virtual bool hasEvidence (NodeId id) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasEvidence (const std::string &nodeName) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasHardEvidence (NodeId id) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasHardEvidence (const std::string &nodeName) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasSoftEvidence (NodeId id) const final
 indicates whether node id has received a soft evidence More...
 
virtual bool hasSoftEvidence (const std::string &nodeName) const final
 indicates whether node id has received a soft evidence More...
 
virtual Size nbrEvidence () const final
 returns the number of evidence entered into the Bayesian network More...
 
virtual Size nbrHardEvidence () const final
 returns the number of hard evidence entered into the Bayesian network More...
 
virtual Size nbrSoftEvidence () const final
 returns the number of soft evidence entered into the Bayesian network More...
 
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence () const
 returns the set of evidence More...
 
const NodeSetsoftEvidenceNodes () const
 returns the set of nodes with soft evidence More...
 
const NodeSethardEvidenceNodes () const
 returns the set of nodes with hard evidence More...
 
const NodeProperty< Idx > & hardEvidence () const
 indicate for each node with hard evidence which value it took More...
 
Getters and setters
void setEpsilon (double eps)
 Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|. More...
 
double epsilon () const
 Returns the value of epsilon. More...
 
void disableEpsilon ()
 Disable stopping criterion on epsilon. More...
 
void enableEpsilon ()
 Enable stopping criterion on epsilon. More...
 
bool isEnabledEpsilon () const
 Returns true if stopping criterion on epsilon is enabled, false otherwise. More...
 
void setMinEpsilonRate (double rate)
 Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|). More...
 
double minEpsilonRate () const
 Returns the value of the minimal epsilon rate. More...
 
void disableMinEpsilonRate ()
 Disable stopping criterion on epsilon rate. More...
 
void enableMinEpsilonRate ()
 Enable stopping criterion on epsilon rate. More...
 
bool isEnabledMinEpsilonRate () const
 Returns true if stopping criterion on epsilon rate is enabled, false otherwise. More...
 
void setMaxIter (Size max)
 Stopping criterion on number of iterations. More...
 
Size maxIter () const
 Returns the criterion on number of iterations. More...
 
void disableMaxIter ()
 Disable stopping criterion on max iterations. More...
 
void enableMaxIter ()
 Enable stopping criterion on max iterations. More...
 
bool isEnabledMaxIter () const
 Returns true if stopping criterion on max iterations is enabled, false otherwise. More...
 
void setMaxTime (double timeout)
 Stopping criterion on timeout. More...
 
double maxTime () const
 Returns the timeout (in seconds). More...
 
double currentTime () const
 Returns the current running time in second. More...
 
void disableMaxTime ()
 Disable stopping criterion on timeout. More...
 
void enableMaxTime ()
 Enable stopping criterion on timeout. More...
 
bool isEnabledMaxTime () const
 Returns true if stopping criterion on timeout is enabled, false otherwise. More...
 
void setPeriodSize (Size p)
 How many samples between two stopping is enable. More...
 
Size periodSize () const
 Returns the period size. More...
 
void setVerbosity (bool v)
 Set the verbosity on (true) or off (false). More...
 
bool verbosity () const
 Returns true if verbosity is enabled. More...
 
ApproximationSchemeSTATE stateApproximationScheme () const
 Returns the approximation scheme state. More...
 
Size nbrIterations () const
 Returns the number of iterations. More...
 
const std::vector< double > & history () const
 Returns the scheme history. More...
 
void initApproximationScheme ()
 Initialise the scheme. More...
 
bool startOfPeriod ()
 Returns true if we are at the beginning of a period (compute error is mandatory). More...
 
void updateApproximationScheme (unsigned int incr=1)
 Update the scheme w.r.t the new error and increment steps. More...
 
Size remainingBurnIn ()
 Returns the remaining burn in. More...
 
void stopApproximationScheme ()
 Stop the approximation scheme. More...
 
bool continueApproximationScheme (double error)
 Update the scheme w.r.t the new error. More...
 
Getters and setters
std::string messageApproximationScheme () const
 Returns the approximation scheme message. More...
 

Public Types

enum  StateOfInference { StateOfInference::OutdatedStructure, StateOfInference::OutdatedPotentials, StateOfInference::ReadyForInference, StateOfInference::Done }
 current state of the inference More...
 
enum  ApproximationSchemeSTATE : char {
  ApproximationSchemeSTATE::Undefined, ApproximationSchemeSTATE::Continue, ApproximationSchemeSTATE::Epsilon, ApproximationSchemeSTATE::Rate,
  ApproximationSchemeSTATE::Limit, ApproximationSchemeSTATE::TimeLimit, ApproximationSchemeSTATE::Stopped
}
 The different state of an approximation scheme. More...
 

Protected Attributes

Estimator< GUM_SCALAR > estimator__
 Estimator object designed to approximate target posteriors. More...
 
bool isSetEstimator = false
 whether the Estimator object has been initialized More...
 
bool isContextualized = false
 whether the referenced Bayesian network has been "contextualized" More...
 
double current_epsilon_
 Current epsilon. More...
 
double last_epsilon_
 Last epsilon value. More...
 
double current_rate_
 Current rate. More...
 
Size current_step_
 The current step. More...
 
Timer timer_
 The timer. More...
 
ApproximationSchemeSTATE current_state_
 The current state. More...
 
std::vector< doublehistory_
 The scheme history, used only if verbosity == true. More...
 
double eps_
 Threshold for convergence. More...
 
bool enabled_eps_
 If true, the threshold convergence is enabled. More...
 
double min_rate_eps_
 Threshold for the epsilon rate. More...
 
bool enabled_min_rate_eps_
 If true, the minimal threshold for epsilon rate is enabled. More...
 
double max_time_
 The timeout. More...
 
bool enabled_max_time_
 If true, the timeout is enabled. More...
 
Size max_iter_
 The maximum iterations. More...
 
bool enabled_max_iter_
 If true, the maximum iterations stopping criterion is enabled. More...
 
Size burn_in_
 Number of iterations before checking stopping criteria. More...
 
Size period_size_
 Checking criteria frequency. More...
 
bool verbosity_
 If true, verbosity is enabled. More...
 

Protected Member Functions

virtual Instantiation burnIn_ ()=0
 draws samples without updating the estimators More...
 
virtual Instantiation draw_ (GUM_SCALAR *w, Instantiation prev)=0
 draws a sample in the Bayesian network given a previous one More...
 
void makeInference_ () override
 makes the inference by generating samples More...
 
void loopApproxInference_ ()
 
virtual void addVarSample_ (NodeId nod, Instantiation *I)
 adds a node to current instantiation More...
 
virtual void onContextualize_ (BayesNetFragment< GUM_SCALAR > *bn)
 fired when Bayesian network is contextualized More...
 
void onEvidenceAdded_ (const NodeId id, bool isHardEvidence) override
 fired after a new evidence is inserted More...
 
void onEvidenceErased_ (const NodeId id, bool isHardEvidence) override
 fired before an evidence is removed More...
 
void onAllEvidenceErased_ (bool contains_hard_evidence) override
 fired before all the evidence are erased More...
 
void onEvidenceChanged_ (const NodeId id, bool hasChangedSoftHard) override
 fired after an evidence is changed, in particular when its status (soft/hard) changes More...
 
void onModelChanged_ (const GraphicalModel *bn) override
 fired after a new Bayes net has been assigned to the engine More...
 
void updateOutdatedStructure_ () override
 prepares inference when the latter is in OutdatedStructure state More...
 
void updateOutdatedPotentials_ () override
 prepares inference when the latter is in OutdatedPotentials state More...
 
void onMarginalTargetAdded_ (const NodeId id) override
 fired after a new marginal target is inserted More...
 
void onMarginalTargetErased_ (const NodeId id) override
 fired before a marginal target is removed More...
 
void onAllMarginalTargetsAdded_ () override
 fired after all the nodes of the BN are added as marginal targets More...
 
void onAllMarginalTargetsErased_ () override
 fired before a all marginal targets are removed More...
 
void onStateChanged_ () override
 fired when the stage is changed More...
 
void setTargetedMode_ ()
 
bool isTargetedMode_ () const
 
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::SamplingInference< GUM_SCALAR >

<agrum/BN/inference/samplingInference.h>

A generic class for making sampling inference in Bayesian networks adapted

The goal of this class is to define the genereal scheme used by all sampling inference algorithms, which are implemented as derived classes of ApproximateInference. This class inherits from MarginalTargetedInference for the handling of marginal targets and from ApproximationScheme.

Definition at line 59 of file samplingInference.h.

Member Enumeration Documentation

◆ ApproximationSchemeSTATE

The different state of an approximation scheme.

Enumerator
Undefined 
Continue 
Epsilon 
Rate 
Limit 
TimeLimit 
Stopped 

Definition at line 64 of file IApproximationSchemeConfiguration.h.

64  : char
65  {
66  Undefined,
67  Continue,
68  Epsilon,
69  Rate,
70  Limit,
71  TimeLimit,
72  Stopped
73  };

◆ 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

◆ SamplingInference()

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

default constructor

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

Definition at line 47 of file samplingInference_tpl.h.

48  :
49  ApproximateInference< GUM_SCALAR >(bn),
50  estimator__(), samplingBN__(nullptr) {
57  GUM_CONSTRUCTOR(SamplingInference);
58  }
#define DEFAULT_MAXITER
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
#define DEFAULT_EPSILON
void setPeriodSize(Size p)
How many samples between two stopping is enable.
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
#define DEFAULT_PERIOD_SIZE
void setMaxTime(double timeout)
Stopping criterion on timeout.
#define DEFAULT_TIMEOUT
#define DEFAULT_MIN_EPSILON_RATE
void setMaxIter(Size max)
Stopping criterion on number of iterations.
BayesNetFragment< GUM_SCALAR > * samplingBN__
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
#define DEFAULT_VERBOSITY

◆ ~SamplingInference()

template<typename GUM_SCALAR >
gum::SamplingInference< GUM_SCALAR >::~SamplingInference ( )
override

destructor

Definition at line 62 of file samplingInference_tpl.h.

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

62  {
63  GUM_DESTRUCTOR(SamplingInference);
64  if (samplingBN__ != nullptr) {
65  if (isContextualized) { // otherwise samplingBN__==&BN()
66  delete samplingBN__;
67  }
68  }
69  }
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
bool isContextualized
whether the referenced Bayesian network has been "contextualized"
BayesNetFragment< GUM_SCALAR > * samplingBN__
+ Here is the call graph for this function:

Member Function Documentation

◆ addAllTargets()

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

adds all nodes as targets

Definition at line 135 of file marginalTargetedInference_tpl.h.

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

◆ addEvidence() [1/8]

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

adds a new hard evidence on node id

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

Definition at line 243 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [2/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 250 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [3/8]

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

adds a new hard evidence on node id

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

Definition at line 259 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [4/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 266 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [5/8]

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

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

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

Definition at line 275 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [6/8]

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

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

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

Definition at line 302 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [7/8]

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

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

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

Definition at line 352 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [8/8]

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

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

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

Definition at line 310 of file graphicalModelInference_tpl.h.

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

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

◆ addListOfEvidence()

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

adds a new list of evidence

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

Definition at line 361 of file graphicalModelInference_tpl.h.

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

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

◆ addSetOfEvidence()

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

adds a new set of evidence

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

Definition at line 370 of file graphicalModelInference_tpl.h.

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

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

◆ addTarget() [1/2]

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

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 111 of file marginalTargetedInference_tpl.h.

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

◆ addTarget() [2/2]

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

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 157 of file marginalTargetedInference_tpl.h.

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

◆ addVarSample_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::addVarSample_ ( NodeId  nod,
Instantiation I 
)
protectedvirtual

adds a node to current instantiation

Parameters
nodthe node to add to the sample
Ithe current sample

generates random value based on the BN's CPT's and adds the node to the Instantiation with that value

Definition at line 186 of file samplingInference_tpl.h.

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

187  {
189 
190  I->add(samplingBN().variable(nod));
191  I->chgVal(samplingBN().variable(nod),
192  samplingBN().cpt(nod).extract(Itop).draw());
193  }
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
+ Here is the call graph for this function:

◆ BN()

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

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

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

Definition at line 59 of file BayesNetInference_tpl.h.

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

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

◆ burnIn_()

template<typename GUM_SCALAR >
virtual Instantiation gum::SamplingInference< GUM_SCALAR >::burnIn_ ( )
protectedpure virtual

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

◆ contextualize()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::contextualize ( )
virtual

Simplifying the Bayesian network with relevance reasonning to lighten the computational charge.

Sets the reference Bayesian network as a BayesNetFragment after having eliminated nodes that are idle for simulation and computation, such as barren or d-separated nodes. Eliminates the arcs from evidence nodes to it's children, after setting new CPT's for them.

Definition at line 114 of file samplingInference_tpl.h.

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

114  {
115  // Finding Barren nodes
116 
117  BarrenNodesFinder barr_nodes = BarrenNodesFinder(&this->BN().dag());
118  barr_nodes.setTargets(&this->targets());
119  barr_nodes.setEvidence(&this->hardEvidenceNodes());
120  const NodeSet& barren = barr_nodes.barrenNodes();
121 
122  // creating BN fragment
123  samplingBN__ = new BayesNetFragment< GUM_SCALAR >(this->BN());
124  for (const auto elmt: this->BN().dag().asNodeSet() - barren)
125  samplingBN__->installNode(elmt);
126 
127  // D-separated nodes
128 
129  dSeparation dsep = gum::dSeparation();
130  NodeSet requisite;
131  dsep.requisiteNodes(
132  this->BN().dag(),
133  this->BN().nodes().asNodeSet(), // no target for approximateInference
134  this->hardEvidenceNodes(),
135  this->softEvidenceNodes(), // should be empty
136  requisite);
137  requisite += this->hardEvidenceNodes();
138 
139  auto nonRequisite = this->BN().dag().asNodeSet() - requisite;
140 
141  for (const auto elmt: nonRequisite)
142  samplingBN__->uninstallNode(elmt);
143  for (const auto hard: this->hardEvidenceNodes()) {
145  I.add(this->BN().variable(hard));
146  I.chgVal(this->BN().variable(hard), this->hardEvidence()[hard]);
147 
148  for (const auto& child: this->BN().children(hard)) {
149  samplingBN__->installCPT(child, this->BN().cpt(child).extract(I));
150  }
151  }
152 
153  this->isContextualized = true;
155  }
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
the d-separation algorithm as described in Koller & Friedman (2009)
Definition: dSeparation.h:43
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
const NodeSet & softEvidenceNodes() const
returns the set of nodes with soft evidence
bool isContextualized
whether the referenced Bayesian network has been "contextualized"
BayesNetFragment< GUM_SCALAR > * samplingBN__
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void onContextualize_(BayesNetFragment< GUM_SCALAR > *bn)
fired when Bayesian network is contextualized
+ Here is the call graph for this function:

◆ continueApproximationScheme()

INLINE bool gum::ApproximationScheme::continueApproximationScheme ( double  error)
inherited

Update the scheme w.r.t the new error.

Test the stopping criterion that are enabled.

Parameters
errorThe new error value.
Returns
false if state become != ApproximationSchemeSTATE::Continue
Exceptions
OperationNotAllowedRaised if state != ApproximationSchemeSTATE::Continue.

Definition at line 226 of file approximationScheme_inl.h.

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

226  {
227  // For coherence, we fix the time used in the method
228 
229  double timer_step = timer_.step();
230 
231  if (enabled_max_time_) {
232  if (timer_step > max_time_) {
234  return false;
235  }
236  }
237 
238  if (!startOfPeriod()) { return true; }
239 
241  GUM_ERROR(OperationNotAllowed,
242  "state of the approximation scheme is not correct : "
244  }
245 
246  if (verbosity()) { history_.push_back(error); }
247 
248  if (enabled_max_iter_) {
249  if (current_step_ > max_iter_) {
251  return false;
252  }
253  }
254 
256  current_epsilon_ = error; // eps rate isEnabled needs it so affectation was
257  // moved from eps isEnabled below
258 
259  if (enabled_eps_) {
260  if (current_epsilon_ <= eps_) {
262  return false;
263  }
264  }
265 
266  if (last_epsilon_ >= 0.) {
267  if (current_epsilon_ > .0) {
268  // ! current_epsilon_ can be 0. AND epsilon
269  // isEnabled can be disabled !
272  }
273  // limit with current eps ---> 0 is | 1 - ( last_eps / 0 ) | --->
274  // infinity the else means a return false if we isEnabled the rate below,
275  // as we would have returned false if epsilon isEnabled was enabled
276  else {
278  }
279 
280  if (enabled_min_rate_eps_) {
281  if (current_rate_ <= min_rate_eps_) {
283  return false;
284  }
285  }
286  }
287 
289  if (onProgress.hasListener()) {
291  }
292 
293  return true;
294  } else {
295  return false;
296  }
297  }
double max_time_
The timeout.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:41
Signaler3< Size, double, double > onProgress
Progression, error and time.
ApproximationSchemeSTATE current_state_
The current state.
void stopScheme_(ApproximationSchemeSTATE new_state)
Stop the scheme given a new state.
bool startOfPeriod()
Returns true if we are at the beginning of a period (compute error is mandatory). ...
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
double last_epsilon_
Last epsilon value.
double eps_
Threshold for convergence.
double min_rate_eps_
Threshold for the epsilon rate.
bool enabled_max_time_
If true, the timeout is enabled.
double current_rate_
Current rate.
Size max_iter_
The maximum iterations.
double current_epsilon_
Current epsilon.
bool enabled_eps_
If true, the threshold convergence is enabled.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
std::vector< double > history_
The scheme history, used only if verbosity == true.
bool verbosity() const
Returns true if verbosity is enabled.
std::string messageApproximationScheme() const
Returns the approximation scheme message.
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
Size current_step_
The current step.
#define GUM_EMIT3(signal, arg1, arg2, arg3)
Definition: signaler3.h:41
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ currentPosterior() [1/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::SamplingInference< GUM_SCALAR >::currentPosterior ( NodeId  id)

Computes and returns the actual estimation of the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
idthe node for which we need a posterior probability
Warning
for efficiency reasons, the potential 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.
Exceptions
UndefinedElementif node is not in the set of targets.
NotFoundif node is not in the BN.

Definition at line 97 of file samplingInference_tpl.h.

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

97  {
98  return estimator__.posterior(this->BN().variable(id));
99  }
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
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:

◆ currentPosterior() [2/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::SamplingInference< GUM_SCALAR >::currentPosterior ( const std::string &  name)

Computes and returns the actual estimation of the posterior of a node by its name.

Returns
a const ref to the posterior probability of the node referred by name.
Parameters
namethe name of the node for which we need a posterior probability
Warning
for efficiency reasons, the potential 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.
Exceptions
UndefinedElementif node corresponding to name is not in the set of targets.
NotFoundif node corresponding to name is not in the BN.

Definition at line 103 of file samplingInference_tpl.h.

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

103  {
104  return currentPosterior(this->BN().idFromName(name));
105  }
const Potential< GUM_SCALAR > & currentPosterior(NodeId id)
Computes and returns the actual estimation of the posterior of a node.
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:

◆ currentTime()

INLINE double gum::ApproximationScheme::currentTime ( ) const
virtualinherited

Returns the current running time in second.

Returns
Returns the current running time in second.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 127 of file approximationScheme_inl.h.

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

127 { return timer_.step(); }
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:41
+ Here is the call graph for this function:

◆ disableEpsilon()

INLINE void gum::ApproximationScheme::disableEpsilon ( )
virtualinherited

Disable stopping criterion on epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 53 of file approximationScheme_inl.h.

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

53 { enabled_eps_ = false; }
bool enabled_eps_
If true, the threshold convergence is enabled.
+ Here is the call graph for this function:

◆ disableMaxIter()

INLINE void gum::ApproximationScheme::disableMaxIter ( )
virtualinherited

Disable stopping criterion on max iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 104 of file approximationScheme_inl.h.

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

104 { enabled_max_iter_ = false; }
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
+ Here is the call graph for this function:

◆ disableMaxTime()

INLINE void gum::ApproximationScheme::disableMaxTime ( )
virtualinherited

Disable stopping criterion on timeout.

Returns
Disable stopping criterion on timeout.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 130 of file approximationScheme_inl.h.

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

130 { enabled_max_time_ = false; }
bool enabled_max_time_
If true, the timeout is enabled.
+ Here is the call graph for this function:

◆ disableMinEpsilonRate()

INLINE void gum::ApproximationScheme::disableMinEpsilonRate ( )
virtualinherited

Disable stopping criterion on epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 78 of file approximationScheme_inl.h.

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

78  {
79  enabled_min_rate_eps_ = false;
80  }
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
+ 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:

◆ draw_()

template<typename GUM_SCALAR >
virtual Instantiation gum::SamplingInference< GUM_SCALAR >::draw_ ( GUM_SCALAR *  w,
Instantiation  prev 
)
protectedpure virtual

draws a sample in the Bayesian network given a previous one

Parameters
wthe weight of sample being generated
prevthe previous sample generated

Implemented in gum::GibbsSampling< GUM_SCALAR >, gum::ImportanceSampling< GUM_SCALAR >, gum::MonteCarloSampling< GUM_SCALAR >, and gum::WeightedSampling< GUM_SCALAR >.

◆ enableEpsilon()

INLINE void gum::ApproximationScheme::enableEpsilon ( )
virtualinherited

Enable stopping criterion on epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 56 of file approximationScheme_inl.h.

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

56 { enabled_eps_ = true; }
bool enabled_eps_
If true, the threshold convergence is enabled.
+ Here is the call graph for this function:

◆ enableMaxIter()

INLINE void gum::ApproximationScheme::enableMaxIter ( )
virtualinherited

Enable stopping criterion on max iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 107 of file approximationScheme_inl.h.

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

107 { enabled_max_iter_ = true; }
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
+ Here is the call graph for this function:

◆ enableMaxTime()

INLINE void gum::ApproximationScheme::enableMaxTime ( )
virtualinherited

Enable stopping criterion on timeout.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 133 of file approximationScheme_inl.h.

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

133 { enabled_max_time_ = true; }
bool enabled_max_time_
If true, the timeout is enabled.
+ Here is the call graph for this function:

◆ enableMinEpsilonRate()

INLINE void gum::ApproximationScheme::enableMinEpsilonRate ( )
virtualinherited

Enable stopping criterion on epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 83 of file approximationScheme_inl.h.

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

83  {
84  enabled_min_rate_eps_ = true;
85  }
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
+ Here is the call graph for this function:

◆ epsilon()

INLINE double gum::ApproximationScheme::epsilon ( ) const
virtualinherited

Returns the value of epsilon.

Returns
Returns the value of epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 50 of file approximationScheme_inl.h.

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

50 { return eps_; }
double eps_
Threshold for convergence.
+ Here is the call graph for this function:

◆ eraseAllEvidence()

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

removes all the evidence entered into the network

Definition at line 597 of file graphicalModelInference_tpl.h.

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

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

◆ eraseAllTargets()

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

Clear all previously defined targets.

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 98 of file marginalTargetedInference_tpl.h.

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

◆ eraseEvidence() [1/2]

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

removed the evidence, if any, corresponding to node id

Definition at line 568 of file graphicalModelInference_tpl.h.

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

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

◆ eraseEvidence() [2/2]

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

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

Definition at line 589 of file graphicalModelInference_tpl.h.

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

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

◆ eraseTarget() [1/2]

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

removes an existing (marginal) target

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

Definition at line 171 of file marginalTargetedInference_tpl.h.

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

◆ eraseTarget() [2/2]

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

removes an existing (marginal) target

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

Definition at line 196 of file marginalTargetedInference_tpl.h.

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

◆ evidence()

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

returns the set of evidence

Definition at line 652 of file graphicalModelInference_tpl.h.

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

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

◆ evidenceImpact() [1/2]

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

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

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

Definition at line 284 of file marginalTargetedInference_tpl.h.

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

◆ evidenceImpact() [2/2]

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

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

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

Definition at line 323 of file marginalTargetedInference_tpl.h.

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

◆ H() [1/2]

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

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

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

Definition at line 268 of file marginalTargetedInference_tpl.h.

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

◆ H() [2/2]

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

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

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

Definition at line 277 of file marginalTargetedInference_tpl.h.

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

◆ hardEvidence()

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

indicate for each node with hard evidence which value it took

Definition at line 644 of file graphicalModelInference_tpl.h.

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

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

◆ hardEvidenceNodes()

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

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 668 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [1/3]

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

indicates whether some node(s) have received evidence

Definition at line 379 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [2/3]

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

indicates whether node id has received an evidence

Definition at line 386 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [3/3]

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

indicates whether node id has received an evidence

Definition at line 409 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [1/2]

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

indicates whether node id has received a hard evidence

Definition at line 394 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [2/2]

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

indicates whether node id has received a hard evidence

Definition at line 417 of file graphicalModelInference_tpl.h.

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

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

◆ hasNoModel_()

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

Definition at line 533 of file graphicalModelInference.h.

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

◆ hasSoftEvidence() [1/2]

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

indicates whether node id has received a soft evidence

Definition at line 402 of file graphicalModelInference_tpl.h.

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

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

◆ hasSoftEvidence() [2/2]

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

indicates whether node id has received a soft evidence

Definition at line 425 of file graphicalModelInference_tpl.h.

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

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

◆ history()

INLINE const std::vector< double > & gum::ApproximationScheme::history ( ) const
virtualinherited

Returns the scheme history.

Returns
Returns the scheme history.
Exceptions
OperationNotAllowedRaised if the scheme did not performed or if verbosity is set to false.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 172 of file approximationScheme_inl.h.

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

172  {
174  GUM_ERROR(OperationNotAllowed,
175  "state of the approximation scheme is udefined");
176  }
177 
178  if (verbosity() == false) {
179  GUM_ERROR(OperationNotAllowed, "No history when verbosity=false");
180  }
181 
182  return history_;
183  }
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
std::vector< double > history_
The scheme history, used only if verbosity == true.
bool verbosity() const
Returns true if verbosity is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ initApproximationScheme()

INLINE void gum::ApproximationScheme::initApproximationScheme ( )
inherited

Initialise the scheme.

Definition at line 186 of file approximationScheme_inl.h.

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

186  {
188  current_step_ = 0;
190  history_.clear();
191  timer_.reset();
192  }
ApproximationSchemeSTATE current_state_
The current state.
void reset()
Reset the timer.
Definition: timer_inl.h:31
double current_rate_
Current rate.
double current_epsilon_
Current epsilon.
std::vector< double > history_
The scheme history, used only if verbosity == true.
Size current_step_
The current step.
+ Here is the call graph for this function:

◆ isEnabledEpsilon()

INLINE bool gum::ApproximationScheme::isEnabledEpsilon ( ) const
virtualinherited

Returns true if stopping criterion on epsilon is enabled, false otherwise.

Returns
Returns true if stopping criterion on epsilon is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 60 of file approximationScheme_inl.h.

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

60  {
61  return enabled_eps_;
62  }
bool enabled_eps_
If true, the threshold convergence is enabled.
+ Here is the call graph for this function:

◆ isEnabledMaxIter()

INLINE bool gum::ApproximationScheme::isEnabledMaxIter ( ) const
virtualinherited

Returns true if stopping criterion on max iterations is enabled, false otherwise.

Returns
Returns true if stopping criterion on max iterations is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 111 of file approximationScheme_inl.h.

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

111  {
112  return enabled_max_iter_;
113  }
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
+ Here is the call graph for this function:

◆ isEnabledMaxTime()

INLINE bool gum::ApproximationScheme::isEnabledMaxTime ( ) const
virtualinherited

Returns true if stopping criterion on timeout is enabled, false otherwise.

Returns
Returns true if stopping criterion on timeout is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 137 of file approximationScheme_inl.h.

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

137  {
138  return enabled_max_time_;
139  }
bool enabled_max_time_
If true, the timeout is enabled.
+ Here is the call graph for this function:

◆ isEnabledMinEpsilonRate()

INLINE bool gum::ApproximationScheme::isEnabledMinEpsilonRate ( ) const
virtualinherited

Returns true if stopping criterion on epsilon rate is enabled, false otherwise.

Returns
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 89 of file approximationScheme_inl.h.

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

89  {
90  return enabled_min_rate_eps_;
91  }
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
+ Here is the call graph for this function:

◆ isInferenceDone()

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

returns whether the inference object is in a InferenceDone state

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

Definition at line 89 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceOutdatedPotentials()

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

returns whether the inference object is in a OutdatedPotential state

Definition at line 82 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceOutdatedStructure()

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

returns whether the inference object is in a OutdatedStructure state

Definition at line 75 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceReady()

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

returns whether the inference object is in a ready state

Definition at line 70 of file graphicalModelInference_tpl.h.

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

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

◆ isTarget() [1/2]

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

return true if variable is a (marginal) target

Definition at line 75 of file marginalTargetedInference_tpl.h.

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

◆ isTarget() [2/2]

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

return true if variable is a (marginal) target

Definition at line 90 of file marginalTargetedInference_tpl.h.

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

◆ isTargetedMode_()

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

Definition at line 332 of file marginalTargetedInference_tpl.h.

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

◆ loopApproxInference_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::loopApproxInference_ ( )
protected

Definition at line 165 of file samplingInference_tpl.h.

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

165  {
166  //@todo This should be in prepareInference__
167  if (!isContextualized) { this->contextualize(); }
168 
169  this->initApproximationScheme();
171  GUM_SCALAR w = .0; //
172 
173  // Burn in
174  Ip = this->burnIn_();
175  do {
176  Ip = this->draw_(&w, Ip);
177  estimator__.update(Ip, w);
179  } while (this->continueApproximationScheme(estimator__.confidence()));
180 
181  this->isSetEstimator = false;
182  }
virtual void contextualize()
Simplifying the Bayesian network with relevance reasonning to lighten the computational charge...
virtual Instantiation draw_(GUM_SCALAR *w, Instantiation prev)=0
draws a sample in the Bayesian network given a previous one
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
void initApproximationScheme()
Initialise the scheme.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
virtual Instantiation burnIn_()=0
draws samples without updating the estimators
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
bool isContextualized
whether the referenced Bayesian network has been "contextualized"
bool isSetEstimator
whether the Estimator object has been initialized
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
+ Here is the call graph for this function:

◆ makeInference()

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

perform the heavy computations needed to compute the targets' posteriors

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

Definition at line 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 >
void gum::SamplingInference< GUM_SCALAR >::makeInference_ ( )
overrideprotectedvirtual

makes the inference by generating samples

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 159 of file samplingInference_tpl.h.

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

159  {
160  if (!isSetEstimator) this->setEstimatorFromBN_();
162  }
bool isSetEstimator
whether the Estimator object has been initialized
virtual void setEstimatorFromBN_()
Initializes the estimators object linked to the simulation.
+ Here is the call graph for this function:

◆ maxIter()

INLINE Size gum::ApproximationScheme::maxIter ( ) const
virtualinherited

Returns the criterion on number of iterations.

Returns
Returns the criterion on number of iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 101 of file approximationScheme_inl.h.

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

101 { return max_iter_; }
Size max_iter_
The maximum iterations.
+ Here is the call graph for this function:

◆ maxTime()

INLINE double gum::ApproximationScheme::maxTime ( ) const
virtualinherited

Returns the timeout (in seconds).

Returns
Returns the timeout (in seconds).

Implements gum::IApproximationSchemeConfiguration.

Definition at line 124 of file approximationScheme_inl.h.

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

124 { return max_time_; }
double max_time_
The timeout.
+ Here is the call graph for this function:

◆ messageApproximationScheme()

INLINE std::string gum::IApproximationSchemeConfiguration::messageApproximationScheme ( ) const
inherited

Returns the approximation scheme message.

Returns
Returns the approximation scheme message.

Definition at line 39 of file IApproximationSchemeConfiguration_inl.h.

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

39  {
40  std::stringstream s;
41 
42  switch (stateApproximationScheme()) {
44  s << "in progress";
45  break;
46 
48  s << "stopped with epsilon=" << epsilon();
49  break;
50 
52  s << "stopped with rate=" << minEpsilonRate();
53  break;
54 
56  s << "stopped with max iteration=" << maxIter();
57  break;
58 
60  s << "stopped with timeout=" << maxTime();
61  break;
62 
64  s << "stopped on request";
65  break;
66 
68  s << "undefined state";
69  break;
70  };
71 
72  return s.str();
73  }
virtual double epsilon() const =0
Returns the value of epsilon.
virtual ApproximationSchemeSTATE stateApproximationScheme() const =0
Returns the approximation scheme state.
virtual double maxTime() const =0
Returns the timeout (in seconds).
virtual Size maxIter() const =0
Returns the criterion on number of iterations.
virtual double minEpsilonRate() const =0
Returns the value of the minimal epsilon rate.
+ Here is the call graph for this function:

◆ minEpsilonRate()

INLINE double gum::ApproximationScheme::minEpsilonRate ( ) const
virtualinherited

Returns the value of the minimal epsilon rate.

Returns
Returns the value of the minimal epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 73 of file approximationScheme_inl.h.

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

73  {
74  return min_rate_eps_;
75  }
double min_rate_eps_
Threshold for the epsilon rate.
+ 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:

◆ nbrIterations()

INLINE Size gum::ApproximationScheme::nbrIterations ( ) const
virtualinherited

Returns the number of iterations.

Returns
Returns the number of iterations.
Exceptions
OperationNotAllowedRaised if the scheme did not perform.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 162 of file approximationScheme_inl.h.

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

162  {
164  GUM_ERROR(OperationNotAllowed,
165  "state of the approximation scheme is undefined");
166  }
167 
168  return current_step_;
169  }
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
Size current_step_
The current step.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ nbrSoftEvidence()

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

returns the number of soft evidence entered into the Bayesian network

Definition at line 636 of file graphicalModelInference_tpl.h.

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

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

◆ nbrTargets()

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

returns the number of marginal targets

Definition at line 218 of file marginalTargetedInference_tpl.h.

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

◆ onAllEvidenceErased_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onAllEvidenceErased_ ( bool  contains_hard_evidence)
overrideprotectedvirtual

fired before all the evidence are erased

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 213 of file samplingInference_tpl.h.

214  {}

◆ onAllMarginalTargetsAdded_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onAllMarginalTargetsAdded_ ( )
overrideprotectedvirtual

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

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 242 of file samplingInference_tpl.h.

242 {}

◆ onAllMarginalTargetsErased_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onAllMarginalTargetsErased_ ( )
overrideprotectedvirtual

fired before a all marginal targets are removed

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 245 of file samplingInference_tpl.h.

245 {}

◆ onContextualize_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onContextualize_ ( BayesNetFragment< GUM_SCALAR > *  bn)
protectedvirtual

fired when Bayesian network is contextualized

Parameters
bnthe contextualized BayesNetFragment
targetsinference target variables
hardEvNodeshard evidence nodes
hardEvhard evidences values

Reimplemented in gum::ImportanceSampling< GUM_SCALAR >.

Definition at line 196 of file samplingInference_tpl.h.

197  {}

◆ onEvidenceAdded_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onEvidenceAdded_ ( const NodeId  id,
bool  isHardEvidence 
)
overrideprotectedvirtual

fired after a new evidence is inserted

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 201 of file samplingInference_tpl.h.

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

202  {
203  if (!isHardEvidence) {
204  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence");
205  }
206  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ onEvidenceChanged_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onEvidenceChanged_ ( const NodeId  id,
bool  hasChangedSoftHard 
)
overrideprotectedvirtual

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

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 218 of file samplingInference_tpl.h.

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

219  {
220  if (hasChangedSoftHard) {
221  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence");
222  }
223  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ onEvidenceErased_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onEvidenceErased_ ( const NodeId  id,
bool  isHardEvidence 
)
overrideprotectedvirtual

fired before an evidence is removed

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 209 of file samplingInference_tpl.h.

210  {}

◆ onMarginalTargetAdded_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onMarginalTargetAdded_ ( const NodeId  id)
overrideprotectedvirtual

fired after a new marginal target is inserted

Parameters
idThe target variable's id.

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 236 of file samplingInference_tpl.h.

236 {}

◆ onMarginalTargetErased_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onMarginalTargetErased_ ( const NodeId  id)
overrideprotectedvirtual

fired before a marginal target is removed

Parameters
idThe target variable's id.

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 239 of file samplingInference_tpl.h.

239 {}

◆ onModelChanged_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onModelChanged_ ( const GraphicalModel bn)
overrideprotectedvirtual

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

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 226 of file samplingInference_tpl.h.

226  {
227  }

◆ onStateChanged_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::onStateChanged_ ( )
overrideprotectedvirtual

fired when the stage is changed

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 248 of file samplingInference_tpl.h.

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

248  {
249  if (this->isInferenceReady()) {
250  estimator__.clear();
251  this->initApproximationScheme();
252  }
253  }
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
void initApproximationScheme()
Initialise the scheme.
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
+ Here is the call graph for this function:

◆ periodSize()

INLINE Size gum::ApproximationScheme::periodSize ( ) const
virtualinherited

Returns the period size.

Returns
Returns the period size.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 148 of file approximationScheme_inl.h.

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

148 { return period_size_; }
Size period_size_
Checking criteria frequency.
+ Here is the call graph for this function:

◆ posterior() [1/2]

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

Computes and returns the posterior of a node.

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

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 241 of file marginalTargetedInference_tpl.h.

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

◆ posterior() [2/2]

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

Computes and returns the posterior of a node.

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

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 259 of file marginalTargetedInference_tpl.h.

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

◆ posterior_()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::SamplingInference< GUM_SCALAR >::posterior_ ( NodeId  id)
overridevirtual

Computes and returns the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
idthe node for which we need a posterior probability
Warning
for efficiency reasons, the potential 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.
Exceptions
UndefinedElementif node is not in the set of targets.
NotFoundif node is not in the BN.

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 109 of file samplingInference_tpl.h.

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

109  {
110  return estimator__.posterior(this->BN().variable(id));
111  }
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
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:

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

◆ remainingBurnIn()

INLINE Size gum::ApproximationScheme::remainingBurnIn ( )
inherited

Returns the remaining burn in.

Returns
Returns the remaining burn in.

Definition at line 209 of file approximationScheme_inl.h.

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

209  {
210  if (burn_in_ > current_step_) {
211  return burn_in_ - current_step_;
212  } else {
213  return 0;
214  }
215  }
Size burn_in_
Number of iterations before checking stopping criteria.
Size current_step_
The current step.
+ Here is the call graph for this function:

◆ samplingBN()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::SamplingInference< GUM_SCALAR >::samplingBN ( )

get the BayesNet which is used to really perform the sampling

Definition at line 73 of file samplingInference_tpl.h.

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

73  {
74  this->prepareInference();
75  if (samplingBN__ == nullptr)
76  return this->BN();
77  else
78  return *samplingBN__;
79  }
virtual void prepareInference() final
prepare the internal inference structures for the next inference
BayesNetFragment< GUM_SCALAR > * samplingBN__
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:

◆ setBN()

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

assigns a new BN to the inference engine

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

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

Definition at line 66 of file BayesNetInference_tpl.h.

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

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

◆ setEpsilon()

INLINE void gum::ApproximationScheme::setEpsilon ( double  eps)
virtualinherited

Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.

If the criterion was disabled it will be enabled.

Parameters
epsThe new epsilon value.
Exceptions
OutOfLowerBoundRaised if eps < 0.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 42 of file approximationScheme_inl.h.

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

42  {
43  if (eps < 0.) { GUM_ERROR(OutOfLowerBound, "eps should be >=0"); }
44 
45  eps_ = eps;
46  enabled_eps_ = true;
47  }
double eps_
Threshold for convergence.
bool enabled_eps_
If true, the threshold convergence is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setEstimatorFromBN_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::setEstimatorFromBN_ ( )
virtual

Initializes the estimators object linked to the simulation.

Initializes the estimator object by creating a hashtable between non evidence nodes and a 0-filled potential which will approximate the node's posterior

Definition at line 81 of file samplingInference_tpl.h.

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

81  {
82  estimator__.setFromBN(&samplingBN(), this->hardEvidenceNodes());
83  this->isSetEstimator = true;
84  }
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
bool isSetEstimator
whether the Estimator object has been initialized
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
+ Here is the call graph for this function:

◆ setEstimatorFromLBP_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::setEstimatorFromLBP_ ( LoopyBeliefPropagation< GUM_SCALAR > *  lbp,
GUM_SCALAR  virtualLBPSize 
)
virtual

Initializes the estimators object linked to the simulation.

Parameters
lbpa LoopyBeliefPropagation object
virtualLBPSizethe size of the equivalent sampling by LBP

Initializes the estimator object by creating a hashtable between non evidence nodes and the current approximation of the node's posterior obtained by running LoopyBeliefPropagation algorithm

Definition at line 87 of file samplingInference_tpl.h.

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

89  {
90  estimator__.setFromLBP(lbp, this->hardEvidenceNodes(), virtualLBPSize);
91  this->isSetEstimator = true;
92  }
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Estimator< GUM_SCALAR > estimator__
Estimator object designed to approximate target posteriors.
bool isSetEstimator
whether the Estimator object has been initialized
+ Here is the call graph for this function:

◆ setMaxIter()

INLINE void gum::ApproximationScheme::setMaxIter ( Size  max)
virtualinherited

Stopping criterion on number of iterations.

If the criterion was disabled it will be enabled.

Parameters
maxThe maximum number of iterations.
Exceptions
OutOfLowerBoundRaised if max <= 1.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 94 of file approximationScheme_inl.h.

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

94  {
95  if (max < 1) { GUM_ERROR(OutOfLowerBound, "max should be >=1"); }
96  max_iter_ = max;
97  enabled_max_iter_ = true;
98  }
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
Size max_iter_
The maximum iterations.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setMaxTime()

INLINE void gum::ApproximationScheme::setMaxTime ( double  timeout)
virtualinherited

Stopping criterion on timeout.

If the criterion was disabled it will be enabled.

Parameters
timeoutThe timeout value in seconds.
Exceptions
OutOfLowerBoundRaised if timeout <= 0.0.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 117 of file approximationScheme_inl.h.

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

117  {
118  if (timeout <= 0.) { GUM_ERROR(OutOfLowerBound, "timeout should be >0."); }
119  max_time_ = timeout;
120  enabled_max_time_ = true;
121  }
double max_time_
The timeout.
bool enabled_max_time_
If true, the timeout is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setMinEpsilonRate()

INLINE void gum::ApproximationScheme::setMinEpsilonRate ( double  rate)
virtualinherited

Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).

If the criterion was disabled it will be enabled

Parameters
rateThe minimal epsilon rate.
Exceptions
OutOfLowerBoundif rate<0

Implements gum::IApproximationSchemeConfiguration.

Definition at line 65 of file approximationScheme_inl.h.

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

65  {
66  if (rate < 0) { GUM_ERROR(OutOfLowerBound, "rate should be >=0"); }
67 
68  min_rate_eps_ = rate;
69  enabled_min_rate_eps_ = true;
70  }
double min_rate_eps_
Threshold for the epsilon rate.
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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:

◆ setPeriodSize()

INLINE void gum::ApproximationScheme::setPeriodSize ( Size  p)
virtualinherited

How many samples between two stopping is enable.

Parameters
pThe new period value.
Exceptions
OutOfLowerBoundRaised if p < 1.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 142 of file approximationScheme_inl.h.

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

142  {
143  if (p < 1) { GUM_ERROR(OutOfLowerBound, "p should be >=1"); }
144 
145  period_size_ = p;
146  }
Size period_size_
Checking criteria frequency.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setState_()

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

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

Definition at line 103 of file graphicalModelInference_tpl.h.

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

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

◆ setTargetedMode_()

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

Definition at line 336 of file marginalTargetedInference_tpl.h.

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

◆ setVerbosity()

INLINE void gum::ApproximationScheme::setVerbosity ( bool  v)
virtualinherited

Set the verbosity on (true) or off (false).

Parameters
vIf true, then verbosity is turned on.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 151 of file approximationScheme_inl.h.

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

151 { verbosity_ = v; }
bool verbosity_
If true, verbosity is enabled.
+ Here is the call graph for this function:

◆ softEvidenceNodes()

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

returns the set of nodes with soft evidence

the set of nodes that received soft evidence

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

◆ startOfPeriod()

INLINE bool gum::ApproximationScheme::startOfPeriod ( )
inherited

Returns true if we are at the beginning of a period (compute error is mandatory).

Returns
Returns true if we are at the beginning of a period (compute error is mandatory).

Definition at line 196 of file approximationScheme_inl.h.

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

196  {
197  if (current_step_ < burn_in_) { return false; }
198 
199  if (period_size_ == 1) { return true; }
200 
201  return ((current_step_ - burn_in_) % period_size_ == 0);
202  }
Size burn_in_
Number of iterations before checking stopping criteria.
Size period_size_
Checking criteria frequency.
Size current_step_
The current step.
+ 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:

◆ stateApproximationScheme()

INLINE IApproximationSchemeConfiguration::ApproximationSchemeSTATE gum::ApproximationScheme::stateApproximationScheme ( ) const
virtualinherited

Returns the approximation scheme state.

Returns
Returns the approximation scheme state.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 157 of file approximationScheme_inl.h.

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

157  {
158  return current_state_;
159  }
ApproximationSchemeSTATE current_state_
The current state.
+ Here is the call graph for this function:

◆ stopApproximationScheme()

INLINE void gum::ApproximationScheme::stopApproximationScheme ( )
inherited

Stop the approximation scheme.

Definition at line 218 of file approximationScheme_inl.h.

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

+ Here is the call graph for this function:

◆ targets()

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

returns the list of marginal targets

Definition at line 211 of file marginalTargetedInference_tpl.h.

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

◆ updateApproximationScheme()

INLINE void gum::ApproximationScheme::updateApproximationScheme ( unsigned int  incr = 1)
inherited

Update the scheme w.r.t the new error and increment steps.

Parameters
incrThe new increment steps.

Definition at line 205 of file approximationScheme_inl.h.

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

205  {
206  current_step_ += incr;
207  }
Size current_step_
The current step.
+ Here is the call graph for this function:

◆ updateOutdatedPotentials_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::updateOutdatedPotentials_ ( )
overrideprotectedvirtual

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_.

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 233 of file samplingInference_tpl.h.

233 {}

◆ updateOutdatedStructure_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::updateOutdatedStructure_ ( )
overrideprotectedvirtual

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_.

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 230 of file samplingInference_tpl.h.

230 {}

◆ verbosity()

INLINE bool gum::ApproximationScheme::verbosity ( ) const
virtualinherited

Returns true if verbosity is enabled.

Returns
Returns true if verbosity is enabled.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 153 of file approximationScheme_inl.h.

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

153 { return verbosity_; }
bool verbosity_
If true, verbosity is enabled.
+ Here is the call graph for this function:

Member Data Documentation

◆ burn_in_

Size gum::ApproximationScheme::burn_in_
protectedinherited

Number of iterations before checking stopping criteria.

Definition at line 413 of file approximationScheme.h.

◆ current_epsilon_

double gum::ApproximationScheme::current_epsilon_
protectedinherited

Current epsilon.

Definition at line 368 of file approximationScheme.h.

◆ current_rate_

double gum::ApproximationScheme::current_rate_
protectedinherited

Current rate.

Definition at line 374 of file approximationScheme.h.

◆ current_state_

ApproximationSchemeSTATE gum::ApproximationScheme::current_state_
protectedinherited

The current state.

Definition at line 383 of file approximationScheme.h.

◆ current_step_

Size gum::ApproximationScheme::current_step_
protectedinherited

The current step.

Definition at line 377 of file approximationScheme.h.

◆ enabled_eps_

bool gum::ApproximationScheme::enabled_eps_
protectedinherited

If true, the threshold convergence is enabled.

Definition at line 392 of file approximationScheme.h.

◆ enabled_max_iter_

bool gum::ApproximationScheme::enabled_max_iter_
protectedinherited

If true, the maximum iterations stopping criterion is enabled.

Definition at line 410 of file approximationScheme.h.

◆ enabled_max_time_

bool gum::ApproximationScheme::enabled_max_time_
protectedinherited

If true, the timeout is enabled.

Definition at line 404 of file approximationScheme.h.

◆ enabled_min_rate_eps_

bool gum::ApproximationScheme::enabled_min_rate_eps_
protectedinherited

If true, the minimal threshold for epsilon rate is enabled.

Definition at line 398 of file approximationScheme.h.

◆ eps_

double gum::ApproximationScheme::eps_
protectedinherited

Threshold for convergence.

Definition at line 389 of file approximationScheme.h.

◆ estimator__

template<typename GUM_SCALAR >
Estimator< GUM_SCALAR > gum::SamplingInference< GUM_SCALAR >::estimator__
protected

Estimator object designed to approximate target posteriors.

Definition at line 178 of file samplingInference.h.

◆ history_

std::vector< double > gum::ApproximationScheme::history_
protectedinherited

The scheme history, used only if verbosity == true.

Definition at line 386 of file approximationScheme.h.

◆ isContextualized

template<typename GUM_SCALAR >
bool gum::SamplingInference< GUM_SCALAR >::isContextualized = false
protected

whether the referenced Bayesian network has been "contextualized"

Definition at line 184 of file samplingInference.h.

◆ isSetEstimator

template<typename GUM_SCALAR >
bool gum::SamplingInference< GUM_SCALAR >::isSetEstimator = false
protected

whether the Estimator object has been initialized

Definition at line 181 of file samplingInference.h.

◆ last_epsilon_

double gum::ApproximationScheme::last_epsilon_
protectedinherited

Last epsilon value.

Definition at line 371 of file approximationScheme.h.

◆ max_iter_

Size gum::ApproximationScheme::max_iter_
protectedinherited

The maximum iterations.

Definition at line 407 of file approximationScheme.h.

◆ max_time_

double gum::ApproximationScheme::max_time_
protectedinherited

The timeout.

Definition at line 401 of file approximationScheme.h.

◆ min_rate_eps_

double gum::ApproximationScheme::min_rate_eps_
protectedinherited

Threshold for the epsilon rate.

Definition at line 395 of file approximationScheme.h.

◆ onProgress

Signaler3< Size, double, double > gum::IApproximationSchemeConfiguration::onProgress
inherited

Progression, error and time.

Definition at line 58 of file IApproximationSchemeConfiguration.h.

◆ onStop

Signaler1< std::string > gum::IApproximationSchemeConfiguration::onStop
inherited

Criteria messageApproximationScheme.

Definition at line 61 of file IApproximationSchemeConfiguration.h.

◆ period_size_

Size gum::ApproximationScheme::period_size_
protectedinherited

Checking criteria frequency.

Definition at line 416 of file approximationScheme.h.

◆ samplingBN__

template<typename GUM_SCALAR >
BayesNetFragment< GUM_SCALAR >* gum::SamplingInference< GUM_SCALAR >::samplingBN__
private

Definition at line 247 of file samplingInference.h.

◆ timer_

Timer gum::ApproximationScheme::timer_
protectedinherited

The timer.

Definition at line 380 of file approximationScheme.h.

◆ verbosity_

bool gum::ApproximationScheme::verbosity_
protectedinherited

If true, verbosity is enabled.

Definition at line 419 of file approximationScheme.h.


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