aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
multiDimICIModel_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
/**
23
* @file
24
* @brief A Interface to all Causal Independence models
25
*
26
* Causal Independence (CI) is a method of defining a discrete distribution
27
* that can dramatically reduce the number of prior probabilities necessary to
28
* define a distribution.
29
*
30
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
31
*/
32
#
include
<
agrum
/
tools
/
core
/
bijection
.
h
>
33
#
include
<
agrum
/
tools
/
multidim
/
ICIModels
/
multiDimICIModel
.
h
>
34
#
include
<
agrum
/
tools
/
multidim
/
implementations
/
multiDimReadOnly
.
h
>
35
36
namespace
gum
{
37
38
// Default constructor
39
template
<
typename
GUM_SCALAR >
40
INLINE
41
MultiDimICIModel<
GUM_SCALAR
>::
MultiDimICIModel
(
GUM_SCALAR
external_weight
,
42
GUM_SCALAR
default_weight
) :
43
MultiDimReadOnly
<
GUM_SCALAR
>(),
44
external_weight__
(
external_weight
),
default_weight__
(
default_weight
) {
45
GUM_CONSTRUCTOR
(
MultiDimICIModel
);
46
}
47
48
// Default constructor
49
template
<
typename
GUM_SCALAR
>
50
INLINE
MultiDimICIModel
<
GUM_SCALAR
>::
MultiDimICIModel
(
51
const
MultiDimICIModel
<
GUM_SCALAR
>&
from
) :
52
MultiDimReadOnly
<
GUM_SCALAR
>(
from
) {
53
GUM_CONS_CPY
(
MultiDimICIModel
);
54
default_weight__
=
from
.
default_weight__
;
55
external_weight__
=
from
.
external_weight__
;
56
causal_weights__
=
from
.
causal_weights__
;
57
}
58
59
// Copy constructor using a bijection to replace variables from source.
60
template
<
typename
GUM_SCALAR
>
61
INLINE
MultiDimICIModel
<
GUM_SCALAR
>::
MultiDimICIModel
(
62
const
Bijection
<
const
DiscreteVariable
*,
const
DiscreteVariable
* >&
bij
,
63
const
MultiDimICIModel
<
GUM_SCALAR
>&
from
) :
64
MultiDimReadOnly
<
GUM_SCALAR
>() {
65
GUM_CONSTRUCTOR
(
MultiDimICIModel
);
66
default_weight__
=
from
.
default_weight__
;
67
external_weight__
=
from
.
external_weight__
;
68
69
for
(
HashTableConstIteratorSafe
<
const
DiscreteVariable
*,
GUM_SCALAR
>
iter
70
=
from
.
causal_weights__
.
beginSafe
();
71
iter
!=
from
.
causal_weights__
.
endSafe
();
72
++
iter
) {
73
try
{
74
causalWeight
(*(
bij
.
first
(
iter
.
key
())),
iter
.
val
());
75
}
catch
(
NotFound
&) {
causalWeight
(*(
iter
.
key
()),
iter
.
val
()); }
76
}
77
}
78
79
// destructor
80
template
<
typename
GUM_SCALAR
>
81
INLINE
MultiDimICIModel
<
GUM_SCALAR
>::~
MultiDimICIModel
() {
82
GUM_DESTRUCTOR
(
MultiDimICIModel
);
83
}
84
85
template
<
typename
GUM_SCALAR
>
86
INLINE
GUM_SCALAR
MultiDimICIModel
<
GUM_SCALAR
>::
causalWeight
(
87
const
DiscreteVariable
&
v
)
const
{
88
return
(
causal_weights__
.
exists
(&
v
)) ?
causal_weights__
[&
v
] :
default_weight__
;
89
}
90
91
template
<
typename
GUM_SCALAR
>
92
INLINE
void
93
MultiDimICIModel
<
GUM_SCALAR
>::
causalWeight
(
const
DiscreteVariable
&
v
,
94
GUM_SCALAR
w
)
const
{
95
if
(!
this
->
contains
(
v
)) {
96
GUM_ERROR
(
InvalidArgument
,
v
.
name
() <<
" is not a cause for this CI Model"
);
97
}
98
99
if
(
w
== (
GUM_SCALAR
)0) {
100
GUM_ERROR
(
gum
::
OutOfBounds
,
"causal weight in CI Model>0"
);
101
}
102
103
causal_weights__
.
set
(&
v
,
w
);
104
}
105
106
template
<
typename
GUM_SCALAR
>
107
INLINE
GUM_SCALAR
MultiDimICIModel
<
GUM_SCALAR
>::
externalWeight
()
const
{
108
return
external_weight__
;
109
}
110
111
template
<
typename
GUM_SCALAR
>
112
INLINE
void
MultiDimICIModel
<
GUM_SCALAR
>::
externalWeight
(
GUM_SCALAR
w
)
const
{
113
external_weight__
=
w
;
114
}
115
116
template
<
typename
GUM_SCALAR
>
117
std
::
string
MultiDimICIModel
<
GUM_SCALAR
>::
toString
()
const
{
118
std
::
stringstream
s
;
119
s
<<
this
->
variable
(0) <<
"=CIModel(["
<<
externalWeight
() <<
"],"
;
120
121
for
(
Idx
i
= 1;
i
<
this
->
nbrDim
();
i
++) {
122
s
<<
this
->
variable
(
i
) <<
"["
<<
causalWeight
(
this
->
variable
(
i
)) <<
"]"
;
123
}
124
125
s
<<
")"
;
126
127
std
::
string
res
;
128
s
>>
res
;
129
return
res
;
130
}
131
template
<
typename
GUM_SCALAR
>
132
void
MultiDimICIModel
<
GUM_SCALAR
>::
copyFrom
(
133
const
MultiDimContainer
<
GUM_SCALAR
>&
src
)
const
{
134
auto
p
=
dynamic_cast
<
const
MultiDimICIModel
<
GUM_SCALAR
>* >(&
src
);
135
if
(
p
==
nullptr
)
136
MultiDimReadOnly
<
GUM_SCALAR
>::
copyFrom
(
src
);
137
else
{
138
if
(
src
.
domainSize
() !=
this
->
domainSize
()) {
139
GUM_ERROR
(
OperationNotAllowed
,
"Domain sizes do not fit"
);
140
}
141
external_weight__
=
p
->
external_weight__
;
142
default_weight__
=
p
->
default_weight__
;
143
for
(
Idx
i
= 1;
i
<
this
->
nbrDim
();
i
++) {
144
causal_weights__
.
set
(
145
const_cast
<
const
DiscreteVariable
* >(&
this
->
variable
(
i
)),
146
p
->
causalWeight
(
this
->
variable
(
i
)));
147
}
148
}
149
}
150
151
// returns the name of the implementation
152
template
<
typename
GUM_SCALAR
>
153
INLINE
const
std
::
string
&
MultiDimICIModel
<
GUM_SCALAR
>::
name
()
const
{
154
static
const
std
::
string
str
=
"MultiDimICIModel"
;
155
return
str
;
156
}
157
158
template
<
typename
GUM_SCALAR
>
159
INLINE
void
MultiDimICIModel
<
GUM_SCALAR
>::
replace_
(
const
DiscreteVariable
*
x
,
160
const
DiscreteVariable
*
y
) {
161
MultiDimReadOnly
<
GUM_SCALAR
>::
replace_
(
x
,
y
);
162
causal_weights__
.
insert
(
y
,
causal_weights__
[
x
]);
163
causal_weights__
.
erase
(
x
);
164
}
165
166
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669