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