SUMO - Simulation of Urban MObility
MSMeanData_Net.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Network state mean data collector for edges/lanes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2004-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
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/MSEdgeControl.h>
34 #include <microsim/MSEdge.h>
35 #include <microsim/MSLane.h>
36 #include <microsim/MSVehicle.h>
37 #include <utils/common/SUMOTime.h>
38 #include <utils/common/ToString.h>
40 #include "MSMeanData_Net.h"
41 #include <limits>
42 
43 #include <microsim/MSGlobals.h>
44 #include <mesosim/MELoop.h>
45 #include <mesosim/MESegment.h>
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // MSMeanData_Net::MSLaneMeanDataValues - methods
53 // ---------------------------------------------------------------------------
55  const double length,
56  const bool doAdd,
57  const MSMeanData_Net* parent)
58  : MSMeanData::MeanDataValues(lane, length, doAdd, parent),
59  nVehDeparted(0), nVehArrived(0), nVehEntered(0), nVehLeft(0),
60  nVehVaporized(0), waitSeconds(0),
61  nVehLaneChangeFrom(0), nVehLaneChangeTo(0),
62  frontSampleSeconds(0), frontTravelledDistance(0),
63  vehLengthSum(0), myParent(parent) {}
64 
65 
67 }
68 
69 
70 void
72  nVehDeparted = 0;
73  nVehArrived = 0;
74  nVehEntered = 0;
75  nVehLeft = 0;
76  nVehVaporized = 0;
78  nVehLaneChangeTo = 0;
79  sampleSeconds = 0.;
81  waitSeconds = 0;
84  vehLengthSum = 0;
85 }
86 
87 
88 void
94  v.nVehLeft += nVehLeft;
104 }
105 
106 
107 void
108 MSMeanData_Net::MSLaneMeanDataValues::notifyMoveInternal(const SUMOVehicle& veh, const double frontOnLane, const double timeOnLane, const double /*meanSpeedFrontOnLane*/, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane) {
109  sampleSeconds += timeOnLane;
110  travelledDistance += travelledDistanceVehicleOnLane;
111  vehLengthSum += veh.getVehicleType().getLength() * timeOnLane;
112  // XXX: recheck, which value to use here for the speed. (Leo) Refs. #2579
113  if (myParent != 0 && meanSpeedVehicleOnLane < myParent->myHaltSpeed) {
114  waitSeconds += timeOnLane;
115  }
116  frontSampleSeconds += frontOnLane;
117  frontTravelledDistance += travelledDistanceFrontOnLane;
118 }
119 
120 
121 bool
122 MSMeanData_Net::MSLaneMeanDataValues::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
123  if ((myParent == 0 || myParent->vehicleApplies(veh)) && (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane())) {
126  }
127  if (reason == MSMoveReminder::NOTIFICATION_ARRIVED) {
128  ++nVehArrived;
129  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
131  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
132  ++nVehLeft;
134  ++nVehVaporized;
135  }
136  }
137  }
139  return false;
140  }
141  return reason == MSMoveReminder::NOTIFICATION_JUNCTION;
142 }
143 
144 
145 bool
147  if (myParent == 0 || myParent->vehicleApplies(veh)) {
148  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
150  ++nVehDeparted;
151  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
153  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
154  ++nVehEntered;
155  }
156  }
157  return true;
158  }
159  return false;
160 }
161 
162 
163 bool
165  return sampleSeconds == 0 && nVehDeparted == 0 && nVehArrived == 0 && nVehEntered == 0
166  && nVehLeft == 0 && nVehVaporized == 0 && nVehLaneChangeFrom == 0 && nVehLaneChangeTo == 0;
167 }
168 
169 
170 void
172  const double numLanes, const double defaultTravelTime, const int numVehicles) const {
173  if (myParent == 0) {
174  if (sampleSeconds > 0) {
175  dev.writeAttr("density", sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength)
176  .writeAttr("occupancy", vehLengthSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
177  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
178  }
179  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft);
180  if (nVehVaporized > 0) {
181  dev.writeAttr("vaporized", nVehVaporized);
182  }
183  dev.closeTag();
184  return;
185  }
187  double overlapTraveltime = myParent->myMaxTravelTime;
188  if (travelledDistance > 0.f) {
189  // one vehicle has to drive lane length + vehicle length before it has left the lane
190  // thus we need to scale with an extended length, approximated by lane length + average vehicle length
191  overlapTraveltime = MIN2(overlapTraveltime, (myLaneLength + vehLengthSum / sampleSeconds) * sampleSeconds / travelledDistance);
192  }
193  if (numVehicles > 0) {
194  dev.writeAttr("traveltime", sampleSeconds / numVehicles).writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
195  } else {
196  double traveltime = myParent->myMaxTravelTime;
197  if (frontTravelledDistance > 0.f) {
198  traveltime = MIN2(traveltime, myLaneLength * frontSampleSeconds / frontTravelledDistance);
199  dev.writeAttr("traveltime", traveltime);
200  } else if (defaultTravelTime >= 0.) {
201  dev.writeAttr("traveltime", defaultTravelTime);
202  }
203  dev.writeAttr("overlapTraveltime", overlapTraveltime)
204  .writeAttr("density", sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength)
205  .writeAttr("occupancy", vehLengthSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
206  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
207  }
208  } else if (defaultTravelTime >= 0.) {
209  dev.writeAttr("traveltime", defaultTravelTime).writeAttr("speed", myLaneLength / defaultTravelTime);
210  }
211  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft)
212  .writeAttr("laneChangedFrom", nVehLaneChangeFrom).writeAttr("laneChangedTo", nVehLaneChangeTo);
213  if (nVehVaporized > 0) {
214  dev.writeAttr("vaporized", nVehVaporized);
215  }
216  dev.closeTag();
217 }
218 
219 // ---------------------------------------------------------------------------
220 // MSMeanData_Net - methods
221 // ---------------------------------------------------------------------------
222 MSMeanData_Net::MSMeanData_Net(const std::string& id,
223  const SUMOTime dumpBegin,
224  const SUMOTime dumpEnd, const bool useLanes,
225  const bool withEmpty, const bool printDefaults,
226  const bool withInternal,
227  const bool trackVehicles,
228  const double maxTravelTime,
229  const double minSamples,
230  const double haltSpeed,
231  const std::string& vTypes)
232  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
233  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
234  myHaltSpeed(haltSpeed) {
235 }
236 
237 
239 
240 
242 MSMeanData_Net::createValues(MSLane* const lane, const double length, const bool doAdd) const {
243  return new MSLaneMeanDataValues(lane, length, doAdd, this);
244 }
245 
246 
247 /****************************************************************************/
248 
Data collector for edges/lanes.
Definition: MSMeanData.h:67
const MSLane * getLane() const
Returns the lane the reminder works on.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
double vehLengthSum
The sum of the lengths the vehicles had.
int nVehEntered
The number of vehicles that entered this lane within the sample interval.
virtual ~MSLaneMeanDataValues()
Destructor.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called if the vehicle leaves the reminder&#39;s lane.
int nVehVaporized
The number of vehicles that left this lane within the sample interval.
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given type.
double frontSampleSeconds
The number of vehicle probes regarding the vehicle front.
const double myMaxTravelTime
the maximum travel time to write
Definition: MSMeanData.h:432
The vehicle arrived at a junction.
void removeFromVehicleUpdateValues(SUMOVehicle &veh)
Notification
Definition of a vehicle state.
const MSMeanData_Net * myParent
The meandata parent.
const double myHaltSpeed
the minimum sample seconds
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.
The vehicle got vaporized.
The vehicle changes the segment (meso only)
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Net *parent)
Constructor.
Data structure for mean (aggregated) edge/lane values.
void write(OutputDevice &dev, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
The vehicle changes lanes (micro only)
int nVehLaneChangeTo
The number of vehicles that changed to this lane.
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:183
Representation of a vehicle.
Definition: SUMOVehicle.h:67
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:76
virtual ~MSMeanData_Net()
Destructor.
The vehicle arrived at its destination (is deleted)
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
T MIN2(T a, T b)
Definition: StdDefs.h:64
const double myLaneLength
The length of the lane / edge the data collector is on.
Definition: MSMeanData.h:175
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
const double myMinSamples
the minimum sample seconds
Definition: MSMeanData.h:429
MSMeanData_Net(const std::string &id, const SUMOTime dumpBegin, const SUMOTime dumpEnd, const bool useLanes, const bool withEmpty, const bool printDefaults, const bool withInternal, const bool trackVehicles, const double maxTravelTime, const double minSamples, const double haltSpeed, const std::string &vTypes)
Constructor.
double waitSeconds
The number of vehicle probes with small speed.
int nVehLeft
The number of vehicles that left this lane within the sample interval.
int nVehArrived
The number of vehicles that finished on the lane.
The vehicle has departed (was inserted into the network)
double frontTravelledDistance
The travelled distance regarding the vehicle front.
int nVehLaneChangeFrom
The number of vehicles that changed from this lane.
double getLength() const
Get vehicle&#39;s length [m].
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
bool isEmpty() const
Returns whether any data was collected.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
Network state mean data collector for edges/lanes.
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
static bool gUseMesoSim
Definition: MSGlobals.h:98
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.