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