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  myDepartPosLat(0),
85  myWaitingTime(0),
87  myArrivalLane(""),
88  myArrivalPos(-1),
89  myArrivalPosLat(0),
90  myArrivalSpeed(-1),
91  myTimeLoss(0) {
92 }
93 
94 
96  // ensure clean up for vaporized vehicles which do not generate output
97  myPendingOutput.erase(this);
98 }
99 
100 
101 bool
103  SUMOReal /*newPos*/, SUMOReal newSpeed) {
104  if (veh.isStopped()) {
105  return true;
106  }
107  if (newSpeed <= SUMO_const_haltingSpeed) {
109  }
110  // @note we are including the speed factor here, thus myTimeLoss can never be
111  // negative. The value is that of a driver who compares his travel time when
112  // the road is clear (which includes speed factor) with the actual travel time.
113  // @todo It might be useful to recognize a departing vehicle and not
114  // count the time spent accelerating towards time loss since it is unavoidable
115  // (current interfaces do not give access to maximum acceleration)
116  const SUMOReal vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
117  if (vmax > 0) {
118  myTimeLoss += TIME2STEPS(TS * (vmax - newSpeed) / vmax);
119  }
120  return true;
121 }
122 
123 void
125  const SUMOReal /* frontOnLane */,
126  const SUMOReal timeOnLane,
127  const SUMOReal /* meanSpeedFrontOnLane */,
128  const SUMOReal meanSpeedVehicleOnLane,
129  const SUMOReal /* travelledDistanceFrontOnLane */,
130  const SUMOReal /* travelledDistanceVehicleOnLane */) {
131 
132  // called by meso
133  const SUMOReal vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
134  if (vmax > 0) {
135  myTimeLoss += TIME2STEPS(timeOnLane * (vmax - meanSpeedVehicleOnLane) / vmax);
136  }
137  myWaitingTime += veh.getWaitingTime();
138 }
139 
140 bool
143  if (!MSGlobals::gUseMesoSim) {
144  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
145  myDepartPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
146  }
147  myDepartSpeed = veh.getSpeed();
148  }
149  return true;
150 }
151 
152 
153 bool
156  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
158  if (!MSGlobals::gUseMesoSim) {
159  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
160  myArrivalPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
161  }
162  // @note vehicle may have moved past its arrivalPos during the last step
163  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
164  // However, vaporization may happen anywhere (via TraCI)
167  } else {
169  }
170  myArrivalSpeed = veh.getSpeed();
171  }
172  return true;
173 }
174 
175 void
177  SUMOTime finalTime;
178  SUMOReal finalPos;
179  SUMOReal finalPosOnInternal = 0;
180  if (myArrivalTime == NOT_ARRIVED) {
181  finalTime = MSNet::getInstance()->getCurrentTimeStep();
182  finalPos = myHolder.getPositionOnLane();
183  if (!MSGlobals::gUseMesoSim) {
184  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
185  if (lane->getEdge().isInternal()) {
186  finalPosOnInternal = finalPos;
187  finalPos = myHolder.getEdge()->getLength();
188  }
189  }
190  } else {
191  finalTime = myArrivalTime;
192  finalPos = myArrivalPos;
193  }
194  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
195  routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
196  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
197 
198  duration = finalTime - myHolder.getDeparture();
199 }
200 
201 
202 void
205  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
206  return;
207  }
208  myPendingOutput.erase(this);
209  SUMOReal routeLength;
210  SUMOTime duration;
211  computeLengthAndDuration(routeLength, duration);
212 
213  // write
214  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
215  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
216  os.writeAttr("depart", time2string(myHolder.getDeparture()));
217  os.writeAttr("departLane", myDepartLane);
218  os.writeAttr("departPos", myHolder.getDepartPos());
220  os.writeAttr("departPosLat", myDepartPosLat);
221  }
222  os.writeAttr("departSpeed", myDepartSpeed);
223  os.writeAttr("departDelay", time2string(myHolder.getDepartDelay()));
224  os.writeAttr("arrival", time2string(myArrivalTime));
225  os.writeAttr("arrivalLane", myArrivalLane);
226  os.writeAttr("arrivalPos", myArrivalPos);
228  os.writeAttr("arrivalPosLat", myArrivalPosLat);
229  }
230  os.writeAttr("arrivalSpeed", myArrivalSpeed);
231  os.writeAttr("duration", time2string(duration));
232  os.writeAttr("routeLength", routeLength);
233  os.writeAttr("waitSteps", myWaitingTime / DELTA_T);
234  os.writeAttr("timeLoss", time2string(myTimeLoss));
235  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
236  const std::vector<MSDevice*>& devices = myHolder.getDevices();
237  std::ostringstream str;
238  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
239  if (i != devices.begin()) {
240  str << ' ';
241  }
242  str << (*i)->getID();
243  }
244  os.writeAttr("devices", str.str());
245  os.writeAttr("vType", myHolder.getVehicleType().getID());
246  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
247  // cannot close tag because emission device output might follow
248 }
249 
250 
251 void
253  while (myPendingOutput.size() > 0) {
254  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
255  if (d->myHolder.hasDeparted()) {
256  d->generateOutput();
257  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
258  return;
259  }
260  // @todo also generate emission output if holder has a device
261  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
262  } else {
263  myPendingOutput.erase(d);
264  }
265  }
266 }
267 
268 
269 void
271  SUMOReal routeLength;
272  SUMOTime duration;
273  computeLengthAndDuration(routeLength, duration);
274 
275  myVehicleCount++;
276  myTotalRouteLength += routeLength;
277  myTotalDuration += duration;
281 }
282 
283 
284 std::string
286  std::ostringstream msg;
287  msg.setf(msg.fixed);
288  msg.precision(OUTPUT_ACCURACY);
289  msg << "Statistics (avg):\n"
290  << " RouteLength: " << getAvgRouteLength() << "\n"
291  << " Duration: " << getAvgDuration() << "\n"
292  << " WaitingTime: " << getAvgWaitingTime() << "\n"
293  << " TimeLoss: " << getAvgTimeLoss() << "\n"
294  << " DepartDelay: " << getAvgDepartDelay() << "\n";
295  return msg.str();
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 SUMOReal
328  if (myVehicleCount > 0) {
330  } else {
331  return 0;
332  }
333 }
334 
335 SUMOReal
337  if (myVehicleCount > 0) {
339  } else {
340  return 0;
341  }
342 }
343 
344 
345 /****************************************************************************/
346 
const MSLane * getLane() const
Returns the lane the reminder works on.
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:571
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:82
long long int SUMOTime
Definition: SUMOTime.h:43
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
static SUMOTime myTotalWaitingTime
void computeLengthAndDuration(SUMOReal &routeLength, SUMOTime &duration) const
static SUMOReal getAvgDepartDelay()
virtual const MSRoute & getRoute() const =0
Returns the current route.
void notifyMoveInternal(const SUMOVehicle &veh, const SUMOReal frontOnLane, const SUMOReal timeOnLane, const SUMOReal meanSpeedFrontOnLane, const SUMOReal meanSpeedVehicleOnLane, const SUMOReal travelledDistanceFrontOnLane, const SUMOReal travelledDistanceVehicleOnLane)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal() ...
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.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Notification
Definition of a vehicle state.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
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.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
#define TS
Definition: SUMOTime.h:52
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Checks for waiting steps when the vehicle moves.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
SUMOReal myDepartSpeed
The speed on departure.
SUMOReal myArrivalSpeed
The speed when arriving.
SUMOTime myWaitingTime
The overall waiting time.
#define OUTPUT_ACCURACY
Definition: config.h:163
SUMOReal myArrivalPosLat
The lateral position on the lane the vehicle arrived at.
#define NOT_ARRIVED
static DeviceSet myPendingOutput
static SUMOReal getAvgRouteLength()
accessors for GUINet-Parameters
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
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Saves departure info on insertion.
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:776
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
static SUMOReal getAvgDuration()
static void generateOutputForUnfinished()
generate output for vehicles which are still in the network
SUMOReal myDepartPosLat
The lateral depart position.
static std::string printStatistics()
get statistics for printing to stdout
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
bool hasInternalLinks() const
return whether the network contains internal links
Definition: MSNet.h:639
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:73
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.
std::string myArrivalLane
The lane the vehicle arrived at.
void updateStatistics() const
update tripinfo statistics
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
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:591
static SUMOReal gLateralResolution
Definition: MSGlobals.h:89
const std::string & getID() const
Returns the name of the vehicle type.
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
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
std::set< const MSDevice_Tripinfo *, Named::NamedLikeComparatorIdLess< MSDevice_Tripinfo > > DeviceSet
devices which may still need to produce output
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:78
void generateOutput() const
Called on writing tripinfo 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:95
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:84
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.