SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SUMOVehicleParameter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Structure representing possible vehicle parameter
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "SUMOVehicleParameter.h"
34 #include <utils/common/ToString.h>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
49  : vtypeid(DEFAULT_VTYPE_ID), color(RGBColor::DEFAULT_COLOR),
50  depart(-1), departProcedure(DEPART_GIVEN),
51  departLane(0), departLaneProcedure(DEPART_LANE_DEFAULT),
52  departPos(0), departPosProcedure(DEPART_POS_DEFAULT),
53  departSpeed(-1), departSpeedProcedure(DEPART_SPEED_DEFAULT),
54  arrivalLane(0), arrivalLaneProcedure(ARRIVAL_LANE_DEFAULT),
55  arrivalPos(0), arrivalPosProcedure(ARRIVAL_POS_DEFAULT),
56  arrivalSpeed(-1), arrivalSpeedProcedure(ARRIVAL_SPEED_DEFAULT),
57  repetitionNumber(-1), repetitionsDone(-1), repetitionOffset(-1), repetitionProbability(-1), repetitionEnd(-1),
58  line(), fromTaz(), toTaz(), personCapacity(0), personNumber(0), containerNumber(0), setParameter(0) {
59 }
60 
61 
62 bool
63 SUMOVehicleParameter::defaultOptionOverrides(const OptionsCont& oc, const std::string& optionName) const {
64  return oc.isSet(optionName) && oc.getBool("defaults-override");
65 }
66 
67 
68 void
73  }
75  dev.writeAttr(SUMO_ATTR_DEPART, "triggered");
77  dev.writeAttr(SUMO_ATTR_DEPART, "containerTriggered");
78  } else {
80  }
81 
82  // optional parameter
83  // departlane
84  if (wasSet(VEHPARS_DEPARTLANE_SET) && !defaultOptionOverrides(oc, "departlane")) {
85  std::string val;
86  switch (departLaneProcedure) {
87  case DEPART_LANE_GIVEN:
88  val = toString(departLane);
89  break;
90  case DEPART_LANE_RANDOM:
91  val = "random";
92  break;
93  case DEPART_LANE_FREE:
94  val = "free";
95  break;
97  val = "allowed";
98  break;
100  val = "best";
101  break;
103  val = "first";
104  break;
105  case DEPART_LANE_DEFAULT:
106  default:
107  break;
108  }
110  } else if (oc.isSet("departlane")) {
111  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
112  }
113  // departpos
114  if (wasSet(VEHPARS_DEPARTPOS_SET) && !defaultOptionOverrides(oc, "departpos")) {
115  std::string val;
116  switch (departPosProcedure) {
117  case DEPART_POS_GIVEN:
118  val = toString(departPos);
119  break;
120  case DEPART_POS_RANDOM:
121  val = "random";
122  break;
124  val = "random_free";
125  break;
126  case DEPART_POS_FREE:
127  val = "free";
128  break;
130  val = "pwagSimple";
131  break;
133  val = "pwagGeneric";
134  break;
136  val = "maxSpeedGap";
137  break;
138  case DEPART_POS_BASE:
139  val = "base";
140  break;
141  case DEPART_POS_DEFAULT:
142  default:
143  break;
144  }
146  } else if (oc.isSet("departpos")) {
147  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTPOS, oc.getString("departpos"));
148  }
149  // departspeed
150  if (wasSet(VEHPARS_DEPARTSPEED_SET) && !defaultOptionOverrides(oc, "departspeed")) {
151  std::string val;
152  switch (departSpeedProcedure) {
153  case DEPART_SPEED_GIVEN:
154  val = toString(departSpeed);
155  break;
156  case DEPART_SPEED_RANDOM:
157  val = "random";
158  break;
159  case DEPART_SPEED_MAX:
160  val = "max";
161  break;
163  default:
164  break;
165  }
167  } else if (oc.isSet("departspeed")) {
168  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
169  }
170 
171  // arrivallane
172  if (wasSet(VEHPARS_ARRIVALLANE_SET) && !defaultOptionOverrides(oc, "arrivallane")) {
173  std::string val;
174  switch (arrivalLaneProcedure) {
175  case ARRIVAL_LANE_GIVEN:
176  val = toString(arrivalLane);
177  break;
179  val = "current";
180  break;
182  default:
183  break;
184  }
186  } else if (oc.isSet("arrivallane")) {
187  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
188  }
189  // arrivalpos
190  if (wasSet(VEHPARS_ARRIVALPOS_SET) && !defaultOptionOverrides(oc, "arrivalpos")) {
191  std::string val;
192  switch (arrivalPosProcedure) {
193  case ARRIVAL_POS_GIVEN:
194  val = toString(arrivalPos);
195  break;
196  case ARRIVAL_POS_RANDOM:
197  val = "random";
198  break;
199  case ARRIVAL_POS_MAX:
200  val = "max";
201  break;
202  case ARRIVAL_POS_DEFAULT:
203  default:
204  break;
205  }
207  } else if (oc.isSet("arrivalpos")) {
208  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
209  }
210  // arrivalspeed
211  if (wasSet(VEHPARS_ARRIVALSPEED_SET) && !defaultOptionOverrides(oc, "arrivalspeed")) {
212  std::string val;
213  switch (arrivalSpeedProcedure) {
214  case ARRIVAL_SPEED_GIVEN:
215  val = toString(arrivalSpeed);
216  break;
218  val = "current";
219  break;
221  default:
222  break;
223  }
225  } else if (oc.isSet("arrivalspeed")) {
226  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
227  }
228 
229  // color
230  if (wasSet(VEHPARS_COLOR_SET)) {
232  }
233  if (wasSet(VEHPARS_LINE_SET)) {
235  }
238  }
239  if (wasSet(VEHPARS_TO_TAZ_SET)) {
241  }
244  }
247  }
250  }
251 }
252 
253 
254 void
256  for (std::vector<Stop>::const_iterator stop = stops.begin(); stop != stops.end(); ++stop) {
257  dev.openTag(SUMO_TAG_STOP);
258  if (stop->busstop != "") {
259  dev.writeAttr(SUMO_ATTR_BUS_STOP, stop->busstop);
260  }
261  if (stop->containerstop != "") {
262  dev.writeAttr(SUMO_ATTR_CONTAINER_STOP, stop->containerstop);
263  }
264  if (stop->busstop == "" && stop->containerstop == "") {
265  dev.writeAttr(SUMO_ATTR_LANE, stop->lane);
266  if ((stop->setParameter & STOP_START_SET) != 0) {
267  dev.writeAttr(SUMO_ATTR_STARTPOS, stop->startPos);
268  }
269  if ((stop->setParameter & STOP_END_SET) != 0) {
270  dev.writeAttr(SUMO_ATTR_ENDPOS, stop->endPos);
271  }
272  }
273  if (stop->duration >= 0) {
274  dev.writeAttr(SUMO_ATTR_DURATION, STEPS2TIME(stop->duration));
275  }
276  if (stop->until >= 0) {
277  dev.writeAttr(SUMO_ATTR_UNTIL, STEPS2TIME(stop->until));
278  }
279  if ((stop->setParameter & STOP_TRIGGER_SET) != 0) {
280  dev.writeAttr(SUMO_ATTR_TRIGGERED, stop->triggered);
281  }
282  if ((stop->setParameter & STOP_CONTAINER_TRIGGER_SET) != 0) {
283  dev.writeAttr(SUMO_ATTR_CONTAINER_TRIGGERED, stop->containerTriggered);
284  }
285  if ((stop->setParameter & STOP_PARKING_SET) != 0) {
286  dev.writeAttr(SUMO_ATTR_PARKING, stop->parking);
287  }
288  // look, we are writing the set of expected persons in its current state...
289  // if this method is used somewhere in the simulation output,
290  // one should consider keeping the original values additionally,
291  // as the ones we write may hev changed.
292  if ((stop->setParameter & STOP_EXPECTED_SET) != 0) {
293  dev.writeAttr(SUMO_ATTR_EXPECTED, stop->awaitedPersons);
294  }
295  if ((stop->setParameter & STOP_EXPECTED_CONTAINERS_SET) != 0) {
296  dev.writeAttr(SUMO_ATTR_EXPECTED_CONTAINERS, stop->awaitedContainers);
297  }
298  dev.closeTag();
299  }
300 }
301 
302 
303 bool
304 SUMOVehicleParameter::parseDepart(const std::string& val, const std::string& element, const std::string& id,
305  SUMOTime& depart, DepartDefinition& dd, std::string& error) {
306  if (val == "triggered") {
307  dd = DEPART_TRIGGERED;
308  } else if (val == "containerTriggered") {
310  } else if (val == "now") {
311  dd = DEPART_NOW;
312  } else {
313  try {
314  depart = string2time(val);
315  dd = DEPART_GIVEN;
316  if (depart < 0) {
317  error = "Negative departure time in the definition of '" + id + "'.";
318  return false;
319  }
320  } catch (...) {
321  error = "Invalid departure time for " + element + " '" + id + "';\n must be one of (\"triggered\", \"containerTriggered\", \"now\", or a float >= 0)";
322  return false;
323  }
324  }
325  return true;
326 }
327 
328 
329 bool
330 SUMOVehicleParameter::parseDepartLane(const std::string& val, const std::string& element, const std::string& id,
331  int& lane, DepartLaneDefinition& dld, std::string& error) {
332  bool ok = true;
333  if (val == "random") {
334  dld = DEPART_LANE_RANDOM;
335  } else if (val == "free") {
336  dld = DEPART_LANE_FREE;
337  } else if (val == "allowed") {
339  } else if (val == "best") {
340  dld = DEPART_LANE_BEST_FREE;
341  } else if (val == "first") {
343  } else {
344  try {
345  lane = TplConvert::_2int(val.c_str());
346  dld = DEPART_LANE_GIVEN;
347  if (lane < 0) {
348  ok = false;
349  }
350  } catch (...) {
351  ok = false;
352  }
353  }
354  if (!ok) {
355  error = "Invalid departLane definition for " + element + " '" + id + "';\n must be one of (\"random\", \"free\", \"allowed\", \"best\", \"first\", or an int>=0)";
356  }
357  return ok;
358 }
359 
360 
361 bool
362 SUMOVehicleParameter::parseDepartPos(const std::string& val, const std::string& element, const std::string& id,
363  SUMOReal& pos, DepartPosDefinition& dpd, std::string& error) {
364  bool ok = true;
365  if (val == "random") {
366  dpd = DEPART_POS_RANDOM;
367  } else if (val == "random_free") {
369  } else if (val == "free") {
370  dpd = DEPART_POS_FREE;
371  } else if (val == "base") {
372  dpd = DEPART_POS_BASE;
373  } else if (val == "pwagSimple") {
375  } else if (val == "pwagGeneric") {
377  } else if (val == "maxSpeedGap") {
379  } else {
380  try {
381  pos = TplConvert::_2SUMOReal(val.c_str());
382  dpd = DEPART_POS_GIVEN;
383  } catch (...) {
384  ok = false;
385  }
386  }
387  if (!ok) {
388  error = "Invalid departPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"random_free\", \"free\", \"base\", \"pwagSimple\", \"pwagGeneric\", \"maxSpeedGap\", or a float)";
389  }
390  return ok;
391 }
392 
393 
394 bool
395 SUMOVehicleParameter::parseDepartSpeed(const std::string& val, const std::string& element, const std::string& id,
396  SUMOReal& speed, DepartSpeedDefinition& dsd, std::string& error) {
397  bool ok = true;
398  if (val == "random") {
399  dsd = DEPART_SPEED_RANDOM;
400  } else if (val == "max") {
401  dsd = DEPART_SPEED_MAX;
402  } else {
403  try {
404  speed = TplConvert::_2SUMOReal(val.c_str());
405  dsd = DEPART_SPEED_GIVEN;
406  if (speed < 0) {
407  ok = false;
408  }
409  } catch (...) {
410  ok = false;
411  }
412  }
413  if (!ok) {
414  error = "Invalid departSpeed definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float>=0)";
415  }
416  return ok;
417 }
418 
419 
420 bool
421 SUMOVehicleParameter::parseArrivalLane(const std::string& val, const std::string& element, const std::string& id,
422  int& lane, ArrivalLaneDefinition& ald, std::string& error) {
423  bool ok = true;
424  if (val == "current") {
425  ald = ARRIVAL_LANE_CURRENT;
426  } else {
427  try {
428  lane = TplConvert::_2int(val.c_str());
429  ald = ARRIVAL_LANE_GIVEN;
430  if (lane < 0) {
431  ok = false;
432  }
433  } catch (...) {
434  ok = false;
435  }
436  }
437  if (!ok) {
438  error = "Invalid arrivalLane definition for " + element + " '" + id + "';\n must be one of (\"current\", or an int>=0)";
439  }
440  return ok;
441 }
442 
443 
444 bool
445 SUMOVehicleParameter::parseArrivalPos(const std::string& val, const std::string& element, const std::string& id,
446  SUMOReal& pos, ArrivalPosDefinition& apd, std::string& error) {
447  bool ok = true;
448  if (val == "random") {
449  apd = ARRIVAL_POS_RANDOM;
450  } else if (val == "max") {
451  apd = ARRIVAL_POS_MAX;
452  } else {
453  try {
454  pos = TplConvert::_2SUMOReal(val.c_str());
455  apd = ARRIVAL_POS_GIVEN;
456  } catch (...) {
457  ok = false;
458  }
459  }
460  if (!ok) {
461  error = "Invalid arrivalPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float)";
462  }
463  return ok;
464 }
465 
466 
467 bool
468 SUMOVehicleParameter::parseArrivalSpeed(const std::string& val, const std::string& element, const std::string& id,
469  SUMOReal& speed, ArrivalSpeedDefinition& asd, std::string& error) {
470  bool ok = true;
471  if (val == "current") {
472  asd = ARRIVAL_SPEED_CURRENT;
473  } else {
474  try {
475  speed = TplConvert::_2SUMOReal(val.c_str());
476  if (speed < 0) {
477  ok = false;
478  }
479  asd = ARRIVAL_SPEED_GIVEN;
480  } catch (...) {
481  ok = false;
482  }
483  }
484  if (!ok) {
485  error = "Invalid arrivalSpeed definition for " + element + " '" + id + "';\n must be one of (\"current\", or a float>=0)";
486  }
487  return ok;
488 }
489 
490 
491 SUMOReal
492 SUMOVehicleParameter::interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string& id) {
493  if (pos < 0) {
494  pos = maximumValue + pos;
495  }
496  if (pos > maximumValue) {
497  WRITE_WARNING("Invalid " + toString(attr) + " " + toString(pos) + " given for " + id + ". Inserting at edge end instead.");
498  pos = maximumValue;
499  }
500  return pos;
501 }
502 
503 
504 /****************************************************************************/
The departure is person triggered.
const int VEHPARS_TO_TAZ_SET
The current lane shall be used.
const int STOP_CONTAINER_TRIGGER_SET
No information given; use default.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
RGBColor color
The vehicle's color.
The time is given.
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
The vehicle is discarded if emission fails (not fully implemented yet)
std::string vtypeid
The vehicle's type id.
static bool parseArrivalPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, ArrivalPosDefinition &apd, std::string &error)
Validates a given arrivalPos value.
ArrivalLaneDefinition
Possible ways to choose the arrival lane.
void writeStops(OutputDevice &dev) const
Writes the enclosed stops.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
The speed is given.
The departure is container triggered.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
SUMOReal departSpeed
(optional) The initial speed of the vehicle
The position is given.
unsigned int personCapacity
The vehicle's capacity (persons)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
SUMOReal arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
The least occupied lane is used.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
const int VEHPARS_ARRIVALLANE_SET
The speed is chosen randomly.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
The lane is chosen randomly.
unsigned int personNumber
The number of persons in the vehicle.
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle's end speed shall be chosen.
The arrival position is given.
const std::string DEFAULT_VTYPE_ID
The least occupied lane from best lanes.
The position is chosen randomly.
Generic max-flow insertion by P.Wagner.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The speed is given.
A gap is chosen where the maximum speed may be achieved.
std::string toTaz
The vehicle's destination zone (district)
The lane is given.
std::vector< Stop > stops
List of the stops the vehicle will make.
const int VEHPARS_ARRIVALSPEED_SET
static SUMOReal interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
SUMOVehicleParameter()
Constructor.
const int STOP_START_SET
bool defaultOptionOverrides(const OptionsCont &oc, const std::string &optionName) const
Returns whether the defaults shall be used.
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle's initial speed shall be chosen.
DepartLaneDefinition
Possible ways to choose a lane on depart.
OutputDevice & writeNonEmptyAttr(const SumoXMLAttr attr, const std::string &val)
writes a string attribute only if it is not the empty string and not the string "default" ...
Definition: OutputDevice.h:290
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
The least occupied lane from lanes which allow the continuation.
bool wasSet(int what) const
Returns whether the given parameter was set.
const int VEHPARS_DEPARTSPEED_SET
The current speed is used.
int arrivalLane
(optional) The lane the vehicle shall arrive on (not used yet)
SUMOTime depart
The vehicle's departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
The maximum speed is used.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
std::string fromTaz
The vehicle's origin zone (district)
No information given; use default.
const int STOP_EXPECTED_SET
static bool parseArrivalLane(const std::string &val, const std::string &element, const std::string &id, int &lane, ArrivalLaneDefinition &ald, std::string &error)
Validates a given arrivalLane value.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
const int VEHPARS_COLOR_SET
If a fixed number of random choices fails, a free position is chosen.
static bool parseDepartPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, DepartPosDefinition &dpd, std::string &error)
Validates a given departPos value.
const int VEHPARS_FROM_TAZ_SET
The rightmost lane the vehicle may use.
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
std::string line
The vehicle's line (mainly for public transport)
DepartSpeedDefinition
Possible ways to choose the departure speed.
const int STOP_END_SET
const int VEHPARS_LINE_SET
static bool parseArrivalSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, ArrivalSpeedDefinition &asd, std::string &error)
Validates a given arrivalSpeed value.
const int VEHPARS_ARRIVALPOS_SET
No information given; use default.
const int STOP_PARKING_SET
static int _2int(const E *const data)
Definition: TplConvert.h:114
const int STOP_TRIGGER_SET
const int STOP_EXPECTED_CONTAINERS_SET
A storage for options typed value containers)
Definition: OptionsCont.h:108
const int VEHPARS_DEPARTLANE_SET
No information given; use default.
The arrival lane is given.
int SUMOTime
Definition: SUMOTime.h:43
SUMOReal departPos
(optional) The position the vehicle shall depart from
void write(OutputDevice &dev, const OptionsCont &oc) const
Writes the parameters as a beginning element.
const int VEHPARS_VTYPE_SET
No information given; use default.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
ArrivalPosDefinition
Possible ways to choose the arrival position.
No information given; use default.
A free position is chosen.
const int VEHPARS_DEPARTPOS_SET
Simple max-flow insertion by P.Wagner.
const int VEHPARS_PERSON_NUMBER_SET
unsigned int containerNumber
The number of containers in the vehicle.
ArrivalSpeedDefinition
Possible ways to choose the arrival speed.
DepartPosDefinition
Possible ways to choose the departure position.
const int VEHPARS_CONTAINER_NUMBER_SET
A color information.
The maximum arrival position is used.
Back-at-zero position.
const int VEHPARS_PERSON_CAPACITY_SET
DepartDefinition
Possible ways to depart.
static bool parseDepart(const std::string &val, const std::string &element, const std::string &id, SUMOTime &depart, DepartDefinition &dd, std::string &error)
Validates a given depart value.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static bool parseDepartLane(const std::string &val, const std::string &element, const std::string &id, int &lane, DepartLaneDefinition &dld, std::string &error)
Validates a given departLane value.
The arrival position is chosen randomly.