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