aGrUM  0.16.0
gibbsOperator_tpl.h
Go to the documentation of this file.
1 
33 
34 namespace gum {
35 
36  template < typename GUM_SCALAR >
38  const NodeProperty< Idx >* hardEv,
39  Size nbr,
40  bool atRandom) :
41  _counting(0),
42  _sampling_bn(BN), _hardEv(hardEv), _nbr(nbr), _atRandom(atRandom) {
44  GUM_CONSTRUCTOR(GibbsOperator);
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(GibbsOperator);
50  }
51 
52  template < typename GUM_SCALAR >
55  for (const auto node : _sampling_bn.nodes())
56  if (_hardEv == nullptr || !_hardEv->exists(node))
57  _samplingNodes.insert(node);
58  if (_samplingNodes.size() == 0) {
60  "No node to sample (too many nodes or too much evidence)!")
61  }
63  }
64 
69  template < typename GUM_SCALAR >
72 
73  for (const auto nod : _sampling_bn.topologicalOrder()) {
74  I.add(_sampling_bn.variable(nod));
75  if (_hardEv != nullptr && _hardEv->exists(nod)) {
76  I.chgVal(_sampling_bn.variable(nod), (*_hardEv)[nod]);
77  } else {
78  __drawVarMonteCarlo(nod, &I);
79  }
80  }
81  return I;
82  }
83 
84  template < typename GUM_SCALAR >
86  Instantiation* I) {
87  gum::Instantiation Itop(*I);
88  Itop.erase(_sampling_bn.variable(nod));
89  I->chgVal(_sampling_bn.variable(nod),
90  _sampling_bn.cpt(nod).extract(Itop).draw());
91  }
92 
93 
94  template < typename GUM_SCALAR >
96  for (Idx i = 0; i < _nbr; i++) {
99  this->__GibbsSample(_samplingNodes[pos], &prev);
100  _counting++;
101  }
102  return prev;
103  }
105 
106  template < typename GUM_SCALAR >
108  gum::Instantiation Itop(*I);
109  Itop.erase(_sampling_bn.variable(id));
110  gum::Potential< GUM_SCALAR > p = _sampling_bn.cpt(id).extract(Itop);
111  for (const auto nod : _sampling_bn.children(id))
112  p *= _sampling_bn.cpt(nod).extract(Itop);
113  GUM_ASSERT(p.nbrDim() == 1);
114  if (p.sum() != 0) {
115  p.normalize();
116  I->chgVal(_sampling_bn.variable(id), p.draw());
117  }
118  }
119 } // namespace gum
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
void clear()
Clear the sequence.
Definition: sequence_tpl.h:271
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
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:38
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Sequence< NodeId > _samplingNodes
Definition: gibbsOperator.h:85
void __drawVarMonteCarlo(NodeId nod, Instantiation *I)
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
The class for generic Hash Tables.
Definition: hashTable.h:679
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
Size _counting
number of samples drawn
Definition: gibbsOperator.h:82
Idx draw() const
get a value at random from a 1-D distribution
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
void __GibbsSample(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
virtual ~GibbsOperator()
Destructor.
const NodeProperty< Idx > * _hardEv
Definition: gibbsOperator.h:84
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
Instantiation monteCarloSample()
draws a Monte Carlo sample
GUM_SCALAR sum() const
sum of all elements in the Potential
Size Idx
Type for indexes.
Definition: types.h:53
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
class containing all variables and methods required for Gibbssampling
Definition: gibbsOperator.h:50
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408