SUMO - Simulation of Urban MObility
MSDevice_Vehroutes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A device which collects info on the vehicle trip
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2009-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
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 <microsim/MSGlobals.h>
34 #include <microsim/MSNet.h>
35 #include <microsim/MSLane.h>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSRoute.h>
38 #include <microsim/MSVehicleType.h>
42 #include "MSDevice_Vehroutes.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // static member variables
51 // ===========================================================================
55 bool MSDevice_Vehroutes::mySorted = false;
59 std::map<const SUMOTime, int> MSDevice_Vehroutes::myDepartureCounts;
60 std::map<const SUMOTime, std::map<const std::string, std::string> > MSDevice_Vehroutes::myRouteInfos;
61 
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 // ---------------------------------------------------------------------------
67 // static initialisation methods
68 // ---------------------------------------------------------------------------
69 void
71  if (OptionsCont::getOptions().isSet("vehroute-output")) {
72  OutputDevice::createDeviceByOption("vehroute-output", "routes", "routes_file.xsd");
73  mySaveExits = OptionsCont::getOptions().getBool("vehroute-output.exit-times");
74  myLastRouteOnly = OptionsCont::getOptions().getBool("vehroute-output.last-route");
75  myDUAStyle = OptionsCont::getOptions().getBool("vehroute-output.dua");
76  mySorted = myDUAStyle || OptionsCont::getOptions().getBool("vehroute-output.sorted");
77  myIntendedDepart = OptionsCont::getOptions().getBool("vehroute-output.intended-depart");
78  myRouteLength = OptionsCont::getOptions().getBool("vehroute-output.route-length");
80  }
81 }
82 
83 
85 MSDevice_Vehroutes::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into, unsigned int maxRoutes) {
86  if (maxRoutes < INT_MAX) {
87  return new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
88  }
89  if (OptionsCont::getOptions().isSet("vehroute-output")) {
90  if (myLastRouteOnly) {
91  maxRoutes = 0;
92  }
93  myStateListener.myDevices[&v] = new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
94  into.push_back(myStateListener.myDevices[&v]);
95  return myStateListener.myDevices[&v];
96  }
97  return 0;
98 }
99 
100 
101 // ---------------------------------------------------------------------------
102 // MSDevice_Vehroutes::StateListener-methods
103 // ---------------------------------------------------------------------------
104 void
106  if (to == MSNet::VEHICLE_STATE_NEWROUTE) {
107  myDevices[vehicle]->addRoute();
108  }
109 }
110 
111 
112 // ---------------------------------------------------------------------------
113 // MSDevice_Vehroutes-methods
114 // ---------------------------------------------------------------------------
115 MSDevice_Vehroutes::MSDevice_Vehroutes(SUMOVehicle& holder, const std::string& id, unsigned int maxRoutes)
116  : MSDevice(holder, id), myCurrentRoute(&holder.getRoute()), myMaxRoutes(maxRoutes), myLastSavedAt(0) {
118 }
119 
120 
122  for (std::vector<RouteReplaceInfo>::iterator i = myReplacedRoutes.begin(); i != myReplacedRoutes.end(); ++i) {
123  (*i).route->release();
124  }
127 }
128 
129 
130 bool
134  if (mySorted && myStateListener.myDevices[&veh] == this) {
136  myDepartureCounts[departure]++;
137  }
138  }
139  return mySaveExits;
140 }
141 
142 
143 bool
145  if (mySaveExits && reason != NOTIFICATION_LANE_CHANGE) {
146  if (reason != NOTIFICATION_TELEPORT && myLastSavedAt == veh.getEdge()) { // need to check this for internal lanes
148  } else if (myLastSavedAt != veh.getEdge()) {
149  myExits.push_back(MSNet::getInstance()->getCurrentTimeStep());
150  myLastSavedAt = veh.getEdge();
151  }
152  }
153  return mySaveExits;
154 }
155 
156 
157 void
159  // check if a previous route shall be written
161  if (index >= 0) {
162  assert((int) myReplacedRoutes.size() > index);
163  // write edge on which the vehicle was when the route was valid
164  os << " replacedOnEdge=\"";
165  if (myReplacedRoutes[index].edge) {
166  os << myReplacedRoutes[index].edge->getID();
167  }
168  // write the time at which the route was replaced
169  os << "\" replacedAtTime=\"" << time2string(myReplacedRoutes[index].time) << "\" probability=\"0\" edges=\"";
170  // get the route
171  int i = index;
172  while (i > 0 && myReplacedRoutes[i - 1].edge) {
173  i--;
174  }
175  const MSEdge* lastEdge = 0;
176  for (; i < index; ++i) {
177  myReplacedRoutes[i].route->writeEdgeIDs(os, lastEdge, myReplacedRoutes[i].edge);
178  lastEdge = myReplacedRoutes[i].edge;
179  }
180  myReplacedRoutes[index].route->writeEdgeIDs(os, lastEdge);
181  } else {
182  os << " edges=\"";
183  const MSEdge* lastEdge = 0;
184  int numWritten = 0;
185  if (myHolder.getNumberReroutes() > 0) {
186  assert(myReplacedRoutes.size() <= myHolder.getNumberReroutes());
187  unsigned int i = static_cast<unsigned int>(myReplacedRoutes.size());
188  while (i > 0 && myReplacedRoutes[i - 1].edge) {
189  i--;
190  }
191  for (; i < myReplacedRoutes.size(); ++i) {
192  numWritten += myReplacedRoutes[i].route->writeEdgeIDs(os, lastEdge, myReplacedRoutes[i].edge);
193  lastEdge = myReplacedRoutes[i].edge;
194  }
195  }
196  const MSEdge* upTo = 0;
197  if (mySaveExits) {
198  int remainingWithExitTime = (int)myExits.size() - numWritten;
199  assert(remainingWithExitTime >= 0);
200  assert(remainingWithExitTime <= (int)myCurrentRoute->size());
201  if (remainingWithExitTime < (int)myCurrentRoute->size()) {
202  upTo = *(myCurrentRoute->begin() + remainingWithExitTime);
203  }
204  }
205  myCurrentRoute->writeEdgeIDs(os, lastEdge, upTo);
206  if (mySaveExits) {
207  os << "\" exitTimes=\"";
208  for (std::vector<SUMOTime>::const_iterator it = myExits.begin(); it != myExits.end(); ++it) {
209  if (it != myExits.begin()) {
210  os << " ";
211  }
212  os << time2string(*it);
213  }
214  }
215  }
216  (os << "\"").closeTag();
217 }
218 
219 
220 void
222  OutputDevice& routeOut = OutputDevice::getDeviceByOption("vehroute-output");
223  OutputDevice_String od(routeOut.isBinary(), 1);
225  od.openTag(SUMO_TAG_VEHICLE).writeAttr(SUMO_ATTR_ID, myHolder.getID());
227  od.writeAttr(SUMO_ATTR_TYPE, myHolder.getVehicleType().getID());
228  }
229  od.writeAttr(SUMO_ATTR_DEPART, time2string(departure));
230  if (myHolder.hasArrived()) {
231  od.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
232  if (myRouteLength) {
233  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
235  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths);
236  od.writeAttr("routeLength", routeLength);
237  }
238  }
241  }
243  od.writeAttr(SUMO_ATTR_TO_TAZ, myHolder.getParameter().toTaz);
244  }
245  if (myDUAStyle) {
247  if (routeDist != 0) {
248  const std::vector<const MSRoute*>& routes = routeDist->getVals();
249  unsigned index = 0;
250  while (index < routes.size() && routes[index] != myCurrentRoute) {
251  ++index;
252  }
253  od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, index);
254  const std::vector<SUMOReal>& probs = routeDist->getProbs();
255  for (unsigned int i = 0; i < routes.size(); ++i) {
256  od.setPrecision();
257  od.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_COST, routes[i]->getCosts());
258  od.setPrecision(8);
259  od.writeAttr(SUMO_ATTR_PROB, probs[i]);
260  od.setPrecision();
261  od << " edges=\"";
262  routes[i]->writeEdgeIDs(od, *routes[i]->begin());
263  (od << "\"").closeTag();
264  }
265  od.closeTag();
266  } else {
267  writeXMLRoute(od);
268  }
269  } else {
270  if (myReplacedRoutes.size() > 0) {
271  od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION);
272  for (unsigned int i = 0; i < myReplacedRoutes.size(); ++i) {
273  writeXMLRoute(od, i);
274  }
275  }
276  writeXMLRoute(od);
277  if (myReplacedRoutes.size() > 0) {
278  od.closeTag();
279  }
280  }
281  od.closeTag();
282  od.lf();
283  if (mySorted) {
284  myRouteInfos[departure][myHolder.getID()] = od.getString();
285  myDepartureCounts[departure]--;
286  std::map<const SUMOTime, int>::iterator it = myDepartureCounts.begin();
287  while (it != myDepartureCounts.end() && it->second == 0) {
288  std::map<const std::string, std::string>& infos = myRouteInfos[it->first];
289  for (std::map<const std::string, std::string>::const_iterator it2 = infos.begin(); it2 != infos.end(); ++it2) {
290  routeOut << it2->second;
291  }
292  myRouteInfos.erase(it->first);
293  myDepartureCounts.erase(it);
294  it = myDepartureCounts.begin();
295  }
296  } else {
297  routeOut << od.getString();
298  }
299 }
300 
301 
302 const MSRoute*
304  if (index < (int)myReplacedRoutes.size()) {
305  return myReplacedRoutes[index].route;
306  } else {
307  return 0;
308  }
309 }
310 
311 
312 void
314  if (myMaxRoutes > 0) {
315  if (myHolder.hasDeparted()) {
317  } else {
318  myReplacedRoutes.push_back(RouteReplaceInfo(0, MSNet::getInstance()->getCurrentTimeStep(), myCurrentRoute));
319  }
320  if (myReplacedRoutes.size() > myMaxRoutes) {
321  myReplacedRoutes.front().route->release();
322  myReplacedRoutes.erase(myReplacedRoutes.begin());
323  }
324  } else {
326  }
329 }
330 
331 
332 void
334  for (std::map<const SUMOVehicle*, MSDevice_Vehroutes*, Named::NamedLikeComparatorIdLess<SUMOVehicle> >::const_iterator it = myStateListener.myDevices.begin();
335  it != myStateListener.myDevices.end(); ++it) {
336  if (it->first->hasDeparted()) {
337  it->second->generateOutput();
338  }
339  }
340 }
341 
342 
343 /****************************************************************************/
344 
const int VEHPARS_TO_TAZ_SET
static std::map< const SUMOTime, int > myDepartureCounts
Map needed to sort vehicles by departure time.
long long int SUMOTime
Definition: SUMOTime.h:43
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
const MSRoute * myCurrentRoute
The currently used route.
const std::vector< SUMOReal > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
MSDevice_Vehroutes(SUMOVehicle &holder, const std::string &id, unsigned int maxRoutes)
Constructor.
static StateListener myStateListener
A class that is notified about reroutings.
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:87
virtual const MSRoute & getRoute() const =0
Returns the current route.
int writeEdgeIDs(OutputDevice &os, const MSEdge *const from, const MSEdge *const upTo=0) const
Output the edge ids up to but not including the id of the given edge.
Definition: MSRoute.cpp:199
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to)
Called if a vehicle changes its state.
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSDevice.h:153
static bool myDUAStyle
A shortcut for the Option "vehroute-output.dua".
virtual SUMOReal getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
bool hasInternalLinks() const
return whether the network contains internal links
Definition: MSNet.h:628
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
std::vector< SUMOTime > myExits
The times the vehicle exites an edge.
static bool mySorted
A shortcut for the Option "vehroute-output.sorted".
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:150
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
const std::string DEFAULT_VTYPE_ID
static bool mySaveExits
A shortcut for the Option "vehroute-output.exit-times".
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
Definition: MSNet.cpp:753
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, Notification reason)
Saves exit times if needed.
static bool myIntendedDepart
A shortcut for the Option "vehroute-output.intended-depart".
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
std::string toTaz
The vehicle&#39;s destination zone (district)
const std::vector< T > & getVals() const
Returns the members of the distribution.
static void init()
Static intialization.
A road/street connecting two junctions.
Definition: MSEdge.h:81
SUMOReal myDepartPos
The position on the lane the vehicle departed at.
The vehicle changes lanes (micro only)
static bool myLastRouteOnly
A shortcut for the Option "vehroute-output.last-route".
The vehicle got a new route.
Definition: MSNet.h:538
void addRoute()
Called on route change.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
bool wasSet(int what) const
Returns whether the given parameter was set.
SUMOReal getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge *fromEdge, const MSEdge *toEdge, bool includeInternal=true) const
Compute the distance between 2 given edges on this route, including the length of internal lanes...
Definition: MSRoute.cpp:255
std::vector< RouteReplaceInfo > myReplacedRoutes
Prior routes.
SUMOTime depart
The vehicle&#39;s departure time.
std::string fromTaz
The vehicle&#39;s origin zone (district)
A class that is notified about reroutings.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:71
const unsigned int myMaxRoutes
The maximum number of routes to report.
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:100
static void generateOutputForUnfinished()
generate vehroute output for vehicles which are still in the network
const int VEHPARS_FROM_TAZ_SET
static bool myRouteLength
A shortcut for the Option "vehroute-output.route-length".
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:526
Abstract in-vehicle device.
Definition: MSDevice.h:69
The vehicle has departed (was inserted into the network)
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
virtual bool hasArrived() const =0
Returns whether this vehicle has arrived.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into, unsigned int maxRoutes=INT_MAX)
Build devices for the given vehicle, if needed.
const std::string & getID() const
Returns the name of the vehicle type.
void writeXMLRoute(OutputDevice &os, int index=-1) const
Called on route output.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:87
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:214
static std::map< const SUMOTime, std::map< const std::string, std::string > > myRouteInfos
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:106
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
virtual SUMOReal getArrivalPos() const =0
Returns this vehicle&#39;s desired arrivalPos for its current route (may change on reroute) ...
std::map< const SUMOVehicle *, MSDevice_Vehroutes *, Named::NamedLikeComparatorIdLess< SUMOVehicle > > myDevices
A map for internal notification.
void generateOutput() const
Called on writing tripinfo output.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Does nothing, returns true only if exit times should be collected.
virtual unsigned int getNumberReroutes() const =0
Returns the number of new routes this vehicle got.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
An output device that encapsulates an ofstream.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:75
The vehicle is being teleported.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
const MSRoute * getRoute(int index) const
Called on route retrieval.
const MSEdge * myLastSavedAt
The last edge the exit time was saved for.
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:245
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
~MSDevice_Vehroutes()
Destructor.
Information about a replaced route.