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