aGrUM  0.16.0
simpleCPTGenerator_tpl.h
Go to the documentation of this file.
1 
30 
31 #include <ctime>
32 
33 namespace gum {
34 
35  // Default constructor.
36  template < typename GUM_SCALAR >
38  ICPTGenerator< GUM_SCALAR >() {
39  GUM_CONSTRUCTOR(SimpleCPTGenerator);
40  }
41 
42  // Destructor.
43  template < typename GUM_SCALAR >
45  GUM_DESTRUCTOR(SimpleCPTGenerator);
46  }
47 
48  // Generates a CPT using floats.
49  // @param varID The variable id of the CPT owner.
50  // @param cpt A reference on the CPT to fill.
51 
52  template < typename GUM_SCALAR >
54  const Idx& varId, const Potential< GUM_SCALAR >& cpt) {
55  cpt.random();
56 
57  const auto& var = cpt.variable(varId);
58  Instantiation cptInst(cpt);
59 
60  for (cptInst.setFirstNotVar(var); !cptInst.end(); cptInst.incNotVar(var)) {
61  GUM_SCALAR sum = (GUM_SCALAR)0;
62 
63  for (cptInst.setFirstVar(var); !cptInst.end(); cptInst.incVar(var)) {
64  sum += cpt[cptInst];
65  }
66 
67  for (cptInst.setFirstVar(var); !cptInst.end(); cptInst.incVar(var)) {
68  cpt.set(cptInst, cpt[cptInst] / sum);
69  }
70 
71  cptInst.unsetEnd();
72  }
73  }
74 
75 } /* 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.
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void unsetEnd()
Alias for unsetOverflow().
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
SimpleCPTGenerator()
Default constructor.
~SimpleCPTGenerator() override
Destructor.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
<agrum/BN/generator/simpleCPTGenerator.h>
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
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:53
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.