SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/MSNet.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSEdge.h>
36 #include <microsim/MSRoute.h>
37 #include <microsim/MSVehicleType.h>
41 #include "MSDevice_Vehroutes.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static member variables
50 // ===========================================================================
54 bool MSDevice_Vehroutes::mySorted = false;
58 std::map<const SUMOTime, int> MSDevice_Vehroutes::myDepartureCounts;
59 std::map<const SUMOTime, std::map<const std::string, std::string> > MSDevice_Vehroutes::myRouteInfos;
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 // ---------------------------------------------------------------------------
66 // static initialisation methods
67 // ---------------------------------------------------------------------------
68 void
70  if (OptionsCont::getOptions().isSet("vehroute-output")) {
71  OutputDevice::createDeviceByOption("vehroute-output", "routes", "routes_file.xsd");
72  mySaveExits = OptionsCont::getOptions().getBool("vehroute-output.exit-times");
73  myLastRouteOnly = OptionsCont::getOptions().getBool("vehroute-output.last-route");
74  myDUAStyle = OptionsCont::getOptions().getBool("vehroute-output.dua");
75  mySorted = myDUAStyle || OptionsCont::getOptions().getBool("vehroute-output.sorted");
76  myIntendedDepart = OptionsCont::getOptions().getBool("vehroute-output.intended-depart");
77  myWithTaz = OptionsCont::getOptions().getBool("device.rerouting.with-taz");
79  }
80 }
81 
82 
84 MSDevice_Vehroutes::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into, unsigned int maxRoutes) {
85  if (maxRoutes < INT_MAX) {
86  return new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
87  }
88  if (OptionsCont::getOptions().isSet("vehroute-output")) {
89  if (myLastRouteOnly) {
90  maxRoutes = 0;
91  }
92  myStateListener.myDevices[&v] = new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
93  into.push_back(myStateListener.myDevices[&v]);
94  return myStateListener.myDevices[&v];
95  }
96  return 0;
97 }
98 
99 
100 // ---------------------------------------------------------------------------
101 // MSDevice_Vehroutes::StateListener-methods
102 // ---------------------------------------------------------------------------
103 void
105  if (to == MSNet::VEHICLE_STATE_NEWROUTE) {
106  myDevices[vehicle]->addRoute();
107  }
108 }
109 
110 
111 // ---------------------------------------------------------------------------
112 // MSDevice_Vehroutes-methods
113 // ---------------------------------------------------------------------------
114 MSDevice_Vehroutes::MSDevice_Vehroutes(SUMOVehicle& holder, const std::string& id, unsigned int maxRoutes)
115  : MSDevice(holder, id), myCurrentRoute(&holder.getRoute()), myMaxRoutes(maxRoutes), myLastSavedAt(0) {
117 }
118 
119 
121  for (std::vector<RouteReplaceInfo>::iterator i = myReplacedRoutes.begin(); i != myReplacedRoutes.end(); ++i) {
122  (*i).route->release();
123  }
126 }
127 
128 
129 bool
131  if (mySorted && reason == NOTIFICATION_DEPARTED && myStateListener.myDevices[&veh] == this) {
133  myDepartureCounts[departure]++;
134  }
135  return mySaveExits;
136 }
137 
138 
139 bool
141  if (mySaveExits && reason != NOTIFICATION_LANE_CHANGE) {
142  if (reason != NOTIFICATION_TELEPORT && myLastSavedAt == veh.getEdge()) { // need to check this for internal lanes
144  } else if (myLastSavedAt != veh.getEdge()) {
145  myExits.push_back(MSNet::getInstance()->getCurrentTimeStep());
146  myLastSavedAt = veh.getEdge();
147  }
148  }
149  return mySaveExits;
150 }
151 
152 
153 void
155  // check if a previous route shall be written
157  if (index >= 0) {
158  assert((int) myReplacedRoutes.size() > index);
159  // write edge on which the vehicle was when the route was valid
160  os << " replacedOnEdge=\"";
161  if (myReplacedRoutes[index].edge) {
162  os << myReplacedRoutes[index].edge->getID();
163  }
164  // write the time at which the route was replaced
165  os << "\" replacedAtTime=\"" << time2string(myReplacedRoutes[index].time) << "\" probability=\"0\" edges=\"";
166  // get the route
167  int i = index;
168  while (i > 0 && myReplacedRoutes[i - 1].edge) {
169  i--;
170  }
171  const MSEdge* lastEdge = 0;
172  for (; i < index; ++i) {
173  myReplacedRoutes[i].route->writeEdgeIDs(os, lastEdge, myReplacedRoutes[i].edge);
174  lastEdge = myReplacedRoutes[i].edge;
175  }
176  myReplacedRoutes[index].route->writeEdgeIDs(os, lastEdge);
177  } else {
178  os << " edges=\"";
179  const MSEdge* lastEdge = 0;
180  int numWritten = 0;
181  if (myHolder.getNumberReroutes() > 0) {
182  assert(myReplacedRoutes.size() <= myHolder.getNumberReroutes());
183  unsigned int i = static_cast<unsigned int>(myReplacedRoutes.size());
184  while (i > 0 && myReplacedRoutes[i - 1].edge) {
185  i--;
186  }
187  for (; i < myReplacedRoutes.size(); ++i) {
188  numWritten += myReplacedRoutes[i].route->writeEdgeIDs(os, lastEdge, myReplacedRoutes[i].edge);
189  lastEdge = myReplacedRoutes[i].edge;
190  }
191  }
192  const MSEdge* upTo = 0;
193  if (mySaveExits) {
194  int remainingWithExitTime = (int)myExits.size() - numWritten;
195  assert(remainingWithExitTime >= 0);
196  assert(remainingWithExitTime <= (int)myCurrentRoute->size());
197  if (remainingWithExitTime < (int)myCurrentRoute->size()) {
198  upTo = *(myCurrentRoute->begin() + remainingWithExitTime);
199  }
200  }
201  myCurrentRoute->writeEdgeIDs(os, lastEdge, upTo);
202  if (mySaveExits) {
203  os << "\" exitTimes=\"";
204  for (std::vector<SUMOTime>::const_iterator it = myExits.begin(); it != myExits.end(); ++it) {
205  if (it != myExits.begin()) {
206  os << " ";
207  }
208  os << time2string(*it);
209  }
210  }
211  }
212  (os << "\"").closeTag();
213 }
214 
215 
216 void
218  OutputDevice& routeOut = OutputDevice::getDeviceByOption("vehroute-output");
219  OutputDevice_String od(routeOut.isBinary(), 1);
221  od.openTag(SUMO_TAG_VEHICLE).writeAttr(SUMO_ATTR_ID, myHolder.getID());
223  od.writeAttr(SUMO_ATTR_TYPE, myHolder.getVehicleType().getID());
224  }
225  od.writeAttr(SUMO_ATTR_DEPART, time2string(departure));
226  if (myHolder.hasArrived()) {
227  od.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
228  }
229  if (myWithTaz) {
231  }
232  if (myDUAStyle) {
234  if (routeDist != 0) {
235  const std::vector<const MSRoute*>& routes = routeDist->getVals();
236  unsigned index = 0;
237  while (index < routes.size() && routes[index] != myCurrentRoute) {
238  ++index;
239  }
240  od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, index);
241  const std::vector<SUMOReal>& probs = routeDist->getProbs();
242  for (unsigned int i = 0; i < routes.size(); ++i) {
243  od.setPrecision();
244  od.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_COST, routes[i]->getCosts());
245  od.setPrecision(8);
246  od.writeAttr(SUMO_ATTR_PROB, probs[i]);
247  od.setPrecision();
248  od << " edges=\"";
249  routes[i]->writeEdgeIDs(od, *routes[i]->begin());
250  (od << "\"").closeTag();
251  }
252  od.closeTag();
253  } else {
254  writeXMLRoute(od);
255  }
256  } else {
257  if (myReplacedRoutes.size() > 0) {
258  od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION);
259  for (unsigned int i = 0; i < myReplacedRoutes.size(); ++i) {
260  writeXMLRoute(od, i);
261  }
262  }
263  writeXMLRoute(od);
264  if (myReplacedRoutes.size() > 0) {
265  od.closeTag();
266  }
267  }
268  od.closeTag();
269  od.lf();
270  if (mySorted) {
271  myRouteInfos[departure][myHolder.getID()] = od.getString();
272  myDepartureCounts[departure]--;
273  std::map<const SUMOTime, int>::iterator it = myDepartureCounts.begin();
274  while (it != myDepartureCounts.end() && it->second == 0) {
275  std::map<const std::string, std::string>& infos = myRouteInfos[it->first];
276  for (std::map<const std::string, std::string>::const_iterator it2 = infos.begin(); it2 != infos.end(); ++it2) {
277  routeOut << it2->second;
278  }
279  myRouteInfos.erase(it->first);
280  myDepartureCounts.erase(it);
281  it = myDepartureCounts.begin();
282  }
283  } else {
284  routeOut << od.getString();
285  }
286 }
287 
288 
289 const MSRoute*
291  return myReplacedRoutes[index].route;
292 }
293 
294 
295 void
297  if (myMaxRoutes > 0) {
298  if (myHolder.hasDeparted()) {
300  } else {
301  myReplacedRoutes.push_back(RouteReplaceInfo(0, MSNet::getInstance()->getCurrentTimeStep(), myCurrentRoute));
302  }
303  if (myReplacedRoutes.size() > myMaxRoutes) {
304  myReplacedRoutes.front().route->release();
305  myReplacedRoutes.erase(myReplacedRoutes.begin());
306  }
307  } else {
309  }
312 }
313 
314 
315 void
317  for (std::map<const SUMOVehicle*, MSDevice_Vehroutes*, Named::NamedLikeComparatorIdLess<SUMOVehicle> >::const_iterator it = myStateListener.myDevices.begin();
318  it != myStateListener.myDevices.end(); ++it) {
319  if (it->first->hasDeparted()) {
320  it->second->generateOutput();
321  }
322  }
323 }
324 
325 
326 /****************************************************************************/
327 
static std::map< const SUMOTime, int > myDepartureCounts
Map needed to sort vehicles by departure time.
static bool myWithTaz
A shortcut for the Option "device.routing.with-taz".
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:82
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".
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:159
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:235
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:715
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:67
std::string toTaz
The vehicle'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
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:495
void addRoute()
Called on route change.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
std::vector< RouteReplaceInfo > myReplacedRoutes
Prior routes.
SUMOTime depart
The vehicle's departure time.
std::string fromTaz
The vehicle's origin zone (district)
A class that is notified about reroutings.
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
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:483
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'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'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) ...
int SUMOTime
Definition: SUMOTime.h:43
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:218
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
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'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's type.
~MSDevice_Vehroutes()
Destructor.
Information about a replaced route.