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-2017 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>
41 #include "MSDevice_Tripinfo.h"
42 
43 #define NOT_ARRIVED TIME2STEPS(-1)
44 
45 
46 // ===========================================================================
47 // static members
48 // ===========================================================================
50 
57 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 // ---------------------------------------------------------------------------
66 // static initialisation methods
67 // ---------------------------------------------------------------------------
68 void
69 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
70  if (OptionsCont::getOptions().isSet("tripinfo-output") || OptionsCont::getOptions().getBool("duration-log.statistics")) {
71  MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
72  into.push_back(device);
73  myPendingOutput.insert(device);
74  }
75 }
76 
77 
78 // ---------------------------------------------------------------------------
79 // MSDevice_Tripinfo-methods
80 // ---------------------------------------------------------------------------
81 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) :
82  MSDevice(holder, id),
83  myDepartLane(""),
84  myDepartSpeed(-1),
85  myDepartPosLat(0),
86  myWaitingTime(0),
88  myArrivalLane(""),
89  myArrivalPos(-1),
90  myArrivalPosLat(0),
91  myArrivalSpeed(-1),
92  myMesoTimeLoss(0) {
93 }
94 
95 
97  // ensure clean up for vaporized vehicles which do not generate output
98  myPendingOutput.erase(this);
99 }
100 
101 
102 bool
104  double /*newPos*/, double newSpeed) {
105  if (veh.isStopped()) {
106  return true;
107  }
108  if (newSpeed <= SUMO_const_haltingSpeed) {
110  }
111  return true;
112 }
113 
114 void
116  const double /* frontOnLane */,
117  const double timeOnLane,
118  const double /* meanSpeedFrontOnLane */,
119  const double meanSpeedVehicleOnLane,
120  const double /* travelledDistanceFrontOnLane */,
121  const double /* travelledDistanceVehicleOnLane */,
122  const double /* meanLengthOnLane */) {
123 
124  // called by meso
125  const double vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
126  if (vmax > 0) {
127  myMesoTimeLoss += TIME2STEPS(timeOnLane * (vmax - meanSpeedVehicleOnLane) / vmax);
128  }
129  myWaitingTime += veh.getWaitingTime();
130 }
131 
132 bool
135  if (!MSGlobals::gUseMesoSim) {
136  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
137  myDepartPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
138  }
139  myDepartSpeed = veh.getSpeed();
140  }
141  return true;
142 }
143 
144 
145 bool
147  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
148  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
150  if (!MSGlobals::gUseMesoSim) {
151  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
152  myArrivalPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
153  }
154  // @note vehicle may have moved past its arrivalPos during the last step
155  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
156  // However, vaporization may happen anywhere (via TraCI)
159  } else {
161  }
162  myArrivalSpeed = veh.getSpeed();
163  }
164  return true;
165 }
166 
167 void
168 MSDevice_Tripinfo::computeLengthAndDuration(double& routeLength, SUMOTime& duration) const {
169  SUMOTime finalTime;
170  double finalPos;
171  double finalPosOnInternal = 0;
172  if (myArrivalTime == NOT_ARRIVED) {
173  finalTime = MSNet::getInstance()->getCurrentTimeStep();
174  finalPos = myHolder.getPositionOnLane();
175  if (!MSGlobals::gUseMesoSim) {
176  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
177  if (lane->getEdge().isInternal()) {
178  finalPosOnInternal = finalPos;
179  finalPos = myHolder.getEdge()->getLength();
180  }
181  }
182  } else {
183  finalTime = myArrivalTime;
184  finalPos = myArrivalPos;
185  }
186  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
187  routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
188  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
189 
190  duration = finalTime - myHolder.getDeparture();
191 }
192 
193 
194 void
196  const SUMOTime timeLoss = MSGlobals::gUseMesoSim ? myMesoTimeLoss : static_cast<MSVehicle&>(myHolder).getTimeLoss();
197  updateStatistics(timeLoss);
198  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
199  return;
200  }
201  myPendingOutput.erase(this);
202  double routeLength;
203  SUMOTime duration;
204  computeLengthAndDuration(routeLength, duration);
205 
206  // write
207  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
208  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
209  os.writeAttr("depart", time2string(myHolder.getDeparture()));
210  os.writeAttr("departLane", myDepartLane);
211  os.writeAttr("departPos", myHolder.getDepartPos());
213  os.writeAttr("departPosLat", myDepartPosLat);
214  }
215  os.writeAttr("departSpeed", myDepartSpeed);
216  os.writeAttr("departDelay", time2string(myHolder.getDepartDelay()));
217  os.writeAttr("arrival", time2string(myArrivalTime));
218  os.writeAttr("arrivalLane", myArrivalLane);
219  os.writeAttr("arrivalPos", myArrivalPos);
221  os.writeAttr("arrivalPosLat", myArrivalPosLat);
222  }
223  os.writeAttr("arrivalSpeed", myArrivalSpeed);
224  os.writeAttr("duration", time2string(duration));
225  os.writeAttr("routeLength", routeLength);
226  os.writeAttr("waitSteps", myWaitingTime / DELTA_T);
227  os.writeAttr("timeLoss", time2string(timeLoss));
228  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
229  const std::vector<MSDevice*>& devices = myHolder.getDevices();
230  std::ostringstream str;
231  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
232  if (i != devices.begin()) {
233  str << ' ';
234  }
235  str << (*i)->getID();
236  }
237  os.writeAttr("devices", str.str());
238  os.writeAttr("vType", myHolder.getVehicleType().getID());
239  os.writeAttr("speedFactor", myHolder.getChosenSpeedFactor());
240  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
241  // cannot close tag because emission device output might follow
242 }
243 
244 
245 void
247  while (myPendingOutput.size() > 0) {
248  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
249  if (d->myHolder.hasDeparted()) {
250  d->generateOutput();
251  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
252  return;
253  }
254  // @todo also generate emission output if holder has a device
255  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
256  } else {
257  myPendingOutput.erase(d);
258  }
259  }
260 }
261 
262 
263 void
265  double routeLength;
266  SUMOTime duration;
267  computeLengthAndDuration(routeLength, duration);
268 
269  myVehicleCount++;
270  myTotalRouteLength += routeLength;
271  myTotalDuration += duration;
273  myTotalTimeLoss += timeLoss;
275 }
276 
277 
278 void
279 MSDevice_Tripinfo::addPedestrianData(double walkLength, SUMOTime walkDuration) {
280  myWalkCount++;
281  myTotalWalkRouteLength += walkLength;
282  myTotalWalkDuration += walkDuration;
283 }
284 
285 std::string
287  std::ostringstream msg;
288  msg.setf(msg.fixed);
289  msg.precision(gPrecision);
290  msg << "Statistics (avg):\n"
291  << " RouteLength: " << getAvgRouteLength() << "\n"
292  << " Duration: " << getAvgDuration() << "\n"
293  << " WaitingTime: " << getAvgWaitingTime() << "\n"
294  << " TimeLoss: " << getAvgTimeLoss() << "\n"
295  << " DepartDelay: " << getAvgDepartDelay() << "\n";
296  if (myWalkCount > 0) {
297  msg << "Pedestrian Statistics (avg of " << myWalkCount << " walks):\n"
298  << " RouteLength: " << getAvgWalkRouteLength() << "\n"
299  << " Duration: " << getAvgWalkDuration() << "\n";
300  }
301  return msg.str();
302 }
303 
304 
305 double
307  if (myVehicleCount > 0) {
309  } else {
310  return 0;
311  }
312 }
313 
314 double
316  if (myVehicleCount > 0) {
318  } else {
319  return 0;
320  }
321 }
322 
323 double
325  if (myVehicleCount > 0) {
327  } else {
328  return 0;
329  }
330 }
331 
332 
333 double
335  if (myVehicleCount > 0) {
337  } else {
338  return 0;
339  }
340 }
341 
342 
343 double
345  if (myVehicleCount > 0) {
347  } else {
348  return 0;
349  }
350 }
351 
352 
353 double
355  if (myWalkCount > 0) {
357  } else {
358  return 0;
359  }
360 }
361 
362 double
364  if (myWalkCount > 0) {
366  } else {
367  return 0;
368  }
369 }
370 
371 
372 void
375  out.writeAttr(SUMO_ATTR_ID, getID());
376  std::vector<std::string> internals;
377  internals.push_back(myDepartLane);
378  internals.push_back(toString(myDepartPosLat));
379  internals.push_back(toString(myDepartSpeed));
380  out.writeAttr(SUMO_ATTR_STATE, toString(internals));
381  out.closeTag();
382 }
383 
384 
385 void
387  std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
388  bis >> myDepartLane;
389  bis >> myDepartPosLat;
390  bis >> myDepartSpeed;
391 }
392 
393 
394 /****************************************************************************/
static SUMOTime myTotalWalkDuration
virtual double getDepartPos() const =0
Returns this vehicle&#39;s real departure position.
double getLength() const
return the length of the edge
Definition: MSEdge.h:570
static double gLateralResolution
Definition: MSGlobals.h:92
SUMOTime myArrivalTime
The vehicle&#39;s arrival time.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
virtual double getChosenSpeedFactor() const =0
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
static SUMOTime myTotalWaitingTime
virtual SUMOTime getWaitingTime() const =0
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
static double myTotalRouteLength
void computeLengthAndDuration(double &routeLength, SUMOTime &duration) const
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
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:187
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:30
static double getAvgTimeLoss()
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
bool hasInternalLinks() const
return whether the network contains internal links
Definition: MSNet.h:673
Notification
Definition of a vehicle state.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
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.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
~MSDevice_Tripinfo()
Destructor.
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
The vehicle got vaporized.
static double myVehicleCount
global tripinfo statistics
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:252
const MSLane * getLane() const
Returns the lane the reminder works on.
double myArrivalSpeed
The speed when arriving.
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
static double getAvgDuration()
SUMOTime myWaitingTime
The overall waiting time.
#define NOT_ARRIVED
The state of a link.
static DeviceSet myPendingOutput
const std::string & getID() const
Returns the id.
Definition: Named.h:66
static void generateOutputForUnfinished()
generate output for vehicles which are still in the network
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:859
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
void notifyMoveInternal(const SUMOVehicle &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal() ...
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
static std::string printStatistics()
get statistics for printing to stdout
Representation of a vehicle.
Definition: SUMOVehicle.h:67
void updateStatistics(SUMOTime timeLoss) const
update tripinfo statistics
static double getAvgWalkRouteLength()
Encapsulated SAX-Attributes.
static SUMOTime myTotalDuration
The vehicle arrived at its destination (is deleted)
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
static void addPedestrianData(double walkLength, SUMOTime walkDuration)
record tripinfo data for pedestrians
void saveState(OutputDevice &out) const
Saves the state of the device.
virtual SUMOTime getDepartDelay() const =0
virtual double getArrivalPos() const =0
Returns this vehicle&#39;s desired arrivalPos for its current route (may change on reroute) ...
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:76
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
double myArrivalPosLat
The lateral position on the lane the vehicle arrived at.
static double getAvgWalkDuration()
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:230
virtual int getNumberReroutes() const =0
Returns the number of new routes this vehicle got.
std::string myDepartLane
The lane the vehicle departed at.
Abstract in-vehicle device.
Definition: MSDevice.h:71
static double getAvgWaitingTime()
The vehicle has departed (was inserted into the network)
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
static double myTotalWalkRouteLength
virtual const MSRoute & getRoute() const =0
Returns the current route.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:80
std::string myArrivalLane
The lane the vehicle arrived at.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
double myDepartSpeed
The speed on departure.
MSDevice_Tripinfo()
dummy constructor
static SUMOTime myTotalDepartDelay
const std::string & getID() const
Returns the name of the vehicle type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static double getAvgRouteLength()
accessors for GUINet-Parameters
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:58
double getDistanceBetween(double fromPos, double 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:281
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
static double getAvgDepartDelay()
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
std::set< const MSDevice_Tripinfo *, Named::NamedLikeComparatorIdLess< MSDevice_Tripinfo > > DeviceSet
devices which may still need to produce output
static SUMOTime myTotalTimeLoss
SUMOTime myMesoTimeLoss
The time loss when compared to the desired and allowed speed.
double myDepartPosLat
The lateral depart position.
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
static bool gUseMesoSim
Definition: MSGlobals.h:98
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
double myArrivalPos
The position on the lane the vehicle arrived at.
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:74