aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
aprioriSmoothing_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 the smooth a priori: adds a weight w to all the countings
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
28
29
namespace
gum
{
30
31
namespace
learning
{
32
33
34
/// default constructor
35
template
<
template
<
typename
>
class
ALLOC >
36
INLINE AprioriSmoothing<
ALLOC
>::
AprioriSmoothing
(
37
const
DatabaseTable
<
ALLOC
>&
database
,
38
const
Bijection
<
NodeId
,
std
::
size_t
,
ALLOC
<
std
::
size_t
> >&
nodeId2columns
,
39
const
typename
AprioriSmoothing
<
ALLOC
>::
allocator_type
&
alloc
) :
40
Apriori
<
ALLOC
>(
database
,
nodeId2columns
,
alloc
) {
41
GUM_CONSTRUCTOR
(
AprioriSmoothing
);
42
}
43
44
45
/// copy constructor with a given allocator
46
template
<
template
<
typename
>
class
ALLOC
>
47
INLINE
AprioriSmoothing
<
ALLOC
>::
AprioriSmoothing
(
48
const
AprioriSmoothing
<
ALLOC
>&
from
,
49
const
typename
AprioriSmoothing
<
ALLOC
>::
allocator_type
&
alloc
) :
50
Apriori
<
ALLOC
>(
from
,
alloc
) {
51
GUM_CONS_CPY
(
AprioriSmoothing
);
52
}
53
54
55
/// copy constructor
56
template
<
template
<
typename
>
class
ALLOC
>
57
INLINE
AprioriSmoothing
<
ALLOC
>::
AprioriSmoothing
(
const
AprioriSmoothing
<
ALLOC
>&
from
) :
58
AprioriSmoothing
<
ALLOC
>(
from
,
from
.
getAllocator
()) {}
59
60
61
/// move constructor with a given allocator
62
template
<
template
<
typename
>
class
ALLOC
>
63
INLINE
AprioriSmoothing
<
ALLOC
>::
AprioriSmoothing
(
64
AprioriSmoothing
<
ALLOC
>&&
from
,
65
const
typename
AprioriSmoothing
<
ALLOC
>::
allocator_type
&
alloc
) :
66
Apriori
<
ALLOC
>(
std
::
move
(
from
),
alloc
) {
67
GUM_CONS_MOV
(
AprioriSmoothing
);
68
}
69
70
71
/// move constructor
72
template
<
template
<
typename
>
class
ALLOC
>
73
INLINE
AprioriSmoothing
<
ALLOC
>::
AprioriSmoothing
(
AprioriSmoothing
<
ALLOC
>&&
from
) :
74
AprioriSmoothing
<
ALLOC
>(
std
::
move
(
from
),
from
.
getAllocator
()) {}
75
76
77
/// virtual copy constructor with a given allocator
78
template
<
template
<
typename
>
class
ALLOC
>
79
AprioriSmoothing
<
ALLOC
>*
AprioriSmoothing
<
ALLOC
>::
clone
(
80
const
typename
AprioriSmoothing
<
ALLOC
>::
allocator_type
&
alloc
)
const
{
81
ALLOC
<
AprioriSmoothing
<
ALLOC
> >
allocator
(
alloc
);
82
AprioriSmoothing
<
ALLOC
>*
apriori
=
allocator
.
allocate
(1);
83
try
{
84
allocator
.
construct
(
apriori
, *
this
,
alloc
);
85
}
catch
(...) {
86
allocator
.
deallocate
(
apriori
, 1);
87
throw
;
88
}
89
90
return
apriori
;
91
}
92
93
94
/// virtual copy constructor
95
template
<
template
<
typename
>
class
ALLOC
>
96
INLINE
AprioriSmoothing
<
ALLOC
>*
AprioriSmoothing
<
ALLOC
>::
clone
()
const
{
97
return
clone
(
this
->
getAllocator
());
98
}
99
100
101
/// destructor
102
template
<
template
<
typename
>
class
ALLOC
>
103
INLINE
AprioriSmoothing
<
ALLOC
>::~
AprioriSmoothing
() {
104
GUM_DESTRUCTOR
(
AprioriSmoothing
);
105
}
106
107
108
/// copy operator
109
template
<
template
<
typename
>
class
ALLOC
>
110
INLINE
AprioriSmoothing
<
ALLOC
>&
111
AprioriSmoothing
<
ALLOC
>::
operator
=(
const
AprioriSmoothing
<
ALLOC
>&
from
) {
112
Apriori
<
ALLOC
>::
operator
=(
from
);
113
return
*
this
;
114
}
115
116
117
/// move operator
118
template
<
template
<
typename
>
class
ALLOC
>
119
INLINE
AprioriSmoothing
<
ALLOC
>&
120
AprioriSmoothing
<
ALLOC
>::
operator
=(
AprioriSmoothing
<
ALLOC
>&&
from
) {
121
Apriori
<
ALLOC
>::
operator
=(
std
::
move
(
from
));
122
return
*
this
;
123
}
124
125
126
/// indicates whether an apriori is of a certain type
127
template
<
template
<
typename
>
class
ALLOC
>
128
INLINE
bool
AprioriSmoothing
<
ALLOC
>::
isOfType
(
const
std
::
string
&
type
) {
129
return
AprioriSmoothingType
::
isOfType
(
type
);
130
}
131
132
133
/// returns the type of the apriori
134
template
<
template
<
typename
>
class
ALLOC
>
135
INLINE
const
std
::
string
&
AprioriSmoothing
<
ALLOC
>::
getType
()
const
{
136
return
AprioriSmoothingType
::
type
;
137
}
138
139
140
/// indicates whether the apriori is potentially informative
141
template
<
template
<
typename
>
class
ALLOC
>
142
INLINE
bool
AprioriSmoothing
<
ALLOC
>::
isInformative
()
const
{
143
return
this
->
weight_
!= 0.0;
144
}
145
146
147
/// returns the apriori vector all the variables in the idset
148
template
<
template
<
typename
>
class
ALLOC
>
149
INLINE
void
150
AprioriSmoothing
<
ALLOC
>::
addAllApriori
(
const
IdCondSet
<
ALLOC
>&
idset
,
151
std
::
vector
<
double
,
ALLOC
<
double
> >&
counts
) {
152
// if the idset is empty or the weight is zero, the apriori is also empty
153
if
(
idset
.
empty
() || (
this
->
weight_
== 0.0))
return
;
154
155
// otherwise, add the weight to all the cells in the counting vector
156
for
(
auto
&
count
:
counts
)
157
count
+=
this
->
weight_
;
158
}
159
160
161
/// returns the apriori vector over only the conditioning set of an idset
162
template
<
template
<
typename
>
class
ALLOC
>
163
void
AprioriSmoothing
<
ALLOC
>::
addConditioningApriori
(
164
const
IdCondSet
<
ALLOC
>&
idset
,
165
std
::
vector
<
double
,
ALLOC
<
double
> >&
counts
) {
166
// if the conditioning set is empty or the weight is equal to zero,
167
// the apriori is also empty
168
if
((
idset
.
size
() ==
idset
.
nbLHSIds
()) || (
this
->
weight_
== 0.0)
169
|| (
idset
.
nbLHSIds
() ==
std
::
size_t
(0)))
170
return
;
171
172
// compute the weight of the conditioning set
173
double
weight
=
this
->
weight_
;
174
if
(
this
->
nodeId2columns_
.
empty
()) {
175
for
(
std
::
size_t
i
=
std
::
size_t
(0);
i
<
idset
.
nbLHSIds
(); ++
i
) {
176
weight
*=
this
->
database_
->
domainSize
(
idset
[
i
]);
177
}
178
}
else
{
179
for
(
std
::
size_t
i
=
std
::
size_t
(0);
i
<
idset
.
nbLHSIds
(); ++
i
) {
180
weight
*=
this
->
database_
->
domainSize
(
this
->
nodeId2columns_
.
second
(
idset
[
i
]));
181
}
182
}
183
184
// add the weight to the counting vector
185
for
(
auto
&
count
:
counts
)
186
count
+=
weight
;
187
}
188
189
190
}
/* namespace learning */
191
192
}
/* namespace gum */
193
194
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31