aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
IBayesNetGenerator_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 IBayesNetGenerator
24
*
25
* @author Christophe GONZALES(@AMU), Pierre-Henri WUILLEMIN(@LIP6), Lionel TORTI
26
*and Ariele-Paolo MAESANO
27
*
28
*/
29
30
#
include
<
agrum
/
BN
/
generator
/
IBayesNetGenerator
.
h
>
31
32
namespace
gum
{
33
34
// Default constructor.
35
// Use the SimpleCPTGenerator for generating the BNs CPT.
36
template
<
typename
GUM_SCALAR,
template
<
typename
>
class
ICPTGenerator >
37
INLINE IBayesNetGenerator<
GUM_SCALAR
,
ICPTGenerator
>::
IBayesNetGenerator
(
38
Size
nbrNodes
,
39
Size
maxArcs
,
40
Size
maxModality
) :
41
bayesNet_
() {
42
GUM_CONSTRUCTOR
(
IBayesNetGenerator
);
43
nbrNodes_
=
nbrNodes
;
44
45
if
(
maxArcs
<
nbrNodes
- 1 ||
maxArcs
> (
nbrNodes
* (
nbrNodes
- 1)) / 2)
46
GUM_ERROR
(
OperationNotAllowed
,
" maxArcs value not possible "
);
47
48
if
(
maxModality
< 2)
49
GUM_ERROR
(
OperationNotAllowed
,
50
" maxModality must be at least equal to two "
);
51
52
maxArcs_
=
maxArcs
;
53
maxModality_
=
maxModality
;
54
}
55
56
// Destructor.
57
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
58
INLINE
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::~
IBayesNetGenerator
() {
59
GUM_DESTRUCTOR
(
IBayesNetGenerator
);
60
// delete cptGenerator_;
61
}
62
63
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
64
void
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
fillCPT
() {
65
for
(
auto
node
:
bayesNet_
.
nodes
())
66
this
->
generateCPT
(
bayesNet_
.
cpt
(
node
).
pos
(
bayesNet_
.
variable
(
node
)),
67
bayesNet_
.
cpt
(
node
));
68
}
69
70
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
71
INLINE
Size
72
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
maxModality
()
const
{
73
return
maxModality_
;
74
}
75
76
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
77
INLINE
Size
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
nbrNodes
()
const
{
78
return
nbrNodes_
;
79
}
80
81
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
82
INLINE
Size
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
maxArcs
()
const
{
83
return
maxArcs_
;
84
}
85
86
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
87
INLINE
void
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
setMaxModality
(
88
Size
maxModality
) {
89
if
(
maxModality
< 2)
90
GUM_ERROR
(
OperationNotAllowed
,
91
" maxModality must be at least equal to two "
);
92
93
maxModality_
=
maxModality
;
94
}
95
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
96
INLINE
void
97
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
setNbrNodes
(
Size
nbrNodes
) {
98
if
((
maxArcs_
<
nbrNodes
- 1) || (
maxArcs_
> (
nbrNodes
* (
nbrNodes
- 1)) / 2))
99
GUM_ERROR
(
OperationNotAllowed
,
" nbrNodes value not possible "
);
100
101
nbrNodes_
=
nbrNodes
;
102
}
103
104
template
<
typename
GUM_SCALAR
,
template
<
typename
>
class
ICPTGenerator
>
105
INLINE
void
106
IBayesNetGenerator
<
GUM_SCALAR
,
ICPTGenerator
>::
setMaxArcs
(
Size
maxArcs
) {
107
if
(
maxArcs
<
nbrNodes_
- 1 ||
maxArcs
> (
nbrNodes_
* (
nbrNodes_
- 1)) / 2)
108
GUM_ERROR
(
OperationNotAllowed
,
" maxArcs value not possible "
);
109
110
maxArcs_
=
maxArcs
;
111
}
112
113
// Generates a Bayesian network using floats.
114
// @param nbrNodes The number of nodes in the generated BN.
115
// @param density The probability of adding an arc between two nodes.
116
// @return A BNs randomly generated.
117
118
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669