aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
multiDimNoisyAND_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
/**
23
* @file
24
* @brief class for NoisyAND-net implementation as multiDim
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27
*/
28
29
#
include
<
agrum
/
tools
/
core
/
exceptions
.
h
>
30
#
include
<
agrum
/
tools
/
multidim
/
ICIModels
/
multiDimNoisyAND
.
h
>
31
32
namespace
gum
{
33
34
// Default constructor
35
template
<
typename
GUM_SCALAR >
36
INLINE MultiDimNoisyAND<
GUM_SCALAR
>::
MultiDimNoisyAND
(
GUM_SCALAR
external_weight
,
37
GUM_SCALAR
default_weight
) :
38
MultiDimICIModel
<
GUM_SCALAR
>(
external_weight
,
default_weight
) {
39
if
(
external_weight
== 0) {
40
GUM_ERROR
(
InvalidArgument
,
"external weight can not be null for a NoisyAND"
)
41
}
42
43
GUM_CONSTRUCTOR
(
MultiDimNoisyAND
);
44
}
45
46
// Default constructor
47
template
<
typename
GUM_SCALAR
>
48
INLINE
49
MultiDimNoisyAND
<
GUM_SCALAR
>::
MultiDimNoisyAND
(
const
MultiDimNoisyAND
<
GUM_SCALAR
>&
from
) :
50
MultiDimICIModel
<
GUM_SCALAR
>(
from
) {
51
GUM_CONS_CPY
(
MultiDimNoisyAND
);
52
}
53
54
// Copy constructor using a bijection to replace variables from source.
55
template
<
typename
GUM_SCALAR
>
56
INLINE
MultiDimNoisyAND
<
GUM_SCALAR
>::
MultiDimNoisyAND
(
57
const
Bijection
<
const
DiscreteVariable
*,
const
DiscreteVariable
* >&
bij
,
58
const
MultiDimNoisyAND
<
GUM_SCALAR
>&
from
) :
59
MultiDimICIModel
<
GUM_SCALAR
>(
bij
,
from
) {
60
GUM_CONSTRUCTOR
(
MultiDimNoisyAND
);
61
}
62
63
// destructor
64
template
<
typename
GUM_SCALAR
>
65
INLINE
MultiDimNoisyAND
<
GUM_SCALAR
>::~
MultiDimNoisyAND
() {
66
GUM_DESTRUCTOR
(
MultiDimNoisyAND
);
67
}
68
69
template
<
typename
GUM_SCALAR
>
70
GUM_SCALAR
MultiDimNoisyAND
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
71
if
(
this
->
nbrDim
() < 1) {
72
GUM_ERROR
(
OperationNotAllowed
,
"Not enough variable for a NoisyAND "
)
73
}
74
75
const
DiscreteVariable
&
C
=
this
->
variable
((
Idx
)0);
76
77
if
(
i
.
val
(
C
) > 1)
return
(
GUM_SCALAR
)0.0;
78
79
GUM_SCALAR
fact
=
this
->
externalWeight
();
80
81
for
(
Idx
j
= 1;
j
<
this
->
nbrDim
();
j
++) {
82
const
DiscreteVariable
&
v
=
this
->
variable
(
j
);
83
84
if
(
i
.
val
(
v
) == 0) {
85
fact
*= ((
GUM_SCALAR
)1.0 -
this
->
causalWeight
(
v
));
86
}
else
{
87
fact
*=
this
->
causalWeight
(
v
);
88
}
89
}
90
91
return
(
i
.
val
(
C
) == 1) ?
fact
: (
GUM_SCALAR
)1.0 -
fact
;
92
}
93
94
template
<
typename
GUM_SCALAR
>
95
std
::
string
MultiDimNoisyAND
<
GUM_SCALAR
>::
toString
()
const
{
96
std
::
stringstream
s
;
97
s
<<
MultiDimImplementation
<
GUM_SCALAR
>::
variable
(0) <<
"=noisyAND(["
98
<<
this
->
externalWeight
() <<
"],"
;
99
100
for
(
Idx
i
= 1;
i
<
MultiDimImplementation
<
GUM_SCALAR
>::
nbrDim
();
i
++) {
101
s
<<
MultiDimImplementation
<
GUM_SCALAR
>::
variable
(
i
) <<
"["
102
<<
this
->
causalWeight
(
MultiDimImplementation
<
GUM_SCALAR
>::
variable
(
i
)) <<
"]"
;
103
}
104
105
s
<<
")"
;
106
107
std
::
string
res
;
108
s
>>
res
;
109
return
res
;
110
}
111
112
// For friendly displaying the content of the variable.
113
114
template
<
typename
GUM_SCALAR
>
115
INLINE
std
::
ostream
&
operator
<<(
std
::
ostream
&
s
,
const
MultiDimNoisyAND
<
GUM_SCALAR
>&
ag
) {
116
return
s
<<
ag
.
toString
();
117
}
118
119
template
<
typename
GUM_SCALAR
>
120
INLINE
MultiDimContainer
<
GUM_SCALAR
>*
MultiDimNoisyAND
<
GUM_SCALAR
>::
newFactory
()
const
{
121
return
new
MultiDimNoisyAND
<
GUM_SCALAR
>(
this
->
_external_weight_
,
this
->
_default_weight_
);
122
}
123
124
// returns the name of the implementation
125
template
<
typename
GUM_SCALAR
>
126
INLINE
const
std
::
string
&
MultiDimNoisyAND
<
GUM_SCALAR
>::
name
()
const
{
127
static
const
std
::
string
str
=
"MultiDimNoisyAND"
;
128
return
str
;
129
}
130
131
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643