aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
treeOperatorStrategy_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 Template implementation of FMDP/planning/TreeOperatorStrategy.h
25
* classes.
26
*
27
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
28
* GONZALES(@AMU)
29
*/
30
31
// =========================================================================
32
#
include
<
algorithm
>
33
#
include
<
queue
>
34
#
include
<
utility
>
35
#
include
<
vector
>
36
// =========================================================================
37
#
include
<
agrum
/
tools
/
core
/
math
/
math_utils
.
h
>
38
#
include
<
agrum
/
tools
/
multidim
/
instantiation
.
h
>
39
// =========================================================================
40
#
include
<
agrum
/
FMDP
/
planning
/
treeOperatorStrategy
.
h
>
41
#
include
<
agrum
/
tools
/
multidim
/
utils
/
FunctionGraphUtilities
/
operators
/
treeOperator
.
h
>
42
#
include
<
agrum
/
tools
/
multidim
/
utils
/
FunctionGraphUtilities
/
operators
/
treeRegress
.
h
>
43
// =========================================================================
44
45
/// For shorter line and hence more comprehensive code only
46
#
define
RECAST
(
x
)
reinterpret_cast
<
const
MultiDimFunctionGraph
<
GUM_SCALAR
>
*
>
(
x
)
47
48
namespace
gum
{
49
50
51
/* **************************************************************************************************
52
* **/
53
/* ** **/
54
/* ** Constructors / Destructors **/
55
/* ** **/
56
/* **************************************************************************************************
57
* **/
58
59
// ===========================================================================
60
// Default constructor
61
// ===========================================================================
62
template
<
typename
GUM_SCALAR >
63
INLINE TreeOperatorStrategy<
GUM_SCALAR
>::
TreeOperatorStrategy
() {
64
GUM_CONSTRUCTOR
(
TreeOperatorStrategy
);
65
}
66
67
// ===========================================================================
68
// Default destructor
69
// ===========================================================================
70
template
<
typename
GUM_SCALAR
>
71
INLINE
TreeOperatorStrategy
<
GUM_SCALAR
>::~
TreeOperatorStrategy
() {
72
GUM_DESTRUCTOR
(
TreeOperatorStrategy
);
73
}
74
75
76
/* **************************************************************************************************
77
* **/
78
/* ** **/
79
/* ** Graph Function Operations Methods **/
80
/* ** **/
81
/* **************************************************************************************************
82
* **/
83
84
85
// ==========================================================================
86
/// Performs a multiplication/projection on given qAction
87
/// @param qAction : the computed Q(s,a)
88
/// @param pxip : the transition probabilities for the xip variable
89
/// @param xip : the variable we eliminate on the projection
90
/// @warning given qAction is deleted, return the new one
91
// ==========================================================================
92
template
<
typename
GUM_SCALAR
>
93
MultiDimFunctionGraph
<
GUM_SCALAR
>*
TreeOperatorStrategy
<
GUM_SCALAR
>::
regress
(
94
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
Vold
,
95
Idx
actionId
,
96
const
FMDP
<
GUM_SCALAR
>*
fmdp
,
97
const
Set
<
const
DiscreteVariable
* >&
elVarSeq
) {
98
// ******************************************************************************
99
// Initialisation :
100
// Creating a copy of last Vfunction to deduce from the new Qaction
101
// And finding the first var to eleminate (the one at the end)
102
Bijection
<
const
DiscreteVariable
*,
103
const
MultiDimFunctionGraph
<
GUM_SCALAR
>* >
104
pxi
;
105
for
(
SequenceIteratorSafe
<
const
DiscreteVariable
* >
varIter
106
=
Vold
->
variablesSequence
().
beginSafe
();
107
varIter
!=
Vold
->
variablesSequence
().
endSafe
();
108
++
varIter
) {
109
pxi
.
insert
(
110
*
varIter
,
111
RECAST
(
fmdp
->
transition
(
actionId
,
fmdp
->
mapMainPrime
().
first
(*
varIter
))));
112
}
113
114
TreeRegress
<
GUM_SCALAR
,
std
::
multiplies
,
std
::
plus
>
tr
(
115
Vold
,
116
pxi
);
//, Vold->variablesSequence(), (GUM_SCALAR) 0.0 );
117
return
tr
.
compute
();
118
}
119
120
// ==========================================================================
121
/// @warning given f1 and f2 are deleted, returns the new one
122
// ==========================================================================
123
template
<
typename
GUM_SCALAR
>
124
MultiDimFunctionGraph
<
GUM_SCALAR
>*
125
TreeOperatorStrategy
<
GUM_SCALAR
>::
maximize
(
126
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
127
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
128
Idx
del
) {
129
TreeOperator
<
GUM_SCALAR
,
Maximizes
>
opi
(
f1
,
f2
);
130
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
131
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
132
return
ret
;
133
}
134
135
// ==========================================================================
136
/// @warning given f1 and f2 are deleted, returns the new one
137
// ==========================================================================
138
template
<
typename
GUM_SCALAR
>
139
MultiDimFunctionGraph
<
GUM_SCALAR
>*
140
TreeOperatorStrategy
<
GUM_SCALAR
>::
minimize
(
141
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
142
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
143
Idx
del
) {
144
TreeOperator
<
GUM_SCALAR
,
Minimizes
>
opi
(
f1
,
f2
);
145
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
146
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
147
return
ret
;
148
}
149
150
// ==========================================================================
151
// ==========================================================================
152
template
<
typename
GUM_SCALAR
>
153
MultiDimFunctionGraph
<
GUM_SCALAR
>*
154
TreeOperatorStrategy
<
GUM_SCALAR
>::
multiply
(
155
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
156
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
157
Idx
del
) {
158
TreeOperator
<
GUM_SCALAR
,
std
::
multiplies
>
opi
(
f1
,
f2
);
159
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
160
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
161
return
ret
;
162
}
163
164
165
// ==========================================================================
166
/// ArgMaximizes between f2 and f1
167
/// @warning given f1 and f2 are deleted, returns the new one
168
// ==========================================================================
169
template
<
typename
GUM_SCALAR
>
170
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
SetTerminalNodePolicy
>*
171
TreeOperatorStrategy
<
GUM_SCALAR
>::
argmaximize
(
172
const
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
173
SetTerminalNodePolicy
>*
f1
,
174
const
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
175
SetTerminalNodePolicy
>*
f2
,
176
Idx
del
) {
177
TreeOperator
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
178
ArgumentMaximisesAction
,
179
SetTerminalNodePolicy
>
180
argmaxope
(
f1
,
f2
);
181
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
SetTerminalNodePolicy
>*
182
ret
183
=
argmaxope
.
compute
();
184
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
185
return
ret
;
186
}
187
188
// ==========================================================================
189
/// Adds reward to given function( whether a f2 or f1)
190
/// @param reward : R(s) or R(s,a)
191
/// @param function : either V(s) or Q(s,a)
192
/// @warning given function is deleted, returns the new one
193
// ==========================================================================
194
template
<
typename
GUM_SCALAR
>
195
MultiDimFunctionGraph
<
GUM_SCALAR
>*
TreeOperatorStrategy
<
GUM_SCALAR
>::
add
(
196
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
197
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
198
Idx
del
) {
199
TreeOperator
<
GUM_SCALAR
,
std
::
plus
>
opi
(
f1
,
f2
);
200
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
201
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
202
return
ret
;
203
}
204
205
// ==========================================================================
206
/// Subtract current f1 from old f1 to see if threshold is
207
/// reached or not
208
/// @param old and new VFuntion
209
/// @warning this time, nothing is deleted
210
// ==========================================================================
211
template
<
typename
GUM_SCALAR
>
212
MultiDimFunctionGraph
<
GUM_SCALAR
>*
213
TreeOperatorStrategy
<
GUM_SCALAR
>::
subtract
(
214
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
215
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
216
Idx
del
) {
217
TreeOperator
<
GUM_SCALAR
,
std
::
minus
>
opi
(
f1
,
f2
);
218
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
219
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
220
return
ret
;
221
}
222
223
224
}
// end of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
RECAST
#define RECAST(x)
Definition:
fmdp_tpl.h:36