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-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/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  myDepartPos(-1),
84  myDepartSpeed(-1),
85  myWaitingSteps(0),
87  myArrivalLane(""),
88  myArrivalPos(-1),
89  myArrivalSpeed(-1),
90  myTimeLoss(0) {
91 }
92 
93 
95  // ensure clean up for vaporized vehicles which do not generate output
96  myPendingOutput.erase(this);
97 }
98 
99 
100 bool
102  SUMOReal /*newPos*/, SUMOReal newSpeed) {
103  if (newSpeed <= SUMO_const_haltingSpeed) {
104  myWaitingSteps++;
105  }
106  // @note we are including the speed factor here, thus myTimeLoss can never be
107  // negative. The value is that of a driver who compares his travel time when
108  // the road is clear (which includes speed factor) with the actual travel time.
109  // @todo It might be usefull to recognize a departing vehicle and not
110  // count the time spent accelerating towards time loss since it is unavoidable
111  // (current interfaces do not give access to maximum acceleration)
112  const SUMOReal vmax = MIN2(veh.getMaxSpeed(), veh.getEdge()->getVehicleMaxSpeed(&veh));
113  if (vmax > 0) {
114  myTimeLoss += TIME2STEPS(TS * (vmax - newSpeed) / vmax);
115  }
116  return true;
117 }
118 
119 
120 bool
123  if (!MSGlobals::gUseMesoSim) {
124  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
125  }
127  myDepartSpeed = veh.getSpeed();
128  }
129  return true;
130 }
131 
132 
133 bool
136  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
138  if (!MSGlobals::gUseMesoSim) {
139  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
140  }
141  // @note vehicle may have moved past its arrivalPos during the last step
142  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
143  // However, vaporization may happen anywhere (via TraCI)
146  } else {
148  }
149  myArrivalSpeed = veh.getSpeed();
150  }
151  return true;
152 }
153 
154 void
156  SUMOTime finalTime;
157  SUMOReal finalPos;
158  SUMOReal finalPosOnInternal = 0;
159  if (myArrivalTime == NOT_ARRIVED) {
160  finalTime = MSNet::getInstance()->getCurrentTimeStep();
161  finalPos = myHolder.getPositionOnLane();
162  if (!MSGlobals::gUseMesoSim) {
163  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
164  if (lane->getEdge().isInternal()) {
165  finalPosOnInternal = finalPos;
166  finalPos = myHolder.getEdge()->getLength();
167  }
168  }
169  } else {
170  finalTime = myArrivalTime;
171  finalPos = myArrivalPos;
172  }
173  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
174  routeLength = myHolder.getRoute().getDistanceBetween(myDepartPos, finalPos,
175  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
176 
177  duration = finalTime - myHolder.getDeparture();
178 }
179 
180 
181 void
184  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
185  return;
186  }
187  myPendingOutput.erase(this);
188  SUMOReal routeLength;
189  SUMOTime duration;
190  computeLengthAndDuration(routeLength, duration);
191 
192  // write
193  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
194  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
195  os.writeAttr("depart", time2string(myHolder.getDeparture()));
196  os.writeAttr("departLane", myDepartLane);
197  os.writeAttr("departPos", myDepartPos);
198  os.writeAttr("departSpeed", myDepartSpeed);
199  os.writeAttr("departDelay", time2string(myHolder.getDepartDelay()));
200  os.writeAttr("arrival", time2string(myArrivalTime));
201  os.writeAttr("arrivalLane", myArrivalLane);
202  os.writeAttr("arrivalPos", myArrivalPos);
203  os.writeAttr("arrivalSpeed", myArrivalSpeed);
204  os.writeAttr("duration", time2string(duration));
205  os.writeAttr("routeLength", routeLength);
206  os.writeAttr("waitSteps", myWaitingSteps);
207  os.writeAttr("timeLoss", time2string(myTimeLoss));
208  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
209  const std::vector<MSDevice*>& devices = myHolder.getDevices();
210  std::ostringstream str;
211  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
212  if (i != devices.begin()) {
213  str << ' ';
214  }
215  str << (*i)->getID();
216  }
217  os.writeAttr("devices", str.str());
218  os.writeAttr("vType", myHolder.getVehicleType().getID());
219  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
220  // cannot close tag because emission device output might follow
221 }
222 
223 
224 void
226  while (myPendingOutput.size() > 0) {
227  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
228  if (d->myHolder.hasDeparted()) {
229  d->generateOutput();
230  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
231  return;
232  }
233  // @todo also generate emission output if holder has a device
234  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
235  } else {
236  myPendingOutput.erase(d);
237  }
238  }
239 }
240 
241 
242 void
244  SUMOReal routeLength;
245  SUMOTime duration;
246  computeLengthAndDuration(routeLength, duration);
247 
248  myVehicleCount++;
249  myTotalRouteLength += routeLength;
250  myTotalDuration += duration;
254 }
255 
256 
257 std::string
259  std::ostringstream msg;
260  msg.setf(msg.fixed);
261  msg.precision(OUTPUT_ACCURACY);
262  msg << "Statistics (avg):\n"
263  << " RouteLength: " << getAvgRouteLength() << "\n"
264  << " Duration: " << getAvgDuration() << "\n"
265  << " WaitingTime: " << getAvgWaitingTime() << "\n"
266  << " TimeLoss: " << getAvgTimeLoss() << "\n"
267  << " DepartDelay: " << getAvgDepartDelay() << "\n";
268  return msg.str();
269 }
270 
271 
272 SUMOReal
274  if (myVehicleCount > 0) {
276  } else {
277  return 0;
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 
318 /****************************************************************************/
319 
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:461
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 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:61
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.
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:162
SUMOReal myArrivalPos
The position on the lane the vehicle arrived at.
~MSDevice_Tripinfo()
Destructor.
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:164
#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:535
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:255
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
SUMOReal myDepartPos
The position on the lane the vehicle departed at.
T MIN2(T a, T b)
Definition: StdDefs.h:73
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:71
static SUMOReal myVehicleCount
global tripinfo statistics
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:240
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:81
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.
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:214
static const bool gUseMesoSim
Definition: MSGlobals.h:102
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:689
#define DELTA_T
Definition: SUMOTime.h:50
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) ...
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:75
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.