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-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 <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  }
87 }
88 
90  myRoute->release();
91  if (myParameter->repetitionNumber == 0) {
93  }
94  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
95  delete *dev;
96  }
97  delete myParameter;
98 }
99 
100 
101 const std::string&
103  return myParameter->id;
104 }
105 
106 
109  return *myParameter;
110 }
111 
112 
113 SUMOReal
115  return myType->getMaxSpeed();
116 }
117 
118 
119 const MSEdge*
120 MSBaseVehicle::succEdge(unsigned int nSuccs) const {
121  if (myCurrEdge + nSuccs < myRoute->end()) {
122  return *(myCurrEdge + nSuccs);
123  } else {
124  return 0;
125  }
126 }
127 
128 
129 const MSEdge*
131  return *myCurrEdge;
132 }
133 
134 
135 void
137  // check whether to reroute
138  std::vector<const MSEdge*> edges;
139  if (withTaz && MSEdge::dictionary(myParameter->fromTaz + "-source") && MSEdge::dictionary(myParameter->toTaz + "-sink")) {
140  router.compute(MSEdge::dictionary(myParameter->fromTaz + "-source"), MSEdge::dictionary(myParameter->toTaz + "-sink"), this, t, edges);
141  if (edges.size() >= 2) {
142  edges.erase(edges.begin());
143  edges.pop_back();
144  }
145  } else {
146  router.compute(getRerouteOrigin(), myRoute->getLastEdge(), this, t, edges);
147  }
148  if (edges.empty()) {
149  WRITE_WARNING("No route for vehicle '" + getID() + "' found.");
150  return;
151  }
152  replaceRouteEdges(edges, withTaz);
153 }
154 
155 
156 bool
158  // build a new id, first
159  std::string id = getID();
160  if (id[0] != '!') {
161  id = "!" + id;
162  }
163  if (myRoute->getID().find("!var#") != std::string::npos) {
164  id = myRoute->getID().substr(0, myRoute->getID().rfind("!var#") + 5) + toString(getNumberReroutes() + 1);
165  } else {
166  id = id + "!var#1";
167  }
168  const MSEdge* const origin = getRerouteOrigin();
169  if (origin != *myCurrEdge && edges.front() == origin) {
170  edges.insert(edges.begin(), *myCurrEdge);
171  }
172  const int oldSize = (int)edges.size();
173  edges.insert(edges.begin(), myRoute->begin(), myCurrEdge);
174  if (edges == myRoute->getEdges()) {
175  return true;
176  }
177  const RGBColor& c = myRoute->getColor();
178  MSRoute* newRoute = new MSRoute(id, edges, false, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops());
179 #ifdef HAVE_FOX
180  MSDevice_Routing::lock();
181 #endif
182  if (!MSRoute::dictionary(id, newRoute)) {
183 #ifdef HAVE_FOX
184  MSDevice_Routing::unlock();
185 #endif
186  delete newRoute;
187  return false;
188  }
189 #ifdef HAVE_FOX
190  MSDevice_Routing::unlock();
191 #endif
192  if (!replaceRoute(newRoute, onInit, (int)edges.size() - oldSize)) {
193  newRoute->addReference();
194 #ifdef HAVE_FOX
195  MSDevice_Routing::lock();
196 #endif
197  newRoute->release();
198 #ifdef HAVE_FOX
199  MSDevice_Routing::unlock();
200 #endif
201  return false;
202  }
203  return true;
204 }
205 
206 
207 SUMOReal
209  return 0;
210 }
211 
212 
213 SUMOReal
215  return 0;
216 }
217 
218 
219 void
223 }
224 
225 
226 bool
228  return myDeparture != NOT_YET_DEPARTED;
229 }
230 
231 
232 bool
234  return succEdge(1) == 0;
235 }
236 
237 void
239 }
240 
241 
242 bool
243 MSBaseVehicle::hasValidRoute(std::string& msg) const {
244  MSRouteIterator last = myRoute->end() - 1;
245  // check connectivity, first
246  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
247  if ((*e)->allowedLanes(**(e + 1), myType->getVehicleClass()) == 0) {
248  msg = "No connection between '" + (*e)->getID() + "' and '" + (*(e + 1))->getID() + "'.";
249  return false;
250  }
251  }
252  last = myRoute->end();
253  // check usable lanes, then
254  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
255  if ((*e)->prohibits(this)) {
256  msg = "Edge '" + (*e)->getID() + "' prohibits.";
257  return false;
258  }
259  }
260  return true;
261 }
262 
263 
264 void
266 #ifdef _DEBUG
267  if (myTraceMoveReminders) {
268  traceMoveReminder("add", rem, 0, true);
269  }
270 #endif
271  myMoveReminders.push_back(std::make_pair(rem, 0.));
272 }
273 
274 
275 void
277  for (MoveReminderCont::iterator r = myMoveReminders.begin(); r != myMoveReminders.end(); ++r) {
278  if (r->first == rem) {
279 #ifdef _DEBUG
280  if (myTraceMoveReminders) {
281  traceMoveReminder("remove", rem, 0, false);
282  }
283 #endif
284  myMoveReminders.erase(r);
285  return;
286  }
287  }
288 }
289 
290 
291 void
293  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
294  if (rem->first->notifyEnter(*this, reason)) {
295 #ifdef _DEBUG
296  if (myTraceMoveReminders) {
297  traceMoveReminder("notifyEnter", rem->first, rem->second, true);
298  }
299 #endif
300  ++rem;
301  } else {
302 #ifdef _DEBUG
303  if (myTraceMoveReminders) {
304  traceMoveReminder("notifyEnter", rem->first, rem->second, false);
305  }
306 #endif
307  rem = myMoveReminders.erase(rem);
308  }
309  }
310 }
311 
312 
313 void
315  const SUMOReal lastLaneLength = (myRoute->getLastEdge()->getLanes())[0]->getLength();
316  switch (myParameter->arrivalPosProcedure) {
317  case ARRIVAL_POS_GIVEN:
318  if (fabs(myParameter->arrivalPos) > lastLaneLength) {
319  WRITE_WARNING("Vehicle '" + getID() + "' will not be able to arrive at the given position!");
320  }
321  // Maybe we should warn the user about invalid inputs!
322  myArrivalPos = MIN2(myParameter->arrivalPos, lastLaneLength);
323  if (myArrivalPos < 0) {
324  myArrivalPos = MAX2(myArrivalPos + lastLaneLength, static_cast<SUMOReal>(0));
325  }
326  break;
327  case ARRIVAL_POS_RANDOM:
328  myArrivalPos = RandHelper::rand(static_cast<SUMOReal>(0), lastLaneLength);
329  break;
330  default:
331  myArrivalPos = lastLaneLength;
332  break;
333  }
334 }
335 
336 
337 SUMOReal
341 }
342 
343 
344 MSDevice*
345 MSBaseVehicle::getDevice(const std::type_info& type) const {
346  for (std::vector<MSDevice*>::const_iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
347  if (typeid(**dev) == type) {
348  return *dev;
349  }
350  }
351  return 0;
352 }
353 
354 
355 void
361  // here starts the vehicle internal part (see loading)
362  // @note: remember to close the vehicle tag when calling this in a subclass!
363 }
364 
365 
366 void
367 MSBaseVehicle::addStops(const bool ignoreStopErrors) {
368  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator i = myParameter->stops.begin(); i != myParameter->stops.end(); ++i) {
369  std::string errorMsg;
370  if (!addStop(*i, errorMsg) && !ignoreStopErrors) {
371  throw ProcessError(errorMsg);
372  }
373  if (errorMsg != "") {
374  WRITE_WARNING(errorMsg);
375  }
376  }
377  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator i = myRoute->getStops().begin(); i != myRoute->getStops().end(); ++i) {
378  std::string errorMsg;
379  if (!addStop(*i, errorMsg) && !ignoreStopErrors) {
380  throw ProcessError(errorMsg);
381  }
382  if (errorMsg != "") {
383  WRITE_WARNING(errorMsg);
384  }
385  }
386 }
387 
388 
389 #ifdef _DEBUG
390 void
391 MSBaseVehicle::initMoveReminderOutput(const OptionsCont& oc) {
392  if (oc.isSet("movereminder-output.vehicles")) {
393  const std::vector<std::string> vehicles = oc.getStringVector("movereminder-output.vehicles");
394  myShallTraceMoveReminders.insert(vehicles.begin(), vehicles.end());
395  }
396 }
397 
398 
399 void
400 MSBaseVehicle::traceMoveReminder(const std::string& type, MSMoveReminder* rem, SUMOReal pos, bool keep) const {
401  OutputDevice& od = OutputDevice::getDeviceByOption("movereminder-output");
402  od.openTag("movereminder");
403  od.writeAttr(SUMO_ATTR_TIME, STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()));
404  od.writeAttr("veh", getID());
406  od.writeAttr("type", type);
407  od.writeAttr("pos", toString(pos));
408  od.writeAttr("keep", toString(keep));
409  od.closeTag();
410 }
411 #endif
412 
413 /****************************************************************************/
414 
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].
void reroute(SUMOTime t, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, bool withTaz=false)
Performs a rerouting using the given router.
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:180
virtual void addPerson(MSPerson *person)
Adds a person to this vehicle.
SUMOReal getImpatience() const
Returns this vehicles impatience.
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:60
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
const MSEdgeVector & getEdges() const
Definition: MSRoute.h:123
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:156
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:523
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:327
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:230
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:72
#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:75
std::string routeid
The vehicle&#39;s route id.
virtual SUMOReal getAcceleration() const
Returns the vehicle&#39;s acceleration.
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:57
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:199
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:283
const MSEdge * succEdge(unsigned int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
T MIN2(T a, T b)
Definition: StdDefs.h:68
std::string fromTaz
The vehicle&#39;s origin zone (district)
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
Abstract in-vehicle device.
Definition: MSDevice.h:69
bool replaceRouteEdges(MSEdgeVector &edges, bool onInit=false)
Replaces the current route by the given edges.
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.
int SUMOTime
Definition: SUMOTime.h:43
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:318
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
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.
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.
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.