SUMO - Simulation of Urban MObility
MSDevice_Tripinfo.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-2016 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/MSVehicle.h>
40 #include "MSDevice_Tripinfo.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 #define NOT_ARRIVED TIME2STEPS(-1)
47 
48 
49 // ===========================================================================
50 // static members
51 // ===========================================================================
53 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
64 // ---------------------------------------------------------------------------
65 // static initialisation methods
66 // ---------------------------------------------------------------------------
67 void
68 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
69  if (OptionsCont::getOptions().isSet("tripinfo-output") || OptionsCont::getOptions().getBool("duration-log.statistics")) {
70  MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
71  into.push_back(device);
72  myPendingOutput.insert(device);
73  }
74 }
75 
76 
77 // ---------------------------------------------------------------------------
78 // MSDevice_Tripinfo-methods
79 // ---------------------------------------------------------------------------
80 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) :
81  MSDevice(holder, id),
82  myDepartLane(""),
83  myDepartSpeed(-1),
84  myWaitingSteps(0),
86  myArrivalLane(""),
87  myArrivalPos(-1),
88  myArrivalSpeed(-1),
89  myTimeLoss(0) {
90 }
91 
92 
94  // ensure clean up for vaporized vehicles which do not generate output
95  myPendingOutput.erase(this);
96 }
97 
98 
99 bool
101  SUMOReal /*newPos*/, SUMOReal newSpeed) {
102  if (newSpeed <= SUMO_const_haltingSpeed) {
103  myWaitingSteps++;
104  }
105  // @note we are including the speed factor here, thus myTimeLoss can never be
106  // negative. The value is that of a driver who compares his travel time when
107  // the road is clear (which includes speed factor) with the actual travel time.
108  // @todo It might be usefull to recognize a departing vehicle and not
109  // count the time spent accelerating towards time loss since it is unavoidable
110  // (current interfaces do not give access to maximum acceleration)
111  const SUMOReal vmax = MIN2(veh.getMaxSpeed(), veh.getEdge()->getVehicleMaxSpeed(&veh));
112  if (vmax > 0) {
113  myTimeLoss += TIME2STEPS(TS * (vmax - newSpeed) / vmax);
114  }
115  return true;
116 }
117 
118 
119 void
121  // called by meso
122  const SUMOReal vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
123  if (vmax > 0) {
124  myTimeLoss += TIME2STEPS(timeOnLane - (timeOnLane * speed / vmax));
125  }
127 }
128 
129 
130 bool
133  if (!MSGlobals::gUseMesoSim) {
134  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
135  }
136  myDepartSpeed = veh.getSpeed();
137  }
138  return true;
139 }
140 
141 
142 bool
145  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
147  if (!MSGlobals::gUseMesoSim) {
148  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
149  }
150  // @note vehicle may have moved past its arrivalPos during the last step
151  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
152  // However, vaporization may happen anywhere (via TraCI)
155  } else {
157  }
158  myArrivalSpeed = veh.getSpeed();
159  }
160  return true;
161 }
162 
163 void
165  SUMOTime finalTime;
166  SUMOReal finalPos;
167  SUMOReal finalPosOnInternal = 0;
168  if (myArrivalTime == NOT_ARRIVED) {
169  finalTime = MSNet::getInstance()->getCurrentTimeStep();
170  finalPos = myHolder.getPositionOnLane();
171  if (!MSGlobals::gUseMesoSim) {
172  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
173  if (lane->getEdge().isInternal()) {
174  finalPosOnInternal = finalPos;
175  finalPos = myHolder.getEdge()->getLength();
176  }
177  }
178  } else {
179  finalTime = myArrivalTime;
180  finalPos = myArrivalPos;
181  }
182  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
183  routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
184  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
185 
186  duration = finalTime - myHolder.getDeparture();
187 }
188 
189 
190 void
193  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
194  return;
195  }
196  myPendingOutput.erase(this);
197  SUMOReal routeLength;
198  SUMOTime duration;
199  computeLengthAndDuration(routeLength, duration);
200 
201  // write
202  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
203  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
204  os.writeAttr("depart", time2string(myHolder.getDeparture()));
205  os.writeAttr("departLane", myDepartLane);
206  os.writeAttr("departPos", myHolder.getDepartPos());
207  os.writeAttr("departSpeed", myDepartSpeed);
208  os.writeAttr("departDelay", time2string(myHolder.getDepartDelay()));
209  os.writeAttr("arrival", time2string(myArrivalTime));
210  os.writeAttr("arrivalLane", myArrivalLane);
211  os.writeAttr("arrivalPos", myArrivalPos);
212  os.writeAttr("arrivalSpeed", myArrivalSpeed);
213  os.writeAttr("duration", time2string(duration));
214  os.writeAttr("routeLength", routeLength);
215  os.writeAttr("waitSteps", myWaitingSteps);
216  os.writeAttr("timeLoss", time2string(myTimeLoss));
217  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
218  const std::vector<MSDevice*>& devices = myHolder.getDevices();
219  std::ostringstream str;
220  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
221  if (i != devices.begin()) {
222  str << ' ';
223  }
224  str << (*i)->getID();
225  }
226  os.writeAttr("devices", str.str());
227  os.writeAttr("vType", myHolder.getVehicleType().getID());
228  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
229  // cannot close tag because emission device output might follow
230 }
231 
232 
233 void
235  while (myPendingOutput.size() > 0) {
236  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
237  if (d->myHolder.hasDeparted()) {
238  d->generateOutput();
239  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
240  return;
241  }
242  // @todo also generate emission output if holder has a device
243  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
244  } else {
245  myPendingOutput.erase(d);
246  }
247  }
248 }
249 
250 
251 void
253  SUMOReal routeLength;
254  SUMOTime duration;
255  computeLengthAndDuration(routeLength, duration);
256 
257  myVehicleCount++;
258  myTotalRouteLength += routeLength;
259  myTotalDuration += duration;
263 }
264 
265 
266 std::string
268  std::ostringstream msg;
269  msg.setf(msg.fixed);
270  msg.precision(OUTPUT_ACCURACY);
271  msg << "Statistics (avg):\n"
272  << " RouteLength: " << getAvgRouteLength() << "\n"
273  << " Duration: " << getAvgDuration() << "\n"
274  << " WaitingTime: " << getAvgWaitingTime() << "\n"
275  << " TimeLoss: " << getAvgTimeLoss() << "\n"
276  << " DepartDelay: " << getAvgDepartDelay() << "\n";
277  return msg.str();
278 }
279 
280 
281 SUMOReal
283  if (myVehicleCount > 0) {
285  } else {
286  return 0;
287  }
288 }
289 
290 SUMOReal
292  if (myVehicleCount > 0) {
294  } else {
295  return 0;
296  }
297 }
298 
299 SUMOReal
301  if (myVehicleCount > 0) {
303  } else {
304  return 0;
305  }
306 }
307 
308 SUMOReal
310  if (myVehicleCount > 0) {
312  } else {
313  return 0;
314  }
315 }
316 
317 SUMOReal
319  if (myVehicleCount > 0) {
321  } else {
322  return 0;
323  }
324 }
325 
326 
327 /****************************************************************************/
328 
SUMOTime myArrivalTime
The vehicle&#39;s arrival time.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:467
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:43
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
static SUMOTime myTotalWaitingTime
static SUMOReal getAvgDepartDelay()
virtual const MSRoute & getRoute() const =0
Returns the current route.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSDevice.h:153
virtual SUMOReal getDepartPos() const =0
Returns this vehicle&#39;s real departure position.
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
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:59
void generateOutput() const
Called on writing tripinfo output.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
void notifyMoveInternal(SUMOVehicle &veh, SUMOReal timeOnLane, SUMOReal speed)
Internal notification about the vehicle moves.
SUMOTime myTimeLoss
The time loss when compared to the desired and allowed speed.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
SUMOReal myArrivalPos
The position on the lane the vehicle arrived at.
~MSDevice_Tripinfo()
Destructor.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
The vehicle got vaporized.
virtual SUMOReal getMaxSpeed() const =0
Returns the vehicle&#39;s maximum speed.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
#define TS
Definition: SUMOTime.h:52
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Checks for waiting steps when the vehicle moves.
const MSLane * getLane() const
Returns the lane the reminder works on.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
SUMOReal myDepartSpeed
The speed on departure.
SUMOReal myArrivalSpeed
The speed when arriving.
#define OUTPUT_ACCURACY
Definition: config.h:163
#define NOT_ARRIVED
static DeviceSet myPendingOutput
void updateStatistics() const
update tripinfo statistics
static SUMOReal getAvgRouteLength()
accessors for GUINet-Parameters
const std::string & getID() const
Returns the id.
Definition: Named.h:65
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Saves departure info on insertion.
void computeLengthAndDuration(SUMOReal &routeLength, SUMOTime &duration) const
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:562
static SUMOReal getAvgDuration()
static void generateOutputForUnfinished()
generate output for vehicles which are still in the network
static std::string printStatistics()
get statistics for printing to stdout
Representation of a vehicle.
Definition: SUMOVehicle.h:65
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:285
unsigned int myWaitingSteps
The overall number of waiting steps.
static SUMOReal getAvgWaitingTime()
static SUMOTime myTotalDuration
static SUMOReal myTotalRouteLength
The vehicle arrived at its destination (is deleted)
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
T MIN2(T a, T b)
Definition: StdDefs.h:69
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:69
static SUMOReal myVehicleCount
global tripinfo statistics
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:247
std::string myDepartLane
The lane the vehicle departed at.
Abstract in-vehicle device.
Definition: MSDevice.h:69
virtual SUMOTime getDepartDelay() const =0
The vehicle has departed (was inserted into the network)
virtual SUMOReal getSpeed() const =0
Returns the vehicle&#39;s current speed.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:84
std::string myArrivalLane
The lane the vehicle arrived at.
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
MSDevice_Tripinfo()
dummy constructor
static SUMOTime myTotalDepartDelay
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
const std::string & getID() const
Returns the name of the vehicle type.
virtual SUMOTime getWaitingTime() const =0
static SUMOReal getAvgTimeLoss()
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Saves arrival info.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:681
std::set< const MSDevice_Tripinfo *, Named::NamedLikeComparatorIdLess< MSDevice_Tripinfo > > DeviceSet
devices which may still need to produce output
static SUMOTime myTotalTimeLoss
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) ...
static bool gUseMesoSim
Definition: MSGlobals.h:87
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
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.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:78
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.