34 #define DEFAULT_MAXITER 10000000 35 #define DEFAULT_PERIOD_SIZE 100 36 #define DEFAULT_VERBOSITY false 37 #define DEFAULT_TIMEOUT 6000 38 #define DEFAULT_EPSILON 1e-2 39 #define DEFAULT_MIN_EPSILON_RATE 1e-5 44 template <
typename GUM_SCALAR >
48 __estimator(), __samplingBN(nullptr) {
59 template <
typename GUM_SCALAR >
69 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
84 template <
typename GUM_SCALAR >
92 template <
typename GUM_SCALAR >
98 template <
typename GUM_SCALAR >
104 template <
typename GUM_SCALAR >
110 template <
typename GUM_SCALAR >
121 for (
const auto elmt : this->
BN().dag().asNodeSet() - barren)
130 this->
BN().nodes().asNodeSet(),
136 auto nonRequisite = this->
BN().dag().asNodeSet() - requisite;
138 for (
const auto elmt : nonRequisite)
142 I.
add(this->
BN().variable(hard));
145 for (
const auto& child : this->
BN().children(hard)) {
147 *p = this->
BN().cpt(child).extract(I);
157 template <
typename GUM_SCALAR >
163 template <
typename GUM_SCALAR >
175 Ip = this->
_draw(&w, Ip);
184 template <
typename GUM_SCALAR >
194 template <
typename GUM_SCALAR >
199 template <
typename GUM_SCALAR >
201 bool isHardEvidence) {
202 if (!isHardEvidence) {
207 template <
typename GUM_SCALAR >
209 bool isHardEvidence) {}
211 template <
typename GUM_SCALAR >
213 bool contains_hard_evidence) {}
215 template <
typename GUM_SCALAR >
218 bool hasChangedSoftHard) {
219 if (hasChangedSoftHard) {
224 template <
typename GUM_SCALAR >
228 template <
typename GUM_SCALAR >
231 template <
typename GUM_SCALAR >
234 template <
typename GUM_SCALAR >
237 template <
typename GUM_SCALAR >
240 template <
typename GUM_SCALAR >
243 template <
typename GUM_SCALAR >
246 template <
typename GUM_SCALAR >
void _onAllEvidenceErased(bool contains_hard_evidence) override
fired before all the evidence are erased
aGrUM's Potential is a multi-dimensional array with tensor operators.
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
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
void _updateOutdatedBNStructure() override
prepares inference when the latter is in OutdatedBNStructure state
d-separation analysis (as described in Koller & Friedman 2009)
the d-separation algorithm as described in Koller & Friedman (2009)
void setPeriodSize(Size p)
How many samples between two stopping is enable.
virtual void prepareInference() final
prepare the internal inference structures for the next inference
~SamplingInference() override
destructor
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void _loopApproxInference()
void initApproximationScheme()
Initialise the scheme.
virtual void _setEstimatorFromBN()
Initializes the estimators object linked to the simulation.
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void _onMarginalTargetAdded(const NodeId id) override
fired after a new marginal target is inserted
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
#define DEFAULT_PERIOD_SIZE
void setMaxTime(double timeout)
Stopping criterion on timeout.
void _onStateChanged() override
fired when the stage is changed
void _onAllMarginalTargetsAdded() override
fired after all the nodes of the BN are added as marginal targets
void _onEvidenceAdded(const NodeId id, bool isHardEvidence) override
fired after a new evidence is inserted
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
const NodeSet & softEvidenceNodes() const
returns the set of nodes with soft evidence
void _onEvidenceErased(const NodeId id, bool isHardEvidence) override
fired before an evidence is removed
This file contains general methods for simulation-oriented approximate inference. ...
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
const Potential< GUM_SCALAR > & currentPosterior(NodeId id)
Computes and returns the actual estimation of the posterior of a node.
virtual Instantiation _burnIn()=0
draws samples without updating the estimators
Estimator< GUM_SCALAR > __estimator
Estimator object designed to approximate target posteriors.
<agrum/BN/inference/loopyBeliefPropagation.h>
NodeSet barrenNodes()
returns the set of barren nodes
Portion of a BN identified by the list of nodes and a BayesNet.
BayesNetFragment< GUM_SCALAR > * __samplingBN
const Potential< GUM_SCALAR > & _posterior(NodeId id) override
Computes and returns the posterior of a node.
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
#define DEFAULT_MIN_EPSILON_RATE
void _makeInference() override
makes the inference by generating samples
Class for assigning/browsing values to tuples of discrete variables.
void setMaxIter(Size max)
Stopping criterion on number of iterations.
void requisiteNodes(const DAG &dag, const NodeSet &query, const NodeSet &hardEvidence, const NodeSet &softEvidence, NodeSet &requisite)
Fill the 'requisite' nodeset with the requisite nodes in dag given a query and evidence.
Detect barren nodes for inference in Bayesian networks.
virtual void _setEstimatorFromLBP(LoopyBeliefPropagation< GUM_SCALAR > *lbp, GUM_SCALAR virtualLBPSize)
Initializes the estimators object linked to the simulation.
void _onMarginalTargetErased(const NodeId id) override
fired before a marginal target is removed
bool isContextualized
whether the referenced Bayesian Network has been "contextualized"
void _onAllMarginalTargetsErased() override
fired before a all marginal targets are removed
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
void setTargets(const NodeSet *target_nodes)
sets the set of target nodes we are interested in
void setEvidence(const NodeSet *observed_nodes)
sets the observed nodes in the DAG
virtual void _onContextualize(BayesNetFragment< GUM_SCALAR > *bn)
fired when Bayesian network is 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.
Detect barren nodes for inference in Bayesian networks.
void _updateOutdatedBNPotentials() override
prepares inference when the latter is in OutdatedBNPotentials state
bool isSetEstimator
whether the Estimator object has been initialized
virtual void _addVarSample(NodeId nod, Instantiation *I)
adds a node to current instantiation
void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn) override
fired after a new Bayes net has been assigned to the engine
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size NodeId
Type for node ids.
#define DEFAULT_VERBOSITY
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
#define GUM_ERROR(type, msg)
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
Class representing Fragment of Bayesian networks.
void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard) override
fired after an evidence is changed, in particular when its status (soft/hard) changes ...