aGrUM  0.14.2
simpleBayesNetGenerator_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  ***************************************************************************/
20 
29 
30 namespace gum {
31 
32 #ifdef _MSC_VER
33 # define IBNG IBayesNetGenerator
34 #else
35 # define IBNG IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >
36 #endif
37 
38  // Use the SimpleCPTGenerator for generating the BNs CPT.
39  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
40  INLINE
42  Size nbrNodes, Size maxArcs, Size maxModality) :
43  IBNG(nbrNodes, maxArcs, maxModality) {
44  GUM_CONSTRUCTOR(SimpleBayesNetGenerator);
45  }
46 
47  // Use this constructor if you want to use a different policy for generating
48  // CPT than the default one.
49  // The cptGenerator will be erased when the destructor is called.
50  // @param cptGenerator The policy used to generate CPT.
51  /*template <typename GUM_SCALAR, template<class> class ICPTGenerator>
52  SimpleBayesNetGenerator<GUM_SCALAR,ICPTGenerator>::SimpleBayesNetGenerator(CPTGenerator*
53  cptGenerator ,Size nbrNodes, float density, Size maxModality):
54  IBayesNetGenerator<GUM_SCALAR,ICPTGenerator>(cptGenerator
55  ,nbrNodes,density,maxModality) {
56  GUM_CONSTRUCTOR ( SimpleBayesNetGenerator );
57  }*/
58 
59  // Destructor.
60  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
61  INLINE SimpleBayesNetGenerator< GUM_SCALAR,
62  ICPTGenerator >::~SimpleBayesNetGenerator() {
63  GUM_DESTRUCTOR(SimpleBayesNetGenerator);
64  }
65 
66  // Generates a bayesian network using floats.
67  // @param nbrNodes The number of nodes in the generated BN.
68  // @param density The probability of adding an arc between two nodes.
69  // @return A BNs randomly generated.
70 
71  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
73  BayesNet< GUM_SCALAR >& bayesNet) {
74  this->_bayesNet = bayesNet;
76  std::stringstream strBuff;
77 
78  for (Size i = 0; this->_nbrNodes > i; ++i) {
79  strBuff << "n" << i;
80  Size nb_mod =
81  (this->_maxModality == 2) ? 2 : 2 + randomValue(this->_maxModality - 1);
82  map.insert(
83  i, this->_bayesNet.add(LabelizedVariable(strBuff.str(), "", nb_mod)));
84  strBuff.str("");
85  }
86 
87  // We add arcs
88  float density = (float)(this->_maxArcs * 2)
89  / (float)(this->_nbrNodes * (this->_nbrNodes - 1));
90 
91  for (Size i = 0; i < this->_nbrNodes; ++i)
92  for (Size j = i + 1; j < this->_nbrNodes; ++j)
93  if (randomProba() < density) this->_bayesNet.addArc(map[i], map[j]);
94 
95  this->fillCPT();
96 
97  bayesNet = this->_bayesNet;
98  }
99 } /* namespace gum */
Class representing a Bayesian Network.
Definition: BayesNet.h:76
Class for generating bayesian networks.
class LabelizedVariable
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
void fillCPT()
function that insert random values in the CPT of each nodes according to the CPTGenerator.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
BayesNet< GUM_SCALAR > _bayesNet
#define IBNG
<agrum/BN/generator/simpleBayesNetGenerator.h>
void generateBN(BayesNet< GUM_SCALAR > &bayesNet) override
function that generates a bayesian networks.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
SimpleBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2)
Constructor.
~SimpleBayesNetGenerator() override
Destructor.