aGrUM  0.13.2
weightedSampling_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  ***************************************************************************/
29 
30 
31 namespace gum {
32 
33 
35  template < typename GUM_SCALAR >
37  const IBayesNet< GUM_SCALAR >* bn) :
38  SamplingInference< GUM_SCALAR >(bn) {
39  GUM_CONSTRUCTOR(WeightedSampling);
40  }
41 
42 
44  template < typename GUM_SCALAR >
46  GUM_DESTRUCTOR(WeightedSampling);
47  }
48 
49 
51  template < typename GUM_SCALAR >
54  return I;
55  }
56 
57 
58  template < typename GUM_SCALAR >
60  Instantiation prev) {
61  *w = 1.0f;
62  bool wrongValue = false;
63  do {
64  prev.clear();
65  wrongValue = false;
66  *w = 1.0f;
67 
68  for (const auto nod : this->BN().topologicalOrder()) {
69  if (this->hardEvidenceNodes().contains(nod)) {
70  prev.add(this->BN().variable(nod));
71  prev.chgVal(this->BN().variable(nod), this->hardEvidence()[nod]);
72  auto localp = this->BN().cpt(nod).get(prev);
73 
74  if (localp == 0) {
75  wrongValue = true;
76  break;
77  }
78 
79  *w *= localp;
80  } else {
81  this->_addVarSample(nod, &prev);
82  }
83  }
84  } while (wrongValue);
85  return prev;
86  }
87 } // 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:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
This file contains Weighted sampling class definition.
void clear()
Erase all variables from an Instantiation.
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
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
Instantiation _draw(float *w, Instantiation prev) override
draws a sample according to Weighted sampling