aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
simpleBayesNetGenerator_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by 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 SimpleBayesNetGenerator<
GUM_SCALAR
,
ICPTGenerator
>::
SimpleBayesNetGenerator
(
42
Size
nbrNodes
,
43
Size
maxArcs
,
44
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
,
ICPTGenerator
>::~
SimpleBayesNetGenerator
() {
64
GUM_DESTRUCTOR
(
SimpleBayesNetGenerator
);
65
}
66
67
// Generates a Bayesian network using floats.
68
// @param nbrNodes The number of nodes in the generated BN.
69
// @param density The probability of adding an arc between two nodes.
70
// @return A BNs randomly generated.
71
72
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
73
void
SimpleBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
generateBN
(
74
BayesNet
<
GUM_SCALAR
>&
bayesNet
) {
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
}
97
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
IBNG
#define IBNG
Definition:
maxInducedWidthMCBayesNetGenerator_tpl.h:37