aGrUM  0.14.2
MonteCarloSampling_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES et Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
30 
31 
32 namespace gum {
33 
35  template < typename GUM_SCALAR >
37  const IBayesNet< GUM_SCALAR >* bn) :
38  SamplingInference< GUM_SCALAR >(bn) {
39  GUM_CONSTRUCTOR(MonteCarloSampling);
40  }
41 
43  template < typename GUM_SCALAR >
45  GUM_DESTRUCTOR(MonteCarloSampling);
46  }
47 
49  template < typename GUM_SCALAR >
52  return I;
53  }
54 
55 
56  template < typename GUM_SCALAR >
58  Instantiation prev) {
59  *w = 1.0f;
60  bool wrong_value = false;
61  do {
62  wrong_value = false;
63  prev.clear();
64  for (const auto nod : this->BN().topologicalOrder()) {
65  this->_addVarSample(nod, &prev);
66  if (this->hardEvidenceNodes().contains(nod)
67  && prev.val(this->BN().variable(nod)) != this->hardEvidence()[nod]) {
68  wrong_value = true;
69  break;
70  }
71  }
72  } while (wrong_value);
73  return prev;
74  }
75 } // namespace gum
This file contains Monte Carlo sampling class definition.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Instantiation _burnIn() override
draws a defined number of samples without updating the estimators
Idx val(Idx i) const
Returns the current value of the variable at position i.
void clear()
Erase all variables from an Instantiation.
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
~MonteCarloSampling() override
Destructor.
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:80
virtual void _addVarSample(NodeId nod, Instantiation *I)
adds a node to current instantiation
Instantiation _draw(GUM_SCALAR *w, Instantiation prev) override
draws a sample according to classic Monte Carlo sampling
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
MonteCarloSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.