aGrUM  0.16.0
taxiSimulator.cpp
Go to the documentation of this file.
1 
30 // =====================================================================
31 #include <cstdlib>
32 #include <random>
33 //======================================================================
35 //======================================================================
36 
37 namespace gum {
38 
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");
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
60  new LabelizedVariable("PassengerPos", "Position du Passager", 5);
62  new LabelizedVariable("PassengerDest", "Destination du Passager", 4);
67  __passengerPos->changeLabel(THEATER, "Theater");
68  __passengerDest->changeLabel(THEATER, "Theater");
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);
80  __taxiVars.insert(__fuelLevel);
81 
82  // Prime version creation
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
97  __actionMap.insert(GoNorth, new std::string("Go North"));
99  __actionMap.insert(GoEast, new std::string("Go East"));
101  __actionMap.insert(GoSouth, new std::string("Go South"));
103  __actionMap.insert(GoWest, new std::string("Go West"));
105  __actionMap.insert(PickUp, new std::string("Pick Up"));
107  __actionMap.insert(PutDown, new std::string("Put Down"));
109  __actionMap.insert(FillUp, new std::string("FillUp"));
110  }
111 
113  GUM_DESTRUCTOR(TaxiSimulator);
114 
116  varIter = __primeMap.beginSafe();
117  varIter != __primeMap.endSafe();
118  ++varIter) {
119  delete varIter.first();
120  delete varIter.second();
121  }
122  }
123 
124 
125  // ==================================================================================================================
126  // Reward according to the situation
127  // ==================================================================================================================
130  // Idx curFuelLevel = randy.valFromPtr(__fuelLevel);
131  // while(curFuelLevel > 12 || curFuelLevel < 5)
132  // curFuelLevel = (Idx)(((double)std::rand( ) / (double)RAND_MAX) *
133  // 7.0) + 5;
134  // randy.chgVal(__fuelLevel, curFuelLevel);
135 
136  // TaxiSimulationLandmark passPos = (TaxiSimulationLandmark)
137  // randy.valFromPtr(__passengerPos);
138  // TaxiSimulationLandmark passDest = (TaxiSimulationLandmark)
139  // randy.valFromPtr(__passengerDest);
140  // while( passPos == passDest || passPos == TAXI )
141  // passPos = (TaxiSimulationLandmark) (((double)std::rand( ) /
142  // (double)RAND_MAX) * 3.0);
143  // randy.chgVal(__passengerPos, passPos);
144 
145  return randy;
146  }
147 
148 
149  // ==================================================================================================================
150  // Reward according to the situation
151  // ==================================================================================================================
153  // if( _currentState.valFromPtr(__passengerPos) ==
154  // _currentState.valFromPtr(__passengerDest) )
155  // return true;
156 
157  // if( _currentState.valFromPtr(__fuelLevel) == 0 )
158  // return true;
159 
160  return false;
161  }
162 
163 
164  // ==================================================================================================================
165  // Reward according to the situation
166  // ==================================================================================================================
167  double TaxiSimulator::reward() { return __reward; }
168 
169  // ==================================================================================================================
170  // Reward according to the situation
171  // ==================================================================================================================
172  void TaxiSimulator::perform(Idx actionId) {
174 
175  __evalReward();
176 
177  Idx curFuelLevel = _currentState.valFromPtr(__fuelLevel);
178  if (curFuelLevel > 0) _currentState.chgVal(__fuelLevel, --curFuelLevel);
179 
180  switch (actionId) {
181  case GoNorth: return __performGoNorth();
182  case GoEast: return __performGoEast();
183  case GoSouth: return __performGoSouth();
184  case GoWest: return __performGoWest();
185  case PickUp: return __performPickUp();
186  case PutDown: return __performPutDown();
187  case FillUp: return __performFillUp();
188  }
189  }
190 
191 
192  // ==================================================================================================================
193  // Transition if you go North
194  // ==================================================================================================================
196  Idx curPos = this->_currentState.valFromPtr(__yPos);
197  if (curPos < 4) _currentState.chgVal(__yPos, ++curPos);
198  }
199 
200 
201  // ==================================================================================================================
202  // Transition if you go east
203  // ==================================================================================================================
205  Idx xCurPos = this->_currentState.valFromPtr(__xPos);
206  Idx yCurPos = this->_currentState.valFromPtr(__yPos);
207 
208  if (xCurPos == 4) return;
209  if (xCurPos == 0 || xCurPos == 2)
210  if (yCurPos == 0 || yCurPos == 1) return;
211  if (xCurPos == 1)
212  if (yCurPos == 3 || yCurPos == 4) return;
213 
214  _currentState.chgVal(__xPos, ++xCurPos);
215  }
216 
217 
218  // ==================================================================================================================
219  // Transition if you go south
220  // ==================================================================================================================
222  Idx curPos = this->_currentState.valFromPtr(__yPos);
223  if (curPos > 0) _currentState.chgVal(__yPos, --curPos);
224  }
225 
226 
227  // ==================================================================================================================
228  // Transition if you go west
229  // ==================================================================================================================
231  Idx xCurPos = this->_currentState.valFromPtr(__xPos);
232  Idx yCurPos = this->_currentState.valFromPtr(__yPos);
233 
234  if (xCurPos == 0) return;
235  if (xCurPos == 1 || xCurPos == 3)
236  if (yCurPos == 0 || yCurPos == 1) return;
237  if (xCurPos == 2)
238  if (yCurPos == 3 || yCurPos == 4) return;
239 
240  _currentState.chgVal(__xPos, --xCurPos);
241  }
242 
243 
244  // ==================================================================================================================
245  // Transition if you go pick up sb
246  // ==================================================================================================================
248  TaxiSimulationLandmarkX xCurPos =
250  TaxiSimulationLandmarkY yCurPos =
252  TaxiSimulationLandmark passPos =
254  switch (passPos) {
255  case HOME: {
256  if (xCurPos == HOMEX && yCurPos == HOMEY)
258  return;
259  }
260  case WORK: {
261  if (xCurPos == WORKX && yCurPos == WORKY)
263  return;
264  }
265  case THEATER: {
266  if (xCurPos == THEATERX && yCurPos == THEATERY)
268  return;
269  }
270  case CLUB: {
271  if (xCurPos == CLUBX && yCurPos == CLUBY)
273  return;
274  }
275  case TAXI: return;
276  }
277  }
278 
279 
280  // ==================================================================================================================
281  // Transition if you go put down sb
282  // ==================================================================================================================
284  TaxiSimulationLandmarkX xCurPos =
286  TaxiSimulationLandmarkY yCurPos =
288  TaxiSimulationLandmark passPos =
290  TaxiSimulationLandmark passDest =
292  if (passPos == TAXI) {
293  switch (passDest) {
294  case HOME: {
295  if (xCurPos == HOMEX && yCurPos == HOMEY)
297  return;
298  }
299  case WORK: {
300  if (xCurPos == WORKX && yCurPos == WORKY)
302  return;
303  }
304  case THEATER: {
305  if (xCurPos == THEATERX && yCurPos == THEATERY)
307  return;
308  }
309  case CLUB: {
310  if (xCurPos == CLUBX && yCurPos == CLUBY)
312  return;
313  }
314  case TAXI: return;
315  }
316  }
317  }
318 
319 
320  // ==================================================================================================================
321  // Transition if you go reffill
322  // ==================================================================================================================
324  TaxiSimulationLandmarkX xCurPos =
326  TaxiSimulationLandmarkY yCurPos =
328 
329  if (xCurPos == STATIONX && yCurPos == STATIONY)
331  }
332 
333 
334  // ==================================================================================================================
335  // Reward according to the situation
336  // ==================================================================================================================
338  TaxiSimulationLandmarkX xCurPos =
340  TaxiSimulationLandmarkY yCurPos =
342  TaxiSimulationLandmark passPos =
344  TaxiSimulationLandmark passDest =
346 
347  if (__lastAction == PutDown) {
348  if (passPos == TAXI) {
349  if (__isAtDestination(passDest, xCurPos, yCurPos))
350  __reward = 30.0;
351  else
352  __reward = 0.0;
353  return;
354  }
355  __reward = 0;
356  return;
357  }
358 
359  if (__lastAction == PickUp) {
360  if (__isAtMeetPoint(passPos, xCurPos, yCurPos))
361  __reward = 20.0;
362  else
363  __reward = 0.0;
364  return;
365  }
366 
368  __reward = 0.0;
369  return;
370  }
371 
372  if (__lastAction == FillUp && (xCurPos != STATIONX || yCurPos != STATIONY)) {
373  __reward = 0.0;
374  return;
375  }
376 
377  __reward = 10.0; //-1.0;
378  }
379 
381  TaxiSimulationLandmarkX xCurPos,
382  TaxiSimulationLandmarkY yCurPos) {
383  switch (passDest) {
384  case HOME: {
385  if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
386  break;
387  }
388  case WORK: {
389  if (xCurPos == WORKX && yCurPos == WORKY) return true;
390  break;
391  }
392  case THEATER: {
393  if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
394  break;
395  }
396  case CLUB: {
397  if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
398  break;
399  }
400  case TAXI: return false;
401  }
402  return false;
403  }
404 
406  TaxiSimulationLandmarkX xCurPos,
407  TaxiSimulationLandmarkY yCurPos) {
408  switch (passPos) {
409  case HOME: {
410  if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
411  break;
412  }
413  case WORK: {
414  if (xCurPos == WORKX && yCurPos == WORKY) return true;
415  break;
416  }
417  case THEATER: {
418  if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
419  break;
420  }
421  case CLUB: {
422  if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
423  break;
424  }
425  case TAXI: return false;
426  }
427  return false;
428  }
429 } // End of namespace gum
void perform(Idx)
Iteration over the variables of the simulated probleme.
Safe iterators for Sequence.
Definition: sequence.h:1206
void setName(const std::string &theValue)
sets the name of the variable
void __performFillUp()
Iteration over the variables of the simulated probleme.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __performGoEast()
Iteration over the variables of the simulated probleme.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
void __performGoNorth()
Iteration over the variables of the simulated probleme.
class LabelizedVariable
<agrum/FMDP/simulation/abstractSimulator.h>
SequenceIteratorSafe< const DiscreteVariable *> beginVariables()
Iteration over the variables of the simulated probleme.
TaxiSimulationAction
Definition: taxiSimulator.h:65
LabelizedVariable * __passengerDest
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
Iteration over the variables of the simulated probleme.
TaxiSimulationLandmarkX
Definition: taxiSimulator.h:51
Safe iterators for bijectionIterator.
Definition: bijection.h:1411
LabelizedVariable * __xPos
SequenceIteratorSafe< const DiscreteVariable *> endVariables()
Iteration over the variables of the simulated probleme.
virtual DiscreteVariable * clone() const =0
Copy Factory.
LabelizedVariable * __yPos
void eraseLabels()
erase all the labels
void __performGoWest()
Iteration over the variables of the simulated probleme.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Base class for discrete random variable.
TaxiSimulationLandmark
Definition: taxiSimulator.h:44
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
TaxiSimulationLandmarkY
Definition: taxiSimulator.h:58
Instantiation _currentState
Tha state in which the system currently is.
bool hasReachEnd()
Choses a random state as the first test for a run.
Bijection< const DiscreteVariable *, const DiscreteVariable *> __primeMap
LabelizedVariable * __fuelLevel
double reward()
Sets the intial statefrom which we begun the simulation.
bool __isAtMeetPoint(TaxiSimulationLandmark passpos, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
HashTable< Idx, std::string *> __actionMap
void __performGoSouth()
Iteration over the variables of the simulated probleme.
virtual Instantiation _randomState()
Choses a random state as the first test for a run.
A class to simulate the Taxi problem.
Definition: taxiSimulator.h:83
Sequence< const DiscreteVariable *> __taxiVars
Variables data structures.
~TaxiSimulator()
Default destructor.
double __reward
Reward.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
LabelizedVariable * __passengerPos
Instantiation _randomState()
Choses a random state as the first test for a run.
void __performPutDown()
Iteration over the variables of the simulated probleme.
Size Idx
Type for indexes.
Definition: types.h:53
void __performPickUp()
Iteration over the variables of the simulated probleme.
Sequence< Idx > __taxiActions
Actions.
TaxiSimulator()
Default constructor.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
bool __isAtDestination(TaxiSimulationLandmark passDest, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
TaxiSimulationAction __lastAction
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408