aGrUM  0.14.2
simpleCPTGenerator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and 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  ***************************************************************************/
27 
28 #include <ctime>
29 
30 namespace gum {
31 
32  // Default constructor.
33  template < typename GUM_SCALAR >
35  ICPTGenerator< GUM_SCALAR >() {
36  GUM_CONSTRUCTOR(SimpleCPTGenerator);
37  }
38 
39  // Destructor.
40  template < typename GUM_SCALAR >
42  GUM_DESTRUCTOR(SimpleCPTGenerator);
43  }
44 
45  // Generates a CPT using floats.
46  // @param varID The variable id of the CPT owner.
47  // @param cpt A reference on the CPT to fill.
48 
49  template < typename GUM_SCALAR >
51  const Idx& varId, const Potential< GUM_SCALAR >& cpt) {
52  std::vector< GUM_SCALAR > v;
53 
54  v.reserve(cpt.domainSize());
55  for (Size i = 0; i < cpt.domainSize(); ++i) {
56  v.push_back((GUM_SCALAR)randomValue(10000));
57  }
58 
59  cpt.fillWith(v);
60  Instantiation varInst;
61  varInst.add(cpt.variable(varId));
62  Instantiation cptInst(cpt);
63 
64  for (cptInst.setFirstOut(varInst); !cptInst.end(); cptInst.incOut(varInst)) {
65  GUM_SCALAR sum = (GUM_SCALAR)0;
66 
67  for (cptInst.setFirstIn(varInst); !cptInst.end(); cptInst.incIn(varInst)) {
68  sum += cpt[cptInst];
69  }
70 
71  for (cptInst.setFirstIn(varInst); !cptInst.end(); cptInst.incIn(varInst)) {
72  cpt.set(cptInst, cpt[cptInst] / sum);
73  }
74 
75  cptInst.unsetEnd();
76  }
77  }
78 
79 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
Abstract class for generating Conditional Probability Tables.
virtual Size domainSize() const final
Returns the product of the variables domain size.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void unsetEnd()
Alias for unsetOverflow().
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
SimpleCPTGenerator()
Default constructor.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
~SimpleCPTGenerator() override
Destructor.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
<agrum/BN/generator/simpleCPTGenerator.h>
void incIn(const Instantiation &i)
Operator increment for the variables in i.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
Size Idx
Type for indexes.
Definition: types.h:50
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void generateCPT(const Idx &varId, const Potential< GUM_SCALAR > &cpt) override
Generates a CPT using floats.
bool end() const
Returns true if the Instantiation reached the end.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.