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-2013 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  delete myParameter;
92  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
93  delete *dev;
94  }
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(*myCurrEdge, 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
154 MSBaseVehicle::replaceRouteEdges(const MSEdgeVector& edges, bool onInit) {
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 RGBColor& c = myRoute->getColor();
166  MSRoute* newRoute = new MSRoute(id, edges, false, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops());
167  if (!MSRoute::dictionary(id, newRoute)) {
168  delete newRoute;
169  return false;
170  }
171  if (!replaceRoute(newRoute, onInit)) {
172  newRoute->addReference();
173  newRoute->release();
174  return false;
175  }
176  return true;
177 }
178 
179 
180 SUMOReal
182  return 0;
183 }
184 
185 
186 void
190 }
191 
192 
193 bool
195  return myDeparture != NOT_YET_DEPARTED;
196 }
197 
198 
199 bool
201  return succEdge(1) == 0;
202 }
203 
204 void
206 }
207 
208 
209 bool
210 MSBaseVehicle::hasValidRoute(std::string& msg) const {
211  MSRouteIterator last = myRoute->end() - 1;
212  // check connectivity, first
213  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
214  if ((*e)->allowedLanes(**(e + 1), myType->getVehicleClass()) == 0) {
215  msg = "No connection between '" + (*e)->getID() + "' and '" + (*(e + 1))->getID() + "'.";
216  return false;
217  }
218  }
219  last = myRoute->end();
220  // check usable lanes, then
221  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
222  if ((*e)->prohibits(this)) {
223  msg = "Edge '" + (*e)->getID() + "' prohibits.";
224  return false;
225  }
226  }
227  return true;
228 }
229 
230 
231 void
233 #ifdef _DEBUG
234  if (myTraceMoveReminders) {
235  traceMoveReminder("add", rem, 0, true);
236  }
237 #endif
238  myMoveReminders.push_back(std::make_pair(rem, 0.));
239 }
240 
241 
242 void
244  for (MoveReminderCont::iterator r = myMoveReminders.begin(); r != myMoveReminders.end(); ++r) {
245  if (r->first == rem) {
246 #ifdef _DEBUG
247  if (myTraceMoveReminders) {
248  traceMoveReminder("remove", rem, 0, false);
249  }
250 #endif
251  myMoveReminders.erase(r);
252  return;
253  }
254  }
255 }
256 
257 
258 void
260  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
261  if (rem->first->notifyEnter(*this, reason)) {
262 #ifdef _DEBUG
263  if (myTraceMoveReminders) {
264  traceMoveReminder("notifyEnter", rem->first, rem->second, true);
265  }
266 #endif
267  ++rem;
268  } else {
269 #ifdef _DEBUG
270  if (myTraceMoveReminders) {
271  traceMoveReminder("notifyEnter", rem->first, rem->second, false);
272  }
273 #endif
274  rem = myMoveReminders.erase(rem);
275  }
276  }
277 }
278 
279 
280 void
282  const SUMOReal lastLaneLength = (myRoute->getLastEdge()->getLanes())[0]->getLength();
283  switch (myParameter->arrivalPosProcedure) {
284  case ARRIVAL_POS_GIVEN:
285  if (fabs(myParameter->arrivalPos) > lastLaneLength) {
286  WRITE_WARNING("Vehicle '" + getID() + "' will not be able to arrive at the given position!");
287  }
288  // Maybe we should warn the user about invalid inputs!
289  myArrivalPos = MIN2(myParameter->arrivalPos, lastLaneLength);
290  if (myArrivalPos < 0) {
291  myArrivalPos = MAX2(myArrivalPos + lastLaneLength, static_cast<SUMOReal>(0));
292  }
293  break;
294  case ARRIVAL_POS_RANDOM:
295  myArrivalPos = RandHelper::rand(static_cast<SUMOReal>(0), lastLaneLength);
296  break;
297  default:
298  myArrivalPos = lastLaneLength;
299  break;
300  }
301 }
302 
303 
304 MSDevice*
305 MSBaseVehicle::getDevice(const std::type_info& type) const {
306  for (std::vector<MSDevice*>::const_iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
307  if (typeid(**dev) == type) {
308  return *dev;
309  }
310  }
311  return 0;
312 }
313 
314 
315 void
321  // here starts the vehicle internal part (see loading)
322  // @note: remember to close the vehicle tag when calling this in a subclass!
323 }
324 
325 
326 #ifdef _DEBUG
327 void
328 MSBaseVehicle::initMoveReminderOutput(const OptionsCont& oc) {
329  if (oc.isSet("movereminder-output.vehicles")) {
330  const std::vector<std::string> vehicles = oc.getStringVector("movereminder-output.vehicles");
331  myShallTraceMoveReminders.insert(vehicles.begin(), vehicles.end());
332  }
333 }
334 
335 
336 void
337 MSBaseVehicle::traceMoveReminder(const std::string& type, MSMoveReminder* rem, SUMOReal pos, bool keep) const {
338  OutputDevice& od = OutputDevice::getDeviceByOption("movereminder-output");
339  od.openTag("movereminder");
340  od.writeAttr(SUMO_ATTR_TIME, STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()));
341  od.writeAttr("veh", getID());
343  od.writeAttr("type", type);
344  od.writeAttr("pos", toString(pos));
345  od.writeAttr("keep", toString(keep));
346  od.closeTag();
347 }
348 #endif
349 
350 /****************************************************************************/
351 
void removeReminder(MSMoveReminder *rem)
Removes a MoveReminder dynamically.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
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.
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
virtual bool replaceRoute(const MSRoute *route, bool onInit=false)=0
Replaces the current route by the given one.
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
bool replaceRouteEdges(const MSEdgeVector &edges, bool onInit=false)
Replaces the current route by the given edges.
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:150
T MAX2(T a, T b)
Definition: StdDefs.h:63
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 (in s)
Definition: MSNet.cpp:502
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:253
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:57
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:51
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:99
Abstract in-vehicle device.
Definition: MSDevice.h:68
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 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
const std::string & getID() const
Returns the name of the vehicle type.
int SUMOTime
Definition: SUMOTime.h:43
const SUMOVehicleParameter & getParameter() const
Returns the vehicle&#39;s parameter (including departure definition)
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
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.
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.