aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
treeOperatorStrategy_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/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
>*
94
TreeOperatorStrategy
<
GUM_SCALAR
>::
regress
(
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
*,
const
MultiDimFunctionGraph
<
GUM_SCALAR
>* >
pxi
;
103
for
(
SequenceIteratorSafe
<
const
DiscreteVariable
* >
varIter
104
=
Vold
->
variablesSequence
().
beginSafe
();
105
varIter
!=
Vold
->
variablesSequence
().
endSafe
();
106
++
varIter
) {
107
pxi
.
insert
(*
varIter
,
108
RECAST
(
fmdp
->
transition
(
actionId
,
fmdp
->
mapMainPrime
().
first
(*
varIter
))));
109
}
110
111
TreeRegress
<
GUM_SCALAR
,
std
::
multiplies
,
std
::
plus
>
tr
(
112
Vold
,
113
pxi
);
//, Vold->variablesSequence(), (GUM_SCALAR) 0.0 );
114
return
tr
.
compute
();
115
}
116
117
// ==========================================================================
118
/// @warning given f1 and f2 are deleted, returns the new one
119
// ==========================================================================
120
template
<
typename
GUM_SCALAR
>
121
MultiDimFunctionGraph
<
GUM_SCALAR
>*
122
TreeOperatorStrategy
<
GUM_SCALAR
>::
maximize
(
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
123
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
124
Idx
del
) {
125
TreeOperator
<
GUM_SCALAR
,
Maximizes
>
opi
(
f1
,
f2
);
126
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
127
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
128
return
ret
;
129
}
130
131
// ==========================================================================
132
/// @warning given f1 and f2 are deleted, returns the new one
133
// ==========================================================================
134
template
<
typename
GUM_SCALAR
>
135
MultiDimFunctionGraph
<
GUM_SCALAR
>*
136
TreeOperatorStrategy
<
GUM_SCALAR
>::
minimize
(
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
137
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
138
Idx
del
) {
139
TreeOperator
<
GUM_SCALAR
,
Minimizes
>
opi
(
f1
,
f2
);
140
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
141
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
142
return
ret
;
143
}
144
145
// ==========================================================================
146
// ==========================================================================
147
template
<
typename
GUM_SCALAR
>
148
MultiDimFunctionGraph
<
GUM_SCALAR
>*
149
TreeOperatorStrategy
<
GUM_SCALAR
>::
multiply
(
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
150
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
151
Idx
del
) {
152
TreeOperator
<
GUM_SCALAR
,
std
::
multiplies
>
opi
(
f1
,
f2
);
153
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
154
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
155
return
ret
;
156
}
157
158
159
// ==========================================================================
160
/// ArgMaximizes between f2 and f1
161
/// @warning given f1 and f2 are deleted, returns the new one
162
// ==========================================================================
163
template
<
typename
GUM_SCALAR
>
164
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
SetTerminalNodePolicy
>*
165
TreeOperatorStrategy
<
GUM_SCALAR
>::
argmaximize
(
166
const
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
SetTerminalNodePolicy
>*
f1
,
167
const
MultiDimFunctionGraph
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
SetTerminalNodePolicy
>*
f2
,
168
Idx
del
) {
169
TreeOperator
<
ArgMaxSet
<
GUM_SCALAR
,
Idx
>,
ArgumentMaximisesAction
,
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 f2 or f1)
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
TreeOperatorStrategy
<
GUM_SCALAR
>::
add
(
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
186
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
187
Idx
del
) {
188
TreeOperator
<
GUM_SCALAR
,
std
::
plus
>
opi
(
f1
,
f2
);
189
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
190
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
191
return
ret
;
192
}
193
194
// ==========================================================================
195
/// Subtract current f1 from old f1 to see if threshold is
196
/// reached or not
197
/// @param old and new VFuntion
198
/// @warning this time, nothing is deleted
199
// ==========================================================================
200
template
<
typename
GUM_SCALAR
>
201
MultiDimFunctionGraph
<
GUM_SCALAR
>*
202
TreeOperatorStrategy
<
GUM_SCALAR
>::
subtract
(
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f1
,
203
const
MultiDimFunctionGraph
<
GUM_SCALAR
>*
f2
,
204
Idx
del
) {
205
TreeOperator
<
GUM_SCALAR
,
std
::
minus
>
opi
(
f1
,
f2
);
206
MultiDimFunctionGraph
<
GUM_SCALAR
>*
ret
=
opi
.
compute
();
207
this
->
deleteFunctionGraph_
(
f1
,
f2
,
del
);
208
return
ret
;
209
}
210
211
212
}
// 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