aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
taxiSimulator.cpp
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 Sources of
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*
29
*/
30
// =====================================================================
31
#
include
<
cstdlib
>
32
#
include
<
random
>
33
//======================================================================
34
#
include
<
agrum
/
FMDP
/
simulation
/
taxiSimulator
.
h
>
35
//======================================================================
36
37
namespace
gum
{
38
39
TaxiSimulator
::
TaxiSimulator
() :
AbstractSimulator
() {
40
GUM_CONSTRUCTOR
(
TaxiSimulator
);
41
42
// *****************************************************************************************
43
// Défintion des variables du problème
44
45
// Position TaxiSimulator
46
xPos__
47
=
new
LabelizedVariable
(
"xPos"
,
"Position horizontale du TaxiSimulator"
);
48
yPos__
=
new
LabelizedVariable
(
"yPos"
,
"Position verticale du TaxiSimulator"
);
49
xPos__
->
eraseLabels
();
50
yPos__
->
eraseLabels
();
51
for
(
Idx
pos
= 0;
pos
< 5;
pos
++) {
52
std
::
stringstream
ss
;
53
ss
<<
pos
;
54
xPos__
->
addLabel
(
ss
.
str
());
55
yPos__
->
addLabel
(
ss
.
str
());
56
}
57
58
// Position et destination passager
59
passengerPos__
60
=
new
LabelizedVariable
(
"PassengerPos"
,
"Position du Passager"
, 5);
61
passengerDest__
62
=
new
LabelizedVariable
(
"PassengerDest"
,
"Destination du Passager"
, 4);
63
passengerPos__
->
changeLabel
(
HOME
,
"Home"
);
64
passengerDest__
->
changeLabel
(
HOME
,
"Home"
);
65
passengerPos__
->
changeLabel
(
WORK
,
"Work"
);
66
passengerDest__
->
changeLabel
(
WORK
,
"Work"
);
67
passengerPos__
->
changeLabel
(
THEATER
,
"Theater"
);
68
passengerDest__
->
changeLabel
(
THEATER
,
"Theater"
);
69
passengerPos__
->
changeLabel
(
CLUB
,
"Club"
);
70
passengerDest__
->
changeLabel
(
CLUB
,
"Club"
);
71
passengerPos__
->
changeLabel
(
TAXI
,
"Taxi"
);
72
73
fuelLevel__
=
new
LabelizedVariable
(
"FuelLevel"
,
"Niveau du réservoir"
, 14);
74
75
// Ajout à séquence
76
taxiVars__
.
insert
(
xPos__
);
77
taxiVars__
.
insert
(
yPos__
);
78
taxiVars__
.
insert
(
passengerPos__
);
79
taxiVars__
.
insert
(
passengerDest__
);
80
taxiVars__
.
insert
(
fuelLevel__
);
81
82
// Prime version creation
83
for
(
SequenceIteratorSafe
<
const
DiscreteVariable
* >
varIter
84
=
this
->
beginVariables
();
85
varIter
!=
this
->
endVariables
();
86
++
varIter
) {
87
DiscreteVariable
*
primeVar
= (*
varIter
)->
clone
();
88
primeVar
->
setName
((*
varIter
)->
name
() +
"'"
);
89
primeMap__
.
insert
((*
varIter
),
primeVar
);
90
}
91
92
// *****************************************************************************************
93
94
// *****************************************************************************************
95
// Défintion des actions du problème
96
taxiActions__
.
insert
(
GoNorth
);
97
actionMap__
.
insert
(
GoNorth
,
new
std
::
string
(
"Go North"
));
98
taxiActions__
.
insert
(
GoEast
);
99
actionMap__
.
insert
(
GoEast
,
new
std
::
string
(
"Go East"
));
100
taxiActions__
.
insert
(
GoSouth
);
101
actionMap__
.
insert
(
GoSouth
,
new
std
::
string
(
"Go South"
));
102
taxiActions__
.
insert
(
GoWest
);
103
actionMap__
.
insert
(
GoWest
,
new
std
::
string
(
"Go West"
));
104
taxiActions__
.
insert
(
PickUp
);
105
actionMap__
.
insert
(
PickUp
,
new
std
::
string
(
"Pick Up"
));
106
taxiActions__
.
insert
(
PutDown
);
107
actionMap__
.
insert
(
PutDown
,
new
std
::
string
(
"Put Down"
));
108
taxiActions__
.
insert
(
FillUp
);
109
actionMap__
.
insert
(
FillUp
,
new
std
::
string
(
"FillUp"
));
110
}
111
112
TaxiSimulator
::~
TaxiSimulator
() {
113
GUM_DESTRUCTOR
(
TaxiSimulator
);
114
115
for
(
BijectionIteratorSafe
<
const
DiscreteVariable
*,
const
DiscreteVariable
* >
116
varIter
117
=
primeMap__
.
beginSafe
();
118
varIter
!=
primeMap__
.
endSafe
();
119
++
varIter
) {
120
delete
varIter
.
first
();
121
delete
varIter
.
second
();
122
}
123
}
124
125
126
// ==================================================================================================================
127
// Reward according to the situation
128
// ==================================================================================================================
129
Instantiation
TaxiSimulator
::
randomState_
() {
130
Instantiation
randy
=
AbstractSimulator
::
randomState_
();
131
// Idx curFuelLevel = randy.valFromPtr(fuelLevel__);
132
// while(curFuelLevel > 12 || curFuelLevel < 5)
133
// curFuelLevel = (Idx)(((double)std::rand( ) / (double)RAND_MAX) *
134
// 7.0) + 5;
135
// randy.chgVal(fuelLevel__, curFuelLevel);
136
137
// TaxiSimulationLandmark passPos = (TaxiSimulationLandmark)
138
// randy.valFromPtr(passengerPos__);
139
// TaxiSimulationLandmark passDest = (TaxiSimulationLandmark)
140
// randy.valFromPtr(passengerDest__);
141
// while( passPos == passDest || passPos == TAXI )
142
// passPos = (TaxiSimulationLandmark) (((double)std::rand( ) /
143
// (double)RAND_MAX) * 3.0);
144
// randy.chgVal(passengerPos__, passPos);
145
146
return
randy
;
147
}
148
149
150
// ==================================================================================================================
151
// Reward according to the situation
152
// ==================================================================================================================
153
bool
TaxiSimulator
::
hasReachEnd
() {
154
// if( currentState_.valFromPtr(passengerPos__) ==
155
// currentState_.valFromPtr(passengerDest__) )
156
// return true;
157
158
// if( currentState_.valFromPtr(fuelLevel__) == 0 )
159
// return true;
160
161
return
false
;
162
}
163
164
165
// ==================================================================================================================
166
// Reward according to the situation
167
// ==================================================================================================================
168
double
TaxiSimulator
::
reward
() {
return
reward__
; }
169
170
// ==================================================================================================================
171
// Reward according to the situation
172
// ==================================================================================================================
173
void
TaxiSimulator
::
perform
(
Idx
actionId
) {
174
lastAction__
= (
TaxiSimulationAction
)
actionId
;
175
176
evalReward__
();
177
178
Idx
curFuelLevel
=
currentState_
.
valFromPtr
(
fuelLevel__
);
179
if
(
curFuelLevel
> 0)
currentState_
.
chgVal
(
fuelLevel__
, --
curFuelLevel
);
180
181
switch
(
actionId
) {
182
case
GoNorth
:
183
return
performGoNorth__
();
184
case
GoEast
:
185
return
performGoEast__
();
186
case
GoSouth
:
187
return
performGoSouth__
();
188
case
GoWest
:
189
return
performGoWest__
();
190
case
PickUp
:
191
return
performPickUp__
();
192
case
PutDown
:
193
return
performPutDown__
();
194
case
FillUp
:
195
return
performFillUp__
();
196
}
197
}
198
199
200
// ==================================================================================================================
201
// Transition if you go North
202
// ==================================================================================================================
203
void
TaxiSimulator
::
performGoNorth__
() {
204
Idx
curPos
=
this
->
currentState_
.
valFromPtr
(
yPos__
);
205
if
(
curPos
< 4)
currentState_
.
chgVal
(
yPos__
, ++
curPos
);
206
}
207
208
209
// ==================================================================================================================
210
// Transition if you go east
211
// ==================================================================================================================
212
void
TaxiSimulator
::
performGoEast__
() {
213
Idx
xCurPos
=
this
->
currentState_
.
valFromPtr
(
xPos__
);
214
Idx
yCurPos
=
this
->
currentState_
.
valFromPtr
(
yPos__
);
215
216
if
(
xCurPos
== 4)
return
;
217
if
(
xCurPos
== 0 ||
xCurPos
== 2)
218
if
(
yCurPos
== 0 ||
yCurPos
== 1)
return
;
219
if
(
xCurPos
== 1)
220
if
(
yCurPos
== 3 ||
yCurPos
== 4)
return
;
221
222
currentState_
.
chgVal
(
xPos__
, ++
xCurPos
);
223
}
224
225
226
// ==================================================================================================================
227
// Transition if you go south
228
// ==================================================================================================================
229
void
TaxiSimulator
::
performGoSouth__
() {
230
Idx
curPos
=
this
->
currentState_
.
valFromPtr
(
yPos__
);
231
if
(
curPos
> 0)
currentState_
.
chgVal
(
yPos__
, --
curPos
);
232
}
233
234
235
// ==================================================================================================================
236
// Transition if you go west
237
// ==================================================================================================================
238
void
TaxiSimulator
::
performGoWest__
() {
239
Idx
xCurPos
=
this
->
currentState_
.
valFromPtr
(
xPos__
);
240
Idx
yCurPos
=
this
->
currentState_
.
valFromPtr
(
yPos__
);
241
242
if
(
xCurPos
== 0)
return
;
243
if
(
xCurPos
== 1 ||
xCurPos
== 3)
244
if
(
yCurPos
== 0 ||
yCurPos
== 1)
return
;
245
if
(
xCurPos
== 2)
246
if
(
yCurPos
== 3 ||
yCurPos
== 4)
return
;
247
248
currentState_
.
chgVal
(
xPos__
, --
xCurPos
);
249
}
250
251
252
// ==================================================================================================================
253
// Transition if you go pick up sb
254
// ==================================================================================================================
255
void
TaxiSimulator
::
performPickUp__
() {
256
TaxiSimulationLandmarkX
xCurPos
257
= (
TaxiSimulationLandmarkX
)
this
->
currentState_
.
valFromPtr
(
xPos__
);
258
TaxiSimulationLandmarkY
yCurPos
259
= (
TaxiSimulationLandmarkY
)
this
->
currentState_
.
valFromPtr
(
yPos__
);
260
TaxiSimulationLandmark
passPos
261
= (
TaxiSimulationLandmark
)
this
->
currentState_
.
valFromPtr
(
passengerPos__
);
262
switch
(
passPos
) {
263
case
HOME
: {
264
if
(
xCurPos
==
HOMEX
&&
yCurPos
==
HOMEY
)
265
currentState_
.
chgVal
(
passengerPos__
,
TAXI
);
266
return
;
267
}
268
case
WORK
: {
269
if
(
xCurPos
==
WORKX
&&
yCurPos
==
WORKY
)
270
currentState_
.
chgVal
(
passengerPos__
,
TAXI
);
271
return
;
272
}
273
case
THEATER
: {
274
if
(
xCurPos
==
THEATERX
&&
yCurPos
==
THEATERY
)
275
currentState_
.
chgVal
(
passengerPos__
,
TAXI
);
276
return
;
277
}
278
case
CLUB
: {
279
if
(
xCurPos
==
CLUBX
&&
yCurPos
==
CLUBY
)
280
currentState_
.
chgVal
(
passengerPos__
,
TAXI
);
281
return
;
282
}
283
case
TAXI
:
284
return
;
285
}
286
}
287
288
289
// ==================================================================================================================
290
// Transition if you go put down sb
291
// ==================================================================================================================
292
void
TaxiSimulator
::
performPutDown__
() {
293
TaxiSimulationLandmarkX
xCurPos
294
= (
TaxiSimulationLandmarkX
)
this
->
currentState_
.
valFromPtr
(
xPos__
);
295
TaxiSimulationLandmarkY
yCurPos
296
= (
TaxiSimulationLandmarkY
)
this
->
currentState_
.
valFromPtr
(
yPos__
);
297
TaxiSimulationLandmark
passPos
298
= (
TaxiSimulationLandmark
)
this
->
currentState_
.
valFromPtr
(
passengerPos__
);
299
TaxiSimulationLandmark
passDest
300
= (
TaxiSimulationLandmark
)
this
->
currentState_
.
valFromPtr
(
passengerDest__
);
301
if
(
passPos
==
TAXI
) {
302
switch
(
passDest
) {
303
case
HOME
: {
304
if
(
xCurPos
==
HOMEX
&&
yCurPos
==
HOMEY
)
305
currentState_
.
chgVal
(
passengerPos__
,
HOME
);
306
return
;
307
}
308
case
WORK
: {
309
if
(
xCurPos
==
WORKX
&&
yCurPos
==
WORKY
)
310
currentState_
.
chgVal
(
passengerPos__
,
WORK
);
311
return
;
312
}
313
case
THEATER
: {
314
if
(
xCurPos
==
THEATERX
&&
yCurPos
==
THEATERY
)
315
currentState_
.
chgVal
(
passengerPos__
,
THEATER
);
316
return
;
317
}
318
case
CLUB
: {
319
if
(
xCurPos
==
CLUBX
&&
yCurPos
==
CLUBY
)
320
currentState_
.
chgVal
(
passengerPos__
,
CLUB
);
321
return
;
322
}
323
case
TAXI
:
324
return
;
325
}
326
}
327
}
328
329
330
// ==================================================================================================================
331
// Transition if you go reffill
332
// ==================================================================================================================
333
void
TaxiSimulator
::
performFillUp__
() {
334
TaxiSimulationLandmarkX
xCurPos
335
= (
TaxiSimulationLandmarkX
)
this
->
currentState_
.
valFromPtr
(
xPos__
);
336
TaxiSimulationLandmarkY
yCurPos
337
= (
TaxiSimulationLandmarkY
)
this
->
currentState_
.
valFromPtr
(
yPos__
);
338
339
if
(
xCurPos
==
STATIONX
&&
yCurPos
==
STATIONY
)
340
currentState_
.
chgVal
(
fuelLevel__
, 13);
341
}
342
343
344
// ==================================================================================================================
345
// Reward according to the situation
346
// ==================================================================================================================
347
void
TaxiSimulator
::
evalReward__
() {
348
TaxiSimulationLandmarkX
xCurPos
349
= (
TaxiSimulationLandmarkX
)
this
->
currentState_
.
valFromPtr
(
xPos__
);
350
TaxiSimulationLandmarkY
yCurPos
351
= (
TaxiSimulationLandmarkY
)
this
->
currentState_
.
valFromPtr
(
yPos__
);
352
TaxiSimulationLandmark
passPos
353
= (
TaxiSimulationLandmark
)
this
->
currentState_
.
valFromPtr
(
passengerPos__
);
354
TaxiSimulationLandmark
passDest
355
= (
TaxiSimulationLandmark
)
this
->
currentState_
.
valFromPtr
(
passengerDest__
);
356
357
if
(
lastAction__
==
PutDown
) {
358
if
(
passPos
==
TAXI
) {
359
if
(
isAtDestination__
(
passDest
,
xCurPos
,
yCurPos
))
360
reward__
= 30.0;
361
else
362
reward__
= 0.0;
363
return
;
364
}
365
reward__
= 0;
366
return
;
367
}
368
369
if
(
lastAction__
==
PickUp
) {
370
if
(
isAtMeetPoint__
(
passPos
,
xCurPos
,
yCurPos
))
371
reward__
= 20.0;
372
else
373
reward__
= 0.0;
374
return
;
375
}
376
377
if
(
currentState_
.
valFromPtr
(
fuelLevel__
) == 0 &&
lastAction__
!=
FillUp
) {
378
reward__
= 0.0;
379
return
;
380
}
381
382
if
(
lastAction__
==
FillUp
&& (
xCurPos
!=
STATIONX
||
yCurPos
!=
STATIONY
)) {
383
reward__
= 0.0;
384
return
;
385
}
386
387
reward__
= 10.0;
//-1.0;
388
}
389
390
bool
TaxiSimulator
::
isAtDestination__
(
TaxiSimulationLandmark
passDest
,
391
TaxiSimulationLandmarkX
xCurPos
,
392
TaxiSimulationLandmarkY
yCurPos
) {
393
switch
(
passDest
) {
394
case
HOME
: {
395
if
(
xCurPos
==
HOMEX
&&
yCurPos
==
HOMEY
)
return
true
;
396
break
;
397
}
398
case
WORK
: {
399
if
(
xCurPos
==
WORKX
&&
yCurPos
==
WORKY
)
return
true
;
400
break
;
401
}
402
case
THEATER
: {
403
if
(
xCurPos
==
THEATERX
&&
yCurPos
==
THEATERY
)
return
true
;
404
break
;
405
}
406
case
CLUB
: {
407
if
(
xCurPos
==
CLUBX
&&
yCurPos
==
CLUBY
)
return
true
;
408
break
;
409
}
410
case
TAXI
:
411
return
false
;
412
}
413
return
false
;
414
}
415
416
bool
TaxiSimulator
::
isAtMeetPoint__
(
TaxiSimulationLandmark
passPos
,
417
TaxiSimulationLandmarkX
xCurPos
,
418
TaxiSimulationLandmarkY
yCurPos
) {
419
switch
(
passPos
) {
420
case
HOME
: {
421
if
(
xCurPos
==
HOMEX
&&
yCurPos
==
HOMEY
)
return
true
;
422
break
;
423
}
424
case
WORK
: {
425
if
(
xCurPos
==
WORKX
&&
yCurPos
==
WORKY
)
return
true
;
426
break
;
427
}
428
case
THEATER
: {
429
if
(
xCurPos
==
THEATERX
&&
yCurPos
==
THEATERY
)
return
true
;
430
break
;
431
}
432
case
CLUB
: {
433
if
(
xCurPos
==
CLUBX
&&
yCurPos
==
CLUBY
)
return
true
;
434
break
;
435
}
436
case
TAXI
:
437
return
false
;
438
}
439
return
false
;
440
}
441
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669