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

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

#include <GibbsSampling.h>

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

Public Attributes

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

Public Member Functions

 GibbsSampling (const IBayesNet< GUM_SCALAR > *bn)
 Default constructor. More...
 
 ~GibbsSampling () override
 Destructor. More...
 
void setBurnIn (Size b)
 Number of burn in for one iteration. More...
 
Size burnIn () const
 Returns the number of burn in. More...
 
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...
 
Size nbrDrawnVar () const
 Getters and setters. More...
 
void setNbrDrawnVar (Size nbr)
 
bool isDrawnAtRandom () const
 
void setDrawnAtRandom (bool atRandom)
 
Instantiation monteCarloSample ()
 draws a Monte Carlo sample More...
 
Instantiation nextSample (Instantiation prev)
 draws next sample of Gibbs sampling More...
 
Constructors / Destructors
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...
 
Size counting_
 
const IBayesNet< GUM_SCALAR > & samplingBn_
 
const NodeProperty< Idx > * hardEv_
 
Sequence< NodeIdsamplingNodes_
 
Size nbr_
 
bool atRandom_
 

Protected Member Functions

Instantiation burnIn_ () override
 draws a defined number of samples without updating the estimators More...
 
Instantiation draw_ (GUM_SCALAR *w, Instantiation prev) override
 draws a sample given previous one according to Gibbs sampling More...
 
Instantiation monteCarloSample_ ()
 draws a Monte Carlo sample 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::GibbsSampling< GUM_SCALAR >

<agrum/BN/inference/gibbsSampling.h>

class for making Gibbs sampling inference in Bayesian networks.

This class overrides pure function declared in the inherited class ApproximateInference. It defines the way Gibbs sampling draws a sample. It also inherits GibbsOperator which contains Gibbs sampling methods.

Definition at line 54 of file GibbsSampling.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

◆ GibbsSampling()

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

Default constructor.

default constructor

Definition at line 42 of file GibbsSampling_tpl.h.

42  :
43  SamplingInference< GUM_SCALAR >(bn),
44  GibbsOperator< GUM_SCALAR >(*bn,
45  &this->hardEvidence(),
46  1 + (bn->size() * GIBBS_SAMPLING_POURCENT_DRAWN_SAMPLE / 100),
48  GUM_CONSTRUCTOR(GibbsSampling);
49 
53  }
GibbsSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
void setBurnIn(Size b)
Number of burn in for one iteration.
Definition: GibbsSampling.h:71
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
#define GIBBS_SAMPLING_DEFAULT_BURNIN
#define GIBBS_SAMPLING_DEFAULT_MIN_EPSILON_RATE
#define GIBBS_SAMPLING_DEFAULT_EPSILON
#define GIBBS_SAMPLING_POURCENT_DRAWN_SAMPLE
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
#define GIBBS_SAMPLING_DRAWN_AT_RANDOM
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took

◆ ~GibbsSampling()

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

Destructor.

destructor

Definition at line 57 of file GibbsSampling_tpl.h.

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

57  {
58  GUM_DESTRUCTOR(GibbsSampling);
59  }
GibbsSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
+ 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 130 of file marginalTargetedInference_tpl.h.

130  {
131  // check if the node belongs to the Bayesian network
132  if (this->hasNoModel_())
133  GUM_ERROR(NullElement,
134  "No Bayes net has been assigned to the "
135  "inference algorithm");
136 
137 
138  setTargetedMode_(); // does nothing if already in targeted mode
139  for (const auto target: this->BN().dag()) {
140  if (!_targets_.contains(target)) {
141  _targets_.insert(target);
142  onMarginalTargetAdded_(target);
144  }
145  }
146  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
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:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addEvidence() [1/8]

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

adds a new hard evidence on node id

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

Definition at line 228 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [2/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 234 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [3/8]

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

adds a new hard evidence on node id

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

Definition at line 241 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [4/8]

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

adds a new hard evidence on node named nodeName

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

Definition at line 248 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [5/8]

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

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

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

Definition at line 256 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [6/8]

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

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

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

Definition at line 280 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [7/8]

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

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

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

Definition at line 325 of file graphicalModelInference_tpl.h.

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

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

◆ addEvidence() [8/8]

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

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

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

Definition at line 287 of file graphicalModelInference_tpl.h.

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

287  {
288  // check if the potential corresponds to an evidence
289  if (pot.nbrDim() != 1) { GUM_ERROR(InvalidArgument, pot << " is not mono-dimensional.") }
290  if (_model_ == nullptr)
291  GUM_ERROR(NullElement,
292  "No Bayes net has been assigned to the "
293  "inference algorithm");
294 
295  NodeId id = _model_->nodeId(pot.variable(0));
296 
297  if (hasEvidence(id)) {
298  GUM_ERROR(InvalidArgument,
299  " node " << id << " already has an evidence. Please use chgEvidence().");
300  }
301 
302  // check whether we have a hard evidence (and also check whether the
303  // potential only contains 0 (in this case, this will automatically raise
304  // an exception) )
305  Idx val;
306  bool is_hard_evidence = _isHardEvidence_(pot, val);
307 
308  // insert the evidence
309  _evidence_.insert(id,
310  new Potential< GUM_SCALAR >(std::forward< Potential< GUM_SCALAR > >(pot)));
311  if (is_hard_evidence) { // pot is deterministic
312  _hard_evidence_.insert(id, val);
314  } else {
316  }
318  onEvidenceAdded_(id, is_hard_evidence);
319  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
bool _isHardEvidence_(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
const GraphicalModel * _model_
the Bayes net on which we perform inferences
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ addListOfEvidence()

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

adds a new list of evidence

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

Definition at line 333 of file graphicalModelInference_tpl.h.

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

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

◆ addSetOfEvidence()

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

adds a new set of evidence

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

Definition at line 342 of file graphicalModelInference_tpl.h.

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

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

◆ addTarget() [1/2]

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

107  {
108  // check if the node belongs to the Bayesian network
109  if (this->hasNoModel_())
110  GUM_ERROR(NullElement,
111  "No Bayes net has been assigned to the "
112  "inference algorithm");
113 
114  if (!this->BN().dag().exists(target)) {
115  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn")
116  }
117 
118  setTargetedMode_(); // does nothing if already in targeted mode
119  // add the new target
120  if (!_targets_.contains(target)) {
121  _targets_.insert(target);
122  onMarginalTargetAdded_(target);
124  }
125  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
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:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 151 of file marginalTargetedInference_tpl.h.

151  {
152  // check if the node belongs to the Bayesian network
153  if (this->hasNoModel_())
154  GUM_ERROR(NullElement,
155  "No Bayes net has been assigned to the "
156  "inference algorithm");
157 
158  addTarget(this->BN().idFromName(nodeName));
159  }
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:51

◆ addVarSample_()

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

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 180 of file samplingInference_tpl.h.

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

180  {
182 
183  I->add(samplingBN().variable(nod));
184  I->chgVal(samplingBN().variable(nod), samplingBN().cpt(nod).extract(Itop).draw());
185  }
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 57 of file BayesNetInference_tpl.h.

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

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

◆ burnIn()

template<typename GUM_SCALAR >
Size gum::GibbsSampling< GUM_SCALAR >::burnIn ( ) const
inline

Returns the number of burn in.

Returns
Returns the number of burn in.

Definition at line 77 of file GibbsSampling.h.

77 { return this->burn_in_; };
Size burn_in_
Number of iterations before checking stopping criteria.

◆ burnIn_()

template<typename GUM_SCALAR >
Instantiation gum::GibbsSampling< GUM_SCALAR >::burnIn_ ( )
overrideprotectedvirtual

draws a defined number of samples without updating the estimators

Implements gum::SamplingInference< GUM_SCALAR >.

Definition at line 69 of file GibbsSampling_tpl.h.

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

69  {
71  if (this->burnIn() == 0) return Ip;
72 
73  GUM_SCALAR w = 1.0f;
74  Ip = monteCarloSample_();
75  for (Size i = 1; i < this->burnIn(); i++)
76  Ip = this->draw_(&w, Ip);
77 
78  return Ip;
79  }
Instantiation draw_(GUM_SCALAR *w, Instantiation prev) override
draws a sample given previous one according to Gibbs sampling
Size burnIn() const
Returns the number of burn in.
Definition: GibbsSampling.h:77
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
Instantiation monteCarloSample_()
draws a Monte Carlo sample
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ chgEvidence() [1/7]

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

change the value of an already existing hard evidence

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

Definition at line 402 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [2/7]

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

change the value of an already existing hard evidence

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

Definition at line 408 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [3/7]

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

change the value of an already existing hard evidence

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

Definition at line 415 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [4/7]

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

change the value of an already existing hard evidence

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

Definition at line 422 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [5/7]

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

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

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

Definition at line 431 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [6/7]

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

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

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

Definition at line 456 of file graphicalModelInference_tpl.h.

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

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

◆ chgEvidence() [7/7]

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

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

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

Definition at line 464 of file graphicalModelInference_tpl.h.

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

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

◆ clear()

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

clears all the data structures allocated for the last inference

Reimplemented in gum::ShaferShenoyLIMIDInference< GUM_SCALAR >.

Definition at line 139 of file graphicalModelInference_tpl.h.

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

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

◆ contextualize()

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

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 109 of file samplingInference_tpl.h.

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

109  {
110  // Finding Barren nodes
111 
112  BarrenNodesFinder barr_nodes = BarrenNodesFinder(&this->BN().dag());
113  barr_nodes.setTargets(&this->targets());
114  barr_nodes.setEvidence(&this->hardEvidenceNodes());
115  const NodeSet& barren = barr_nodes.barrenNodes();
116 
117  // creating BN fragment
118  _samplingBN_ = new BayesNetFragment< GUM_SCALAR >(this->BN());
119  for (const auto elmt: this->BN().dag().asNodeSet() - barren)
120  _samplingBN_->installNode(elmt);
121 
122  // D-separated nodes
123 
124  dSeparation dsep = gum::dSeparation();
125  NodeSet requisite;
126  dsep.requisiteNodes(this->BN().dag(),
127  this->BN().nodes().asNodeSet(), // no target for approximateInference
128  this->hardEvidenceNodes(),
129  this->softEvidenceNodes(), // should be empty
130  requisite);
131  requisite += this->hardEvidenceNodes();
132 
133  auto nonRequisite = this->BN().dag().asNodeSet() - requisite;
134 
135  for (const auto elmt: nonRequisite)
136  _samplingBN_->uninstallNode(elmt);
137  for (const auto hard: this->hardEvidenceNodes()) {
139  I.add(this->BN().variable(hard));
140  I.chgVal(this->BN().variable(hard), this->hardEvidence()[hard]);
141 
142  for (const auto& child: this->BN().children(hard)) {
143  _samplingBN_->installCPT(child, this->BN().cpt(child).extract(I));
144  }
145  }
146 
147  this->isContextualized = true;
149  }
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.
BayesNetFragment< GUM_SCALAR > * _samplingBN_
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"
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 208 of file approximationScheme_inl.h.

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

208  {
209  // For coherence, we fix the time used in the method
210 
211  double timer_step = timer_.step();
212 
213  if (enabled_max_time_) {
214  if (timer_step > max_time_) {
216  return false;
217  }
218  }
219 
220  if (!startOfPeriod()) { return true; }
221 
223  GUM_ERROR(OperationNotAllowed,
224  "state of the approximation scheme is not correct : "
226  }
227 
228  if (verbosity()) { history_.push_back(error); }
229 
230  if (enabled_max_iter_) {
231  if (current_step_ > max_iter_) {
233  return false;
234  }
235  }
236 
238  current_epsilon_ = error; // eps rate isEnabled needs it so affectation was
239  // moved from eps isEnabled below
240 
241  if (enabled_eps_) {
242  if (current_epsilon_ <= eps_) {
244  return false;
245  }
246  }
247 
248  if (last_epsilon_ >= 0.) {
249  if (current_epsilon_ > .0) {
250  // ! current_epsilon_ can be 0. AND epsilon
251  // isEnabled can be disabled !
253  }
254  // limit with current eps ---> 0 is | 1 - ( last_eps / 0 ) | --->
255  // infinity the else means a return false if we isEnabled the rate below,
256  // as we would have returned false if epsilon isEnabled was enabled
257  else {
259  }
260 
261  if (enabled_min_rate_eps_) {
262  if (current_rate_ <= min_rate_eps_) {
264  return false;
265  }
266  }
267  }
268 
270  if (onProgress.hasListener()) {
272  }
273 
274  return true;
275  } else {
276  return false;
277  }
278  }
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:51
+ 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)
inherited

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 93 of file samplingInference_tpl.h.

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

93  {
94  return _estimator_.posterior(this->BN().variable(id));
95  }
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)
inherited

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 99 of file samplingInference_tpl.h.

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

99  {
100  return currentPosterior(this->BN().idFromName(name));
101  }
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 115 of file approximationScheme_inl.h.

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

115 { 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 94 of file approximationScheme_inl.h.

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

94 { 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 118 of file approximationScheme_inl.h.

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

118 { 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 74 of file approximationScheme_inl.h.

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

74 { enabled_min_rate_eps_ = false; }
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 159 of file graphicalModelInference_tpl.h.

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

159  {
160  return _domain_sizes_;
161  }
NodeProperty< Size > _domain_sizes_
the domain sizes of the random variables
+ Here is the call graph for this function:

◆ draw_()

template<typename GUM_SCALAR >
Instantiation gum::GibbsSampling< GUM_SCALAR >::draw_ ( GUM_SCALAR *  w,
Instantiation  prev 
)
overrideprotectedvirtual

draws a sample given previous one according to Gibbs sampling

draws next sample for gibbs sampling

Parameters
wthe weight of sample being generated
prevthe previous sample generated
bnthe Bayesian network containing the evidence
hardEvNodeshard evidence nodes
hardEvhard evidences values

Uses the Gibbs sampling method to generate a new sample given the previous one. The method is implemented in the inherited class GibbsOperator. This function only makes the call to it. It consists of choosing one node x to sample, given the instantiation of all other nodes. It requires computing of P( x instantiation_markovblanket(x)).

Implements gum::SamplingInference< GUM_SCALAR >.

Definition at line 84 of file GibbsSampling_tpl.h.

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

84  {
85  *w = 1.0;
87  }
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
+ Here is the call graph for this function:

◆ 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 97 of file approximationScheme_inl.h.

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

97 { 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 121 of file approximationScheme_inl.h.

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

121 { 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 77 of file approximationScheme_inl.h.

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

77 { enabled_min_rate_eps_ = true; }
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 553 of file graphicalModelInference_tpl.h.

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

553  {
554  bool has_hard_evidence = !_hard_evidence_.empty();
555  this->onAllEvidenceErased_(has_hard_evidence);
556 
557  for (const auto& pair: _evidence_) {
558  if (pair.second != nullptr) { delete (pair.second); }
559  }
560 
561  _evidence_.clear();
562  _hard_evidence_.clear();
565 
566  if (has_hard_evidence) {
568  } else {
570  }
571  }
virtual void onAllEvidenceErased_(bool contains_hard_evidence)=0
fired before all the evidence are erased
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:361
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
+ Here is the call graph for this function:

◆ eraseAllTargets()

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

Clear all previously defined targets.

Reimplemented in gum::JointTargetedInference< GUM_SCALAR >.

Definition at line 95 of file marginalTargetedInference_tpl.h.

95  {
97 
98  _targets_.clear();
99  setTargetedMode_(); // does nothing if already in targeted mode
100 
102  }
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
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:361
NodeSet _targets_
the set of marginal targets

◆ eraseEvidence() [1/2]

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

removed the evidence, if any, corresponding to node id

Definition at line 527 of file graphicalModelInference_tpl.h.

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

527  {
528  if (hasEvidence(id)) {
529  if (hasHardEvidence(id)) {
530  onEvidenceErased_(id, true);
531  _hard_evidence_.erase(id);
534  } else {
535  onEvidenceErased_(id, false);
538  }
539 
540  delete (_evidence_[id]);
541  _evidence_.erase(id);
542  }
543  }
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:649
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
+ Here is the call graph for this function:

◆ eraseEvidence() [2/2]

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

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

Definition at line 546 of file graphicalModelInference_tpl.h.

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

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

◆ eraseTarget() [1/2]

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

164  {
165  // check if the node belongs to the Bayesian network
166  if (this->hasNoModel_())
167  GUM_ERROR(NullElement,
168  "No Bayes net has been assigned to the "
169  "inference algorithm");
170 
171  if (!this->BN().dag().exists(target)) {
172  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn")
173  }
174 
175 
176  if (_targets_.contains(target)) {
177  _targeted_mode_ = true; // we do not use setTargetedMode_ because we do not
178  // want to clear the targets
179  onMarginalTargetErased_(target);
180  _targets_.erase(target);
182  }
183  }
bool _targeted_mode_
whether the actual targets are default
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:649
NodeSet _targets_
the set of marginal targets
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:51

◆ 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 188 of file marginalTargetedInference_tpl.h.

188  {
189  // check if the node belongs to the Bayesian network
190  if (this->hasNoModel_())
191  GUM_ERROR(NullElement,
192  "No Bayes net has been assigned to the "
193  "inference algorithm");
194 
195  eraseTarget(this->BN().idFromName(nodeName));
196  }
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:51

◆ evidence()

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

returns the set of evidence

Definition at line 605 of file graphicalModelInference_tpl.h.

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

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

◆ evidenceImpact() [1/2]

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

268  {
269  const auto& vtarget = this->BN().variable(target);
270 
271  if (evs.contains(target)) {
272  GUM_ERROR(InvalidArgument,
273  "Target <" << vtarget.name() << "> (" << target << ") can not be in evs (" << evs
274  << ").");
275  }
276  auto condset = this->BN().minimalCondSet(target, evs);
277 
278  Potential< GUM_SCALAR > res;
279  this->eraseAllTargets();
280  this->eraseAllEvidence();
281  res.add(this->BN().variable(target));
282  this->addTarget(target);
283  for (const auto& n: condset) {
284  res.add(this->BN().variable(n));
285  this->addEvidence(n, 0);
286  }
287 
288  Instantiation inst(res);
289  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
290  // inferring
291  for (const auto& n: condset)
292  this->chgEvidence(n, inst.val(this->BN().variable(n)));
293  this->makeInference();
294  // populate res
295  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
296  res.set(inst, this->posterior(target)[inst]);
297  }
298  inst.setFirstVar(vtarget); // remove inst.end() flag
299  }
300 
301  return res;
302  }
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:51

◆ 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 306 of file marginalTargetedInference_tpl.h.

308  {
309  const auto& bn = this->BN();
310  return evidenceImpact(bn.idFromName(target), bn.nodeset(evs));
311  }
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 253 of file marginalTargetedInference_tpl.h.

253  {
254  return posterior(X).entropy();
255  }
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 261 of file marginalTargetedInference_tpl.h.

261  {
262  return H(this->BN().idFromName(nodeName));
263  }
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 597 of file graphicalModelInference_tpl.h.

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

597  {
598  return _hard_evidence_;
599  }
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
+ Here is the call graph for this function:

◆ hardEvidenceNodes()

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

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 619 of file graphicalModelInference_tpl.h.

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

619  {
620  return _hard_evidence_nodes_;
621  }
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
+ Here is the call graph for this function:

◆ hasEvidence() [1/3]

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

indicates whether some node(s) have received evidence

Definition at line 351 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [2/3]

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

indicates whether node id has received an evidence

Definition at line 358 of file graphicalModelInference_tpl.h.

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

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

◆ hasEvidence() [3/3]

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

indicates whether node id has received an evidence

Definition at line 380 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [1/2]

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

indicates whether node id has received a hard evidence

Definition at line 365 of file graphicalModelInference_tpl.h.

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

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

◆ hasHardEvidence() [2/2]

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

indicates whether node id has received a hard evidence

Definition at line 388 of file graphicalModelInference_tpl.h.

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

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

◆ hasNoModel_()

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

Definition at line 527 of file graphicalModelInference.h.

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

◆ hasSoftEvidence() [1/2]

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

indicates whether node id has received a soft evidence

Definition at line 372 of file graphicalModelInference_tpl.h.

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

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

◆ hasSoftEvidence() [2/2]

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

indicates whether node id has received a soft evidence

Definition at line 396 of file graphicalModelInference_tpl.h.

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

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

◆ 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 157 of file approximationScheme_inl.h.

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

157  {
159  GUM_ERROR(OperationNotAllowed, "state of the approximation scheme is udefined")
160  }
161 
162  if (verbosity() == false) { GUM_ERROR(OperationNotAllowed, "No history when verbosity=false") }
163 
164  return history_;
165  }
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:51
+ Here is the call graph for this function:

◆ initApproximationScheme()

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

Initialise the scheme.

Definition at line 168 of file approximationScheme_inl.h.

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

168  {
170  current_step_ = 0;
172  history_.clear();
173  timer_.reset();
174  }
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:

◆ isDrawnAtRandom()

template<typename GUM_SCALAR >
bool gum::GibbsOperator< GUM_SCALAR >::isDrawnAtRandom ( ) const
inlineinherited

Definition at line 69 of file gibbsOperator.h.

References gum::GibbsOperator< GUM_SCALAR >::atRandom_.

69 { return atRandom_; }

◆ 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 { return enabled_eps_; }
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 101 of file approximationScheme_inl.h.

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

101 { return enabled_max_iter_; }
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 125 of file approximationScheme_inl.h.

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

125 { return enabled_max_time_; }
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 81 of file approximationScheme_inl.h.

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

81 { return enabled_min_rate_eps_; }
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 84 of file graphicalModelInference_tpl.h.

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

84  {
85  return (_state_ == StateOfInference::Done);
86  }
StateOfInference _state_
the current state of the inference (outdated/ready/done)
+ Here is the call graph for this function:

◆ isInferenceOutdatedPotentials()

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

returns whether the inference object is in a OutdatedPotential state

Definition at line 79 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceOutdatedStructure()

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

returns whether the inference object is in a OutdatedStructure state

Definition at line 73 of file graphicalModelInference_tpl.h.

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

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

◆ isInferenceReady()

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

returns whether the inference object is in a ready state

Definition at line 68 of file graphicalModelInference_tpl.h.

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

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

◆ isTarget() [1/2]

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

return true if variable is a (marginal) target

Definition at line 73 of file marginalTargetedInference_tpl.h.

73  {
74  // check that the variable belongs to the bn
75  if (this->hasNoModel_())
76  GUM_ERROR(NullElement,
77  "No Bayes net has been assigned to the "
78  "inference algorithm");
79  if (!this->BN().dag().exists(node)) {
80  GUM_ERROR(UndefinedElement, node << " is not a NodeId in the bn")
81  }
82 
83  return _targets_.contains(node);
84  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
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:51

◆ 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 88 of file marginalTargetedInference_tpl.h.

88  {
89  return isTarget(this->BN().idFromName(nodeName));
90  }
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 315 of file marginalTargetedInference_tpl.h.

315  {
316  return _targeted_mode_;
317  }
bool _targeted_mode_
whether the actual targets are default

◆ loopApproxInference_()

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

Definition at line 159 of file samplingInference_tpl.h.

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

159  {
160  //@todo This should be in _prepareInference_
161  if (!isContextualized) { this->contextualize(); }
162 
163  this->initApproximationScheme();
165  GUM_SCALAR w = .0; //
166 
167  // Burn in
168  Ip = this->burnIn_();
169  do {
170  Ip = this->draw_(&w, Ip);
171  _estimator_.update(Ip, w);
173  } while (this->continueApproximationScheme(_estimator_.confidence()));
174 
175  this->isSetEstimator = false;
176  }
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
void initApproximationScheme()
Initialise the scheme.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
Estimator< GUM_SCALAR > _estimator_
Estimator object designed to approximate target posteriors.
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 664 of file graphicalModelInference_tpl.h.

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

664  {
665  if (isInferenceDone()) { return; }
666 
667  if (!isInferenceReady()) { prepareInference(); }
668 
669  makeInference_();
670 
672  }
virtual void prepareInference() final
prepare the internal inference structures for the next inference
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual void makeInference_()=0
called when the inference has to be performed effectively
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
+ Here is the call graph for this function:

◆ makeInference_()

template<typename GUM_SCALAR >
void gum::SamplingInference< GUM_SCALAR >::makeInference_ ( )
overrideprotectedvirtualinherited

makes the inference by generating samples

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 153 of file samplingInference_tpl.h.

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

153  {
154  if (!isSetEstimator) this->setEstimatorFromBN_();
156  }
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 91 of file approximationScheme_inl.h.

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

91 { 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 112 of file approximationScheme_inl.h.

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

112 { 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 38 of file IApproximationSchemeConfiguration_inl.h.

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

38  {
39  std::stringstream s;
40 
41  switch (stateApproximationScheme()) {
43  s << "in progress";
44  break;
45 
47  s << "stopped with epsilon=" << epsilon();
48  break;
49 
51  s << "stopped with rate=" << minEpsilonRate();
52  break;
53 
55  s << "stopped with max iteration=" << maxIter();
56  break;
57 
59  s << "stopped with timeout=" << maxTime();
60  break;
61 
63  s << "stopped on request";
64  break;
65 
67  s << "undefined state";
68  break;
69  };
70 
71  return s.str();
72  }
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 71 of file approximationScheme_inl.h.

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

71 { return min_rate_eps_; }
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 107 of file graphicalModelInference_tpl.h.

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

107  {
108  if (_model_ == nullptr)
109  GUM_ERROR(UndefinedElement,
110  "No Bayes net has been assigned to "
111  "the inference algorithm.");
112  return *_model_;
113  }
const GraphicalModel * _model_
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ monteCarloSample()

template<typename GUM_SCALAR >
Instantiation gum::GibbsOperator< GUM_SCALAR >::monteCarloSample ( )
inherited

draws a Monte Carlo sample

returns a MC sample This is not a really sample since we take into account evidence without care about parent of evidence, etc. This is just a not-so-bad first sample for GibbsSampler

Definition at line 67 of file gibbsOperator_tpl.h.

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

67  {
69 
70  for (const auto nod: samplingBn_.topologicalOrder()) {
71  I.add(samplingBn_.variable(nod));
72  if (hardEv_ != nullptr && hardEv_->exists(nod)) {
73  I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
74  } else {
75  _drawVarMonteCarlo_(nod, &I);
76  }
77  }
78  return I;
79  }
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
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
const NodeProperty< Idx > * hardEv_
Definition: gibbsOperator.h:82
+ Here is the call graph for this function:

◆ monteCarloSample_()

template<typename GUM_SCALAR >
Instantiation gum::GibbsSampling< GUM_SCALAR >::monteCarloSample_ ( )
protected

draws a Monte Carlo sample

Parameters
bnthe reference Bayesian network

This Monte Carlo sample generates a good starting point for Gibbs sampling, because it returns a sample consistent with the evidence, but it differs from the one implemented in the inherited class Approximate Inference because it also initializes attributes needed for Gibbs sampling.

Definition at line 63 of file GibbsSampling_tpl.h.

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

63  {
65  }
Instantiation monteCarloSample()
draws a Monte Carlo sample
+ Here is the call graph for this function:

◆ nbrDrawnVar()

template<typename GUM_SCALAR >
Size gum::GibbsOperator< GUM_SCALAR >::nbrDrawnVar ( ) const
inlineinherited

Getters and setters.

Definition at line 65 of file gibbsOperator.h.

65 { return nbr_; }

◆ nbrEvidence()

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

returns the number of evidence entered into the Bayesian network

Definition at line 576 of file graphicalModelInference_tpl.h.

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

576  {
577  return _evidence_.size();
578  }
NodeProperty< const Potential< GUM_SCALAR > *> _evidence_
the set of evidence entered into the network
+ Here is the call graph for this function:

◆ nbrHardEvidence()

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

returns the number of hard evidence entered into the Bayesian network

Definition at line 583 of file graphicalModelInference_tpl.h.

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

583  {
584  return _hard_evidence_nodes_.size();
585  }
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:694
+ Here is the call graph for this function:

◆ 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 148 of file approximationScheme_inl.h.

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

148  {
150  GUM_ERROR(OperationNotAllowed, "state of the approximation scheme is undefined")
151  }
152 
153  return current_step_;
154  }
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
Size current_step_
The current step.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ nbrSoftEvidence()

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

returns the number of soft evidence entered into the Bayesian network

Definition at line 590 of file graphicalModelInference_tpl.h.

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

590  {
591  return _soft_evidence_nodes_.size();
592  }
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:694
+ Here is the call graph for this function:

◆ nbrTargets()

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

returns the number of marginal targets

Definition at line 207 of file marginalTargetedInference_tpl.h.

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

◆ nextSample()

template<typename GUM_SCALAR >
Instantiation gum::GibbsOperator< GUM_SCALAR >::nextSample ( Instantiation  prev)
inherited

draws next sample of Gibbs sampling

Definition at line 90 of file gibbsOperator_tpl.h.

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

90  {
91  for (Idx i = 0; i < nbr_; i++) {
92  auto pos
94  this->_GibbsSample_(samplingNodes_[pos], &prev);
95  counting_++;
96  }
97  return prev;
98  }
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:37
void _GibbsSample_(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
Sequence< NodeId > samplingNodes_
Definition: gibbsOperator.h:83
+ Here is the call graph for this function:

◆ onAllEvidenceErased_()

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

fired before all the evidence are erased

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 202 of file samplingInference_tpl.h.

202 {}

◆ onAllMarginalTargetsAdded_()

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

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

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 228 of file samplingInference_tpl.h.

228 {}

◆ onAllMarginalTargetsErased_()

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

fired before a all marginal targets are removed

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 231 of file samplingInference_tpl.h.

231 {}

◆ onContextualize_()

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

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 188 of file samplingInference_tpl.h.

188 {}

◆ onEvidenceAdded_()

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

fired after a new evidence is inserted

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 192 of file samplingInference_tpl.h.

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

192  {
193  if (!isHardEvidence) {
194  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence")
195  }
196  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ onEvidenceChanged_()

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

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 205 of file samplingInference_tpl.h.

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

206  {
207  if (hasChangedSoftHard) {
208  GUM_ERROR(FatalError, "Approximated inference only accept hard evidence")
209  }
210  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ onEvidenceErased_()

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

fired before an evidence is removed

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 199 of file samplingInference_tpl.h.

199 {}

◆ onMarginalTargetAdded_()

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

fired after a new marginal target is inserted

Parameters
idThe target variable's id.

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 222 of file samplingInference_tpl.h.

222 {}

◆ onMarginalTargetErased_()

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

fired before a marginal target is removed

Parameters
idThe target variable's id.

Implements gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 225 of file samplingInference_tpl.h.

225 {}

◆ onModelChanged_()

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

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

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 213 of file samplingInference_tpl.h.

213 {}

◆ onStateChanged_()

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

fired when the stage is changed

Implements gum::GraphicalModelInference< GUM_SCALAR >.

Definition at line 234 of file samplingInference_tpl.h.

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

234  {
235  if (this->isInferenceReady()) {
236  _estimator_.clear();
237  this->initApproximationScheme();
238  }
239  }
void initApproximationScheme()
Initialise the scheme.
Estimator< GUM_SCALAR > _estimator_
Estimator object designed to approximate target posteriors.
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 134 of file approximationScheme_inl.h.

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

134 { 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 229 of file marginalTargetedInference_tpl.h.

229  {
230  if (this->hardEvidenceNodes().contains(node)) { return *(this->evidence()[node]); }
231 
232  if (!isTarget(node)) {
233  // throws UndefinedElement if var is not a target
234  GUM_ERROR(UndefinedElement, node << " is not a target node")
235  }
236 
237  if (!this->isInferenceDone()) { this->makeInference(); }
238 
239  return posterior_(node);
240  }
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:51

◆ 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 245 of file marginalTargetedInference_tpl.h.

245  {
246  return posterior(this->BN().idFromName(nodeName));
247  }
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)
overridevirtualinherited

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 104 of file samplingInference_tpl.h.

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

104  {
105  return _estimator_.posterior(this->BN().variable(id));
106  }
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 645 of file graphicalModelInference_tpl.h.

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

645  {
646  if (isInferenceReady() || isInferenceDone()) { return; }
647 
648  if (_model_ == nullptr)
649  GUM_ERROR(NullElement,
650  "No model been assigned to the "
651  "inference algorithm");
652 
655  else
657 
659  }
virtual void updateOutdatedPotentials_()=0
prepares inference when the latter is in OutdatedPotentials state
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i...
virtual void updateOutdatedStructure_()=0
prepares inference when the latter is in OutdatedStructure state
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
StateOfInference _state_
the current state of the inference (outdated/ready/done)
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
const GraphicalModel * _model_
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ remainingBurnIn()

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

Returns the remaining burn in.

Returns
Returns the remaining burn in.

Definition at line 191 of file approximationScheme_inl.h.

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

191  {
192  if (burn_in_ > current_step_) {
193  return burn_in_ - current_step_;
194  } else {
195  return 0;
196  }
197  }
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 ( )
inherited

get the BayesNet which is used to really perform the sampling

Definition at line 70 of file samplingInference_tpl.h.

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

70  {
71  this->prepareInference();
72  if (_samplingBN_ == nullptr)
73  return this->BN();
74  else
75  return *_samplingBN_;
76  }
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 64 of file BayesNetInference_tpl.h.

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

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

◆ setBurnIn()

template<typename GUM_SCALAR >
void gum::GibbsSampling< GUM_SCALAR >::setBurnIn ( Size  b)
inline

Number of burn in for one iteration.

Parameters
bThe number of burn in.
Exceptions
OutOfLowerBoundRaised if b < 1.

Definition at line 71 of file GibbsSampling.h.

71 { this->burn_in_ = b; };
Size burn_in_
Number of iterations before checking stopping criteria.

◆ setDrawnAtRandom()

template<typename GUM_SCALAR >
void gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom ( bool  atRandom)
inlineinherited

Definition at line 71 of file gibbsOperator.h.

References gum::GibbsOperator< GUM_SCALAR >::atRandom_.

71 { atRandom_ = atRandom; }

◆ 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:51
+ Here is the call graph for this function:

◆ setEstimatorFromBN_()

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

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 78 of file samplingInference_tpl.h.

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

78  {
79  _estimator_.setFromBN(&samplingBN(), this->hardEvidenceNodes());
80  this->isSetEstimator = true;
81  }
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 
)
virtualinherited

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 84 of file samplingInference_tpl.h.

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

86  {
87  _estimator_.setFromLBP(lbp, this->hardEvidenceNodes(), virtualLBPSize);
88  this->isSetEstimator = true;
89  }
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 84 of file approximationScheme_inl.h.

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

84  {
85  if (max < 1) { GUM_ERROR(OutOfLowerBound, "max should be >=1") }
86  max_iter_ = max;
87  enabled_max_iter_ = true;
88  }
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:51
+ 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 105 of file approximationScheme_inl.h.

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

105  {
106  if (timeout <= 0.) { GUM_ERROR(OutOfLowerBound, "timeout should be >0.") }
107  max_time_ = timeout;
108  enabled_max_time_ = true;
109  }
double max_time_
The timeout.
bool enabled_max_time_
If true, the timeout is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 63 of file approximationScheme_inl.h.

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

63  {
64  if (rate < 0) { GUM_ERROR(OutOfLowerBound, "rate should be >=0") }
65 
66  min_rate_eps_ = rate;
67  enabled_min_rate_eps_ = true;
68  }
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:51
+ Here is the call graph for this function:

◆ setModel_()

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

Definition at line 118 of file graphicalModelInference_tpl.h.

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

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

◆ setModelDuringConstruction_()

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

assigns a model during the inference engine construction

Definition at line 129 of file graphicalModelInference_tpl.h.

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

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

◆ setNbrDrawnVar()

template<typename GUM_SCALAR >
void gum::GibbsOperator< GUM_SCALAR >::setNbrDrawnVar ( Size  nbr)
inlineinherited

Definition at line 67 of file gibbsOperator.h.

67 { nbr_ = nbr; }

◆ setOutdatedPotentialsState_()

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

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

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

Definition at line 638 of file graphicalModelInference_tpl.h.

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

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

◆ setOutdatedStructureState_()

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

put the inference into an outdated model structure state

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

Definition at line 630 of file graphicalModelInference_tpl.h.

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

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

◆ 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 128 of file approximationScheme_inl.h.

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

128  {
129  if (p < 1) { GUM_ERROR(OutOfLowerBound, "p should be >=1") }
130 
131  period_size_ = p;
132  }
Size period_size_
Checking criteria frequency.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setState_()

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

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

Definition at line 98 of file graphicalModelInference_tpl.h.

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

98  {
99  if (_state_ != state) {
100  _state_ = state;
101  onStateChanged_();
102  }
103  }
virtual StateOfInference state() const noexcept final
returns the state of the inference engine
StateOfInference _state_
the current state of the inference (outdated/ready/done)
virtual void onStateChanged_()=0
fired when the stage is changed
+ Here is the call graph for this function:

◆ setTargetedMode_()

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

Definition at line 319 of file marginalTargetedInference_tpl.h.

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

◆ 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 137 of file approximationScheme_inl.h.

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

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

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

612  {
613  return _soft_evidence_nodes_;
614  }
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
+ Here is the call graph for this function:

◆ 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 178 of file approximationScheme_inl.h.

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

178  {
179  if (current_step_ < burn_in_) { return false; }
180 
181  if (period_size_ == 1) { return true; }
182 
183  return ((current_step_ - burn_in_) % period_size_ == 0);
184  }
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 92 of file graphicalModelInference_tpl.h.

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

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

◆ 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 143 of file approximationScheme_inl.h.

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

143  {
144  return current_state_;
145  }
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 200 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 201 of file marginalTargetedInference_tpl.h.

201  {
202  return _targets_;
203  }
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 187 of file approximationScheme_inl.h.

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

187  {
188  current_step_ += incr;
189  }
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_ ( )
overrideprotectedvirtualinherited

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 219 of file samplingInference_tpl.h.

219 {}

◆ updateOutdatedStructure_()

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

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 216 of file samplingInference_tpl.h.

216 {}

◆ 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 139 of file approximationScheme_inl.h.

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

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

Member Data Documentation

◆ _estimator_

template<typename GUM_SCALAR >
Estimator< GUM_SCALAR > gum::SamplingInference< GUM_SCALAR >::_estimator_
protectedinherited

Estimator object designed to approximate target posteriors.

Definition at line 178 of file samplingInference.h.

◆ atRandom_

template<typename GUM_SCALAR >
bool gum::GibbsOperator< GUM_SCALAR >::atRandom_
protectedinherited

◆ burn_in_

Size gum::ApproximationScheme::burn_in_
protectedinherited

Number of iterations before checking stopping criteria.

Definition at line 413 of file approximationScheme.h.

◆ counting_

template<typename GUM_SCALAR >
Size gum::GibbsOperator< GUM_SCALAR >::counting_
protectedinherited

Definition at line 80 of file gibbsOperator.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.

◆ hardEv_

template<typename GUM_SCALAR >
const NodeProperty< Idx >* gum::GibbsOperator< GUM_SCALAR >::hardEv_
protectedinherited

Definition at line 82 of file gibbsOperator.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
protectedinherited

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
protectedinherited

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.

◆ nbr_

template<typename GUM_SCALAR >
Size gum::GibbsOperator< GUM_SCALAR >::nbr_
protectedinherited

Definition at line 85 of file gibbsOperator.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 >
const IBayesNet< GUM_SCALAR >& gum::GibbsOperator< GUM_SCALAR >::samplingBn_
protectedinherited

Definition at line 81 of file gibbsOperator.h.

◆ samplingNodes_

template<typename GUM_SCALAR >
Sequence< NodeId > gum::GibbsOperator< GUM_SCALAR >::samplingNodes_
protectedinherited

Definition at line 83 of file gibbsOperator.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: