aGrUM  0.14.2
taxiSimulator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // =====================================================================
28 #include <cstdlib>
29 #include <random>
30 //======================================================================
32 //======================================================================
33 
34 namespace gum {
35 
37  GUM_CONSTRUCTOR(TaxiSimulator);
38 
39  // *****************************************************************************************
40  // Défintion des variables du problème
41 
42  // Position TaxiSimulator
43  __xPos =
44  new LabelizedVariable("xPos", "Position horizontale du TaxiSimulator");
45  __yPos = new LabelizedVariable("yPos", "Position verticale du TaxiSimulator");
48  for (Idx pos = 0; pos < 5; pos++) {
49  std::stringstream ss;
50  ss << pos;
51  __xPos->addLabel(ss.str());
52  __yPos->addLabel(ss.str());
53  }
54 
55  // Position et destination passager
57  new LabelizedVariable("PassengerPos", "Position du Passager", 5);
59  new LabelizedVariable("PassengerDest", "Destination du Passager", 4);
64  __passengerPos->changeLabel(THEATER, "Theater");
65  __passengerDest->changeLabel(THEATER, "Theater");
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);
77  __taxiVars.insert(__fuelLevel);
78 
79  // Prime version creation
81  this->beginVariables();
82  varIter != this->endVariables();
83  ++varIter) {
84  DiscreteVariable* primeVar = (*varIter)->clone();
85  primeVar->setName((*varIter)->name() + "'");
86  __primeMap.insert((*varIter), primeVar);
87  }
88 
89  // *****************************************************************************************
90 
91  // *****************************************************************************************
92  // Défintion des actions du problème
94  __actionMap.insert(GoNorth, new std::string("Go North"));
96  __actionMap.insert(GoEast, new std::string("Go East"));
98  __actionMap.insert(GoSouth, new std::string("Go South"));
100  __actionMap.insert(GoWest, new std::string("Go West"));
102  __actionMap.insert(PickUp, new std::string("Pick Up"));
104  __actionMap.insert(PutDown, new std::string("Put Down"));
106  __actionMap.insert(FillUp, new std::string("FillUp"));
107  }
108 
110  GUM_DESTRUCTOR(TaxiSimulator);
111 
113  varIter = __primeMap.beginSafe();
114  varIter != __primeMap.endSafe();
115  ++varIter) {
116  delete varIter.first();
117  delete varIter.second();
118  }
119  }
120 
121 
122  // ==================================================================================================================
123  // Reward according to the situation
124  // ==================================================================================================================
127  // Idx curFuelLevel = randy.valFromPtr(__fuelLevel);
128  // while(curFuelLevel > 12 || curFuelLevel < 5)
129  // curFuelLevel = (Idx)(((double)std::rand( ) / (double)RAND_MAX) *
130  // 7.0) + 5;
131  // randy.chgVal(__fuelLevel, curFuelLevel);
132 
133  // TaxiSimulationLandmark passPos = (TaxiSimulationLandmark)
134  // randy.valFromPtr(__passengerPos);
135  // TaxiSimulationLandmark passDest = (TaxiSimulationLandmark)
136  // randy.valFromPtr(__passengerDest);
137  // while( passPos == passDest || passPos == TAXI )
138  // passPos = (TaxiSimulationLandmark) (((double)std::rand( ) /
139  // (double)RAND_MAX) * 3.0);
140  // randy.chgVal(__passengerPos, passPos);
141 
142  return randy;
143  }
144 
145 
146  // ==================================================================================================================
147  // Reward according to the situation
148  // ==================================================================================================================
150  // if( _currentState.valFromPtr(__passengerPos) ==
151  // _currentState.valFromPtr(__passengerDest) )
152  // return true;
153 
154  // if( _currentState.valFromPtr(__fuelLevel) == 0 )
155  // return true;
156 
157  return false;
158  }
159 
160 
161  // ==================================================================================================================
162  // Reward according to the situation
163  // ==================================================================================================================
164  double TaxiSimulator::reward() { return __reward; }
165 
166  // ==================================================================================================================
167  // Reward according to the situation
168  // ==================================================================================================================
169  void TaxiSimulator::perform(Idx actionId) {
171 
172  __evalReward();
173 
174  Idx curFuelLevel = _currentState.valFromPtr(__fuelLevel);
175  if (curFuelLevel > 0) _currentState.chgVal(__fuelLevel, --curFuelLevel);
176 
177  switch (actionId) {
178  case GoNorth: return __performGoNorth();
179  case GoEast: return __performGoEast();
180  case GoSouth: return __performGoSouth();
181  case GoWest: return __performGoWest();
182  case PickUp: return __performPickUp();
183  case PutDown: return __performPutDown();
184  case FillUp: return __performFillUp();
185  }
186  }
187 
188 
189  // ==================================================================================================================
190  // Transition if you go North
191  // ==================================================================================================================
193  Idx curPos = this->_currentState.valFromPtr(__yPos);
194  if (curPos < 4) _currentState.chgVal(__yPos, ++curPos);
195  }
196 
197 
198  // ==================================================================================================================
199  // Transition if you go east
200  // ==================================================================================================================
202  Idx xCurPos = this->_currentState.valFromPtr(__xPos);
203  Idx yCurPos = this->_currentState.valFromPtr(__yPos);
204 
205  if (xCurPos == 4) return;
206  if (xCurPos == 0 || xCurPos == 2)
207  if (yCurPos == 0 || yCurPos == 1) return;
208  if (xCurPos == 1)
209  if (yCurPos == 3 || yCurPos == 4) return;
210 
211  _currentState.chgVal(__xPos, ++xCurPos);
212  }
213 
214 
215  // ==================================================================================================================
216  // Transition if you go south
217  // ==================================================================================================================
219  Idx curPos = this->_currentState.valFromPtr(__yPos);
220  if (curPos > 0) _currentState.chgVal(__yPos, --curPos);
221  }
222 
223 
224  // ==================================================================================================================
225  // Transition if you go west
226  // ==================================================================================================================
228  Idx xCurPos = this->_currentState.valFromPtr(__xPos);
229  Idx yCurPos = this->_currentState.valFromPtr(__yPos);
230 
231  if (xCurPos == 0) return;
232  if (xCurPos == 1 || xCurPos == 3)
233  if (yCurPos == 0 || yCurPos == 1) return;
234  if (xCurPos == 2)
235  if (yCurPos == 3 || yCurPos == 4) return;
236 
237  _currentState.chgVal(__xPos, --xCurPos);
238  }
239 
240 
241  // ==================================================================================================================
242  // Transition if you go pick up sb
243  // ==================================================================================================================
245  TaxiSimulationLandmarkX xCurPos =
247  TaxiSimulationLandmarkY yCurPos =
249  TaxiSimulationLandmark passPos =
251  switch (passPos) {
252  case HOME: {
253  if (xCurPos == HOMEX && yCurPos == HOMEY)
255  return;
256  }
257  case WORK: {
258  if (xCurPos == WORKX && yCurPos == WORKY)
260  return;
261  }
262  case THEATER: {
263  if (xCurPos == THEATERX && yCurPos == THEATERY)
265  return;
266  }
267  case CLUB: {
268  if (xCurPos == CLUBX && yCurPos == CLUBY)
270  return;
271  }
272  case TAXI: return;
273  }
274  }
275 
276 
277  // ==================================================================================================================
278  // Transition if you go put down sb
279  // ==================================================================================================================
281  TaxiSimulationLandmarkX xCurPos =
283  TaxiSimulationLandmarkY yCurPos =
285  TaxiSimulationLandmark passPos =
287  TaxiSimulationLandmark passDest =
289  if (passPos == TAXI) {
290  switch (passDest) {
291  case HOME: {
292  if (xCurPos == HOMEX && yCurPos == HOMEY)
294  return;
295  }
296  case WORK: {
297  if (xCurPos == WORKX && yCurPos == WORKY)
299  return;
300  }
301  case THEATER: {
302  if (xCurPos == THEATERX && yCurPos == THEATERY)
304  return;
305  }
306  case CLUB: {
307  if (xCurPos == CLUBX && yCurPos == CLUBY)
309  return;
310  }
311  case TAXI: return;
312  }
313  }
314  }
315 
316 
317  // ==================================================================================================================
318  // Transition if you go reffill
319  // ==================================================================================================================
321  TaxiSimulationLandmarkX xCurPos =
323  TaxiSimulationLandmarkY yCurPos =
325 
326  if (xCurPos == STATIONX && yCurPos == STATIONY)
328  }
329 
330 
331  // ==================================================================================================================
332  // Reward according to the situation
333  // ==================================================================================================================
335  TaxiSimulationLandmarkX xCurPos =
337  TaxiSimulationLandmarkY yCurPos =
339  TaxiSimulationLandmark passPos =
341  TaxiSimulationLandmark passDest =
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 
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 
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: return false;
398  }
399  return false;
400  }
401 
403  TaxiSimulationLandmarkX xCurPos,
404  TaxiSimulationLandmarkY yCurPos) {
405  switch (passPos) {
406  case HOME: {
407  if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
408  break;
409  }
410  case WORK: {
411  if (xCurPos == WORKX && yCurPos == WORKY) return true;
412  break;
413  }
414  case THEATER: {
415  if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
416  break;
417  }
418  case CLUB: {
419  if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
420  break;
421  }
422  case TAXI: return false;
423  }
424  return false;
425  }
426 } // End of namespace gum
void perform(Idx)
Iteration over the variables of the simulated probleme.
Safe iterators for Sequence.
Definition: sequence.h:1203
void setName(const std::string &theValue)
sets the name of the variable
void __performFillUp()
Iteration over the variables of the simulated probleme.
Class for simulating a markov decision process.
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:62
LabelizedVariable * __passengerDest
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
Iteration over the variables of the simulated probleme.
TaxiSimulationLandmarkX
Definition: taxiSimulator.h:48
Safe iterators for bijectionIterator.
Definition: bijection.h:1409
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:41
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
TaxiSimulationLandmarkY
Definition: taxiSimulator.h:55
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:80
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:80
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:50
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:405