aGrUM
0.21.0
a C++ library for (probabilistic) graphical models
treeRegress_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 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 >::TreeRegress(
48
const
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* qAction,
49
const
Bijection<
const
DiscreteVariable*,
50
const
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* > pxi) :
51
_vFunc_(qAction),
52
_pxi_(pxi) {
53
GUM_CONSTRUCTOR(TreeRegress);
54
}
55
56
template
<
typename
GUM_SCALAR
,
57
template
<
typename
>
58
class
COMBINEOPERATOR
,
59
template
<
typename
>
60
class
PROJECTOPERATOR
,
61
template
<
typename
>
62
class
TerminalNodePolicy
>
63
TreeRegress
<
GUM_SCALAR
,
COMBINEOPERATOR
,
PROJECTOPERATOR
,
TerminalNodePolicy
>::~
TreeRegress
() {
64
GUM_DESTRUCTOR
(
TreeRegress
);
65
}
66
67
// This function is the main function. To be call every time an operation
68
// between the two given Function Graphs is required
69
template
<
typename
GUM_SCALAR
,
70
template
<
typename
>
71
class
COMBINEOPERATOR
,
72
template
<
typename
>
73
class
PROJECTOPERATOR
,
74
template
<
typename
>
75
class
TerminalNodePolicy
>
76
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
77
TreeRegress
<
GUM_SCALAR
,
COMBINEOPERATOR
,
PROJECTOPERATOR
,
TerminalNodePolicy
>::
compute
() {
78
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
ret
;
79
if
(
_vFunc_
->
isTerminalNode
(
_vFunc_
->
root
())) {
80
ret
=
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>::
getTreeInstance
();
81
ret
->
copy
(*
_vFunc_
);
82
}
else
83
ret
=
_xPloreVFunc_
(
_vFunc_
->
root
());
84
return
ret
;
85
}
86
87
88
template
<
typename
GUM_SCALAR
,
89
template
<
typename
>
90
class
COMBINEOPERATOR
,
91
template
<
typename
>
92
class
PROJECTOPERATOR
,
93
template
<
typename
>
94
class
TerminalNodePolicy
>
95
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
96
TreeRegress
<
GUM_SCALAR
,
COMBINEOPERATOR
,
PROJECTOPERATOR
,
TerminalNodePolicy
>::
_xPloreVFunc_
(
97
NodeId
currentNodeId
) {
98
const
InternalNode
*
currentNode
=
_vFunc_
->
node
(
currentNodeId
);
99
100
std
::
vector
<
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>* >
varbucket
;
101
102
for
(
Idx
moda
= 0;
moda
<
currentNode
->
nodeVar
()->
domainSize
(); ++
moda
) {
103
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
vpxi
=
nullptr
;
104
_context_
.
insert
(
currentNode
->
nodeVar
(),
moda
);
105
if
(
_vFunc_
->
isTerminalNode
(
currentNode
->
son
(
moda
))) {
106
GUM_SCALAR
value
=
_vFunc_
->
nodeValue
(
currentNode
->
son
(
moda
));
107
if
(
value
) {
108
vpxi
=
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>::
getTreeInstance
();
109
vpxi
->
manager
()->
setRootNode
(
vpxi
->
manager
()->
addTerminalNode
(
value
));
110
}
111
}
else
{
112
vpxi
=
_xPloreVFunc_
(
currentNode
->
son
(
moda
));
113
}
114
115
if
(
vpxi
!=
nullptr
) {
116
TreeOperator
<
GUM_SCALAR
,
COMBINEOPERATOR
>
combinope
(
vpxi
,
117
_pxi_
.
second
(
currentNode
->
nodeVar
()),
118
_context_
);
119
varbucket
.
push_back
(
combinope
.
compute
());
120
}
121
delete
vpxi
;
122
_context_
.
erase
(
currentNode
->
nodeVar
());
123
}
124
125
if
(
varbucket
.
empty
())
return
nullptr
;
126
127
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
vp
=
varbucket
.
back
();
128
varbucket
.
pop_back
();
129
while
(!
varbucket
.
empty
()) {
130
MultiDimFunctionGraph
<
GUM_SCALAR
,
TerminalNodePolicy
>*
temp
=
vp
;
131
TreeOperator
<
GUM_SCALAR
,
PROJECTOPERATOR
>
projope
(
vp
,
varbucket
.
back
());
132
vp
=
projope
.
compute
();
133
delete
temp
;
134
temp
=
varbucket
.
back
();
135
varbucket
.
pop_back
();
136
delete
temp
;
137
}
138
139
return
vp
;
140
}
141
142
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643