aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator > Class Template Reference

<agrum/BN/generator/simpleBayesNetGenerator.h> More...

#include <simpleBayesNetGenerator.h>

+ Inheritance diagram for gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >:
+ Collaboration diagram for gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >:

Public Member Functions

Constructors / Destructor
 SimpleBayesNetGenerator (Size nbrNodes, Size maxArcs, Size maxModality=2)
 Constructor. More...
 
 ~SimpleBayesNetGenerator () override
 Destructor. More...
 
BN generation methods
void generateBN (BayesNet< GUM_SCALAR > &bayesNet) override
 function that generates a Bayesian networks. More...
 
BN generation methods
void fillCPT ()
 function that insert random values in the CPT of each nodes according to the CPTGenerator. More...
 
Getters
Size nbrNodes () const
 Return a constant reference to the number of nodes imposed on the IBayesNetGenerator. More...
 
Size maxArcs () const
 Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator. More...
 
Size maxModality () const
 Return a constant reference to the maximum modality imposed on the IBayesNetGenerator. More...
 
Setters
void setNbrNodes (Size nbrNodes)
 Modifies the value of the number of nodes imposed on the BayesGenerator. More...
 
void setMaxArcs (Size maxArcs)
 Modifies the value of the number of nodes imposed on the BayesGenerator. More...
 
void setMaxModality (Size maxModality)
 Modifies the value of the number of nodes imposed on the BayesGenerator. More...
 

Protected Attributes

Size nbrNodes_
 
Size maxArcs_
 
Size maxModality_
 
BayesNet< GUM_SCALAR > bayesNet_
 

Detailed Description

template<typename GUM_SCALAR, template< typename > class ICPTGenerator = SimpleCPTGenerator>
class gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >

<agrum/BN/generator/simpleBayesNetGenerator.h>

Class for generating Bayesian networks.

This class randomly generates a Bayesian network given three parameters: the number of nodes and the max number of arcs and the number of maximum modality for each nodes.

Warning
Be Careful when entering the parameters, high Values may cause the density of the Bayesian network to be too high resulting in the failure of most of the inference Methods.

Definition at line 57 of file simpleBayesNetGenerator.h.

Constructor & Destructor Documentation

◆ SimpleBayesNetGenerator()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator ( Size  nbrNodes,
Size  maxArcs,
Size  maxModality = 2 
)

Constructor.

Use by default the SimpleCPTGenerator for generating the BNs CPT.

Parameters
nbrNodesThe number of nodes imposed on the generator.
maxArcsThe number of maximum arcs imposed on the generator.
maxModalityEach DRV has from 2 to maxModality modalities

Definition at line 41 of file simpleBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

44  :
46  GUM_CONSTRUCTOR(SimpleBayesNetGenerator);
47  }
Size nbrNodes() const
Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.
#define IBNG
Size maxArcs() const
Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.
SimpleBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2)
Constructor.
Size maxModality() const
Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.
+ Here is the call graph for this function:

◆ ~SimpleBayesNetGenerator()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::~SimpleBayesNetGenerator ( )
override

Destructor.

Definition at line 63 of file simpleBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

63  {
64  GUM_DESTRUCTOR(SimpleBayesNetGenerator);
65  }
SimpleBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2)
Constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ fillCPT()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
void gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::fillCPT ( )
inherited

function that insert random values in the CPT of each nodes according to the CPTGenerator.

Returns
null but modify inputed empty Bayesian network

Definition at line 62 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

62  {
63  for (auto node: bayesNet_.nodes())
64  this->generateCPT(bayesNet_.cpt(node).pos(bayesNet_.variable(node)), bayesNet_.cpt(node));
65  }
BayesNet< GUM_SCALAR > bayesNet_
+ Here is the call graph for this function:

◆ generateBN()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
void gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN ( BayesNet< GUM_SCALAR > &  bayesNet)
overridevirtual

function that generates a Bayesian networks.

Parameters
bayesNetBayesian network to be completed after initialisation
Returns
null but modify inputed Bayesian network

Implements gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >.

Definition at line 73 of file simpleBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

74  {
75  this->bayesNet_ = bayesNet;
76  HashTable< Size, NodeId > map;
77  std::stringstream strBuff;
78 
79  for (Size i = 0; this->nbrNodes_ > i; ++i) {
80  strBuff << "n" << i;
81  Size nb_mod = (this->maxModality_ == 2) ? 2 : 2 + randomValue(this->maxModality_ - 1);
82  map.insert(i, this->bayesNet_.add(LabelizedVariable(strBuff.str(), "", nb_mod)));
83  strBuff.str("");
84  }
85 
86  // We add arcs
87  float density = (float)(this->maxArcs_ * 2) / (float)(this->nbrNodes_ * (this->nbrNodes_ - 1));
88 
89  for (Size i = 0; i < this->nbrNodes_; ++i)
90  for (Size j = i + 1; j < this->nbrNodes_; ++j)
91  if (randomProba() < density) this->bayesNet_.addArc(map[i], map[j]);
92 
93  this->fillCPT();
94 
95  bayesNet = this->bayesNet_;
96  }
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
BayesNet< GUM_SCALAR > bayesNet_
+ Here is the call graph for this function:

◆ maxArcs()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::maxArcs ( ) const
inherited

Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.

Definition at line 78 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

78  {
79  return maxArcs_;
80  }
+ Here is the call graph for this function:

◆ maxModality()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::maxModality ( ) const
inherited

Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.

Definition at line 68 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

68  {
69  return maxModality_;
70  }
+ Here is the call graph for this function:

◆ nbrNodes()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::nbrNodes ( ) const
inherited

Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.

Definition at line 73 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

73  {
74  return nbrNodes_;
75  }
+ Here is the call graph for this function:

◆ setMaxArcs()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE void gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::setMaxArcs ( Size  maxArcs)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 98 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

98  {
99  if (maxArcs < nbrNodes_ - 1 || maxArcs > (nbrNodes_ * (nbrNodes_ - 1)) / 2)
100  GUM_ERROR(OperationNotAllowed, " maxArcs value not possible ")
101 
102  maxArcs_ = maxArcs;
103  }
Size maxArcs() const
Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setMaxModality()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE void gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::setMaxModality ( Size  maxModality)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 83 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

83  {
84  if (maxModality < 2)
85  GUM_ERROR(OperationNotAllowed, " maxModality must be at least equal to two ")
86 
88  }
Size maxModality() const
Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setNbrNodes()

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
INLINE void gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::setNbrNodes ( Size  nbrNodes)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 90 of file IBayesNetGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

90  {
91  if ((maxArcs_ < nbrNodes - 1) || (maxArcs_ > (nbrNodes * (nbrNodes - 1)) / 2))
92  GUM_ERROR(OperationNotAllowed, " nbrNodes value not possible ")
93 
95  }
Size nbrNodes() const
Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

Member Data Documentation

◆ bayesNet_

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
BayesNet< GUM_SCALAR > gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::bayesNet_
protectedinherited

Definition at line 166 of file IBayesNetGenerator.h.

◆ maxArcs_

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::maxArcs_
protectedinherited

Definition at line 164 of file IBayesNetGenerator.h.

◆ maxModality_

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::maxModality_
protectedinherited

Definition at line 165 of file IBayesNetGenerator.h.

◆ nbrNodes_

template<typename GUM_SCALAR , template< typename > class ICPTGenerator>
Size gum::IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::nbrNodes_
protectedinherited

Definition at line 163 of file IBayesNetGenerator.h.


The documentation for this class was generated from the following files: