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 (veh.isStopped()) {
103  return true;
104  }
105  if (newSpeed <= SUMO_const_haltingSpeed) {
106  myWaitingSteps++;
107  }
108  // @note we are including the speed factor here, thus myTimeLoss can never be
109  // negative. The value is that of a driver who compares his travel time when
110  // the road is clear (which includes speed factor) with the actual travel time.
111  // @todo It might be usefull to recognize a departing vehicle and not
112  // count the time spent accelerating towards time loss since it is unavoidable
113  // (current interfaces do not give access to maximum acceleration)
114  const SUMOReal vmax = MIN2(veh.getMaxSpeed(), veh.getEdge()->getVehicleMaxSpeed(&veh));
115  if (vmax > 0) {
116  myTimeLoss += TIME2STEPS(TS * (vmax - newSpeed) / vmax);
117  }
118  return true;
119 }
120 
121 
122 void
124  // called by meso
125  const SUMOReal vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
126  if (vmax > 0) {
127  myTimeLoss += TIME2STEPS(timeOnLane - (timeOnLane * speed / vmax));
128  }
130 }
131 
132 
133 bool
136  if (!MSGlobals::gUseMesoSim) {
137  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
138  }
139  myDepartSpeed = veh.getSpeed();
140  }
141  return true;
142 }
143 
144 
145 bool
148  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
150  if (!MSGlobals::gUseMesoSim) {
151  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
152  }
153  // @note vehicle may have moved past its arrivalPos during the last step
154  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
155  // However, vaporization may happen anywhere (via TraCI)
158  } else {
160  }
161  myArrivalSpeed = veh.getSpeed();
162  }
163  return true;
164 }
165 
166 void
168  SUMOTime finalTime;
169  SUMOReal finalPos;
170  SUMOReal finalPosOnInternal = 0;
171  if (myArrivalTime == NOT_ARRIVED) {
172  finalTime = MSNet::getInstance()->getCurrentTimeStep();
173  finalPos = myHolder.getPositionOnLane();
174  if (!MSGlobals::gUseMesoSim) {
175  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
176  if (lane->getEdge().isInternal()) {
177  finalPosOnInternal = finalPos;
178  finalPos = myHolder.getEdge()->getLength();
179  }
180  }
181  } else {
182  finalTime = myArrivalTime;
183  finalPos = myArrivalPos;
184  }
185  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
186  routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
187  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
188 
189  duration = finalTime - myHolder.getDeparture();
190 }
191 
192 
193 void
196  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
197  return;
198  }
199  myPendingOutput.erase(this);
200  SUMOReal routeLength;
201  SUMOTime duration;
202  computeLengthAndDuration(routeLength, duration);
203 
204  // write
205  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
206  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
207  os.writeAttr("depart", time2string(myHolder.getDeparture()));
208  os.writeAttr("departLane", myDepartLane);
209  os.writeAttr("departPos", myHolder.getDepartPos());
210  os.writeAttr("departSpeed", myDepartSpeed);
211  os.writeAttr("departDelay", time2string(myHolder.getDepartDelay()));
212  os.writeAttr("arrival", time2string(myArrivalTime));
213  os.writeAttr("arrivalLane", myArrivalLane);
214  os.writeAttr("arrivalPos", myArrivalPos);
215  os.writeAttr("arrivalSpeed", myArrivalSpeed);
216  os.writeAttr("duration", time2string(duration));
217  os.writeAttr("routeLength", routeLength);
218  os.writeAttr("waitSteps", myWaitingSteps);
219  os.writeAttr("timeLoss", time2string(myTimeLoss));
220  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
221  const std::vector<MSDevice*>& devices = myHolder.getDevices();
222  std::ostringstream str;
223  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
224  if (i != devices.begin()) {
225  str << ' ';
226  }
227  str << (*i)->getID();
228  }
229  os.writeAttr("devices", str.str());
230  os.writeAttr("vType", myHolder.getVehicleType().getID());
231  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
232  // cannot close tag because emission device output might follow
233 }
234 
235 
236 void
238  while (myPendingOutput.size() > 0) {
239  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
240  if (d->myHolder.hasDeparted()) {
241  d->generateOutput();
242  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
243  return;
244  }
245  // @todo also generate emission output if holder has a device
246  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
247  } else {
248  myPendingOutput.erase(d);
249  }
250  }
251 }
252 
253 
254 void
256  SUMOReal routeLength;
257  SUMOTime duration;
258  computeLengthAndDuration(routeLength, duration);
259 
260  myVehicleCount++;
261  myTotalRouteLength += routeLength;
262  myTotalDuration += duration;
266 }
267 
268 
269 std::string
271  std::ostringstream msg;
272  msg.setf(msg.fixed);
273  msg.precision(OUTPUT_ACCURACY);
274  msg << "Statistics (avg):\n"
275  << " RouteLength: " << getAvgRouteLength() << "\n"
276  << " Duration: " << getAvgDuration() << "\n"
277  << " WaitingTime: " << getAvgWaitingTime() << "\n"
278  << " TimeLoss: " << getAvgTimeLoss() << "\n"
279  << " DepartDelay: " << getAvgDepartDelay() << "\n";
280  return msg.str();
281 }
282 
283 
284 SUMOReal
286  if (myVehicleCount > 0) {
288  } else {
289  return 0;
290  }
291 }
292 
293 SUMOReal
295  if (myVehicleCount > 0) {
297  } else {
298  return 0;
299  }
300 }
301 
302 SUMOReal
304  if (myVehicleCount > 0) {
306  } else {
307  return 0;
308  }
309 }
310 
311 SUMOReal
313  if (myVehicleCount > 0) {
315  } else {
316  return 0;
317  }
318 }
319 
320 SUMOReal
322  if (myVehicleCount > 0) {
324  } else {
325  return 0;
326  }
327 }
328 
329 
330 /****************************************************************************/
331 
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:567
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:81
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:158
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:639
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:159
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:254
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:66
int myWaitingSteps
The overall number of waiting steps.
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:585
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:66
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
virtual int getNumberReroutes() const =0
Returns the number of new routes this vehicle got.
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:254
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()
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
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:769
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:90
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
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.