Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2009-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSDevice_Tripinfo.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date Fri, 30.01.2009
20 : ///
21 : // A device which collects info on the vehicle trip
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/MSGlobals.h>
26 : #include <microsim/MSNet.h>
27 : #include <microsim/MSLane.h>
28 : #include <microsim/MSEdge.h>
29 : #include <microsim/MSVehicle.h>
30 : #include <microsim/transportables/MSTransportableControl.h>
31 : #include <mesosim/MEVehicle.h>
32 : #include <utils/options/OptionsCont.h>
33 : #include <utils/iodevices/OutputDevice.h>
34 : #include <utils/xml/SUMOSAXAttributes.h>
35 : #include "MSDevice_Vehroutes.h"
36 : #include "MSDevice_Tripinfo.h"
37 :
38 : #define NOT_ARRIVED TIME2STEPS(-1)
39 : #define STATE_EMPTY_ARRIVALLANE "NONE"
40 :
41 :
42 : // ===========================================================================
43 : // static members
44 : // ===========================================================================
45 : std::set<const MSDevice_Tripinfo*, ComparatorNumericalIdLess> MSDevice_Tripinfo::myPendingOutput;
46 :
47 : int MSDevice_Tripinfo::myVehicleCount(0);
48 : int MSDevice_Tripinfo::myUndepartedVehicleCount(0);
49 : double MSDevice_Tripinfo::myTotalRouteLength(0);
50 : double MSDevice_Tripinfo::myTotalSpeed(0);
51 : SUMOTime MSDevice_Tripinfo::myTotalDuration(0);
52 : SUMOTime MSDevice_Tripinfo::myTotalWaitingTime(0);
53 : SUMOTime MSDevice_Tripinfo::myTotalTimeLoss(0);
54 : SUMOTime MSDevice_Tripinfo::myTotalDepartDelay(0);
55 : SUMOTime MSDevice_Tripinfo::myWaitingDepartDelay(-1);
56 :
57 : int MSDevice_Tripinfo::myBikeCount(0);
58 : double MSDevice_Tripinfo::myTotalBikeRouteLength(0);
59 : double MSDevice_Tripinfo::myTotalBikeSpeed(0);
60 : SUMOTime MSDevice_Tripinfo::myTotalBikeDuration(0);
61 : SUMOTime MSDevice_Tripinfo::myTotalBikeWaitingTime(0);
62 : SUMOTime MSDevice_Tripinfo::myTotalBikeTimeLoss(0);
63 : SUMOTime MSDevice_Tripinfo::myTotalBikeDepartDelay(0);
64 :
65 : int MSDevice_Tripinfo::myWalkCount(0);
66 : double MSDevice_Tripinfo::myTotalWalkRouteLength(0);
67 : SUMOTime MSDevice_Tripinfo::myTotalWalkDuration(0);
68 : SUMOTime MSDevice_Tripinfo::myTotalWalkTimeLoss(0);
69 : std::vector<int> MSDevice_Tripinfo::myRideCount({0, 0});
70 : std::vector<int> MSDevice_Tripinfo::myRideBusCount({0, 0});
71 : std::vector<int> MSDevice_Tripinfo::myRideRailCount({0, 0});
72 : std::vector<int> MSDevice_Tripinfo::myRideTaxiCount({0, 0});
73 : std::vector<int> MSDevice_Tripinfo::myRideBikeCount({0, 0});
74 : std::vector<int> MSDevice_Tripinfo::myRideAbortCount({0, 0});
75 : std::vector<SUMOTime> MSDevice_Tripinfo::myTotalRideWaitingTime({0, 0});
76 : std::vector<double> MSDevice_Tripinfo::myTotalRideRouteLength({0., 0.});
77 : std::vector<SUMOTime> MSDevice_Tripinfo::myTotalRideDuration({0, 0});
78 :
79 : // ===========================================================================
80 : // method definitions
81 : // ===========================================================================
82 : // ---------------------------------------------------------------------------
83 : // static initialisation methods
84 : // ---------------------------------------------------------------------------
85 : void
86 45329 : MSDevice_Tripinfo::insertOptions(OptionsCont& oc) {
87 45329 : oc.addOptionSubTopic("Tripinfo Device");
88 90658 : insertDefaultAssignmentOptions("tripinfo", "Tripinfo Device", oc);
89 45329 : }
90 :
91 :
92 : void
93 5256341 : MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
94 5256341 : OptionsCont& oc = OptionsCont::getOptions();
95 10239523 : const bool enableByOutputOption = oc.isSet("tripinfo-output") || oc.getBool("duration-log.statistics");
96 10512682 : if (equippedByDefaultAssignmentOptions(oc, "tripinfo", v, enableByOutputOption)) {
97 2300335 : MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
98 2300335 : into.push_back(device);
99 : myPendingOutput.insert(device);
100 : }
101 5256341 : }
102 :
103 :
104 : // ---------------------------------------------------------------------------
105 : // MSDevice_Tripinfo-methods
106 : // ---------------------------------------------------------------------------
107 2300335 : MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) :
108 : MSVehicleDevice(holder, id),
109 0 : myDepartLane(""),
110 2300335 : myDepartSpeed(-1),
111 2300335 : myDepartPosLat(0),
112 2300335 : myWaitingTime(0),
113 2300335 : myAmWaiting(false),
114 2300335 : myWaitingCount(0),
115 2300335 : myStoppingTime(0),
116 2300335 : myParkingStarted(-1),
117 2300335 : myArrivalTime(NOT_ARRIVED),
118 2300335 : myArrivalLane(""),
119 2300335 : myArrivalPos(-1),
120 2300335 : myArrivalPosLat(0.),
121 2300335 : myArrivalSpeed(-1),
122 2300335 : myArrivalReason(MSMoveReminder::NOTIFICATION_ARRIVED),
123 2300335 : myMesoTimeLoss(0),
124 2300335 : myRouteLength(0.) {
125 2300335 : }
126 :
127 :
128 4600622 : MSDevice_Tripinfo::~MSDevice_Tripinfo() {
129 : // ensure clean up for vaporized vehicles which do not generate output
130 2300311 : myPendingOutput.erase(this);
131 4600622 : }
132 :
133 : void
134 42169 : MSDevice_Tripinfo::cleanup() {
135 42169 : myVehicleCount = 0;
136 42169 : myTotalRouteLength = 0;
137 42169 : myTotalSpeed = 0;
138 42169 : myTotalDuration = 0;
139 42169 : myTotalWaitingTime = 0;
140 42169 : myTotalTimeLoss = 0;
141 42169 : myTotalDepartDelay = 0;
142 42169 : myWaitingDepartDelay = -1;
143 :
144 42169 : myBikeCount = 0;
145 42169 : myTotalBikeRouteLength = 0;
146 42169 : myTotalBikeSpeed = 0;
147 42169 : myTotalBikeDuration = 0;
148 42169 : myTotalBikeWaitingTime = 0;
149 42169 : myTotalBikeTimeLoss = 0;
150 42169 : myTotalBikeDepartDelay = 0;
151 :
152 42169 : myWalkCount = 0;
153 42169 : myTotalWalkRouteLength = 0;
154 42169 : myTotalWalkDuration = 0;
155 42169 : myTotalWalkTimeLoss = 0;
156 :
157 42169 : myRideCount = {0, 0};
158 42169 : myRideBusCount = {0, 0};
159 42169 : myRideRailCount = {0, 0};
160 42169 : myRideTaxiCount = {0, 0};
161 42169 : myRideBikeCount = {0, 0};
162 42169 : myRideAbortCount = {0, 0};
163 42169 : myTotalRideWaitingTime = {0, 0};
164 42169 : myTotalRideRouteLength = {0., 0.};
165 42169 : myTotalRideDuration = {0, 0};
166 42169 : }
167 :
168 : bool
169 4755 : MSDevice_Tripinfo::notifyIdle(SUMOTrafficObject& veh) {
170 4755 : if (veh.isVehicle()) {
171 4755 : myWaitingTime += DELTA_T;
172 4755 : if (!myAmWaiting) {
173 146 : myWaitingCount++;
174 146 : myAmWaiting = true;
175 : }
176 : }
177 4755 : return true;
178 : }
179 :
180 :
181 : bool
182 348053469 : MSDevice_Tripinfo::notifyMove(SUMOTrafficObject& veh, double /*oldPos*/,
183 : double /*newPos*/, double newSpeed) {
184 348053469 : if (veh.isStopped()) {
185 2262144 : if (newSpeed <= SUMO_const_haltingSpeed) {
186 2261952 : myStoppingTime += DELTA_T;
187 : }
188 345791325 : } else if (newSpeed <= SUMO_const_haltingSpeed && lowAcceleration(veh)) {
189 54768419 : myWaitingTime += DELTA_T;
190 54768419 : if (!myAmWaiting) {
191 2685655 : myWaitingCount++;
192 2685655 : myAmWaiting = true;
193 : }
194 : } else {
195 291022906 : myAmWaiting = false;
196 : }
197 348053469 : return true;
198 : }
199 :
200 :
201 : bool
202 54769259 : MSDevice_Tripinfo::lowAcceleration(const SUMOTrafficObject& veh) {
203 54769259 : if (MSGlobals::gUseMesoSim) {
204 : // acceleration is not modelled
205 : return false;
206 : } else {
207 54769259 : const MSVehicle& v = dynamic_cast<const MSVehicle&>(veh);
208 54769259 : return v.getAcceleration() <= v.accelThresholdForWaiting();
209 : }
210 : }
211 :
212 :
213 : void
214 14317760 : MSDevice_Tripinfo::notifyMoveInternal(const SUMOTrafficObject& veh,
215 : const double /* frontOnLane */,
216 : const double timeOnLane,
217 : const double /* meanSpeedFrontOnLane */,
218 : const double meanSpeedVehicleOnLane,
219 : const double /* travelledDistanceFrontOnLane */,
220 : const double /* travelledDistanceVehicleOnLane */,
221 : const double /* meanLengthOnLane */) {
222 :
223 : // called by meso
224 14317760 : const double vmax = veh.getEdge()->getVehicleMaxSpeed(&veh);
225 14317760 : if (vmax > 0) {
226 17702841 : myMesoTimeLoss += TIME2STEPS(timeOnLane * (vmax - meanSpeedVehicleOnLane) / vmax);
227 : }
228 14317760 : myWaitingTime += veh.getWaitingTime();
229 14317760 : if (veh.getWaitingTime() >= TIME2STEPS(1)) {
230 : // waiting counts the time spent waiting to enter the next link (when it's occupied).
231 174501 : myWaitingCount++;
232 : }
233 14317760 : }
234 :
235 :
236 : void
237 5 : MSDevice_Tripinfo::recordMesoParkingTimeLoss(SUMOTime waitingTime) {
238 5 : myMesoTimeLoss += waitingTime;
239 5 : myWaitingTime += waitingTime;
240 5 : myWaitingCount++;
241 5 : }
242 :
243 : void
244 1739947 : MSDevice_Tripinfo::updateParkingStopTime() {
245 1739947 : if (myParkingStarted >= 0) {
246 4384 : myStoppingTime += (MSNet::getInstance()->getCurrentTimeStep() - myParkingStarted);
247 4384 : myParkingStarted = -1;
248 : }
249 1739947 : }
250 :
251 : bool
252 28922121 : MSDevice_Tripinfo::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
253 28922121 : if (reason == MSMoveReminder::NOTIFICATION_DEPARTED) {
254 1772315 : if (!MSGlobals::gUseMesoSim) {
255 1398515 : myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
256 1398515 : myDepartPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
257 : } else {
258 373800 : myDepartLane = veh.getEdge()->getFirstAllowed(veh.getVClass(), true)->getID();
259 : }
260 1772315 : myDepartSpeed = veh.getSpeed();
261 1772315 : myRouteLength = -veh.getPositionOnLane();
262 27149806 : } else if (reason == MSMoveReminder::NOTIFICATION_PARKING) {
263 : // notifyMove is not called while parking
264 : // @note insertion delay when resuming after parking is included
265 3755 : updateParkingStopTime();
266 : }
267 28922121 : return true;
268 : }
269 :
270 :
271 : bool
272 28848761 : MSDevice_Tripinfo::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/,
273 : MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
274 28848761 : if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
275 1702500 : myArrivalTime = MSNet::getInstance()->getCurrentTimeStep();
276 1702500 : myArrivalReason = reason;
277 1702500 : if (!MSGlobals::gUseMesoSim) {
278 1335978 : myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
279 1335978 : myArrivalPosLat = static_cast<MSVehicle&>(veh).getLateralPositionOnLane();
280 : } else {
281 366522 : myArrivalLane = veh.getEdge()->getFirstAllowed(veh.getVClass(), true)->getID();
282 : }
283 : // @note vehicle may have moved past its arrivalPos during the last step
284 : // due to non-zero arrivalspeed but we consider it as arrived at the desired position
285 : // However, vaporization may happen anywhere (via TraCI)
286 1702500 : if (reason > MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED) {
287 : // vaporized
288 549 : myArrivalPos = veh.getPositionOnLane();
289 : } else {
290 1701951 : myArrivalPos = myHolder.getArrivalPos();
291 : }
292 1702500 : myArrivalSpeed = veh.getSpeed();
293 1702500 : updateParkingStopTime();
294 27146261 : } else if (reason == MSMoveReminder::NOTIFICATION_PARKING) {
295 4558 : myParkingStarted = MSNet::getInstance()->getCurrentTimeStep();
296 27141703 : } else if (reason == NOTIFICATION_JUNCTION
297 27141703 : || reason == NOTIFICATION_TELEPORT
298 12382334 : || reason == NOTIFICATION_TELEPORT_CONTINUATION) {
299 14764906 : if (MSGlobals::gUseMesoSim) {
300 2378480 : myRouteLength += myHolder.getCurrentEdge()->getLength();
301 : } else {
302 12386426 : const MSLane* lane = static_cast<MSVehicle&>(veh).getLane();
303 12386426 : if (lane != nullptr) {
304 12386402 : myRouteLength += lane->getLength();
305 : }
306 : }
307 : }
308 28848761 : return true;
309 : }
310 :
311 :
312 : void
313 1736970 : MSDevice_Tripinfo::generateOutput(OutputDevice* tripinfoOut) const {
314 1736970 : const SUMOTime timeLoss = MSGlobals::gUseMesoSim ? myMesoTimeLoss : static_cast<MSVehicle&>(myHolder).getTimeLoss();
315 1736970 : const double routeLength = myRouteLength + (myArrivalTime == NOT_ARRIVED ? myHolder.getPositionOnLane() : myArrivalPos);
316 : SUMOTime duration = 0;
317 1736970 : if (myHolder.hasDeparted()) {
318 1771166 : duration = (myArrivalTime == NOT_ARRIVED ? SIMSTEP : myArrivalTime) - myHolder.getDeparture();
319 1736726 : if (myHolder.getVClass() == SVC_BICYCLE) {
320 24363 : myBikeCount++;
321 24363 : myTotalBikeRouteLength += routeLength;
322 24363 : myTotalBikeSpeed += routeLength / STEPS2TIME(duration);
323 24363 : myTotalBikeDuration += duration;
324 24363 : myTotalBikeWaitingTime += myWaitingTime;
325 24363 : myTotalBikeTimeLoss += timeLoss;
326 24363 : myTotalBikeDepartDelay += myHolder.getDepartDelay();
327 : } else {
328 1712363 : myVehicleCount++;
329 1712363 : myTotalRouteLength += routeLength;
330 1712363 : myTotalSpeed += routeLength / STEPS2TIME(duration);
331 1712363 : myTotalDuration += duration;
332 1712363 : myTotalWaitingTime += myWaitingTime;
333 1712363 : myTotalTimeLoss += timeLoss;
334 1712363 : myTotalDepartDelay += myHolder.getDepartDelay();
335 : }
336 : }
337 :
338 1736970 : myPendingOutput.erase(this);
339 1736970 : if (tripinfoOut == nullptr) {
340 1494803 : return;
341 : }
342 : // write
343 : OutputDevice& os = *tripinfoOut;
344 242167 : os.openTag("tripinfo").writeAttr("id", myHolder.getID());
345 242167 : os.writeAttr("depart", myHolder.hasDeparted() ? time2string(myHolder.getDeparture()) : "-1");
346 242167 : os.writeAttr("departLane", myDepartLane);
347 242167 : os.writeAttr("departPos", myHolder.getDepartPos());
348 242167 : if (MSGlobals::gLateralResolution > 0) {
349 26003 : os.writeAttr("departPosLat", myDepartPosLat);
350 : }
351 242167 : os.writeAttr("departSpeed", myDepartSpeed);
352 242167 : SUMOTime departDelay = myHolder.getDepartDelay();
353 242167 : const SUMOVehicleParameter& param = myHolder.getParameter();
354 242167 : if (!myHolder.hasDeparted()) {
355 : assert(param.depart <= SIMSTEP || param.departProcedure != DepartDefinition::GIVEN);
356 244 : departDelay = SIMSTEP - param.depart;
357 : }
358 242167 : os.writeAttr("departDelay", time2string(departDelay));
359 242167 : os.writeAttr("arrival", time2string(myArrivalTime));
360 242167 : os.writeAttr("arrivalLane", myArrivalLane);
361 242167 : os.writeAttr("arrivalPos", myArrivalPos);
362 242167 : if (MSGlobals::gLateralResolution > 0) {
363 26003 : os.writeAttr("arrivalPosLat", myArrivalPosLat);
364 : }
365 242167 : os.writeAttr("arrivalSpeed", myArrivalSpeed);
366 242167 : os.writeAttr("duration", time2string(duration));
367 242167 : os.writeAttr("routeLength", routeLength);
368 242167 : os.writeAttr(SUMO_ATTR_WAITINGTIME, time2string(myWaitingTime));
369 242167 : os.writeAttr(SUMO_ATTR_WAITINGCOUNT, myWaitingCount);
370 242167 : os.writeAttr(SUMO_ATTR_STOPTIME, time2string(myStoppingTime));
371 242167 : os.writeAttr(SUMO_ATTR_TIMELOSS, time2string(timeLoss));
372 242167 : os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
373 484334 : os.writeAttr("devices", toString(myHolder.getDevices()));
374 242167 : os.writeAttr("vType", myHolder.getVehicleType().getID());
375 242167 : os.writeAttr("speedFactor", myHolder.getChosenSpeedFactor());
376 : std::string vaporized;
377 242167 : switch (myArrivalReason) {
378 : case MSMoveReminder::NOTIFICATION_VAPORIZED_CALIBRATOR:
379 : vaporized = "calibrator";
380 : break;
381 : case MSMoveReminder::NOTIFICATION_VAPORIZED_GUI:
382 : vaporized = "gui";
383 : break;
384 : case MSMoveReminder::NOTIFICATION_VAPORIZED_COLLISION:
385 : vaporized = "collision";
386 : break;
387 : case MSMoveReminder::NOTIFICATION_VAPORIZED_VAPORIZER:
388 : vaporized = "vaporizer";
389 : break;
390 : case MSMoveReminder::NOTIFICATION_VAPORIZED_TRACI:
391 : vaporized = "traci";
392 : break;
393 : case MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED:
394 : vaporized = "teleport";
395 : break;
396 241754 : default:
397 241754 : if (myHolder.getEdge() == myHolder.getRoute().getLastEdge() ||
398 1607 : (param.arrivalEdge >= 0 && myHolder.getRoutePosition() >= param.arrivalEdge)) {
399 : vaporized = "";
400 : } else {
401 : vaporized = "end";
402 : }
403 : break;
404 : }
405 242167 : os.writeAttr("vaporized", vaporized);
406 : // cannot close tag because emission device output might follow
407 : }
408 :
409 :
410 : void
411 1082 : MSDevice_Tripinfo::generateOutputForUnfinished() {
412 1082 : MSNet* net = MSNet::getInstance();
413 1082 : OutputDevice* tripinfoOut = (OptionsCont::getOptions().isSet("tripinfo-output") ?
414 1910 : &OutputDevice::getDeviceByOption("tripinfo-output") : nullptr);
415 1082 : myWaitingDepartDelay = 0;
416 1082 : myUndepartedVehicleCount = 0;
417 2164 : const bool writeUndeparted = OptionsCont::getOptions().getBool("tripinfo-output.write-undeparted");
418 : const SUMOTime t = net->getCurrentTimeStep();
419 66531 : while (myPendingOutput.size() > 0) {
420 65449 : const MSDevice_Tripinfo* d = *myPendingOutput.begin();
421 65449 : const bool departed = d->myHolder.hasDeparted();
422 65449 : const bool departDelayed = d->myHolder.getParameter().depart <= t;
423 65449 : if (!departed && departDelayed) {
424 31989 : myUndepartedVehicleCount++;
425 31989 : myWaitingDepartDelay += (t - d->myHolder.getParameter().depart);
426 : }
427 65449 : if (departed || (writeUndeparted && departDelayed)) {
428 33692 : const_cast<MSDevice_Tripinfo*>(d)->updateParkingStopTime();
429 33692 : d->generateOutput(tripinfoOut);
430 33692 : if (tripinfoOut != nullptr) {
431 7911 : for (MSVehicleDevice* const dev : d->myHolder.getDevices()) {
432 5668 : if (typeid(*dev) == typeid(MSDevice_Tripinfo) || typeid(*dev) == typeid(MSDevice_Vehroutes)) {
433 : // tripinfo is special and vehroute has its own write-unfinished option
434 2889 : continue;
435 : }
436 2779 : dev->generateOutput(tripinfoOut);
437 : }
438 4486 : OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
439 : }
440 : } else {
441 : myPendingOutput.erase(d);
442 : }
443 : }
444 : // unfinished persons
445 1082 : if (net->hasPersons()) {
446 641 : net->getPersonControl().eraseAll();
447 : }
448 :
449 1082 : }
450 :
451 :
452 : void
453 55461 : MSDevice_Tripinfo::addPedestrianData(double walkLength, SUMOTime walkDuration, SUMOTime walkTimeLoss) {
454 55461 : myWalkCount++;
455 55461 : myTotalWalkRouteLength += walkLength;
456 55461 : myTotalWalkDuration += walkDuration;
457 55461 : myTotalWalkTimeLoss += walkTimeLoss;
458 55461 : }
459 :
460 :
461 : void
462 7580 : MSDevice_Tripinfo::addRideTransportData(const bool isPerson, const double distance, const SUMOTime duration,
463 : const SUMOVehicleClass vClass, const std::string& line, const SUMOTime waitingTime) {
464 7580 : const int index = isPerson ? 0 : 1;
465 7580 : myRideCount[index]++;
466 7580 : if (duration > 0) {
467 7241 : myTotalRideWaitingTime[index] += waitingTime;
468 7241 : myTotalRideRouteLength[index] += distance;
469 7241 : myTotalRideDuration[index] += duration;
470 7241 : if (vClass == SVC_BICYCLE) {
471 25 : myRideBikeCount[index]++;
472 7216 : } else if (!line.empty()) {
473 6005 : if (isRailway(vClass)) {
474 834 : myRideRailCount[index]++;
475 5171 : } else if (vClass == SVC_TAXI) {
476 1934 : myRideTaxiCount[index]++;
477 : } else {
478 : // some kind of road vehicle
479 3237 : myRideBusCount[index]++;
480 : }
481 : }
482 : } else {
483 339 : myRideAbortCount[index]++;
484 : }
485 7580 : }
486 :
487 :
488 : std::string
489 3260 : MSDevice_Tripinfo::printStatistics() {
490 3260 : std::ostringstream msg;
491 : msg.setf(msg.fixed);
492 3260 : msg.precision(gPrecision);
493 3260 : if (myBikeCount == 0 || myVehicleCount > 0) {
494 3188 : msg << "Statistics (avg of " << myVehicleCount << "):\n";
495 3188 : msg << " RouteLength: " << getAvgRouteLength() << "\n"
496 3188 : << " Speed: " << getAvgTripSpeed() << "\n"
497 3188 : << " Duration: " << getAvgDuration() << "\n"
498 3188 : << " WaitingTime: " << getAvgWaitingTime() << "\n"
499 3188 : << " TimeLoss: " << getAvgTimeLoss() << "\n"
500 6376 : << " DepartDelay: " << getAvgDepartDelay() << "\n";
501 : }
502 3260 : if (myBikeCount > 0) {
503 : msg << "Bike Statistics (avg of " << myBikeCount << "):\n"
504 312 : << " RouteLength: " << getAvgBikeRouteLength() << "\n"
505 156 : << " Speed: " << getAvgBikeTripSpeed() << "\n"
506 156 : << " Duration: " << getAvgBikeDuration() << "\n"
507 156 : << " WaitingTime: " << getAvgBikeWaitingTime() << "\n"
508 156 : << " TimeLoss: " << getAvgBikeTimeLoss() << "\n"
509 312 : << " DepartDelay: " << getAvgBikeDepartDelay() << "\n";
510 156 : if (myVehicleCount > 0 && myWaitingDepartDelay >= 0) {
511 19 : msg << "Statistics (avg of " << (myVehicleCount + myBikeCount) << "):\n";
512 : }
513 : }
514 3260 : if (myWaitingDepartDelay >= 0) {
515 812 : msg << " DepartDelayWaiting: " << getAvgDepartDelayWaiting() << "\n";
516 : }
517 3260 : if (myWalkCount > 0) {
518 : msg << "Pedestrian Statistics (avg of " << myWalkCount << " walks):\n"
519 588 : << " RouteLength: " << getAvgWalkRouteLength() << "\n"
520 294 : << " Duration: " << getAvgWalkDuration() << "\n"
521 588 : << " TimeLoss: " << getAvgWalkTimeLoss() << "\n";
522 : }
523 6520 : printRideStatistics(msg, "Ride", "rides", 0);
524 6520 : printRideStatistics(msg, "Transport", "transports", 1);
525 3260 : return msg.str();
526 3260 : }
527 :
528 : void
529 6520 : MSDevice_Tripinfo::printRideStatistics(std::ostringstream& msg, const std::string& category, const std::string& modeName, const int index) {
530 6520 : if (myRideCount[index] > 0) {
531 441 : msg << category << " Statistics (avg of " << myRideCount[index] << " " << modeName << "):\n";
532 147 : msg << " WaitingTime: " << STEPS2TIME(myTotalRideWaitingTime[index] / myRideCount[index]) << "\n";
533 147 : msg << " RouteLength: " << myTotalRideRouteLength[index] / myRideCount[index] << "\n";
534 147 : msg << " Duration: " << STEPS2TIME(myTotalRideDuration[index] / myRideCount[index]) << "\n";
535 147 : if (myRideBusCount[index] > 0) {
536 15 : msg << " Bus: " << myRideBusCount[index] << "\n";
537 : }
538 147 : if (myRideRailCount[index] > 0) {
539 9 : msg << " Train: " << myRideRailCount[index] << "\n";
540 : }
541 147 : if (myRideTaxiCount[index] > 0) {
542 93 : msg << " Taxi: " << myRideTaxiCount[index] << "\n";
543 : }
544 147 : if (myRideBikeCount[index] > 0) {
545 19 : msg << " Bike: " << myRideBikeCount[index] << "\n";
546 : }
547 147 : if (myRideAbortCount[index] > 0) {
548 13 : msg << " Aborted: " << myRideAbortCount[index] << "\n";
549 : }
550 : }
551 :
552 6520 : }
553 :
554 :
555 : void
556 264 : MSDevice_Tripinfo::writeStatistics(OutputDevice& od) {
557 264 : od.setPrecision(gPrecision);
558 264 : od.openTag("vehicleTripStatistics");
559 264 : od.writeAttr("count", myVehicleCount);
560 264 : od.writeAttr("routeLength", getAvgRouteLength());
561 264 : od.writeAttr("speed", getAvgTripSpeed());
562 264 : od.writeAttr("duration", getAvgDuration());
563 264 : od.writeAttr("waitingTime", getAvgWaitingTime());
564 264 : od.writeAttr("timeLoss", getAvgTimeLoss());
565 264 : od.writeAttr("departDelay", getAvgDepartDelay());
566 264 : od.writeAttr("departDelayWaiting", getAvgDepartDelayWaiting());
567 264 : od.writeAttr("totalTravelTime", time2string(myTotalDuration));
568 264 : od.writeAttr("totalDepartDelay", time2string(TIME2STEPS(getTotalDepartDelay() + getTotalBikeDepartDelay())));
569 264 : od.closeTag();
570 264 : if (myBikeCount > 0) {
571 6 : od.openTag("bikeTripStatistics");
572 6 : od.writeAttr("count", myBikeCount);
573 6 : od.writeAttr("routeLength", getAvgBikeRouteLength());
574 6 : od.writeAttr("speed", getAvgBikeTripSpeed());
575 6 : od.writeAttr("duration", getAvgBikeDuration());
576 6 : od.writeAttr("waitingTime", getAvgBikeWaitingTime());
577 6 : od.writeAttr("timeLoss", getAvgBikeTimeLoss());
578 6 : od.writeAttr("departDelay", getAvgBikeDepartDelay());
579 6 : od.writeAttr("totalTravelTime", time2string(myTotalBikeDuration));
580 12 : od.closeTag();
581 : }
582 264 : od.openTag("pedestrianStatistics");
583 264 : od.writeAttr("number", myWalkCount);
584 264 : od.writeAttr("routeLength", getAvgWalkRouteLength());
585 264 : od.writeAttr("duration", getAvgWalkDuration());
586 264 : od.writeAttr("timeLoss", getAvgWalkTimeLoss());
587 264 : od.closeTag();
588 264 : writeRideStatistics(od, "rideStatistics", 0);
589 264 : writeRideStatistics(od, "transportStatistics", 1);
590 264 : }
591 :
592 : void
593 528 : MSDevice_Tripinfo::writeRideStatistics(OutputDevice& od, const std::string& category, const int index) {
594 528 : od.openTag(category);
595 528 : od.writeAttr("number", myRideCount[index]);
596 528 : if (myRideCount[index] > 0) {
597 2 : od.writeAttr("waitingTime", STEPS2TIME(myTotalRideWaitingTime[index] / myRideCount[index]));
598 2 : od.writeAttr("routeLength", myTotalRideRouteLength[index] / myRideCount[index]);
599 2 : od.writeAttr("duration", STEPS2TIME(myTotalRideDuration[index] / myRideCount[index]));
600 2 : od.writeAttr("bus", myRideBusCount[index]);
601 2 : od.writeAttr("train", myRideRailCount[index]);
602 2 : od.writeAttr("taxi", myRideTaxiCount[index]);
603 2 : od.writeAttr("bike", myRideBikeCount[index]);
604 2 : od.writeAttr("aborted", myRideAbortCount[index]);
605 : }
606 528 : od.closeTag();
607 528 : }
608 :
609 :
610 : double
611 3568 : MSDevice_Tripinfo::getAvgRouteLength() {
612 3568 : if (myVehicleCount > 0) {
613 3326 : return myTotalRouteLength / myVehicleCount;
614 : } else {
615 : return 0;
616 : }
617 : }
618 :
619 : double
620 3568 : MSDevice_Tripinfo::getAvgTripSpeed() {
621 3568 : if (myVehicleCount > 0) {
622 3326 : return myTotalSpeed / myVehicleCount;
623 : } else {
624 : return 0;
625 : }
626 : }
627 :
628 : double
629 3568 : MSDevice_Tripinfo::getAvgDuration() {
630 3568 : if (myVehicleCount > 0) {
631 3326 : return STEPS2TIME(myTotalDuration / myVehicleCount);
632 : } else {
633 : return 0;
634 : }
635 : }
636 :
637 : double
638 3568 : MSDevice_Tripinfo::getAvgWaitingTime() {
639 3568 : if (myVehicleCount > 0) {
640 3326 : return STEPS2TIME(myTotalWaitingTime / myVehicleCount);
641 : } else {
642 : return 0;
643 : }
644 : }
645 :
646 :
647 : double
648 3568 : MSDevice_Tripinfo::getAvgTimeLoss() {
649 3568 : if (myVehicleCount > 0) {
650 3326 : return STEPS2TIME(myTotalTimeLoss / myVehicleCount);
651 : } else {
652 : return 0;
653 : }
654 : }
655 :
656 :
657 : double
658 3568 : MSDevice_Tripinfo::getAvgDepartDelay() {
659 3568 : if (myVehicleCount > 0) {
660 3326 : return STEPS2TIME(myTotalDepartDelay / myVehicleCount);
661 : } else {
662 : return 0;
663 : }
664 : }
665 :
666 : double
667 786 : MSDevice_Tripinfo::getAvgDepartDelayWaiting() {
668 786 : if (myWaitingDepartDelay >= 0) {
669 418 : return STEPS2TIME(myWaitingDepartDelay / MAX2(1, myUndepartedVehicleCount));
670 : } else {
671 : return -1;
672 : }
673 : }
674 :
675 :
676 : double
677 644 : MSDevice_Tripinfo::getTotalDepartDelay() {
678 644 : return STEPS2TIME(myTotalDepartDelay + MAX2((SUMOTime)0, myWaitingDepartDelay));
679 : }
680 :
681 : double
682 220 : MSDevice_Tripinfo::getAvgBikeRouteLength() {
683 220 : if (myBikeCount > 0) {
684 162 : return myTotalBikeRouteLength / myBikeCount;
685 : } else {
686 : return 0;
687 : }
688 : }
689 :
690 : double
691 220 : MSDevice_Tripinfo::getAvgBikeTripSpeed() {
692 220 : if (myBikeCount > 0) {
693 162 : return myTotalBikeSpeed / myBikeCount;
694 : } else {
695 : return 0;
696 : }
697 : }
698 :
699 : double
700 220 : MSDevice_Tripinfo::getAvgBikeDuration() {
701 220 : if (myBikeCount > 0) {
702 162 : return STEPS2TIME(myTotalBikeDuration / myBikeCount);
703 : } else {
704 : return 0;
705 : }
706 : }
707 :
708 : double
709 220 : MSDevice_Tripinfo::getAvgBikeWaitingTime() {
710 220 : if (myBikeCount > 0) {
711 162 : return STEPS2TIME(myTotalBikeWaitingTime / myBikeCount);
712 : } else {
713 : return 0;
714 : }
715 : }
716 :
717 :
718 : double
719 220 : MSDevice_Tripinfo::getAvgBikeTimeLoss() {
720 220 : if (myBikeCount > 0) {
721 162 : return STEPS2TIME(myTotalBikeTimeLoss / myBikeCount);
722 : } else {
723 : return 0;
724 : }
725 : }
726 :
727 : double
728 162 : MSDevice_Tripinfo::getAvgBikeDepartDelay() {
729 162 : if (myBikeCount > 0) {
730 162 : return STEPS2TIME(myTotalBikeDepartDelay / myBikeCount);
731 : } else {
732 : return 0;
733 : }
734 : }
735 :
736 :
737 : double
738 528 : MSDevice_Tripinfo::getTotalBikeDepartDelay() {
739 528 : return STEPS2TIME(myTotalBikeDepartDelay);
740 : }
741 :
742 : double
743 616 : MSDevice_Tripinfo::getAvgWalkRouteLength() {
744 616 : if (myWalkCount > 0) {
745 300 : return myTotalWalkRouteLength / myWalkCount;
746 : } else {
747 : return 0;
748 : }
749 : }
750 :
751 : double
752 616 : MSDevice_Tripinfo::getAvgWalkDuration() {
753 616 : if (myWalkCount > 0) {
754 300 : return STEPS2TIME(myTotalWalkDuration / myWalkCount);
755 : } else {
756 : return 0;
757 : }
758 : }
759 :
760 :
761 : double
762 616 : MSDevice_Tripinfo::getAvgWalkTimeLoss() {
763 616 : if (myWalkCount > 0) {
764 300 : return STEPS2TIME(myTotalWalkTimeLoss / myWalkCount);
765 : } else {
766 : return 0;
767 : }
768 : }
769 :
770 :
771 : double
772 0 : MSDevice_Tripinfo::getAvgRideDuration() {
773 0 : if (myRideCount[0] > 0) {
774 0 : return STEPS2TIME(myTotalRideDuration[0] / myRideCount[0]);
775 : } else {
776 : return 0;
777 : }
778 : }
779 :
780 : double
781 0 : MSDevice_Tripinfo::getAvgRideWaitingTime() {
782 0 : if (myRideCount[0] > 0) {
783 0 : return STEPS2TIME(myTotalRideWaitingTime[0] / myRideCount[0]);
784 : } else {
785 : return 0;
786 : }
787 : }
788 :
789 : double
790 0 : MSDevice_Tripinfo::getAvgRideRouteLength() {
791 0 : if (myRideCount[0] > 0) {
792 0 : return myTotalRideRouteLength[0] / myRideCount[0];
793 : } else {
794 : return 0;
795 : }
796 : }
797 :
798 :
799 : std::string
800 239 : MSDevice_Tripinfo::getParameter(const std::string& key) const {
801 239 : if (key == toString(SUMO_ATTR_WAITINGTIME)) {
802 48 : return toString(STEPS2TIME(myWaitingTime));
803 191 : } else if (key == toString(SUMO_ATTR_WAITINGCOUNT)) {
804 48 : return toString(myWaitingCount);
805 143 : } else if (key == toString(SUMO_ATTR_STOPTIME)) {
806 48 : return toString(STEPS2TIME(myStoppingTime));
807 95 : } else if (key == toString(SUMO_ATTR_ARRIVALTIME)) {
808 19 : return toString(STEPS2TIME(myArrivalTime));
809 76 : } else if (key == toString(SUMO_ATTR_ARRIVALLANE)) {
810 : return toString(myArrivalLane);
811 57 : } else if (key == toString(SUMO_ATTR_ARRIVALPOS)) {
812 19 : return toString(myArrivalPos);
813 38 : } else if (key == toString(SUMO_ATTR_ARRIVALPOS_LAT)) {
814 19 : return toString(myArrivalPosLat);
815 19 : } else if (key == toString(SUMO_ATTR_ARRIVALSPEED)) {
816 19 : return toString(myArrivalSpeed);
817 : }
818 0 : throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
819 : }
820 :
821 :
822 : std::string
823 3016 : MSDevice_Tripinfo::getGlobalParameter(const std::string& prefixedKey) {
824 : std::string key = prefixedKey; // by default, assume vehicleTripStatistics;
825 3016 : const std::string err = "Parameter '" + prefixedKey + "' is not supported for device of type 'tripinfo'";
826 6032 : if (StringUtils::startsWith(key, "vehicleTripStatistics.")) {
827 1160 : key = prefixedKey.substr(22);
828 4872 : } else if (StringUtils::startsWith(key, "bikeTripStatistics.")) {
829 406 : key = prefixedKey.substr(19);
830 406 : if (key == toString(SUMO_ATTR_COUNT)) {
831 58 : return toString(myBikeCount);
832 348 : } else if (key == "routeLength") {
833 58 : return toString(getAvgBikeRouteLength());
834 290 : } else if (key == toString(SUMO_ATTR_SPEED)) {
835 58 : return toString(getAvgBikeTripSpeed());
836 232 : } else if (key == toString(SUMO_ATTR_DURATION)) {
837 58 : return toString(getAvgBikeDuration());
838 174 : } else if (key == toString(SUMO_ATTR_WAITINGTIME)) {
839 58 : return toString(getAvgBikeWaitingTime());
840 116 : } else if (key == toString(SUMO_ATTR_TIMELOSS)) {
841 58 : return toString(getAvgBikeTimeLoss());
842 58 : } else if (key == "departDelay") {
843 0 : return toString(getAvgBikeDepartDelay());
844 58 : } else if (key == "totalTravelTime") {
845 : // avoid human readable output
846 58 : return toString(STEPS2TIME((myTotalBikeDuration)));
847 : }
848 0 : throw InvalidArgument(err);
849 :
850 4060 : } else if (StringUtils::startsWith(key, "pedestrianStatistics.")) {
851 290 : key = prefixedKey.substr(21);
852 522 : if (key == toString(SUMO_ATTR_NUMBER) || key == toString(SUMO_ATTR_COUNT)) {
853 116 : return toString(myWalkCount);
854 174 : } else if (key == "routeLength") {
855 58 : return toString(getAvgWalkRouteLength());
856 116 : } else if (key == toString(SUMO_ATTR_DURATION)) {
857 58 : return toString(getAvgWalkDuration());
858 58 : } else if (key == toString(SUMO_ATTR_TIMELOSS)) {
859 58 : return toString(getAvgWalkTimeLoss());
860 : }
861 0 : throw InvalidArgument(err);
862 :
863 3480 : } else if (StringUtils::startsWith(key, "rideStatistics.") ||
864 2900 : StringUtils::startsWith(key, "transportStatistics.")) {
865 : int index = 0;
866 2320 : if (StringUtils::startsWith(key, "rideStatistics.")) {
867 1160 : key = prefixedKey.substr(15);
868 : } else {
869 : index = 1;
870 1160 : key = prefixedKey.substr(20);
871 : }
872 2204 : if (key == toString(SUMO_ATTR_NUMBER) || key == toString(SUMO_ATTR_COUNT)) {
873 232 : return toString(myRideCount[index]);
874 928 : } else if (key == toString(SUMO_ATTR_WAITINGTIME)) {
875 116 : return toString(STEPS2TIME(myTotalRideWaitingTime[index] / MAX2(1, myRideCount[index])));
876 812 : } else if (key == "routeLength") {
877 116 : return toString(myTotalRideRouteLength[index] / MAX2(1, myRideCount[index]));
878 696 : } else if (key == toString(SUMO_ATTR_DURATION)) {
879 116 : return toString(myTotalRideRouteLength[index] / MAX2(1, myRideCount[index]));
880 580 : } else if (key == "bus") {
881 116 : return toString(myRideBusCount[index]);
882 464 : } else if (key == "train") {
883 116 : return toString(myRideRailCount[index]);
884 348 : } else if (key == "taxi") {
885 116 : return toString(myRideTaxiCount[index]);
886 232 : } else if (key == "bike") {
887 116 : return toString(myRideBikeCount[index]);
888 116 : } else if (key == "aborted") {
889 116 : return toString(myRideAbortCount[index]);
890 : }
891 0 : throw InvalidArgument(err);
892 : }
893 : // vehicleTripStatistics
894 1160 : if (key == toString(SUMO_ATTR_COUNT)) {
895 116 : return toString(myVehicleCount);
896 1044 : } else if (key == "routeLength") {
897 116 : return toString(getAvgRouteLength());
898 928 : } else if (key == toString(SUMO_ATTR_SPEED)) {
899 116 : return toString(getAvgTripSpeed());
900 812 : } else if (key == toString(SUMO_ATTR_DURATION)) {
901 116 : return toString(getAvgDuration());
902 696 : } else if (key == toString(SUMO_ATTR_WAITINGTIME)) {
903 116 : return toString(getAvgWaitingTime());
904 580 : } else if (key == toString(SUMO_ATTR_TIMELOSS)) {
905 116 : return toString(getAvgTimeLoss());
906 464 : } else if (key == "departDelay") {
907 116 : return toString(getAvgDepartDelay());
908 348 : } else if (key == "departDelayWaiting") {
909 116 : return toString(getAvgDepartDelayWaiting());
910 232 : } else if (key == "totalTravelTime") {
911 : // avoid human readable output
912 116 : return toString(STEPS2TIME((myTotalDuration)));
913 116 : } else if (key == "totalDepartDelay") {
914 116 : return toString(getTotalDepartDelay());
915 : }
916 0 : throw InvalidArgument(err);
917 : }
918 :
919 :
920 : void
921 385 : MSDevice_Tripinfo::saveState(OutputDevice& out) const {
922 : // always write device id to replicate stochastic assignment
923 385 : out.openTag(SUMO_TAG_DEVICE);
924 385 : out.writeAttr(SUMO_ATTR_ID, getID());
925 385 : if (myHolder.hasDeparted()) {
926 300 : std::ostringstream internals;
927 300 : internals << myDepartLane << " ";
928 300 : if (!MSGlobals::gUseMesoSim) {
929 218 : internals << myDepartPosLat << " ";
930 : }
931 300 : std::string state_arrivalLane = myArrivalLane == "" ? STATE_EMPTY_ARRIVALLANE : myArrivalLane;
932 1500 : internals << myDepartSpeed << " " << myRouteLength << " " << myWaitingTime << " " << myAmWaiting << " " << myWaitingCount << " ";
933 600 : internals << myStoppingTime << " " << myParkingStarted << " ";
934 1200 : internals << myArrivalTime << " " << state_arrivalLane << " " << myArrivalPos << " " << myArrivalPosLat << " " << myArrivalSpeed;
935 300 : out.writeAttr(SUMO_ATTR_STATE, internals.str());
936 300 : }
937 385 : out.closeTag();
938 385 : }
939 :
940 :
941 : void
942 346 : MSDevice_Tripinfo::loadState(const SUMOSAXAttributes& attrs) {
943 346 : if (attrs.hasAttribute(SUMO_ATTR_STATE)) {
944 279 : std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
945 279 : bis >> myDepartLane;
946 279 : if (!MSGlobals::gUseMesoSim) {
947 208 : bis >> myDepartPosLat;
948 : }
949 279 : bis >> myDepartSpeed >> myRouteLength >> myWaitingTime >> myAmWaiting >> myWaitingCount;
950 279 : bis >> myStoppingTime >> myParkingStarted;
951 279 : bis >> myArrivalTime >> myArrivalLane >> myArrivalPos >> myArrivalPosLat >> myArrivalSpeed;
952 279 : if (myArrivalLane == STATE_EMPTY_ARRIVALLANE) {
953 : myArrivalLane = "";
954 : }
955 279 : }
956 346 : }
957 :
958 :
959 : /****************************************************************************/
|