SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSBaseVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A base class for vehicle implementations
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 <iostream>
34 #include <cassert>
35 #include <utils/common/StdDefs.h>
39 #include "MSGlobals.h"
40 #include "MSVehicleType.h"
41 #include "MSEdge.h"
42 #include "MSLane.h"
43 #include "MSMoveReminder.h"
44 #include "MSBaseVehicle.h"
45 #include "MSNet.h"
46 #include "devices/MSDevice.h"
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 // ===========================================================================
54 // static members
55 // ===========================================================================
57 #ifdef _DEBUG
58 std::set<std::string> MSBaseVehicle::myShallTraceMoveReminders;
59 #endif
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  const MSVehicleType* type, const SUMOReal speedFactor) :
66  myParameter(pars),
67  myRoute(route),
68  myType(type),
69  myCurrEdge(route->begin()),
70  myChosenSpeedFactor(speedFactor),
71  myMoveReminders(0),
72  myDeparture(NOT_YET_DEPARTED),
73  myArrivalPos(-1),
74  myNumberReroutes(0)
75 #ifdef _DEBUG
76  , myTraceMoveReminders(myShallTraceMoveReminders.count(pars->id) > 0)
77 #endif
78 {
79  // init devices
81  //
82  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
83  myMoveReminders.push_back(std::make_pair(*dev, 0.));
84  }
86  if (!pars->wasSet(VEHPARS_FORCE_REROUTE)) {
88  }
89 }
90 
92  myRoute->release();
93  if (myParameter->repetitionNumber == 0) {
95  }
96  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
97  delete *dev;
98  }
99  delete myParameter;
100 }
101 
102 
103 const std::string&
105  return myParameter->id;
106 }
107 
108 
111  return *myParameter;
112 }
113 
114 
115 SUMOReal
117  return myType->getMaxSpeed();
118 }
119 
120 
121 const MSEdge*
122 MSBaseVehicle::succEdge(unsigned int nSuccs) const {
123  if (myCurrEdge + nSuccs < myRoute->end()) {
124  return *(myCurrEdge + nSuccs);
125  } else {
126  return 0;
127  }
128 }
129 
130 
131 const MSEdge*
133  return *myCurrEdge;
134 }
135 
136 
137 void
138 MSBaseVehicle::reroute(SUMOTime t, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router, const bool onInit, const bool withTaz) {
139  // check whether to reroute
140  const MSEdge* source = withTaz && onInit ? MSEdge::dictionary(myParameter->fromTaz + "-source") : getRerouteOrigin();
141  if (source == 0) {
142  source = getRerouteOrigin();
143  }
144  const MSEdge* sink = withTaz ? MSEdge::dictionary(myParameter->toTaz + "-sink") : myRoute->getLastEdge();
145  if (sink == 0) {
146  sink = myRoute->getLastEdge();
147  }
148  ConstMSEdgeVector edges;
149  const ConstMSEdgeVector stops = getStopEdges();
150  for (MSRouteIterator s = stops.begin(); s != stops.end(); ++s) {
151  if (*s != source) {
152  // !!! need to adapt t here
153  router.compute(source, *s, this, t, edges);
154  source = *s;
155  edges.pop_back();
156  }
157  }
158  router.compute(source, sink, this, t, edges);
159  if (!edges.empty() && edges.front()->getPurpose() == MSEdge::EDGEFUNCTION_DISTRICT) {
160  edges.erase(edges.begin());
161  }
162  if (!edges.empty() && edges.back()->getPurpose() == MSEdge::EDGEFUNCTION_DISTRICT) {
163  edges.pop_back();
164  }
165  replaceRouteEdges(edges, onInit);
166 }
167 
168 
169 bool
171  if (edges.empty()) {
172  WRITE_WARNING("No route for vehicle '" + getID() + "' found" + (onInit ? " (skipping vehicle)." : "."));
173  return false;
174  }
175  // build a new id, first
176  std::string id = getID();
177  if (id[0] != '!') {
178  id = "!" + id;
179  }
180  if (myRoute->getID().find("!var#") != std::string::npos) {
181  id = myRoute->getID().substr(0, myRoute->getID().rfind("!var#") + 5) + toString(getNumberReroutes() + 1);
182  } else {
183  id = id + "!var#1";
184  }
185  int oldSize = (int)edges.size();
186  if (!onInit) {
187  const MSEdge* const origin = getRerouteOrigin();
188  if (origin != *myCurrEdge && edges.front() == origin) {
189  edges.insert(edges.begin(), *myCurrEdge);
190  oldSize = (int)edges.size();
191  }
192  edges.insert(edges.begin(), myRoute->begin(), myCurrEdge);
193  }
194  if (edges == myRoute->getEdges()) {
195  if (onInit) {
196  // if edges = 'from to' we still need to calculate the arrivalPos once
198  }
199  return true;
200  }
201  const RGBColor& c = myRoute->getColor();
202  MSRoute* newRoute = new MSRoute(id, edges, false, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops());
203 #ifdef HAVE_FOX
204  MSDevice_Routing::lock();
205 #endif
206  if (!MSRoute::dictionary(id, newRoute)) {
207 #ifdef HAVE_FOX
208  MSDevice_Routing::unlock();
209 #endif
210  delete newRoute;
211  return false;
212  }
213 #ifdef HAVE_FOX
214  MSDevice_Routing::unlock();
215 #endif
216  if (!replaceRoute(newRoute, onInit, (int)edges.size() - oldSize)) {
217  newRoute->addReference();
218 #ifdef HAVE_FOX
219  MSDevice_Routing::lock();
220 #endif
221  newRoute->release();
222 #ifdef HAVE_FOX
223  MSDevice_Routing::unlock();
224 #endif
225  return false;
226  }
228  return true;
229 }
230 
231 
232 SUMOReal
234  return 0;
235 }
236 
237 
238 SUMOReal
240  return 0;
241 }
242 
243 
244 void
248 }
249 
250 
251 bool
253  return myDeparture != NOT_YET_DEPARTED;
254 }
255 
256 
257 bool
259  return succEdge(1) == 0;
260 }
261 
262 void
264 }
265 
266 void
268 }
269 
270 bool
271 MSBaseVehicle::hasValidRoute(std::string& msg) const {
272  MSRouteIterator last = myRoute->end() - 1;
273  // check connectivity, first
274  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
275  if ((*e)->allowedLanes(**(e + 1), myType->getVehicleClass()) == 0) {
276  msg = "No connection between '" + (*e)->getID() + "' and '" + (*(e + 1))->getID() + "'.";
277  return false;
278  }
279  }
280  last = myRoute->end();
281  // check usable lanes, then
282  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
283  if ((*e)->prohibits(this)) {
284  msg = "Edge '" + (*e)->getID() + "' prohibits.";
285  return false;
286  }
287  }
288  return true;
289 }
290 
291 
292 void
294 #ifdef _DEBUG
295  if (myTraceMoveReminders) {
296  traceMoveReminder("add", rem, 0, true);
297  }
298 #endif
299  myMoveReminders.push_back(std::make_pair(rem, 0.));
300 }
301 
302 
303 void
305  for (MoveReminderCont::iterator r = myMoveReminders.begin(); r != myMoveReminders.end(); ++r) {
306  if (r->first == rem) {
307 #ifdef _DEBUG
308  if (myTraceMoveReminders) {
309  traceMoveReminder("remove", rem, 0, false);
310  }
311 #endif
312  myMoveReminders.erase(r);
313  return;
314  }
315  }
316 }
317 
318 
319 void
321  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
322  if (rem->first->notifyEnter(*this, reason)) {
323 #ifdef _DEBUG
324  if (myTraceMoveReminders) {
325  traceMoveReminder("notifyEnter", rem->first, rem->second, true);
326  }
327 #endif
328  ++rem;
329  } else {
330 #ifdef _DEBUG
331  if (myTraceMoveReminders) {
332  traceMoveReminder("notifyEnter", rem->first, rem->second, false);
333  }
334 #endif
335  rem = myMoveReminders.erase(rem);
336  }
337  }
338 }
339 
340 
341 void
343  const SUMOReal lastLaneLength = (myRoute->getLastEdge()->getLanes())[0]->getLength();
344  switch (myParameter->arrivalPosProcedure) {
345  case ARRIVAL_POS_GIVEN:
346  if (fabs(myParameter->arrivalPos) > lastLaneLength) {
347  WRITE_WARNING("Vehicle '" + getID() + "' will not be able to arrive at the given position!");
348  }
349  // Maybe we should warn the user about invalid inputs!
350  myArrivalPos = MIN2(myParameter->arrivalPos, lastLaneLength);
351  if (myArrivalPos < 0) {
352  myArrivalPos = MAX2(myArrivalPos + lastLaneLength, static_cast<SUMOReal>(0));
353  }
354  break;
355  case ARRIVAL_POS_RANDOM:
356  myArrivalPos = RandHelper::rand(static_cast<SUMOReal>(0), lastLaneLength);
357  break;
358  default:
359  myArrivalPos = lastLaneLength;
360  break;
361  }
362 }
363 
364 
365 SUMOReal
369 }
370 
371 
372 MSDevice*
373 MSBaseVehicle::getDevice(const std::type_info& type) const {
374  for (std::vector<MSDevice*>::const_iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
375  if (typeid(**dev) == type) {
376  return *dev;
377  }
378  }
379  return 0;
380 }
381 
382 
383 void
389  // here starts the vehicle internal part (see loading)
390  // @note: remember to close the vehicle tag when calling this in a subclass!
391 }
392 
393 
394 void
395 MSBaseVehicle::addStops(const bool ignoreStopErrors) {
396  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator i = myParameter->stops.begin(); i != myParameter->stops.end(); ++i) {
397  std::string errorMsg;
398  if (!addStop(*i, errorMsg) && !ignoreStopErrors) {
399  throw ProcessError(errorMsg);
400  }
401  if (errorMsg != "") {
402  WRITE_WARNING(errorMsg);
403  }
404  }
405  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator i = myRoute->getStops().begin(); i != myRoute->getStops().end(); ++i) {
406  std::string errorMsg;
407  if (!addStop(*i, errorMsg) && !ignoreStopErrors) {
408  throw ProcessError(errorMsg);
409  }
410  if (errorMsg != "") {
411  WRITE_WARNING(errorMsg);
412  }
413  }
414 }
415 
416 
417 #ifdef _DEBUG
418 void
419 MSBaseVehicle::initMoveReminderOutput(const OptionsCont& oc) {
420  if (oc.isSet("movereminder-output.vehicles")) {
421  const std::vector<std::string> vehicles = oc.getStringVector("movereminder-output.vehicles");
422  myShallTraceMoveReminders.insert(vehicles.begin(), vehicles.end());
423  }
424 }
425 
426 
427 void
428 MSBaseVehicle::traceMoveReminder(const std::string& type, MSMoveReminder* rem, SUMOReal pos, bool keep) const {
429  OutputDevice& od = OutputDevice::getDeviceByOption("movereminder-output");
430  od.openTag("movereminder");
431  od.writeAttr(SUMO_ATTR_TIME, STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()));
432  od.writeAttr("veh", getID());
434  od.writeAttr("type", type);
435  od.writeAttr("pos", toString(pos));
436  od.writeAttr("keep", toString(keep));
437  od.closeTag();
438 }
439 #endif
440 
441 /****************************************************************************/
442 
void removeReminder(MSMoveReminder *rem)
Removes a MoveReminder dynamically.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const MSVehicleType * myType
This Vehicle&#39;s type.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
MSDevice * getDevice(const std::type_info &type) const
Returns a device of the given type if it exists or 0.
SUMOReal getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
long long int SUMOTime
Definition: SUMOTime.h:43
const int VEHPARS_FORCE_REROUTE
unsigned int getNumberReroutes() const
Returns the number of new routes this vehicle got.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
const std::string & getDescription() const
SUMOReal myArrivalPos
the position on the destination lane where the vehicle stops
MoveReminderCont myMoveReminders
Current lane&#39;s move reminder.
int repetitionNumber
The number of times the vehicle shall be repeatedly inserted.
SUMOReal getMaxSpeed() const
Returns the maximum speed.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:186
SUMOReal getImpatience() const
Returns this vehicles impatience.
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:93
Notification
Definition of a vehicle state.
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
T MAX2(T a, T b)
Definition: StdDefs.h:74
virtual ~MSBaseVehicle()
Destructor.
const MSRoute * myRoute
This Vehicle&#39;s route.
bool hasDeparted() const
Returns whether this vehicle has already departed.
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:553
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:79
const SUMOVehicleParameter * myParameter
This Vehicle&#39;s parameter.
The arrival position is given.
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:337
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:251
MSBaseVehicle(SUMOVehicleParameter *pars, const MSRoute *route, const MSVehicleType *type, const SUMOReal speedFactor)
Constructor.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:75
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void saveState(OutputDevice &out)
Saves the (common) state of a vehicle.
virtual bool addStop(const SUMOVehicleParameter::Stop &stopPar, std::string &errorMsg, SUMOTime untilOffset=0)=0
Adds a stop.
std::string toTaz
The vehicle&#39;s destination zone (district)
std::vector< Stop > stops
List of the stops the vehicle will make.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:81
virtual void addPerson(MSTransportable *person)
Adds a person to this vehicle.
The edge is a district edge.
Definition: MSEdge.h:100
std::string routeid
The vehicle&#39;s route id.
virtual SUMOReal getAcceleration() const
Returns the vehicle&#39;s acceleration.
bool wasSet(int what) const
Returns whether the given parameter was set.
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:200
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:304
const MSEdge * succEdge(unsigned int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:61
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:124
T MIN2(T a, T b)
Definition: StdDefs.h:68
std::string fromTaz
The vehicle&#39;s origin zone (district)
virtual const ConstMSEdgeVector getStopEdges() const =0
Returns the list of still pending stop edges.
Something on a lane to be noticed about vehicle movement.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:100
bool replaceRouteEdges(ConstMSEdgeVector &edges, bool onInit=false)
Replaces the current route by the given edges.
Abstract in-vehicle device.
Definition: MSDevice.h:69
void addStops(const bool ignoreStopErrors)
Adds stops to the built vehicle.
Structure representing possible vehicle parameter.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:81
#define SUMOTime_MAX
Definition: SUMOTime.h:44
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual void activateReminders(const MSMoveReminder::Notification reason)
&quot;Activates&quot; all current move reminder
void onDepart()
Called when the vehicle is inserted into the network.
A storage for options typed value containers)
Definition: OptionsCont.h:108
virtual bool replaceRoute(const MSRoute *route, bool onInit=false, int offset=0)=0
Replaces the current route by the given one.
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
Definition: MSBaseVehicle.h:90
virtual bool hasArrived() const
Returns whether this vehicle has already arived (by default this is true if the vehicle has reached i...
void calculateArrivalPos()
(Re-)Calculates the arrival position from the vehicle parameters
virtual const MSEdge * getRerouteOrigin() const
Returns the starting point for reroutes (usually the current edge)
const std::string & getID() const
Returns the name of the vehicle type.
static SUMOTime gTimeToGridlock
Definition: MSGlobals.h:65
const SUMOVehicleParameter & getParameter() const
Returns the vehicle&#39;s parameter (including departure definition)
virtual SUMOTime getWaitingTime() const =0
bool hasValidRoute(std::string &msg) const
Validates the current route.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:328
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
MSRouteIterator myCurrEdge
Iterator to current route-edge.
void reroute(SUMOTime t, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, const bool onInit=false, const bool withTaz=false)
Performs a rerouting using the given router.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:106
virtual SUMOReal getSlope() const
Returns the slope of the road at vehicle&#39;s position.
void addReminder(MSMoveReminder *rem)
Adds a MoveReminder dynamically.
std::vector< MSDevice * > myDevices
The devices this vehicle has.
virtual void addContainer(MSTransportable *container)
Adds a container to this vehicle.
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:173
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOTime myDeparture
The real departure time.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:75
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
static const SUMOTime NOT_YET_DEPARTED
std::string id
The vehicle&#39;s id.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
const std::string & getID() const
Returns the name of the vehicle.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116
The arrival position is chosen randomly.