aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
treeRegress_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 used to compute the operation between two decision diagrams
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
29
*/
30
31
#
include
<
agrum
/
tools
/
multidim
/
utils
/
FunctionGraphUtilities
/
internalNode
.
h
>
32
#
include
<
agrum
/
tools
/
multidim
/
utils
/
FunctionGraphUtilities
/
operators
/
treeOperator
.
h
>
33
#
include
<
agrum
/
tools
/
multidim
/
utils
/
FunctionGraphUtilities
/
operators
/
treeRegress
.
h
>
34
35
#
define
ALLOCATE
(
x
)
SmallObjectAllocator
::
instance
(
)
.
allocate
(
x
)
36
#
define
DEALLOCATE
(
x
,
y
)
SmallObjectAllocator
::
instance
(
)
.
deallocate
(
x
,
y
)
37
38
namespace
gum
{
39
40
template
<
typename
GUM_SCALAR,
41
template
<
typename
>
42
class
COMBINEOPERATOR,
43
template
<
typename
>
44
class
PROJECTOPERATOR,
45
template
<
typename
>
46
class
TerminalNodePolicy >
47
TreeRegress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::
48
TreeRegress(
49
const
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* qAction,
50
const
Bijection<
51
const
DiscreteVariable*,
52
const
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* > pxi) :
53
vFunc__(qAction),
54
pxi__(pxi) {
55
GUM_CONSTRUCTOR(TreeRegress);
56
}
57
58
template
<
typename
GUM_SCALAR
,
59
template
<
typename
>
60
class
COMBINEOPERATOR
,
61
template
<
typename
>
62
class
PROJECTOPERATOR
,
63
template
<
typename
>
64
class
TerminalNodePolicy
>
65
TreeRegress
<
GUM_SCALAR
,
COMBINEOPERATOR
,
PROJECTOPERATOR
,
TerminalNodePolicy
>::
66
~
TreeRegress
() {
67
GUM_DESTRUCTOR
(
TreeRegress
);
68
}
69
70
// This function is the main function. To be call every time an operation
71
// between the two given Function Graphs is required
72
template
<
typename
GUM_SCALAR
,
73
template
<
typename
>
74
class
COMBINEOPERATOR
,
75
template
<
typename
>
76
class
PROJECTOPERATOR
,
77
template
<
typename
>
78
class
TerminalNodePolicy
>
79
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
80
TreeRegress
<
GUM_SCALAR
,
81
COMBINEOPERATOR
,
82
PROJECTOPERATOR
,
83
TerminalNodePolicy
>::
compute
() {
84
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
ret
;
85
if
(
vFunc__
->
isTerminalNode
(
vFunc__
->
root
())) {
86
ret
=
MultiDimFunctionGraph
<
GUM_SCALAR
,
87
TerminalNodePolicy
>::
getTreeInstance
();
88
ret
->
copy
(*
vFunc__
);
89
}
else
90
ret
=
xPloreVFunc__
(
vFunc__
->
root
());
91
return
ret
;
92
}
93
94
95
template
<
typename
GUM_SCALAR
,
96
template
<
typename
>
97
class
COMBINEOPERATOR
,
98
template
<
typename
>
99
class
PROJECTOPERATOR
,
100
template
<
typename
>
101
class
TerminalNodePolicy
>
102
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
103
TreeRegress
<
GUM_SCALAR
,
104
COMBINEOPERATOR
,
105
PROJECTOPERATOR
,
106
TerminalNodePolicy
>::
xPloreVFunc__
(
NodeId
currentNodeId
) {
107
const
InternalNode
*
currentNode
=
vFunc__
->
node
(
currentNodeId
);
108
109
std
::
vector
<
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>* >
110
varbucket
;
111
112
for
(
Idx
moda
= 0;
moda
<
currentNode
->
nodeVar
()->
domainSize
(); ++
moda
) {
113
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
vpxi
=
nullptr
;
114
context__
.
insert
(
currentNode
->
nodeVar
(),
moda
);
115
if
(
vFunc__
->
isTerminalNode
(
currentNode
->
son
(
moda
))) {
116
GUM_SCALAR
value
=
vFunc__
->
nodeValue
(
currentNode
->
son
(
moda
));
117
if
(
value
) {
118
vpxi
=
MultiDimFunctionGraph
<
GUM_SCALAR
,
119
TerminalNodePolicy
>::
getTreeInstance
();
120
vpxi
->
manager
()->
setRootNode
(
vpxi
->
manager
()->
addTerminalNode
(
value
));
121
}
122
}
else
{
123
vpxi
=
xPloreVFunc__
(
currentNode
->
son
(
moda
));
124
}
125
126
if
(
vpxi
!=
nullptr
) {
127
TreeOperator
<
GUM_SCALAR
,
COMBINEOPERATOR
>
combinope
(
128
vpxi
,
129
pxi__
.
second
(
currentNode
->
nodeVar
()),
130
context__
);
131
varbucket
.
push_back
(
combinope
.
compute
());
132
}
133
delete
vpxi
;
134
context__
.
erase
(
currentNode
->
nodeVar
());
135
}
136
137
if
(
varbucket
.
empty
())
return
nullptr
;
138
139
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
vp
=
varbucket
.
back
();
140
varbucket
.
pop_back
();
141
while
(!
varbucket
.
empty
()) {
142
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
temp
=
vp
;
143
TreeOperator
<
GUM_SCALAR
,
PROJECTOPERATOR
>
projope
(
vp
,
varbucket
.
back
());
144
vp
=
projope
.
compute
();
145
delete
temp
;
146
temp
=
varbucket
.
back
();
147
varbucket
.
pop_back
();
148
delete
temp
;
149
}
150
151
return
vp
;
152
}
153
154
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669