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-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 <iostream>
34 #include <cassert>
35 #include <utils/common/StdDefs.h>
39 #include "MSVehicleType.h"
40 #include "MSEdge.h"
41 #include "MSLane.h"
42 #include "MSMoveReminder.h"
43 #include "MSBaseVehicle.h"
44 #include "MSNet.h"
45 #include "devices/MSDevice.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 // ===========================================================================
52 // static members
53 // ===========================================================================
55 #ifdef _DEBUG
56 std::set<std::string> MSBaseVehicle::myShallTraceMoveReminders;
57 #endif
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 MSBaseVehicle::MSBaseVehicle(SUMOVehicleParameter* pars, const MSRoute* route, const MSVehicleType* type, const SUMOReal speedFactor) :
63  myParameter(pars),
64  myRoute(route),
65  myType(type),
66  myCurrEdge(route->begin()),
67  myChosenSpeedFactor(speedFactor),
68  myMoveReminders(0),
69  myDeparture(NOT_YET_DEPARTED),
70  myArrivalPos(-1),
71  myNumberReroutes(0)
72 #ifdef _DEBUG
73  , myTraceMoveReminders(myShallTraceMoveReminders.count(pars->id) > 0)
74 #endif
75 {
76  // init devices
78  //
79  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
80  myMoveReminders.push_back(std::make_pair(*dev, 0.));
81  }
84 }
85 
87  myRoute->release();
88  if (myParameter->repetitionNumber == 0) {
90  }
91  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
92  delete *dev;
93  }
94  delete myParameter;
95 }
96 
97 
98 const std::string&
100  return myParameter->id;
101 }
102 
103 
106  return *myParameter;
107 }
108 
109 
110 SUMOReal
112  return myType->getMaxSpeed();
113 }
114 
115 
116 const MSEdge*
117 MSBaseVehicle::succEdge(unsigned int nSuccs) const {
118  if (myCurrEdge + nSuccs < myRoute->end()) {
119  return *(myCurrEdge + nSuccs);
120  } else {
121  return 0;
122  }
123 }
124 
125 
126 const MSEdge*
128  return *myCurrEdge;
129 }
130 
131 
132 void
134  // check whether to reroute
135  std::vector<const MSEdge*> edges;
136  if (withTaz && MSEdge::dictionary(myParameter->fromTaz + "-source") && MSEdge::dictionary(myParameter->toTaz + "-sink")) {
137  router.compute(MSEdge::dictionary(myParameter->fromTaz + "-source"), MSEdge::dictionary(myParameter->toTaz + "-sink"), this, t, edges);
138  if (edges.size() >= 2) {
139  edges.erase(edges.begin());
140  edges.pop_back();
141  }
142  } else {
143  router.compute(getRerouteOrigin(), myRoute->getLastEdge(), this, t, edges);
144  }
145  if (edges.empty()) {
146  WRITE_WARNING("No route for vehicle '" + getID() + "' found.");
147  return;
148  }
149  replaceRouteEdges(edges, withTaz);
150 }
151 
152 
153 bool
155  // build a new id, first
156  std::string id = getID();
157  if (id[0] != '!') {
158  id = "!" + id;
159  }
160  if (myRoute->getID().find("!var#") != std::string::npos) {
161  id = myRoute->getID().substr(0, myRoute->getID().rfind("!var#") + 5) + toString(getNumberReroutes() + 1);
162  } else {
163  id = id + "!var#1";
164  }
165  const MSEdge* const origin = getRerouteOrigin();
166  if (origin != *myCurrEdge && edges.front() == origin) {
167  edges.insert(edges.begin(), *myCurrEdge);
168  }
169  const int oldSize = (int)edges.size();
170  edges.insert(edges.begin(), myRoute->begin(), myCurrEdge);
171  const RGBColor& c = myRoute->getColor();
172  MSRoute* newRoute = new MSRoute(id, edges, false, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops());
173  if (!MSRoute::dictionary(id, newRoute)) {
174  delete newRoute;
175  return false;
176  }
177  if (!replaceRoute(newRoute, onInit, (int)edges.size() - oldSize)) {
178  newRoute->addReference();
179  newRoute->release();
180  return false;
181  }
182  return true;
183 }
184 
185 
186 SUMOReal
188  return 0;
189 }
190 
191 
192 SUMOReal
194  return 0;
195 }
196 
197 
198 void
202 }
203 
204 
205 bool
207  return myDeparture != NOT_YET_DEPARTED;
208 }
209 
210 
211 bool
213  return succEdge(1) == 0;
214 }
215 
216 void
218 }
219 
220 
221 bool
222 MSBaseVehicle::hasValidRoute(std::string& msg) const {
223  MSRouteIterator last = myRoute->end() - 1;
224  // check connectivity, first
225  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
226  if ((*e)->allowedLanes(**(e + 1), myType->getVehicleClass()) == 0) {
227  msg = "No connection between '" + (*e)->getID() + "' and '" + (*(e + 1))->getID() + "'.";
228  return false;
229  }
230  }
231  last = myRoute->end();
232  // check usable lanes, then
233  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
234  if ((*e)->prohibits(this)) {
235  msg = "Edge '" + (*e)->getID() + "' prohibits.";
236  return false;
237  }
238  }
239  return true;
240 }
241 
242 
243 void
245 #ifdef _DEBUG
246  if (myTraceMoveReminders) {
247  traceMoveReminder("add", rem, 0, true);
248  }
249 #endif
250  myMoveReminders.push_back(std::make_pair(rem, 0.));
251 }
252 
253 
254 void
256  for (MoveReminderCont::iterator r = myMoveReminders.begin(); r != myMoveReminders.end(); ++r) {
257  if (r->first == rem) {
258 #ifdef _DEBUG
259  if (myTraceMoveReminders) {
260  traceMoveReminder("remove", rem, 0, false);
261  }
262 #endif
263  myMoveReminders.erase(r);
264  return;
265  }
266  }
267 }
268 
269 
270 void
272  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
273  if (rem->first->notifyEnter(*this, reason)) {
274 #ifdef _DEBUG
275  if (myTraceMoveReminders) {
276  traceMoveReminder("notifyEnter", rem->first, rem->second, true);
277  }
278 #endif
279  ++rem;
280  } else {
281 #ifdef _DEBUG
282  if (myTraceMoveReminders) {
283  traceMoveReminder("notifyEnter", rem->first, rem->second, false);
284  }
285 #endif
286  rem = myMoveReminders.erase(rem);
287  }
288  }
289 }
290 
291 
292 void
294  const SUMOReal lastLaneLength = (myRoute->getLastEdge()->getLanes())[0]->getLength();
295  switch (myParameter->arrivalPosProcedure) {
296  case ARRIVAL_POS_GIVEN:
297  if (fabs(myParameter->arrivalPos) > lastLaneLength) {
298  WRITE_WARNING("Vehicle '" + getID() + "' will not be able to arrive at the given position!");
299  }
300  // Maybe we should warn the user about invalid inputs!
301  myArrivalPos = MIN2(myParameter->arrivalPos, lastLaneLength);
302  if (myArrivalPos < 0) {
303  myArrivalPos = MAX2(myArrivalPos + lastLaneLength, static_cast<SUMOReal>(0));
304  }
305  break;
306  case ARRIVAL_POS_RANDOM:
307  myArrivalPos = RandHelper::rand(static_cast<SUMOReal>(0), lastLaneLength);
308  break;
309  default:
310  myArrivalPos = lastLaneLength;
311  break;
312  }
313 }
314 
315 
316 SUMOReal
320 }
321 
322 
323 MSDevice*
324 MSBaseVehicle::getDevice(const std::type_info& type) const {
325  for (std::vector<MSDevice*>::const_iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
326  if (typeid(**dev) == type) {
327  return *dev;
328  }
329  }
330  return 0;
331 }
332 
333 
334 void
340  // here starts the vehicle internal part (see loading)
341  // @note: remember to close the vehicle tag when calling this in a subclass!
342 }
343 
344 
345 #ifdef _DEBUG
346 void
347 MSBaseVehicle::initMoveReminderOutput(const OptionsCont& oc) {
348  if (oc.isSet("movereminder-output.vehicles")) {
349  const std::vector<std::string> vehicles = oc.getStringVector("movereminder-output.vehicles");
350  myShallTraceMoveReminders.insert(vehicles.begin(), vehicles.end());
351  }
352 }
353 
354 
355 void
356 MSBaseVehicle::traceMoveReminder(const std::string& type, MSMoveReminder* rem, SUMOReal pos, bool keep) const {
357  OutputDevice& od = OutputDevice::getDeviceByOption("movereminder-output");
358  od.openTag("movereminder");
359  od.writeAttr(SUMO_ATTR_TIME, STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()));
360  od.writeAttr("veh", getID());
362  od.writeAttr("type", type);
363  od.writeAttr("pos", toString(pos));
364  od.writeAttr("keep", toString(keep));
365  od.closeTag();
366 }
367 #endif
368 
369 /****************************************************************************/
370 
void removeReminder(MSMoveReminder *rem)
Removes a MoveReminder dynamically.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:168
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:59
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:92
Notification
Definition of a vehicle state.
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h: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:154
T MAX2(T a, T b)
Definition: StdDefs.h:71
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:465
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:321
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:210
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:71
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void saveState(OutputDevice &out)
Saves the (common) state of a vehicle.
std::string toTaz
The vehicle&#39;s destination zone (district)
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:73
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:53
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:198
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:263
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:65
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:52
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:99
Abstract in-vehicle device.
Definition: MSDevice.h:68
bool replaceRouteEdges(MSEdgeVector &edges, bool onInit=false)
Replaces the current route by the given edges.
Structure representing possible vehicle parameter.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:80
#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:94
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:64
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:70
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:312
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.
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:105
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:172
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:74
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:115
The arrival position is chosen randomly.