aGrUM  0.16.0
simpleBayesNetGenerator_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33 
34 #ifdef _MSC_VER
35 # define IBNG IBayesNetGenerator
36 #else
37 # define IBNG IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >
38 #endif
39 
40  // Use the SimpleCPTGenerator for generating the BNs CPT.
41  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
42  INLINE
44  Size nbrNodes, Size maxArcs, Size maxModality) :
45  IBNG(nbrNodes, maxArcs, maxModality) {
46  GUM_CONSTRUCTOR(SimpleBayesNetGenerator);
47  }
48 
49  // Use this constructor if you want to use a different policy for generating
50  // CPT than the default one.
51  // The cptGenerator will be erased when the destructor is called.
52  // @param cptGenerator The policy used to generate CPT.
53  /*template <typename GUM_SCALAR, template<class> class ICPTGenerator>
54  SimpleBayesNetGenerator<GUM_SCALAR,ICPTGenerator>::SimpleBayesNetGenerator(CPTGenerator*
55  cptGenerator ,Size nbrNodes, float density, Size maxModality):
56  IBayesNetGenerator<GUM_SCALAR,ICPTGenerator>(cptGenerator
57  ,nbrNodes,density,maxModality) {
58  GUM_CONSTRUCTOR ( SimpleBayesNetGenerator );
59  }*/
60 
61  // Destructor.
62  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
63  INLINE SimpleBayesNetGenerator< GUM_SCALAR,
64  ICPTGenerator >::~SimpleBayesNetGenerator() {
65  GUM_DESTRUCTOR(SimpleBayesNetGenerator);
66  }
67 
68  // Generates a bayesian network using floats.
69  // @param nbrNodes The number of nodes in the generated BN.
70  // @param density The probability of adding an arc between two nodes.
71  // @return A BNs randomly generated.
72 
73  template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
75  BayesNet< GUM_SCALAR >& bayesNet) {
76  this->_bayesNet = bayesNet;
78  std::stringstream strBuff;
79 
80  for (Size i = 0; this->_nbrNodes > i; ++i) {
81  strBuff << "n" << i;
82  Size nb_mod =
83  (this->_maxModality == 2) ? 2 : 2 + randomValue(this->_maxModality - 1);
84  map.insert(
85  i, this->_bayesNet.add(LabelizedVariable(strBuff.str(), "", nb_mod)));
86  strBuff.str("");
87  }
88 
89  // We add arcs
90  float density = (float)(this->_maxArcs * 2)
91  / (float)(this->_nbrNodes * (this->_nbrNodes - 1));
92 
93  for (Size i = 0; i < this->_nbrNodes; ++i)
94  for (Size j = i + 1; j < this->_nbrNodes; ++j)
95  if (randomProba() < density) this->_bayesNet.addArc(map[i], map[j]);
96 
97  this->fillCPT();
98 
99  bayesNet = this->_bayesNet;
100  }
101 } /* namespace gum */
Class representing a Bayesian Network.
Definition: BayesNet.h:78
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:48
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.