aGrUM 2.3.2
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.
 ~SimpleBayesNetGenerator () override
 Destructor.
BN generation methods
void generateBN (BayesNet< GUM_SCALAR > &bayesNet) override
 function that generates a Bayesian networks.
BN generation methods
void fromDAG (BayesNet< GUM_SCALAR > &bayesNet)
 Virtual function that Generates a Bayesian networks.
void fillCPT (BayesNet< GUM_SCALAR > &bn) const
 function that insert random values in the CPT of each nodes according to the CPTGenerator.
Getters
Size nbrNodes () const
 Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.
Size maxArcs () const
 Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.
Size maxModality () const
 Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.
Setters
void setNbrNodes (Size nbrNodes)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
void setMaxArcs (Size maxArcs)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
void setMaxModality (Size maxModality)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
CPT generation methods
void generateCPT (const Idx &varId, const Tensor< GUM_SCALAR > &cpt) const override
 Generates a CPT using floats.

Protected Attributes

Size nbrNodes_
Size maxArcs_
Size maxModality_
DAG dag_

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 78 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 57 of file simpleBayesNetGenerator_tpl.h.

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::IBayesNetGenerator(), SimpleBayesNetGenerator(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality(), and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes().

Referenced by SimpleBayesNetGenerator(), and ~SimpleBayesNetGenerator().

Here is the call graph for this function:
Here is the caller 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 79 of file simpleBayesNetGenerator_tpl.h.

References SimpleBayesNetGenerator().

Here is the call graph for this function:

Member Function Documentation

◆ fillCPT()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fillCPT ( BayesNet< GUM_SCALAR > & bn) const
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 130 of file IBayesNetGenerator_tpl.h.

82 {
83 for (auto node: bn.nodes())
84 this->generateCPT(bn.cpt(node).pos(bn.variable(node)), bn.cpt(node));
85 }
Class for generating Bayesian networks.
void generateCPT(const Idx &varId, const Tensor< GUM_SCALAR > &cpt) const override
Generates a CPT using floats.

References dag_, maxModality_, and gum::randomValue().

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::generateBN(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromDAG()

INLINE void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fromDAG ( BayesNet< GUM_SCALAR > & bayesNet)
inherited

Virtual function that Generates a Bayesian networks.

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

Definition at line 122 of file IBayesNetGenerator_tpl.h.

127 {
128 bn.clear();
129
130 const auto width = (this->dag_.size() >= 100) ? 3 : 2;
131 int n = 0;
132 const auto& topo = this->dag_.topologicalOrder();
133 for (const auto node: topo) {
135 strBuff << "X" << std::setfill('0') << std::setw(width) << n++;
136 bn.add(RangeVariable(strBuff.str(), "", 0, long(1 + randomValue(this->maxModality_ - 1))),
137 node);
138 }
139 bn.beginTopologyTransformation();
140 for (auto arc: this->dag_.arcs()) {
141 bn.addArc(arc.tail(), arc.head());
142 }
143 bn.endTopologyTransformation();
144 }

References maxArcs(), and maxArcs_.

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::generateBN(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN().

Here is the call graph for this function:
Here is the caller 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, SimpleCPTGenerator >.

Definition at line 89 of file simpleBayesNetGenerator_tpl.h.

90 {
91 this->dag_.clear();
92 this->dag_.addNodes(this->nbrNodes_);
93
94 // We add arcs
95 float density = (float)(this->maxArcs_ * 2) / (float)(this->nbrNodes_ * (this->nbrNodes_ - 1));
96 for (Size i = 0; i < this->nbrNodes_; ++i)
97 for (Size j = i + 1; j < this->nbrNodes_; ++j)
98 if (randomProba() < density) this->dag_.addArc(i, j);
99
100 // Adding arcs until we reach the maxArcs_ number
101 while (this->dag_.sizeArcs() < this->maxArcs_) {
102 Size i = randomValue(this->nbrNodes_);
103 Size j = randomValue(this->nbrNodes_);
104 if (i != j) {
105 if (i > j) std::swap(i, j);
106
107 if (!this->dag_.existsArc(i, j)) this->dag_.addArc(i, j);
108 }
109 }
110
111 this->fromDAG(bayesNet);
112 this->fillCPT(bayesNet);
113 }
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
double randomProba()
Returns a random double between 0 and 1 included (i.e.

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fillCPT(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fromDAG(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs_, gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes_, gum::randomProba(), and gum::randomValue().

Here is the call graph for this function:

◆ generateCPT()

template<typename GUM_SCALAR>
void gum::SimpleCPTGenerator< GUM_SCALAR >::generateCPT ( const Idx & varId,
const Tensor< GUM_SCALAR > & cpt ) const
overrideinherited

Generates a CPT using floats.

Parameters
varIdThe variable id of the CPT owner.
cptA reference on the CPT to fill.

Definition at line 72 of file simpleCPTGenerator_tpl.h.

73 {
74 cpt.random().normalizeAsCPT(varId);
75 }
<agrum/BN/generator/simpleCPTGenerator.h>

◆ maxArcs()

INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs ( ) const
inherited

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

Definition at line 151 of file IBayesNetGenerator_tpl.h.

98 {
99 return maxArcs_;
100 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator(), and fromDAG().

Here is the caller graph for this function:

◆ maxModality()

INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality ( ) const
inherited

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

Definition at line 157 of file IBayesNetGenerator_tpl.h.

88 {
89 return maxModality_;
90 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator(), and ~IBayesNetGenerator().

Here is the caller graph for this function:

◆ nbrNodes()

INLINE Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes ( ) const
inherited

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

Definition at line 145 of file IBayesNetGenerator_tpl.h.

93 {
94 return nbrNodes_;
95 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator().

Here is the caller graph for this function:

◆ setMaxArcs()

INLINE void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setMaxArcs ( Size maxArcs)
inherited

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

Definition at line 172 of file IBayesNetGenerator_tpl.h.

119 {
121 GUM_ERROR(OperationNotAllowed, " maxArcs value not possible ")
122
124 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

◆ setMaxModality()

INLINE void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setMaxModality ( Size maxModality)
inherited

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

Definition at line 177 of file IBayesNetGenerator_tpl.h.

103 {
104 if (maxModality < 2)
105 GUM_ERROR(OperationNotAllowed, " maxModality must be at least equal to two ")
106
108 }

◆ setNbrNodes()

INLINE void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setNbrNodes ( Size nbrNodes)
inherited

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

Definition at line 167 of file IBayesNetGenerator_tpl.h.

111 {
112 if ((maxArcs_ < nbrNodes - 1) || (maxArcs_ > (nbrNodes * (nbrNodes - 1)) / 2))
113 GUM_ERROR(OperationNotAllowed, " nbrNodes value not possible ")
114
116 }

Member Data Documentation

◆ dag_

◆ maxArcs_

◆ maxModality_

Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality_
protectedinherited

Definition at line 186 of file IBayesNetGenerator.h.

Referenced by ~IBayesNetGenerator(), and fillCPT().

◆ nbrNodes_


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