aGrUM 3.1.1
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-2026 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-2026 *
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#ifdef GUM_NO_INLINE
55#endif // GUM_NO_INLINE
56
57//======================================================================
58
59namespace gum {
60
62 GUM_CONSTRUCTOR(TaxiSimulator);
63
64 // *****************************************************************************************
65 // Défintion des variables du problème
66
67 // Position TaxiSimulator
68 _xPos_ = new LabelizedVariable("xPos", "Position horizontale du TaxiSimulator");
69 _yPos_ = new LabelizedVariable("yPos", "Position verticale du TaxiSimulator");
70 _xPos_->eraseLabels();
71 _yPos_->eraseLabels();
72 for (Idx pos = 0; pos < 5; pos++) {
73 _xPos_->addLabel(std::to_string(pos));
74 _yPos_->addLabel(std::to_string(pos));
75 }
76
77 // Position et destination passager
78 _passengerPos_ = new LabelizedVariable("PassengerPos", "Position du Passager", 5);
79 _passengerDest_ = new LabelizedVariable("PassengerDest", "Destination du Passager", 4);
80 _passengerPos_->changeLabel(HOME, "Home");
81 _passengerDest_->changeLabel(HOME, "Home");
82 _passengerPos_->changeLabel(WORK, "Work");
83 _passengerDest_->changeLabel(WORK, "Work");
84 _passengerPos_->changeLabel(THEATER, "Theater");
85 _passengerDest_->changeLabel(THEATER, "Theater");
86 _passengerPos_->changeLabel(CLUB, "Club");
87 _passengerDest_->changeLabel(CLUB, "Club");
88 _passengerPos_->changeLabel(TAXI, "Taxi");
89
90 _fuelLevel_ = new LabelizedVariable("FuelLevel", "Niveau du réservoir", 14);
91
92 // Ajout à séquence
93 _taxiVars_.insert(_xPos_);
94 _taxiVars_.insert(_yPos_);
97 _taxiVars_.insert(_fuelLevel_);
98
99 // Prime version creation
102 varIter != this->TaxiSimulator::endVariables();
103 ++varIter) {
104 DiscreteVariable* primeVar = (*varIter)->clone();
105 primeVar->setName((*varIter)->name() + "'");
106 _primeMap_.insert((*varIter), primeVar);
107 }
108
109 // *****************************************************************************************
110
111 // *****************************************************************************************
112 // Défintion des actions du problème
113 _taxiActions_.insert(GoNorth);
114 _actionMap_.insert(GoNorth, new std::string("Go North"));
115 _taxiActions_.insert(GoEast);
116 _actionMap_.insert(GoEast, new std::string("Go East"));
117 _taxiActions_.insert(GoSouth);
118 _actionMap_.insert(GoSouth, new std::string("Go South"));
119 _taxiActions_.insert(GoWest);
120 _actionMap_.insert(GoWest, new std::string("Go West"));
121 _taxiActions_.insert(PickUp);
122 _actionMap_.insert(PickUp, new std::string("Pick Up"));
123 _taxiActions_.insert(PutDown);
124 _actionMap_.insert(PutDown, new std::string("Put Down"));
125 _taxiActions_.insert(FillUp);
126 _actionMap_.insert(FillUp, new std::string("FillUp"));
127 }
128
130 GUM_DESTRUCTOR(TaxiSimulator);
131
133 = _primeMap_.beginSafe();
134 varIter != _primeMap_.endSafe();
135 ++varIter) {
136 delete varIter.first();
137 delete varIter.second();
138 }
139 }
140
141 // ==================================================================================================================
142 // Reward according to the situation
143 // ==================================================================================================================
146 // Idx curFuelLevel = randy.valFromPtr( _fuelLevel_);
147 // while(curFuelLevel > 12 || curFuelLevel < 5)
148 // curFuelLevel = (Idx)(((double)std::rand( ) / (double)RAND_MAX) *
149 // 7.0) + 5;
150 // randy.chgVal( _fuelLevel_, curFuelLevel);
151
152 // TaxiSimulationLandmark passPos = (TaxiSimulationLandmark)
153 // randy.valFromPtr( _passengerPos_);
154 // TaxiSimulationLandmark passDest = (TaxiSimulationLandmark)
155 // randy.valFromPtr( _passengerDest_);
156 // while( passPos == passDest || passPos == TAXI )
157 // passPos = (TaxiSimulationLandmark) (((double)std::rand( ) /
158 // (double)RAND_MAX) * 3.0);
159 // randy.chgVal( _passengerPos_, passPos);
160
161 return randy;
162 }
163
164 // ==================================================================================================================
165 // Reward according to the situation
166 // ==================================================================================================================
168 // if( currentState_.valFromPtr( _passengerPos_) ==
169 // currentState_.valFromPtr( _passengerDest_) )
170 // return true;
171
172 // if( currentState_.valFromPtr( _fuelLevel_) == 0 )
173 // return true;
174
175 return false;
176 }
177
178 // ==================================================================================================================
179 // Reward according to the situation
180 // ==================================================================================================================
181 double TaxiSimulator::reward() { return _reward_; }
182
183 // ==================================================================================================================
184 // Reward according to the situation
185 // ==================================================================================================================
188
189 _evalReward_();
190
191 Idx curFuelLevel = currentState_.valFromPtr(_fuelLevel_);
192 if (curFuelLevel > 0) currentState_.chgVal(*_fuelLevel_, --curFuelLevel);
193
194 switch (actionId) {
195 case GoNorth : return _performGoNorth_();
196 case GoEast : return _performGoEast_();
197 case GoSouth : return _performGoSouth_();
198 case GoWest : return _performGoWest_();
199 case PickUp : return _performPickUp_();
200 case PutDown : return _performPutDown_();
201 case FillUp : return _performFillUp_();
202 }
203 }
204
205 // ==================================================================================================================
206 // Transition if you go North
207 // ==================================================================================================================
209 Idx curPos = this->currentState_.valFromPtr(_yPos_);
210 if (curPos < 4) currentState_.chgVal(*_yPos_, ++curPos);
211 }
212
213 // ==================================================================================================================
214 // Transition if you go east
215 // ==================================================================================================================
217 Idx xCurPos = this->currentState_.valFromPtr(_xPos_);
218 Idx yCurPos = this->currentState_.valFromPtr(_yPos_);
219
220 if (xCurPos == 4) return;
221 if (xCurPos == 0 || xCurPos == 2)
222 if (yCurPos == 0 || yCurPos == 1) return;
223 if (xCurPos == 1)
224 if (yCurPos == 3 || yCurPos == 4) return;
225
226 currentState_.chgVal(*_xPos_, ++xCurPos);
227 }
228
229 // ==================================================================================================================
230 // Transition if you go south
231 // ==================================================================================================================
233 Idx curPos = this->currentState_.valFromPtr(_yPos_);
234 if (curPos > 0) currentState_.chgVal(*_yPos_, --curPos);
235 }
236
237 // ==================================================================================================================
238 // Transition if you go west
239 // ==================================================================================================================
241 Idx xCurPos = this->currentState_.valFromPtr(_xPos_);
242 Idx yCurPos = this->currentState_.valFromPtr(_yPos_);
243
244 if (xCurPos == 0) return;
245 if (xCurPos == 1 || xCurPos == 3)
246 if (yCurPos == 0 || yCurPos == 1) return;
247 if (xCurPos == 2)
248 if (yCurPos == 3 || yCurPos == 4) return;
249
250 currentState_.chgVal(*_xPos_, --xCurPos);
251 }
252
253 // ==================================================================================================================
254 // Transition if you go pick up sb
255 // ==================================================================================================================
258 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
260 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
263 switch (passPos) {
264 case HOME : {
265 if (xCurPos == HOMEX && yCurPos == HOMEY) currentState_.chgVal(*_passengerPos_, TAXI);
266 return;
267 }
268 case WORK : {
269 if (xCurPos == WORKX && yCurPos == WORKY) currentState_.chgVal(*_passengerPos_, TAXI);
270 return;
271 }
272 case THEATER : {
273 if (xCurPos == THEATERX && yCurPos == THEATERY) currentState_.chgVal(*_passengerPos_, TAXI);
274 return;
275 }
276 case CLUB : {
277 if (xCurPos == CLUBX && yCurPos == CLUBY) currentState_.chgVal(*_passengerPos_, TAXI);
278 return;
279 }
280 case TAXI : return;
281 }
282 }
283
284 // ==================================================================================================================
285 // Transition if you go put down sb
286 // ==================================================================================================================
289 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
291 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
296 if (passPos == TAXI) {
297 switch (passDest) {
298 case HOME : {
299 if (xCurPos == HOMEX && yCurPos == HOMEY) currentState_.chgVal(*_passengerPos_, HOME);
300 return;
301 }
302 case WORK : {
303 if (xCurPos == WORKX && yCurPos == WORKY) currentState_.chgVal(*_passengerPos_, WORK);
304 return;
305 }
306 case THEATER : {
307 if (xCurPos == THEATERX && yCurPos == THEATERY)
309 return;
310 }
311 case CLUB : {
312 if (xCurPos == CLUBX && yCurPos == CLUBY) currentState_.chgVal(*_passengerPos_, CLUB);
313 return;
314 }
315 case TAXI : return;
316 }
317 }
318 }
319
320 // ==================================================================================================================
321 // Transition if you go reffill
322 // ==================================================================================================================
325 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
327 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
328
329 if (xCurPos == STATIONX && yCurPos == STATIONY) currentState_.chgVal(*_fuelLevel_, 13);
330 }
331
332 // ==================================================================================================================
333 // Reward according to the situation
334 // ==================================================================================================================
337 = (TaxiSimulationLandmarkX)this->currentState_.valFromPtr(_xPos_);
339 = (TaxiSimulationLandmarkY)this->currentState_.valFromPtr(_yPos_);
344
345 if (_lastAction_ == PutDown) {
346 if (passPos == TAXI) {
347 if (_isAtDestination_(passDest, xCurPos, yCurPos)) _reward_ = 30.0;
348 else _reward_ = 0.0;
349 return;
350 }
351 _reward_ = 0;
352 return;
353 }
354
355 if (_lastAction_ == PickUp) {
356 if (_isAtMeetPoint_(passPos, xCurPos, yCurPos)) _reward_ = 20.0;
357 else _reward_ = 0.0;
358 return;
359 }
360
361 if (currentState_.valFromPtr(_fuelLevel_) == 0 && _lastAction_ != FillUp) {
362 _reward_ = 0.0;
363 return;
364 }
365
366 if (_lastAction_ == FillUp && (xCurPos != STATIONX || yCurPos != STATIONY)) {
367 _reward_ = 0.0;
368 return;
369 }
370
371 _reward_ = 10.0; //-1.0;
372 }
373
376 TaxiSimulationLandmarkY yCurPos) {
377 switch (passDest) {
378 case HOME : {
379 if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
380 break;
381 }
382 case WORK : {
383 if (xCurPos == WORKX && yCurPos == WORKY) return true;
384 break;
385 }
386 case THEATER : {
387 if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
388 break;
389 }
390 case CLUB : {
391 if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
392 break;
393 }
394 case TAXI : return false;
395 }
396 return false;
397 }
398
401 TaxiSimulationLandmarkY yCurPos) {
402 switch (passPos) {
403 case HOME : {
404 if (xCurPos == HOMEX && yCurPos == HOMEY) return true;
405 break;
406 }
407 case WORK : {
408 if (xCurPos == WORKX && yCurPos == WORKY) return true;
409 break;
410 }
411 case THEATER : {
412 if (xCurPos == THEATERX && yCurPos == THEATERY) return true;
413 break;
414 }
415 case CLUB : {
416 if (xCurPos == CLUBX && yCurPos == CLUBY) return true;
417 break;
418 }
419 case TAXI : return false;
420 }
421 return false;
422 }
423} // 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:1240
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:1148
Sequence< Idx > _taxiActions_
Actions.
bool _isAtDestination_(TaxiSimulationLandmark passDest, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
LabelizedVariable * _yPos_
Bijection< const DiscreteVariable *, const DiscreteVariable * > _primeMap_
LabelizedVariable * _xPos_
TaxiSimulator()
Default constructor.
double reward() override
Sets the intial statefrom which we begun the simulation.
void perform(Idx) override
Sets the intial statefrom which we begun the simulation.
bool hasReachEnd() override
Choses a random state as the first test for a run.
Sequence< const DiscreteVariable * > _taxiVars_
Variables data structures.
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar) override
Iteration over the variables of the simulated probleme.
TaxiSimulationAction _lastAction_
LabelizedVariable * _passengerDest_
LabelizedVariable * _passengerPos_
~TaxiSimulator() override
Default destructor.
SequenceIteratorSafe< const DiscreteVariable * > beginVariables() override
Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > endVariables() override
Iteration over the variables of the simulated probleme.
double _reward_
Reward.
LabelizedVariable * _fuelLevel_
HashTable< Idx, std::string * > _actionMap_
Instantiation randomState_() override
Choses a random state as the first test for a run.
bool _isAtMeetPoint_(TaxiSimulationLandmark passpos, TaxiSimulationLandmarkX xCurPos, TaxiSimulationLandmarkY yCurPos)
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.