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 
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++) {
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
81 
82  // Prime version creation
84  = this->beginVariables();
85  varIter != this->endVariables();
86  ++varIter) {
88  primeVar->setName((*varIter)->name() + "'");
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 
114 
116  varIter
117  = primeMap__.beginSafe();
119  ++varIter) {
120  delete varIter.first();
121  delete varIter.second();
122  }
123  }
124 
125 
126  // ==================================================================================================================
127  // Reward according to the situation
128  // ==================================================================================================================
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  // ==================================================================================================================
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  // ==================================================================================================================
175 
176  evalReward__();
177 
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  // ==================================================================================================================
205  if (curPos < 4) currentState_.chgVal(yPos__, ++curPos);
206  }
207 
208 
209  // ==================================================================================================================
210  // Transition if you go east
211  // ==================================================================================================================
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 
223  }
224 
225 
226  // ==================================================================================================================
227  // Transition if you go south
228  // ==================================================================================================================
231  if (curPos > 0) currentState_.chgVal(yPos__, --curPos);
232  }
233 
234 
235  // ==================================================================================================================
236  // Transition if you go west
237  // ==================================================================================================================
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 
249  }
250 
251 
252  // ==================================================================================================================
253  // Transition if you go pick up sb
254  // ==================================================================================================================
262  switch (passPos) {
263  case HOME: {
264  if (xCurPos == HOMEX && yCurPos == HOMEY)
266  return;
267  }
268  case WORK: {
269  if (xCurPos == WORKX && yCurPos == WORKY)
271  return;
272  }
273  case THEATER: {
274  if (xCurPos == THEATERX && yCurPos == THEATERY)
276  return;
277  }
278  case CLUB: {
279  if (xCurPos == CLUBX && yCurPos == CLUBY)
281  return;
282  }
283  case TAXI:
284  return;
285  }
286  }
287 
288 
289  // ==================================================================================================================
290  // Transition if you go put down sb
291  // ==================================================================================================================
301  if (passPos == TAXI) {
302  switch (passDest) {
303  case HOME: {
304  if (xCurPos == HOMEX && yCurPos == HOMEY)
306  return;
307  }
308  case WORK: {
309  if (xCurPos == WORKX && yCurPos == WORKY)
311  return;
312  }
313  case THEATER: {
314  if (xCurPos == THEATERX && yCurPos == THEATERY)
316  return;
317  }
318  case CLUB: {
319  if (xCurPos == CLUBX && yCurPos == CLUBY)
321  return;
322  }
323  case TAXI:
324  return;
325  }
326  }
327  }
328 
329 
330  // ==================================================================================================================
331  // Transition if you go reffill
332  // ==================================================================================================================
338 
339  if (xCurPos == STATIONX && yCurPos == STATIONY)
341  }
342 
343 
344  // ==================================================================================================================
345  // Reward according to the situation
346  // ==================================================================================================================
356 
357  if (lastAction__ == PutDown) {
358  if (passPos == TAXI) {
360  reward__ = 30.0;
361  else
362  reward__ = 0.0;
363  return;
364  }
365  reward__ = 0;
366  return;
367  }
368 
369  if (lastAction__ == PickUp) {
371  reward__ = 20.0;
372  else
373  reward__ = 0.0;
374  return;
375  }
376 
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 
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 
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
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669