aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
simpleBayesNetGenerator_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/** @file
23
* @brief Source implementation of SimpleBayesNetGenerator
24
*
25
* @author Pierre-Henri WUILLEMIN(@LIP6) and Lionel TORTI and Ariele-Paolo MAESANO
26
*
27
*/
28
29
#
include
<
agrum
/
BN
/
generator
/
simpleBayesNetGenerator
.
h
>
30
31
namespace
gum
{
32
33
#
ifdef
_MSC_VER
34
#
define
IBNG
IBayesNetGenerator
35
#
else
36
#
define
IBNG
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>
37
#
endif
38
39
// Use the SimpleCPTGenerator for generating the BNs CPT.
40
template
<
typename
GUM_SCALAR,
template
<
typename
>
class
ICPTGenerator >
41
INLINE
42
SimpleBayesNetGenerator<
GUM_SCALAR
,
ICPTGenerator
>::
SimpleBayesNetGenerator
(
43
Size
nbrNodes
,
44
Size
maxArcs
,
45
Size
maxModality
) :
46
IBNG
(
nbrNodes
,
maxArcs
,
maxModality
) {
47
GUM_CONSTRUCTOR
(
SimpleBayesNetGenerator
);
48
}
49
50
// Use this constructor if you want to use a different policy for generating
51
// CPT than the default one.
52
// The cptGenerator will be erased when the destructor is called.
53
// @param cptGenerator The policy used to generate CPT.
54
/*template <typename GUM_SCALAR, template<class> class ICPTGenerator>
55
SimpleBayesNetGenerator<GUM_SCALAR,ICPTGenerator>::SimpleBayesNetGenerator(CPTGenerator*
56
cptGenerator ,Size nbrNodes, float density, Size maxModality):
57
IBayesNetGenerator<GUM_SCALAR,ICPTGenerator>(cptGenerator
58
,nbrNodes,density,maxModality) {
59
GUM_CONSTRUCTOR ( SimpleBayesNetGenerator );
60
}*/
61
62
// Destructor.
63
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
64
INLINE
SimpleBayesNetGenerator
<
GUM_SCALAR
,
65
ICPTGenerator
>::~
SimpleBayesNetGenerator
() {
66
GUM_DESTRUCTOR
(
SimpleBayesNetGenerator
);
67
}
68
69
// Generates a Bayesian network using floats.
70
// @param nbrNodes The number of nodes in the generated BN.
71
// @param density The probability of adding an arc between two nodes.
72
// @return A BNs randomly generated.
73
74
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
75
void
SimpleBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
generateBN
(
76
BayesNet
<
GUM_SCALAR
>&
bayesNet
) {
77
this
->
bayesNet_
=
bayesNet
;
78
HashTable
<
Size
,
NodeId
>
map
;
79
std
::
stringstream
strBuff
;
80
81
for
(
Size
i
= 0;
this
->
nbrNodes_
>
i
; ++
i
) {
82
strBuff
<<
"n"
<<
i
;
83
Size
nb_mod
84
= (
this
->
maxModality_
== 2) ? 2 : 2 +
randomValue
(
this
->
maxModality_
- 1);
85
map
.
insert
(
86
i
,
87
this
->
bayesNet_
.
add
(
LabelizedVariable
(
strBuff
.
str
(),
""
,
nb_mod
)));
88
strBuff
.
str
(
""
);
89
}
90
91
// We add arcs
92
float
density
= (
float
)(
this
->
maxArcs_
* 2)
93
/ (
float
)(
this
->
nbrNodes_
* (
this
->
nbrNodes_
- 1));
94
95
for
(
Size
i
= 0;
i
<
this
->
nbrNodes_
; ++
i
)
96
for
(
Size
j
=
i
+ 1;
j
<
this
->
nbrNodes_
; ++
j
)
97
if
(
randomProba
() <
density
)
this
->
bayesNet_
.
addArc
(
map
[
i
],
map
[
j
]);
98
99
this
->
fillCPT
();
100
101
bayesNet
=
this
->
bayesNet_
;
102
}
103
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
IBNG
#define IBNG
Definition:
maxInducedWidthMCBayesNetGenerator_tpl.h:37