aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
varMod2BNsMap_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
#
include
<
agrum
/
CN
/
tools
/
varMod2BNsMap
.
h
>
23
24
namespace
gum
{
25
namespace
credal
{
26
27
template
<
typename
GUM_SCALAR >
28
VarMod2BNsMap< GUM_SCALAR >::VarMod2BNsMap() {
29
cnet =
nullptr
;
30
31
GUM_CONSTRUCTOR(VarMod2BNsMap);
32
}
33
34
template
<
typename
GUM_SCALAR
>
35
VarMod2BNsMap
<
GUM_SCALAR
>::
VarMod2BNsMap
(
const
CredalNet
<
GUM_SCALAR
>&
cn
) {
36
setCNet
(
cn
);
37
38
GUM_CONSTRUCTOR
(
VarMod2BNsMap
);
39
}
40
41
template
<
typename
GUM_SCALAR
>
42
VarMod2BNsMap
<
GUM_SCALAR
>::~
VarMod2BNsMap
() {
43
GUM_DESTRUCTOR
(
VarMod2BNsMap
);
44
}
45
46
template
<
typename
GUM_SCALAR
>
47
void
VarMod2BNsMap
<
GUM_SCALAR
>::
setCNet
(
const
CredalNet
<
GUM_SCALAR
>&
cn
) {
48
auto
*
cpt
= &
cn
.
credalNet_currentCpt
();
49
auto
nNodes
=
cpt
->
size
();
50
sampleDef_
.
resize
(
nNodes
);
51
52
for
(
NodeId
node
= 0;
node
<
nNodes
;
node
++) {
53
auto
pConfs
= (*
cpt
)[
node
].
size
();
54
sampleDef_
[
node
].
resize
(
pConfs
);
55
56
for
(
Size
pconf
= 0;
pconf
<
pConfs
;
pconf
++) {
57
Size
nVertices
=
Size
((*
cpt
)[
node
][
pconf
].
size
());
58
unsigned
long
b
,
c
;
// needed by superiorPow
59
superiorPow
(
static_cast
<
unsigned
long
>(
nVertices
),
b
,
c
);
60
Size
nBits
=
Size
(
b
);
61
sampleDef_
[
node
][
pconf
].
resize
(
nBits
);
62
}
63
}
64
65
cnet
= &
cn
;
66
}
67
68
template
<
typename
GUM_SCALAR
>
69
bool
VarMod2BNsMap
<
GUM_SCALAR
>::
insert
(
const
std
::
vector
<
bool
>&
bn
,
70
const
std
::
vector
<
Size
>&
key
) {
71
currentHash_
=
Size
(
vectHash_
(
bn
));
72
std
::
list
<
Size
>&
nets
73
=
myVarHashs_
.
getWithDefault
(
key
,
std
::
list
<
Size
>());
//[ key ];
74
75
for
(
std
::
list
<
Size
>::
iterator
it
=
nets
.
begin
();
it
!=
nets
.
end
(); ++
it
) {
76
if
(*
it
==
currentHash_
)
return
false
;
77
}
78
79
// add it
80
myHashNet_
.
set
(
currentHash_
,
bn
);
//[currentHash_] = bn;
81
// insert net hash in our key net list
82
nets
.
push_back
(
currentHash_
);
83
// insert out key in the hash key list
84
myHashVars_
85
.
getWithDefault
(
currentHash_
,
86
std
::
list
<
varKey
>())
/*[currentHash_]*/
.
push_back
(
key
);
87
return
true
;
88
}
89
90
template
<
typename
GUM_SCALAR
>
91
bool
VarMod2BNsMap
<
GUM_SCALAR
>::
insert
(
const
std
::
vector
<
Size
>&
key
,
92
const
bool
isBetter
) {
93
if
(
isBetter
) {
94
// get all nets of this key (maybe entry does not exists)
95
std
::
list
<
Size
>&
old_nets
96
=
myVarHashs_
.
getWithDefault
(
key
,
std
::
list
<
Size
>());
//[ key ];
97
98
// for each one
99
for
(
std
::
list
<
Size
>::
iterator
it
=
old_nets
.
begin
();
100
it
!=
old_nets
.
end
();
101
++
it
) {
102
// get all keys associated to this net
103
std
::
list
<
varKey
>&
netKeys
104
=
myHashVars_
.
getWithDefault
(*
it
,
std
::
list
<
varKey
>());
//[ *it ];
105
106
// if we are the sole user, delete the net entry
107
if
(
netKeys
.
size
() == 1) {
108
myHashVars_
.
erase
(*
it
);
109
}
110
// other keys use the net, delete our key from list
111
else
{
112
for
(
std
::
list
<
varKey
>::
iterator
it2
=
netKeys
.
begin
();
113
it2
!=
netKeys
.
end
();
114
++
it2
) {
115
if
(*
it2
==
key
) {
116
netKeys
.
erase
(
it2
);
117
break
;
118
}
119
}
120
}
121
}
// end of : for each old_net
122
123
// clear all old_nets
124
old_nets
.
clear
();
125
// insert new net with it's hash
126
myHashNet_
.
set
(
currentHash_
,
127
currentSample_
);
//[currentHash_] = currentSample_;
128
// insert net hash in our key net list
129
old_nets
.
push_back
(
currentHash_
);
130
// insert out key in the hash key list
131
myHashVars_
132
.
getWithDefault
(
currentHash_
,
std
::
list
<
varKey
>())
/*[currentHash_]*/
133
.
push_back
(
key
);
134
return
true
;
135
136
}
// end of isBetter
137
// another opt net
138
else
{
139
// check that we didn't add it for this key
140
std
::
list
<
Size
>&
nets
141
=
myVarHashs_
.
getWithDefault
(
key
,
std
::
list
<
Size
>());
//[ key ];
142
143
for
(
std
::
list
<
Size
>::
iterator
it
=
nets
.
begin
();
it
!=
nets
.
end
();
144
++
it
) {
145
if
(*
it
==
currentHash_
)
return
false
;
146
}
147
148
// add it
149
myHashNet_
.
set
(
currentHash_
,
currentSample_
);
150
// insert net hash in our key net list
151
nets
.
push_back
(
currentHash_
);
152
// insert out key in the hash key list
153
myHashVars_
.
getWithDefault
(
currentHash_
,
std
::
list
<
varKey
>())
154
.
push_back
(
key
);
155
156
/*
157
// add it
158
myHashNet_[currentHash_] = currentSample_;
159
// insert net hash in our key net list
160
nets.push_back(currentHash_);
161
// insert out key in the hash key list
162
myHashVars_[currentHash_].push_back(key);
163
*/
164
return
true
;
165
}
// end of ! isBetter
166
}
167
168
template
<
typename
GUM_SCALAR
>
169
void
VarMod2BNsMap
<
GUM_SCALAR
>::
setCurrentSample
(
170
const
std
::
vector
<
std
::
vector
<
std
::
vector
<
bool
> > >&
sample
) {
171
currentSample_
.
clear
();
172
173
for
(
Size
i
= 0;
i
<
sample
.
size
();
i
++)
174
for
(
Size
j
= 0;
j
<
sample
[
j
].
size
();
j
++)
175
for
(
Size
k
= 0;
k
<
sample
[
i
][
j
].
size
();
k
++)
176
currentSample_
.
push_back
(
sample
[
i
][
j
][
k
]);
177
178
// std::cout << sample << std::endl;
179
// std::cout << currentSample_ << std::endl;
180
181
currentHash_
=
Size
(
vectHash_
(
currentSample_
));
182
}
183
184
template
<
typename
GUM_SCALAR
>
185
const
std
::
vector
<
bool
>&
VarMod2BNsMap
<
GUM_SCALAR
>::
getCurrentSample
() {
186
return
currentSample_
;
187
}
188
189
template
<
typename
GUM_SCALAR
>
190
const
std
::
vector
<
std
::
vector
<
std
::
vector
<
bool
> > >&
191
VarMod2BNsMap
<
GUM_SCALAR
>::
getSampleDef
() {
192
return
sampleDef_
;
193
}
194
195
template
<
typename
GUM_SCALAR
>
196
const
std
::
vector
<
std
::
vector
<
bool
>* >
197
VarMod2BNsMap
<
GUM_SCALAR
>::
getBNOptsFromKey
(
198
const
std
::
vector
<
Size
>&
key
) {
199
// return something even if key does not exist
200
if
(!
myVarHashs_
.
exists
(
key
))
return
std
::
vector
<
std
::
vector
<
bool
>* >();
201
202
std
::
list
<
Size
>&
netsHash
=
myVarHashs_
[
key
];
//.at(key);
203
204
std
::
vector
<
dBN
* >
nets
;
205
nets
.
resize
(
netsHash
.
size
());
206
207
std
::
list
<
Size
>::
iterator
it
=
netsHash
.
begin
();
208
209
for
(
Size
i
= 0;
i
<
netsHash
.
size
();
i
++, ++
it
) {
210
nets
[
i
] = &
myHashNet_
/*.at(*/
[*
it
];
//);
211
}
212
213
return
nets
;
214
}
215
216
template
<
typename
GUM_SCALAR
>
217
std
::
vector
<
std
::
vector
<
std
::
vector
<
std
::
vector
<
bool
> > > >
218
VarMod2BNsMap
<
GUM_SCALAR
>::
getFullBNOptsFromKey
(
219
const
std
::
vector
<
Size
>&
key
) {
220
if
(
cnet
==
nullptr
)
221
GUM_ERROR
(
OperationNotAllowed
,
222
"No CredalNet associated to me ! Can't get FullBNOptsFromKey : "
223
<<
key
);
224
225
if
(!
myVarHashs_
.
exists
(
key
))
226
return
std
::
vector
<
std
::
vector
<
std
::
vector
<
std
::
vector
<
bool
> > > >();
227
228
std
::
list
<
Size
>&
netsHash
=
myVarHashs_
[
key
];
//.at(key);
229
230
std
::
vector
<
std
::
vector
<
std
::
vector
<
std
::
vector
<
bool
> > > >
nets
;
231
nets
.
resize
(
netsHash
.
size
(),
sampleDef_
);
232
233
std
::
list
<
Size
>::
iterator
it
=
netsHash
.
begin
();
234
235
for
(
Size
i
= 0;
i
<
netsHash
.
size
();
i
++, ++
it
) {
236
// std::vector< std::vector< std::vector < bool > > > net(sampleDef_);
237
dBN
::
iterator
it2
=
myHashNet_
/*.at(*/
[*
it
]
/*)*/
.
begin
();
238
239
for
(
Size
j
= 0;
j
<
sampleDef_
.
size
();
j
++) {
240
for
(
Size
k
= 0;
k
<
sampleDef_
[
j
].
size
();
k
++) {
241
for
(
Size
l
= 0;
l
<
sampleDef_
[
j
][
k
].
size
();
l
++) {
242
nets
[
i
][
j
][
k
][
l
] = *
it2
;
243
++
it2
;
244
}
245
}
246
}
247
}
248
249
return
nets
;
250
}
251
252
template
<
typename
GUM_SCALAR
>
253
Size
VarMod2BNsMap
<
GUM_SCALAR
>::
getEntrySize
()
const
{
254
return
myHashNet_
.
size
();
255
}
256
257
}
// namespace credal
258
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::credal
namespace for all credal networks entities
Definition:
LpInterface.cpp:37