SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
12 // Copyright (C) 2001-2014 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),
58  line(), fromTaz(), toTaz(), personCapacity(0), personNumber(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");
76  } else {
78  }
79 
80  // optional parameter
81  // departlane
82  if (wasSet(VEHPARS_DEPARTLANE_SET) && !defaultOptionOverrides(oc, "departlane")) {
83  std::string val;
84  switch (departLaneProcedure) {
85  case DEPART_LANE_GIVEN:
86  val = toString(departLane);
87  break;
88  case DEPART_LANE_RANDOM:
89  val = "random";
90  break;
91  case DEPART_LANE_FREE:
92  val = "free";
93  break;
95  val = "allowed";
96  break;
98  val = "best";
99  break;
100  case DEPART_LANE_DEFAULT:
101  default:
102  break;
103  }
105  } else if (oc.isSet("departlane")) {
106  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
107  }
108  // departpos
109  if (wasSet(VEHPARS_DEPARTPOS_SET) && !defaultOptionOverrides(oc, "departpos")) {
110  std::string val;
111  switch (departPosProcedure) {
112  case DEPART_POS_GIVEN:
113  val = toString(departPos);
114  break;
115  case DEPART_POS_RANDOM:
116  val = "random";
117  break;
119  val = "random_free";
120  break;
121  case DEPART_POS_FREE:
122  val = "free";
123  break;
125  val = "pwagSimple";
126  break;
128  val = "pwagGeneric";
129  break;
131  val = "maxSpeedGap";
132  break;
133  case DEPART_POS_BASE:
134  val = "base";
135  break;
136  case DEPART_POS_DEFAULT:
137  default:
138  break;
139  }
141  } else if (oc.isSet("departpos")) {
142  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTPOS, oc.getString("departpos"));
143  }
144  // departspeed
145  if (wasSet(VEHPARS_DEPARTSPEED_SET) && !defaultOptionOverrides(oc, "departspeed")) {
146  std::string val;
147  switch (departSpeedProcedure) {
148  case DEPART_SPEED_GIVEN:
149  val = toString(departSpeed);
150  break;
151  case DEPART_SPEED_RANDOM:
152  val = "random";
153  break;
154  case DEPART_SPEED_MAX:
155  val = "max";
156  break;
158  default:
159  break;
160  }
162  } else if (oc.isSet("departspeed")) {
163  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
164  }
165 
166  // arrivallane
167  if (wasSet(VEHPARS_ARRIVALLANE_SET) && !defaultOptionOverrides(oc, "arrivallane")) {
168  std::string val;
169  switch (arrivalLaneProcedure) {
170  case ARRIVAL_LANE_GIVEN:
171  val = toString(arrivalLane);
172  break;
174  val = "current";
175  break;
177  default:
178  break;
179  }
181  } else if (oc.isSet("arrivallane")) {
182  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
183  }
184  // arrivalpos
185  if (wasSet(VEHPARS_ARRIVALPOS_SET) && !defaultOptionOverrides(oc, "arrivalpos")) {
186  std::string val;
187  switch (arrivalPosProcedure) {
188  case ARRIVAL_POS_GIVEN:
189  val = toString(arrivalPos);
190  break;
191  case ARRIVAL_POS_RANDOM:
192  val = "random";
193  break;
194  case ARRIVAL_POS_MAX:
195  val = "max";
196  break;
197  case ARRIVAL_POS_DEFAULT:
198  default:
199  break;
200  }
202  } else if (oc.isSet("arrivalpos")) {
203  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
204  }
205  // arrivalspeed
206  if (wasSet(VEHPARS_ARRIVALSPEED_SET) && !defaultOptionOverrides(oc, "arrivalspeed")) {
207  std::string val;
208  switch (arrivalSpeedProcedure) {
209  case ARRIVAL_SPEED_GIVEN:
210  val = toString(arrivalSpeed);
211  break;
213  val = "current";
214  break;
216  default:
217  break;
218  }
220  } else if (oc.isSet("arrivalspeed")) {
221  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
222  }
223 
224  // color
225  if (wasSet(VEHPARS_COLOR_SET)) {
227  }
228  if (wasSet(VEHPARS_LINE_SET)) {
230  }
231  if (wasSet(VEHPARS_TAZ_SET)) {
233  }
236  }
239  }
240 }
241 
242 
243 void
245  for (std::vector<Stop>::const_iterator stop = stops.begin(); stop != stops.end(); ++stop) {
246  dev.openTag(SUMO_TAG_STOP);
247  if (stop->busstop != "") {
248  dev.writeAttr(SUMO_ATTR_BUS_STOP, stop->busstop);
249  } else {
250  dev.writeAttr(SUMO_ATTR_LANE, stop->lane);
251  if ((stop->setParameter & STOP_START_SET) != 0) {
252  dev.writeAttr(SUMO_ATTR_STARTPOS, stop->startPos);
253  }
254  if ((stop->setParameter & STOP_END_SET) != 0) {
255  dev.writeAttr(SUMO_ATTR_ENDPOS, stop->endPos);
256  }
257  }
258  if (stop->duration >= 0) {
259  dev.writeAttr(SUMO_ATTR_DURATION, STEPS2TIME(stop->duration));
260  }
261  if (stop->until >= 0) {
262  dev.writeAttr(SUMO_ATTR_UNTIL, STEPS2TIME(stop->until));
263  }
264  if ((stop->setParameter & STOP_TRIGGER_SET) != 0) {
265  dev.writeAttr(SUMO_ATTR_TRIGGERED, stop->triggered);
266  }
267  if ((stop->setParameter & STOP_PARKING_SET) != 0) {
268  dev.writeAttr(SUMO_ATTR_PARKING, stop->parking);
269  }
270  // look, we are writing the set of expected persons in its current state...
271  // if this method is used somewhere in the simulation output,
272  // one should consider keeping the original values additionally,
273  // as the ones we write may hev changed.
274  if ((stop->setParameter & STOP_EXPECTED_SET) != 0) {
275  dev.writeAttr(SUMO_ATTR_EXPECTED, stop->awaitedPersons);
276  }
277  dev.closeTag();
278  }
279 }
280 
281 
282 bool
283 SUMOVehicleParameter::parseDepart(const std::string& val, const std::string& element, const std::string& id,
284  SUMOTime& depart, DepartDefinition& dd, std::string& error) {
285  if (val == "triggered") {
286  dd = DEPART_TRIGGERED;
287  } else if (val == "now") {
288  dd = DEPART_NOW;
289  } else {
290  try {
291  depart = string2time(val);
292  dd = DEPART_GIVEN;
293  if (depart < 0) {
294  error = "Negative departure time in the definition of '" + id + "'.";
295  return false;
296  }
297  } catch (...) {
298  error = "Invalid departure time for " + element + " '" + id + "';\n must be one of (\"triggered\", \"now\", or a float >= 0)";
299  return false;
300  }
301  }
302  return true;
303 }
304 
305 
306 bool
307 SUMOVehicleParameter::parseDepartLane(const std::string& val, const std::string& element, const std::string& id,
308  int& lane, DepartLaneDefinition& dld, std::string& error) {
309  bool ok = true;
310  if (val == "random") {
311  dld = DEPART_LANE_RANDOM;
312  } else if (val == "free") {
313  dld = DEPART_LANE_FREE;
314  } else if (val == "allowed") {
316  } else if (val == "best") {
317  dld = DEPART_LANE_BEST_FREE;
318  } else {
319  try {
320  lane = TplConvert::_2int(val.c_str());
321  dld = DEPART_LANE_GIVEN;
322  if (lane < 0) {
323  ok = false;
324  }
325  } catch (...) {
326  ok = false;
327  }
328  }
329  if (!ok) {
330  error = "Invalid departLane definition for " + element + " '" + id + "';\n must be one of (\"random\", \"free\", \"allowed\", \"best\", or an int>=0)";
331  }
332  return ok;
333 }
334 
335 
336 bool
337 SUMOVehicleParameter::parseDepartPos(const std::string& val, const std::string& element, const std::string& id,
338  SUMOReal& pos, DepartPosDefinition& dpd, std::string& error) {
339  bool ok = true;
340  if (val == "random") {
341  dpd = DEPART_POS_RANDOM;
342  } else if (val == "random_free") {
344  } else if (val == "free") {
345  dpd = DEPART_POS_FREE;
346  } else if (val == "base") {
347  dpd = DEPART_POS_BASE;
348  } else if (val == "pwagSimple") {
350  } else if (val == "pwagGeneric") {
352  } else if (val == "maxSpeedGap") {
354  } else {
355  try {
356  pos = TplConvert::_2SUMOReal(val.c_str());
357  dpd = DEPART_POS_GIVEN;
358  } catch (...) {
359  ok = false;
360  }
361  }
362  if (!ok) {
363  error = "Invalid departPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"random_free\", \"free\", \"base\", \"pwagSimple\", \"pwagGeneric\", \"maxSpeedGap\", or a float)";
364  }
365  return ok;
366 }
367 
368 
369 bool
370 SUMOVehicleParameter::parseDepartSpeed(const std::string& val, const std::string& element, const std::string& id,
371  SUMOReal& speed, DepartSpeedDefinition& dsd, std::string& error) {
372  bool ok = true;
373  if (val == "random") {
374  dsd = DEPART_SPEED_RANDOM;
375  } else if (val == "max") {
376  dsd = DEPART_SPEED_MAX;
377  } else {
378  try {
379  speed = TplConvert::_2SUMOReal(val.c_str());
380  dsd = DEPART_SPEED_GIVEN;
381  if (speed < 0) {
382  ok = false;
383  }
384  } catch (...) {
385  ok = false;
386  }
387  }
388  if (!ok) {
389  error = "Invalid departSpeed definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float>=0)";
390  }
391  return ok;
392 }
393 
394 
395 bool
396 SUMOVehicleParameter::parseArrivalLane(const std::string& val, const std::string& element, const std::string& id,
397  int& lane, ArrivalLaneDefinition& ald, std::string& error) {
398  bool ok = true;
399  if (val == "current") {
400  ald = ARRIVAL_LANE_CURRENT;
401  } else {
402  try {
403  lane = TplConvert::_2int(val.c_str());
404  ald = ARRIVAL_LANE_GIVEN;
405  if (lane < 0) {
406  ok = false;
407  }
408  } catch (...) {
409  ok = false;
410  }
411  }
412  if (!ok) {
413  error = "Invalid arrivalLane definition for " + element + " '" + id + "';\n must be one of (\"current\", or an int>=0)";
414  }
415  return ok;
416 }
417 
418 
419 bool
420 SUMOVehicleParameter::parseArrivalPos(const std::string& val, const std::string& element, const std::string& id,
421  SUMOReal& pos, ArrivalPosDefinition& apd, std::string& error) {
422  bool ok = true;
423  if (val == "random") {
424  apd = ARRIVAL_POS_RANDOM;
425  } else if (val == "max") {
426  apd = ARRIVAL_POS_MAX;
427  } else {
428  try {
429  pos = TplConvert::_2SUMOReal(val.c_str());
430  apd = ARRIVAL_POS_GIVEN;
431  } catch (...) {
432  ok = false;
433  }
434  }
435  if (!ok) {
436  error = "Invalid arrivalPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float)";
437  }
438  return ok;
439 }
440 
441 
442 bool
443 SUMOVehicleParameter::parseArrivalSpeed(const std::string& val, const std::string& element, const std::string& id,
444  SUMOReal& speed, ArrivalSpeedDefinition& asd, std::string& error) {
445  bool ok = true;
446  if (val == "current") {
447  asd = ARRIVAL_SPEED_CURRENT;
448  } else {
449  try {
450  speed = TplConvert::_2SUMOReal(val.c_str());
451  if (speed < 0) {
452  ok = false;
453  }
454  asd = ARRIVAL_SPEED_GIVEN;
455  } catch (...) {
456  ok = false;
457  }
458  }
459  if (!ok) {
460  error = "Invalid arrivalSpeed definition for " + element + " '" + id + "';\n must be one of (\"current\", or a float>=0)";
461  }
462  return ok;
463 }
464 
465 
466 SUMOReal
467 SUMOVehicleParameter::interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string& id) {
468  if (pos < 0) {
469  pos = maximumValue + pos;
470  }
471  if (pos > maximumValue) {
472  WRITE_WARNING("Invalid " + toString(attr) + " " + toString(pos) + " given for " + id + ". Inserting at edge end instead.");
473  pos = maximumValue;
474  }
475  return pos;
476 }
477 
478 
479 /****************************************************************************/
The departure is person triggered.
The current lane shall be used.
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.
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.
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
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.
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_TAZ_SET
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:215
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
ArrivalSpeedDefinition
Possible ways to choose the arrival speed.
DepartPosDefinition
Possible ways to choose the departure position.
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.