aGrUM  0.14.2
simpleCPTDisturber_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 namespace gum {
29 
30  // Default constructor.
31  template < typename GUM_SCALAR >
33  ICPTDisturber< GUM_SCALAR >() {
34  GUM_CONSTRUCTOR(SimpleCPTDisturber);
35  }
36 
37  // Destructor.
38  template < typename GUM_SCALAR >
40  GUM_DESTRUCTOR(SimpleCPTDisturber);
41  }
42 
43  // Generates a CPT using GUM_SCALAR.
44  // @param varID The variable id of the CPT owner.
45  // @param cpt A reference on the CPT to fill.
46 
47  template < typename GUM_SCALAR >
49  NodeId node,
50  BayesNet< GUM_SCALAR >& bayesNet,
53  Instantiation i_src(src);
54  Instantiation i_dest(bayesNet.cpt(node));
55  Instantiation i_marg(marg);
56 
57  for (i_dest.setFirst(); !i_dest.end(); ++i_dest) {
58  GUM_SCALAR potval = 0;
59 
60  i_src.setVals(i_dest);
61  for (i_marg.setFirst(); !i_marg.end(); ++i_marg) {
62  i_src.setVals(i_marg);
63  potval += src.get(i_src) * marg.get(i_marg);
64  }
65 
66  bayesNet.cpt(node).set(i_dest, potval);
67  }
68 
69  bayesNet.cpt(node).normalizeAsCPT();
70  }
71 
72  template < typename GUM_SCALAR >
74  NodeId node,
75  BayesNet< GUM_SCALAR >& bayesNet,
77  GUM_SCALAR variation) {
78  Instantiation i(bayesNet.cpt(node));
79 
80  for (i.setFirst(); !i.end(); ++i)
81  bayesNet.cpt(node).set(i,
82  src.get(i) + GUM_SCALAR(randomProba()) * variation);
83 
84  bayesNet.cpt(node).normalizeAsCPT();
85  }
86 
87 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
Class representing a Bayesian Network.
Definition: BayesNet.h:76
Source implementation of SimpleCPTDisturber.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
double randomProba()
Returns a random double between 0 and 1 included (i.e.
<agrum/BN/generator/simpleCPTDisturber.h>
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:312
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
void disturbAugmCPT(NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Potential< GUM_SCALAR > &src, GUM_SCALAR variation) override
Disturb a CPT using GUM_SCALAR when inserting a new parent new_parent.
SimpleCPTDisturber()
Default constructor.
~SimpleCPTDisturber() override
Destructor.
void disturbReducCPT(NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Potential< GUM_SCALAR > &src, Potential< GUM_SCALAR > &marg) override
Disturb a CPT using GUM_SCALAR when removing parent varIdi.
Size NodeId
Type for node ids.
Definition: graphElements.h:97