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 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 // ---------------------------------------------------------------------------
62 // static initialisation methods
63 // ---------------------------------------------------------------------------
64 void
65 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
66  if (OptionsCont::getOptions().isSet("tripinfo-output") || OptionsCont::getOptions().getBool("duration-log.statistics")) {
67  MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
68  into.push_back(device);
69  myPendingOutput.insert(device);
70  }
71 }
72 
73 
74 // ---------------------------------------------------------------------------
75 // MSDevice_Tripinfo-methods
76 // ---------------------------------------------------------------------------
77 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) :
78  MSDevice(holder, id),
79  myDepartLane(""),
80  myDepartSpeed(-1),
81  myDepartPosLat(0),
82  myWaitingTime(0),
84  myArrivalLane(""),
85  myArrivalPos(-1),
86  myArrivalPosLat(0),
87  myArrivalSpeed(-1),
88  myMesoTimeLoss(0) {
89 }
90 
91 
93  // ensure clean up for vaporized vehicles which do not generate output
94  myPendingOutput.erase(this);
95 }
96 
97 
98 bool
100  double /*newPos*/, double newSpeed) {
101  if (veh.isStopped()) {
102  return true;
103  }
104  if (newSpeed <= SUMO_const_haltingSpeed) {
106  }
107  return true;
108 }
109 
110 void
112  const double /* frontOnLane */,
113  const double timeOnLane,
114  const double /* meanSpeedFrontOnLane */,
115  const double meanSpeedVehicleOnLane,
116  const double /* travelledDistanceFrontOnLane */,
117  const double /* travelledDistanceVehicleOnLane */) {
118 
119  // called by meso
120  const double vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
121  if (vmax > 0) {
122  myMesoTimeLoss += TIME2STEPS(timeOnLane * (vmax - meanSpeedVehicleOnLane) / vmax);
123  }
124  myWaitingTime += veh.getWaitingTime();
125 }
126 
127 bool
130  if (!MSGlobals::gUseMesoSim) {
131  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
132  myDepartPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
133  }
134  myDepartSpeed = veh.getSpeed();
135  }
136  return true;
137 }
138 
139 
140 bool
142  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
143  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
145  if (!MSGlobals::gUseMesoSim) {
146  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
147  myArrivalPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
148  }
149  // @note vehicle may have moved past its arrivalPos during the last step
150  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
151  // However, vaporization may happen anywhere (via TraCI)
154  } else {
156  }
157  myArrivalSpeed = veh.getSpeed();
158  }
159  return true;
160 }
161 
162 void
163 MSDevice_Tripinfo::computeLengthAndDuration(double& routeLength, SUMOTime& duration) const {
164  SUMOTime finalTime;
165  double finalPos;
166  double finalPosOnInternal = 0;
167  if (myArrivalTime == NOT_ARRIVED) {
168  finalTime = MSNet::getInstance()->getCurrentTimeStep();
169  finalPos = myHolder.getPositionOnLane();
170  if (!MSGlobals::gUseMesoSim) {
171  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
172  if (lane->getEdge().isInternal()) {
173  finalPosOnInternal = finalPos;
174  finalPos = myHolder.getEdge()->getLength();
175  }
176  }
177  } else {
178  finalTime = myArrivalTime;
179  finalPos = myArrivalPos;
180  }
181  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
182  routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
183  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
184 
185  duration = finalTime - myHolder.getDeparture();
186 }
187 
188 
189 void
191  const SUMOTime timeLoss = MSGlobals::gUseMesoSim ? myMesoTimeLoss : static_cast<MSVehicle&>(myHolder).getTimeLoss();
192  updateStatistics(timeLoss);
193  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
194  return;
195  }
196  myPendingOutput.erase(this);
197  double 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());
208  os.writeAttr("departPosLat", myDepartPosLat);
209  }
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);
216  os.writeAttr("arrivalPosLat", myArrivalPosLat);
217  }
218  os.writeAttr("arrivalSpeed", myArrivalSpeed);
219  os.writeAttr("duration", time2string(duration));
220  os.writeAttr("routeLength", routeLength);
221  os.writeAttr("waitSteps", myWaitingTime / DELTA_T);
222  os.writeAttr("timeLoss", time2string(timeLoss));
223  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
224  const std::vector<MSDevice*>& devices = myHolder.getDevices();
225  std::ostringstream str;
226  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
227  if (i != devices.begin()) {
228  str << ' ';
229  }
230  str << (*i)->getID();
231  }
232  os.writeAttr("devices", str.str());
233  os.writeAttr("vType", myHolder.getVehicleType().getID());
234  os.writeAttr("speedFactor", myHolder.getChosenSpeedFactor());
235  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
236  // cannot close tag because emission device output might follow
237 }
238 
239 
240 void
242  while (myPendingOutput.size() > 0) {
243  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
244  if (d->myHolder.hasDeparted()) {
245  d->generateOutput();
246  if (!OptionsCont::getOptions().isSet("tripinfo-output")) {
247  return;
248  }
249  // @todo also generate emission output if holder has a device
250  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
251  } else {
252  myPendingOutput.erase(d);
253  }
254  }
255 }
256 
257 
258 void
260  double routeLength;
261  SUMOTime duration;
262  computeLengthAndDuration(routeLength, duration);
263 
264  myVehicleCount++;
265  myTotalRouteLength += routeLength;
266  myTotalDuration += duration;
268  myTotalTimeLoss += timeLoss;
270 }
271 
272 
273 std::string
275  std::ostringstream msg;
276  msg.setf(msg.fixed);
277  msg.precision(gPrecision);
278  msg << "Statistics (avg):\n"
279  << " RouteLength: " << getAvgRouteLength() << "\n"
280  << " Duration: " << getAvgDuration() << "\n"
281  << " WaitingTime: " << getAvgWaitingTime() << "\n"
282  << " TimeLoss: " << getAvgTimeLoss() << "\n"
283  << " DepartDelay: " << getAvgDepartDelay() << "\n";
284  return msg.str();
285 }
286 
287 
288 double
290  if (myVehicleCount > 0) {
292  } else {
293  return 0;
294  }
295 }
296 
297 double
299  if (myVehicleCount > 0) {
301  } else {
302  return 0;
303  }
304 }
305 
306 double
308  if (myVehicleCount > 0) {
310  } else {
311  return 0;
312  }
313 }
314 
315 
316 double
318  if (myVehicleCount > 0) {
320  } else {
321  return 0;
322  }
323 }
324 
325 
326 double
328  if (myVehicleCount > 0) {
330  } else {
331  return 0;
332  }
333 }
334 
335 
336 void
339  out.writeAttr(SUMO_ATTR_ID, getID());
340  std::vector<std::string> internals;
341  internals.push_back(myDepartLane);
342  internals.push_back(toString(myDepartPosLat));
343  internals.push_back(toString(myDepartSpeed));
344  out.writeAttr(SUMO_ATTR_STATE, toString(internals));
345  out.closeTag();
346 }
347 
348 
349 void
351  std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
352  bis >> myDepartLane;
353  bis >> myDepartPosLat;
354  bis >> myDepartSpeed;
355 }
356 
357 
358 /****************************************************************************/
static double gLateralResolution
Definition: MSGlobals.h:92
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:256
void computeLengthAndDuration(double &routeLength, SUMOTime &duration) const
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual double getArrivalPos() const =0
Returns this vehicle&#39;s desired arrivalPos for its current route (may change on reroute) ...
static SUMOTime myTotalWaitingTime
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
virtual const MSRoute & getRoute() const =0
Returns the current route.
void updateStatistics(SUMOTime timeLoss) const
update tripinfo statistics
static double myTotalRouteLength
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 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.
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
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
~MSDevice_Tripinfo()
Destructor.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
The vehicle got vaporized.
static double myVehicleCount
global tripinfo statistics
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
double getLength() const
return the length of the edge
Definition: MSEdge.h:586
double myArrivalSpeed
The speed when arriving.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
static double getAvgDuration()
SUMOTime myWaitingTime
The overall waiting time.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
#define NOT_ARRIVED
The state of a link.
static DeviceSet myPendingOutput
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
static void generateOutputForUnfinished()
generate output for vehicles which are still in the network
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
Encapsulated SAX-Attributes.
virtual int getNumberReroutes() const =0
Returns the number of new routes this vehicle got.
static SUMOTime myTotalDuration
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:257
bool hasInternalLinks() const
return whether the network contains internal links
Definition: MSNet.h:678
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:76
double myArrivalPosLat
The lateral position on the lane the vehicle arrived at.
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:254
virtual double getChosenSpeedFactor() const =0
std::string myDepartLane
The lane the vehicle departed at.
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:836
Abstract in-vehicle device.
Definition: MSDevice.h:71
static double getAvgWaitingTime()
virtual double getDepartPos() const =0
Returns this vehicle&#39;s real departure position.
virtual SUMOTime getDepartDelay() const =0
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.
std::string myArrivalLane
The lane the vehicle arrived at.
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
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 SUMOTime getWaitingTime() const =0
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:56
static double getAvgDepartDelay()
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 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
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:74
void generateOutput() const
Called on writing tripinfo output.
void notifyMoveInternal(const SUMOVehicle &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal() ...
static SUMOTime myTotalTimeLoss
SUMOTime myMesoTimeLoss
The time loss when compared to the desired and allowed speed.
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
double myDepartPosLat
The lateral depart position.
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
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.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:80
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void saveState(OutputDevice &out) const
Saves the state of the device.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.