aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
influenceDiagramGenerator_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 InfluenceDiagramGenerator
24
*
25
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
26
* GONZALES(@AMU)
27
*
28
*/
29
#
include
<
agrum
/
ID
/
generator
/
influenceDiagramGenerator
.
h
>
30
31
namespace
gum
{
32
33
// Default constructor.
34
// Use the SimpleCPTGenerator for generating the IDs CPT.
35
template
<
typename
GUM_SCALAR >
36
InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator() {
37
GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
38
cptGenerator__ =
new
SimpleCPTGenerator< GUM_SCALAR >();
39
utGenerator__ =
new
SimpleUTGenerator();
40
}
41
42
// Use this constructor if you want to use a different policy for generating
43
// CPT than the default one.
44
// The cptGenerator will be erased when the destructor is called.
45
// @param cptGenerator The policy used to generate CPT.
46
template
<
typename
GUM_SCALAR
>
47
InfluenceDiagramGenerator
<
GUM_SCALAR
>::
InfluenceDiagramGenerator
(
48
ICPTGenerator
<
GUM_SCALAR
>*
cptGenerator
) {
49
GUM_CONSTRUCTOR
(
InfluenceDiagramGenerator
);
50
cptGenerator__
=
cptGenerator
;
51
utGenerator__
=
new
SimpleUTGenerator
();
52
}
53
54
// Use this constructor if you want to use a different policy for generating
55
// UT than the default one.
56
// The utGenerator will be erased when the destructor is called.
57
// @param utGenerator The policy used to generate UT.
58
template
<
typename
GUM_SCALAR
>
59
InfluenceDiagramGenerator
<
GUM_SCALAR
>::
InfluenceDiagramGenerator
(
60
UTGenerator
*
utGenerator
) {
61
GUM_CONSTRUCTOR
(
InfluenceDiagramGenerator
);
62
cptGenerator__
=
new
SimpleCPTGenerator
<
GUM_SCALAR
>();
63
utGenerator__
=
utGenerator
;
64
}
65
66
// Use this constructor if you want to use a different policy for generating
67
// both CPT & UT than the defaults ones.
68
// The cptGenerator and utGenerator will be erased when the destructor is
69
// called.
70
// @param cptGenerator The policy used to generate CPT.
71
// @param utGenerator The policy used to generate UT.
72
template
<
typename
GUM_SCALAR
>
73
InfluenceDiagramGenerator
<
GUM_SCALAR
>::
InfluenceDiagramGenerator
(
74
ICPTGenerator
<
GUM_SCALAR
>*
cptGenerator
,
75
UTGenerator
*
utGenerator
) {
76
GUM_CONSTRUCTOR
(
InfluenceDiagramGenerator
);
77
cptGenerator__
=
cptGenerator
;
78
utGenerator__
=
utGenerator
;
79
}
80
81
// Destructor.
82
template
<
typename
GUM_SCALAR
>
83
InfluenceDiagramGenerator
<
GUM_SCALAR
>::~
InfluenceDiagramGenerator
() {
84
GUM_DESTRUCTOR
(
InfluenceDiagramGenerator
);
85
delete
cptGenerator__
;
86
delete
utGenerator__
;
87
}
88
89
// Generates an influence diagram using floats.
90
// @param nbrNodes The number of nodes in the generated ID.
91
// @param arcdensity The probability of adding an arc between two nodes.
92
// @param chanceNodeDensity The proportion of chance node
93
// @param utilityNodeDensity The proportion of utility node
94
// @param max_modality Each DRV has from 2 to max_modality modalities
95
// @return A IDs randomly generated.
96
template
<
typename
GUM_SCALAR
>
97
InfluenceDiagram
<
GUM_SCALAR
>*
98
InfluenceDiagramGenerator
<
GUM_SCALAR
>::
generateID
(
99
Size
nbrNodes
,
100
GUM_SCALAR
arcDensity
,
101
GUM_SCALAR
chanceNodeDensity
,
102
GUM_SCALAR
utilityNodeDensity
,
103
Size
max_modality
) {
104
InfluenceDiagram
<
GUM_SCALAR
>*
influenceDiagram
105
=
new
InfluenceDiagram
<
GUM_SCALAR
>();
106
// First we add nodes
107
HashTable
<
Size
,
NodeId
>
map
;
108
std
::
stringstream
strBuff
;
109
Size
nb_mod
;
110
111
for
(
Idx
i
= 0;
i
<
nbrNodes
; ++
i
) {
112
strBuff
<<
i
;
113
nb_mod
= (
max_modality
== 2) ? 2 : 2 +
rand
() % (
max_modality
- 1);
114
115
GUM_SCALAR
cnd
=
chanceNodeDensity
* (
GUM_SCALAR
)
RAND_MAX
;
116
GUM_SCALAR
und
=
utilityNodeDensity
* (
GUM_SCALAR
)
RAND_MAX
;
117
118
GUM_SCALAR
d
= (
GUM_SCALAR
)
rand
();
119
120
if
(
d
<
cnd
)
121
map
.
insert
(
i
,
122
influenceDiagram
->
addChanceNode
(
123
LabelizedVariable
(
strBuff
.
str
(),
""
,
nb_mod
)));
124
else
if
(
d
< (
cnd
+
und
))
125
map
.
insert
(
i
,
126
influenceDiagram
->
addUtilityNode
(
127
LabelizedVariable
(
strBuff
.
str
(),
""
, 1)));
128
else
129
map
.
insert
(
i
,
130
influenceDiagram
->
addDecisionNode
(
131
LabelizedVariable
(
strBuff
.
str
(),
""
,
nb_mod
)));
132
133
strBuff
.
str
(
""
);
134
}
135
136
// We add arcs
137
GUM_SCALAR
p
=
arcDensity
* (
GUM_SCALAR
)
RAND_MAX
;
138
139
for
(
Size
i
= 0;
i
<
nbrNodes
; ++
i
)
140
if
(!
influenceDiagram
->
isUtilityNode
(
map
[
i
]))
141
for
(
Size
j
=
i
+ 1;
j
<
nbrNodes
; ++
j
)
142
if
(((
GUM_SCALAR
)
rand
()) <
p
) {
143
influenceDiagram
->
addArc
(
map
[
i
],
map
[
j
]);
144
}
145
146
// And fill the CPTs and UTs
147
for
(
Size
i
= 0;
i
<
nbrNodes
; ++
i
)
148
if
(
influenceDiagram
->
isChanceNode
(
map
[
i
]))
149
cptGenerator__
->
generateCPT
(
150
influenceDiagram
->
cpt
(
map
[
i
]).
pos
(
influenceDiagram
->
variable
(
map
[
i
])),
151
influenceDiagram
->
cpt
(
map
[
i
]));
152
else
if
(
influenceDiagram
->
isUtilityNode
(
map
[
i
]))
153
utGenerator__
->
generateUT
(
influenceDiagram
->
utility
(
map
[
i
]).
pos
(
154
influenceDiagram
->
variable
(
map
[
i
])),
155
influenceDiagram
->
utility
(
map
[
i
]));
156
157
checkTemporalOrder__
(
influenceDiagram
);
158
159
return
influenceDiagram
;
160
}
161
162
template
<
typename
GUM_SCALAR
>
163
void
InfluenceDiagramGenerator
<
GUM_SCALAR
>::
checkTemporalOrder__
(
164
InfluenceDiagram
<
GUM_SCALAR
>*
infdiag
) {
165
if
(!
infdiag
->
decisionOrderExists
()) {
166
Sequence
<
NodeId
>
order
=
infdiag
->
topologicalOrder
(
true
);
167
168
auto
orderIter
=
order
.
begin
();
169
170
while
((
orderIter
!=
order
.
end
()) && (!
infdiag
->
isDecisionNode
(*
orderIter
)))
171
++
orderIter
;
172
173
if
(
orderIter
==
order
.
end
())
return
;
174
175
NodeId
parentDecision
= (*
orderIter
);
176
177
++
orderIter
;
178
179
for
(;
orderIter
!=
order
.
end
(); ++
orderIter
)
180
if
(
infdiag
->
isDecisionNode
(*
orderIter
)) {
181
infdiag
->
addArc
(
parentDecision
, (*
orderIter
));
182
parentDecision
= (*
orderIter
);
183
}
184
}
185
}
186
187
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669