aGrUM  0.16.0
weightedSampling_tpl.h
Go to the documentation of this file.
1 
32 
33 
34 namespace gum {
35 
36 
38  template < typename GUM_SCALAR >
40  const IBayesNet< GUM_SCALAR >* bn) :
41  SamplingInference< GUM_SCALAR >(bn) {
42  GUM_CONSTRUCTOR(WeightedSampling);
43  }
44 
45 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(WeightedSampling);
50  }
51 
52 
54  template < typename GUM_SCALAR >
57  return I;
58  }
59 
60 
61  template < typename GUM_SCALAR >
63  Instantiation prev) {
64  *w = 1.0f;
65  bool wrongValue = false;
66  do {
67  prev.clear();
68  wrongValue = false;
69  *w = 1.0f;
70 
71  for (const auto nod : this->BN().topologicalOrder()) {
72  if (this->hardEvidenceNodes().contains(nod)) {
73  prev.add(this->BN().variable(nod));
74  prev.chgVal(this->BN().variable(nod), this->hardEvidence()[nod]);
75  auto localp = this->BN().cpt(nod).get(prev);
76 
77  if (localp == 0) {
78  wrongValue = true;
79  break;
80  }
81 
82  *w *= localp;
83  } else {
84  this->_addVarSample(nod, &prev);
85  }
86  }
87  } while (wrongValue);
88  return prev;
89  }
90 } // namespace gum
WeightedSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
~WeightedSampling() override
Destructor.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void clear()
Erase all variables from an Instantiation.
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
Instantiation _draw(GUM_SCALAR *w, Instantiation prev) override
draws a sample according to Weighted sampling
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
virtual void _addVarSample(NodeId nod, Instantiation *I)
adds a node to current instantiation
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Instantiation _burnIn() override
draws a defined number of samples without updating the estimators