aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
taxiSimulator.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
50//======================================================================
52
53//======================================================================
54
55namespace gum {
56
58 GUM_CONSTRUCTOR(TaxiSimulator);
59
60 // *****************************************************************************************
61 // Défintion des variables du problème
62
63 // Position TaxiSimulator
64 _xPos_ = new LabelizedVariable("xPos", "Position horizontale du TaxiSimulator");
65 _yPos_ = new LabelizedVariable("yPos", "Position verticale du TaxiSimulator");
66 _xPos_->eraseLabels();
67 _yPos_->eraseLabels();
68 for (Idx pos = 0; pos < 5; pos++) {
69 std::stringstream ss;
70 ss << pos;
71 _xPos_->addLabel(ss.str());
72 _yPos_->addLabel(ss.str());
73 }
74
75 // Position et destination passager
76 _passengerPos_ = new LabelizedVariable("PassengerPos", "Position du Passager", 5);
77 _passengerDest_ = new LabelizedVariable("PassengerDest", "Destination du Passager", 4);
78 _passengerPos_->changeLabel(HOME, "Home");
79 _passengerDest_->changeLabel(HOME, "Home");
80 _passengerPos_->changeLabel(WORK, "Work");
81 _passengerDest_->changeLabel(WORK, "Work");
82 _passengerPos_->changeLabel(THEATER, "Theater");
83 _passengerDest_->changeLabel(THEATER, "Theater");
84 _passengerPos_->changeLabel(CLUB, "Club");
85 _passengerDest_->changeLabel(CLUB, "Club");
86 _passengerPos_->changeLabel(TAXI, "Taxi");
87
88 _fuelLevel_ = new LabelizedVariable("FuelLevel", "Niveau du réservoir", 14);
89
90 // Ajout à séquence
91 _taxiVars_.insert(_xPos_);
92 _taxiVars_.insert(_yPos_);
95 _taxiVars_.insert(_fuelLevel_);
96
97 // Prime version creation
100 varIter != this->TaxiSimulator::endVariables();
101 ++varIter) {
102 DiscreteVariable* primeVar = (*varIter)->clone();
103 primeVar->setName((*varIter)->name() + "'");
104 _primeMap_.insert((*varIter), primeVar);
105 }
106
107 // *****************************************************************************************
108
109 // *****************************************************************************************
110 // Défintion des actions du problème
111 _taxiActions_.insert(GoNorth);
112 _actionMap_.insert(GoNorth, new std::string("Go North"));
113 _taxiActions_.insert(GoEast);
114 _actionMap_.insert(GoEast, new std::string("Go East"));
115 _taxiActions_.insert(GoSouth);
116 _actionMap_.insert(GoSouth, new std::string("Go South"));
117 _taxiActions_.insert(GoWest);
118 _actionMap_.insert(GoWest, new std::string("Go West"));
119 _taxiActions_.insert(PickUp);
120 _actionMap_.insert(PickUp, new std::string("Pick Up"));
121 _taxiActions_.insert(PutDown);
122 _actionMap_.insert(PutDown, new std::string("Put Down"));
123 _taxiActions_.insert(FillUp);
124 _actionMap_.insert(FillUp, new std::string("FillUp"));
125 }
126
128 GUM_DESTRUCTOR(TaxiSimulator);
129
131 = _primeMap_.beginSafe();
132 varIter != _primeMap_.endSafe();
133 ++varIter) {
134 delete varIter.first();
135 delete varIter.second();
136 }
137 }
138
139 // ==================================================================================================================
140 // Reward according to the situation
141 // ==================================================================================================================
144 // Idx curFuelLevel = randy.valFromPtr( _fuelLevel_);
145 // while(curFuelLevel > 12 || curFuelLevel < 5)
146 // curFuelLevel = (Idx)(((double)std::rand( ) / (double)RAND_MAX) *
147 // 7.0) + 5;
148 // randy.chgVal( _fuelLevel_, curFuelLevel);
149
150 // TaxiSimulationLandmark passPos = (TaxiSimulationLandmark)
151 // randy.valFromPtr( _passengerPos_);
152 // TaxiSimulationLandmark passDest = (TaxiSimulationLandmark)
153 // randy.valFromPtr( _passengerDest_);
154 // while( passPos == passDest || passPos == TAXI )
155 // passPos = (TaxiSimulationLandmark) (((double)std::rand( ) /
156 // (double)RAND_MAX) * 3.0);
157 // randy.chgVal( _passengerPos_, passPos);
158
159 return randy;
160 }
161
162 // ==================================================================================================================
163 // Reward according to the situation
164 // ==================================================================================================================
166 // if( currentState_.valFromPtr( _passengerPos_) ==
167 // currentState_.valFromPtr( _passengerDest_) )
168 // return true;
169
170 // if( currentState_.valFromPtr( _fuelLevel_) == 0 )
171 // return true;
172
173 return false;
174 }
175
176 // ==================================================================================================================
177 // Reward according to the situation
178 // ==================================================================================================================
179 double TaxiSimulator::reward() { return _reward_; }
180
181 // ==================================================================================================================
182 // Reward according to the situation
183 // ==================================================================================================================
186
187 _evalReward_();
188
189 Idx curFuelLevel = currentState_.valFromPtr(_fuelLevel_);
190 if (curFuelLevel > 0) currentState_.chgVal(*_fuelLevel_, --curFuelLevel);
191
192 switch (actionId) {
193 case GoNorth : return _performGoNorth_();
194 case GoEast : return _performGoEast_();
195 case GoSouth : return _performGoSouth_();
196 case GoWest : return _performGoWest_();
197 case PickUp : return _performPickUp_();
198 case PutDown : return _performPutDown_();
199 case FillUp : return _performFillUp_();
200 }
201 }
202
203 // ==================================================================================================================
204 // Transition if you go North
205 // ==================================================================================================================
207 Idx curPos = this->currentState_.valFromPtr(_yPos_);
208 if (curPos < 4) currentState_.chgVal(*_yPos_, ++curPos);
209 }
210
211 // ==================================================================================================================
212 // Transition if you go east
213 // ==================================================================================================================
215 Idx xCurPos = this->currentState_.valFromPtr(_xPos_);
216 Idx yCurPos = this->currentState_.valFromPtr(_yPos_);
217
218 if (xCurPos == 4) return;
219 if (xCurPos == 0 || xCurPos == 2)
220 if (yCurPos == 0 || yCurPos == 1) return;
221 if (xCurPos == 1)
222 if (yCurPos == 3 || yCurPos == 4) return;
223
224 currentState_.chgVal(*_xPos_, ++xCurPos);
225 }
226
227 // ==================================================================================================================
228 // Transition if you go south
229 // ==================================================================================================================
231 Idx curPos = this->currentState_.valFromPtr(_yPos_);
232 if (curPos > 0) currentState_.chgVal(*_yPos_, --curPos);
233 }
234
235 // ==================================================================================================================
236 // Transition if you go west
237 // ==================================================================================================================
239 Idx xCurPos = this->currentState_.valFromPtr(_xPos_);
240 Idx yCurPos = this->currentState_.valFromPtr(_yPos_);
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
248 currentState_.chgVal(*_xPos_, --xCurPos);
249 }
250
251 // ==================================================================================================================
252 // Transition if you go pick up sb
253 // ==================================================================================================================
256 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
258 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
261 switch (passPos) {
262 case HOME : {
263 if (xCurPos == HOMEX && yCurPos == HOMEY) currentState_.chgVal(*_passengerPos_, TAXI);
264 return;
265 }
266 case WORK : {
267 if (xCurPos == WORKX && yCurPos == WORKY) currentState_.chgVal(*_passengerPos_, TAXI);
268 return;
269 }
270 case THEATER : {
271 if (xCurPos == THEATERX && yCurPos == THEATERY) currentState_.chgVal(*_passengerPos_, TAXI);
272 return;
273 }
274 case CLUB : {
275 if (xCurPos == CLUBX && yCurPos == CLUBY) currentState_.chgVal(*_passengerPos_, TAXI);
276 return;
277 }
278 case TAXI : return;
279 }
280 }
281
282 // ==================================================================================================================
283 // Transition if you go put down sb
284 // ==================================================================================================================
287 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
289 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
294 if (passPos == TAXI) {
295 switch (passDest) {
296 case HOME : {
297 if (xCurPos == HOMEX && yCurPos == HOMEY) currentState_.chgVal(*_passengerPos_, HOME);
298 return;
299 }
300 case WORK : {
301 if (xCurPos == WORKX && yCurPos == WORKY) currentState_.chgVal(*_passengerPos_, WORK);
302 return;
303 }
304 case THEATER : {
305 if (xCurPos == THEATERX && yCurPos == THEATERY)
307 return;
308 }
309 case CLUB : {
310 if (xCurPos == CLUBX && yCurPos == CLUBY) currentState_.chgVal(*_passengerPos_, CLUB);
311 return;
312 }
313 case TAXI : return;
314 }
315 }
316 }
317
318 // ==================================================================================================================
319 // Transition if you go reffill
320 // ==================================================================================================================
323 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
325 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
326
327 if (xCurPos == STATIONX && yCurPos == STATIONY) currentState_.chgVal(*_fuelLevel_, 13);
328 }
329
330 // ==================================================================================================================
331 // Reward according to the situation
332 // ==================================================================================================================
335 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
337 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
342
343 if (_lastAction_ == PutDown) {
344 if (passPos == TAXI) {
345 if (_isAtDestination_(passDest, xCurPos, yCurPos)) _reward_ = 30.0;
346 else _reward_ = 0.0;
347 return;
348 }
349 _reward_ = 0;
350 return;
351 }
352
353 if (_lastAction_ == PickUp) {
354 if (_isAtMeetPoint_(passPos, xCurPos, yCurPos)) _reward_ = 20.0;
355 else _reward_ = 0.0;
356 return;
357 }
358
359 if (currentState_.valFromPtr(_fuelLevel_) == 0 && _lastAction_ != FillUp) {
360 _reward_ = 0.0;
361 return;
362 }
363
364 if (_lastAction_ == FillUp && (xCurPos != STATIONX || yCurPos != STATIONY)) {
365 _reward_ = 0.0;
366 return;
367 }
368
369 _reward_ = 10.0; //-1.0;
370 }
371
374 TaxiSimulationLandmarkY yCurPos) {
375 switch (passDest) {
376 case HOME : {
377 if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
378 break;
379 }
380 case WORK : {
381 if (xCurPos == WORKX && yCurPos == WORKY) return true;
382 break;
383 }
384 case THEATER : {
385 if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
386 break;
387 }
388 case CLUB : {
389 if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
390 break;
391 }
392 case TAXI : return false;
393 }
394 return false;
395 }
396
399 TaxiSimulationLandmarkY yCurPos) {
400 switch (passPos) {
401 case HOME : {
402 if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
403 break;
404 }
405 case WORK : {
406 if (xCurPos == WORKX && yCurPos == WORKY) return true;
407 break;
408 }
409 case THEATER : {
410 if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
411 break;
412 }
413 case CLUB : {
414 if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
415 break;
416 }
417 case TAXI : return false;
418 }
419 return false;
420 }
421} // End of namespace gum
AbstractSimulator()
Default constructor.
virtual Instantiation randomState_()
Choses a random state as the first test for a run.
Instantiation currentState_
Tha state in which the system currently is.
Safe iterators for bijectionIterator.
Definition bijection.h:1194
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
class LabelizedVariable
Safe iterators for Sequence.
Definition sequence.h:1134
Sequence< Idx > _taxiActions_
Actions.
bool _isAtDestination_(TaxiSimulationLandmark passDest, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
LabelizedVariable * _yPos_
double reward()
Sets the intial statefrom which we begun the simulation.
Bijection< const DiscreteVariable *, const DiscreteVariable * > _primeMap_
void _performFillUp_()
Iteration over the variables of the simulated probleme.
void _performGoEast_()
Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > endVariables()
Iteration over the variables of the simulated probleme.
void _performGoWest_()
Iteration over the variables of the simulated probleme.
LabelizedVariable * _xPos_
TaxiSimulator()
Default constructor.
Sequence< const DiscreteVariable * > _taxiVars_
Variables data structures.
TaxiSimulationAction _lastAction_
LabelizedVariable * _passengerDest_
void _performGoSouth_()
Iteration over the variables of the simulated probleme.
Instantiation randomState_()
Choses a random state as the first test for a run.
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
Iteration over the variables of the simulated probleme.
void _performGoNorth_()
Iteration over the variables of the simulated probleme.
LabelizedVariable * _passengerPos_
void _performPickUp_()
Iteration over the variables of the simulated probleme.
double _reward_
Reward.
LabelizedVariable * _fuelLevel_
HashTable< Idx, std::string * > _actionMap_
bool hasReachEnd()
Choses a random state as the first test for a run.
SequenceIteratorSafe< const DiscreteVariable * > beginVariables()
Iteration over the variables of the simulated probleme.
~TaxiSimulator()
Default destructor.
void _performPutDown_()
Iteration over the variables of the simulated probleme.
bool _isAtMeetPoint_(TaxiSimulationLandmark passpos, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
void perform(Idx)
Iteration over the variables of the simulated probleme.
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
TaxiSimulationLandmarkX
TaxiSimulationLandmark
TaxiSimulationLandmarkY
TaxiSimulationAction
Class for simulating a markov decision process.