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 
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");
50  for (Idx pos = 0; pos < 5; pos++) {
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);
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
78 
79  // Prime version creation
81  varIter != this->endVariables();
82  ++varIter) {
84  primeVar->setName((*varIter)->name() + "'");
86  }
87 
88  // *****************************************************************************************
89 
90  // *****************************************************************************************
91  // Défintion des actions du problème
93  _actionMap_.insert(GoNorth, new std::string("Go North"));
95  _actionMap_.insert(GoEast, new std::string("Go East"));
97  _actionMap_.insert(GoSouth, new std::string("Go South"));
99  _actionMap_.insert(GoWest, new std::string("Go West"));
101  _actionMap_.insert(PickUp, new std::string("Pick Up"));
103  _actionMap_.insert(PutDown, new std::string("Put Down"));
105  _actionMap_.insert(FillUp, new std::string("FillUp"));
106  }
107 
110 
112  = _primeMap_.beginSafe();
114  ++varIter) {
115  delete varIter.first();
116  delete varIter.second();
117  }
118  }
119 
120 
121  // ==================================================================================================================
122  // Reward according to the situation
123  // ==================================================================================================================
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  // ==================================================================================================================
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  // ==================================================================================================================
170 
171  _evalReward_();
172 
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  // ==================================================================================================================
200  if (curPos < 4) currentState_.chgVal(_yPos_, ++curPos);
201  }
202 
203 
204  // ==================================================================================================================
205  // Transition if you go east
206  // ==================================================================================================================
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 
218  }
219 
220 
221  // ==================================================================================================================
222  // Transition if you go south
223  // ==================================================================================================================
226  if (curPos > 0) currentState_.chgVal(_yPos_, --curPos);
227  }
228 
229 
230  // ==================================================================================================================
231  // Transition if you go west
232  // ==================================================================================================================
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 
244  }
245 
246 
247  // ==================================================================================================================
248  // Transition if you go pick up sb
249  // ==================================================================================================================
257  switch (passPos) {
258  case HOME: {
260  return;
261  }
262  case WORK: {
264  return;
265  }
266  case THEATER: {
268  return;
269  }
270  case CLUB: {
272  return;
273  }
274  case TAXI:
275  return;
276  }
277  }
278 
279 
280  // ==================================================================================================================
281  // Transition if you go put down sb
282  // ==================================================================================================================
292  if (passPos == TAXI) {
293  switch (passDest) {
294  case HOME: {
296  return;
297  }
298  case WORK: {
300  return;
301  }
302  case THEATER: {
303  if (xCurPos == THEATERX && yCurPos == THEATERY)
305  return;
306  }
307  case CLUB: {
309  return;
310  }
311  case TAXI:
312  return;
313  }
314  }
315  }
316 
317 
318  // ==================================================================================================================
319  // Transition if you go reffill
320  // ==================================================================================================================
326 
328  }
329 
330 
331  // ==================================================================================================================
332  // Reward according to the situation
333  // ==================================================================================================================
343 
344  if (_lastAction_ == PutDown) {
345  if (passPos == TAXI) {
347  _reward_ = 30.0;
348  else
349  _reward_ = 0.0;
350  return;
351  }
352  _reward_ = 0;
353  return;
354  }
355 
356  if (_lastAction_ == PickUp) {
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 
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 
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
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643