Eclipse SUMO - Simulation of Urban MObility
MSMeanData_Net.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Network state mean data collector for edges/lanes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <microsim/MSEdgeControl.h>
27 #include <microsim/MSEdge.h>
28 #include <microsim/MSLane.h>
29 #include <microsim/MSVehicle.h>
30 #include <utils/common/SUMOTime.h>
31 #include <utils/common/ToString.h>
33 #include "MSMeanData_Net.h"
34 
35 #include <microsim/MSGlobals.h>
36 #include <mesosim/MELoop.h>
37 #include <mesosim/MESegment.h>
38 
39 // ===========================================================================
40 // debug constants
41 // ===========================================================================
42 //#define DEBUG_OCCUPANCY
43 //#define DEBUG_OCCUPANCY2
44 //#define DEBUG_NOTIFY_ENTER
45 //#define DEBUG_COND (veh.getLane()->getID() == "31to211_0")
46 #define DEBUG_COND (false)
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 // ---------------------------------------------------------------------------
53 // MSMeanData_Net::MSLaneMeanDataValues - methods
54 // ---------------------------------------------------------------------------
56  const double length,
57  const bool doAdd,
58  const MSMeanData_Net* parent)
59  : MSMeanData::MeanDataValues(lane, length, doAdd, parent),
60  nVehDeparted(0), nVehArrived(0), nVehEntered(0), nVehLeft(0),
61  nVehVaporized(0), waitSeconds(0),
62  nVehLaneChangeFrom(0), nVehLaneChangeTo(0),
63  frontSampleSeconds(0), frontTravelledDistance(0),
64  vehLengthSum(0), occupationSum(0),
65  minimalVehicleLength(INVALID_DOUBLE),
66  myParent(parent) {}
67 
68 
70 }
71 
72 
73 void
75  nVehDeparted = 0;
76  nVehArrived = 0;
77  nVehEntered = 0;
78  nVehLeft = 0;
79  nVehVaporized = 0;
80  nVehLaneChangeFrom = 0;
81  nVehLaneChangeTo = 0;
82  sampleSeconds = 0.;
83  travelledDistance = 0;
84  waitSeconds = 0;
85  frontSampleSeconds = 0;
86  frontTravelledDistance = 0;
87  vehLengthSum = 0;
88  occupationSum = 0;
89  minimalVehicleLength = INVALID_DOUBLE;
90 }
91 
92 
93 void
96  v.nVehDeparted += nVehDeparted;
97  v.nVehArrived += nVehArrived;
98  v.nVehEntered += nVehEntered;
99  v.nVehLeft += nVehLeft;
100  v.nVehVaporized += nVehVaporized;
101  v.nVehLaneChangeFrom += nVehLaneChangeFrom;
102  v.nVehLaneChangeTo += nVehLaneChangeTo;
103  v.sampleSeconds += sampleSeconds;
104  v.travelledDistance += travelledDistance;
105  v.waitSeconds += waitSeconds;
106  v.frontSampleSeconds += frontSampleSeconds;
107  v.frontTravelledDistance += frontTravelledDistance;
108  v.vehLengthSum += vehLengthSum;
109  v.occupationSum += occupationSum;
111  v.minimalVehicleLength = minimalVehicleLength;
112  } else {
113  v.minimalVehicleLength = MIN2(minimalVehicleLength, v.minimalVehicleLength);
114  }
115 }
116 
117 
118 void
120  const SUMOTrafficObject& veh, const double frontOnLane,
121  const double timeOnLane, const double /* meanSpeedFrontOnLane */,
122  const double meanSpeedVehicleOnLane,
123  const double travelledDistanceFrontOnLane,
124  const double travelledDistanceVehicleOnLane,
125  const double meanLengthOnLane) {
126 #ifdef DEBUG_OCCUPANCY
127  if (DEBUG_COND) {
128  std::cout << SIMTIME << "\n MSMeanData_Net::MSLaneMeanDataValues::notifyMoveInternal()\n"
129  << " veh '" << veh.getID() << "' on lane '" << veh.getLane()->getID() << "'"
130  << ", timeOnLane=" << timeOnLane
131  << ", meanSpeedVehicleOnLane=" << meanSpeedVehicleOnLane
132  << ",\ntravelledDistanceFrontOnLane=" << travelledDistanceFrontOnLane
133  << ", travelledDistanceVehicleOnLane=" << travelledDistanceVehicleOnLane
134  << ", meanLengthOnLane=" << meanLengthOnLane
135  << std::endl;
136  }
137 #endif
138  if (myParent != nullptr && !myParent->vehicleApplies(veh)) {
139  return;
140  }
141  sampleSeconds += timeOnLane;
142  travelledDistance += travelledDistanceVehicleOnLane;
143  vehLengthSum += veh.getVehicleType().getLength() * timeOnLane;
145  // For the mesosim case no information on whether the vehicle was occupying
146  // the lane with its whole length is available. We assume the whole length
147  // Therefore this increment is taken out with more information on the vehicle movement.
148  occupationSum += veh.getVehicleType().getLength() * timeOnLane;
149  } else {
150  // for the microsim case more elaborate calculation of the average length on the lane,
151  // is taken out in notifyMove(), refs #153
152  occupationSum += meanLengthOnLane * TS;
153  }
154  if (myParent != nullptr && meanSpeedVehicleOnLane < myParent->myHaltSpeed) {
155  waitSeconds += timeOnLane;
156  }
157  frontSampleSeconds += frontOnLane;
158  frontTravelledDistance += travelledDistanceFrontOnLane;
159  if (minimalVehicleLength == INVALID_DOUBLE) {
160  minimalVehicleLength = veh.getVehicleType().getLength();
161  } else {
162  minimalVehicleLength = MIN2(minimalVehicleLength, veh.getVehicleType().getLength());
163  }
164 #ifdef DEBUG_OCCUPANCY2
165  // refs #3265
166  std::cout << SIMTIME << "ID: " << getDescription() << " minVehicleLength=" << minimalVehicleLength << std::endl;
167 #endif
168 }
169 
170 
171 bool
173  if ((myParent == nullptr || myParent->vehicleApplies(veh)) && (
174  getLane() == nullptr || !veh.isVehicle() || getLane() == static_cast<MSVehicle&>(veh).getLane())) {
176  removeFromVehicleUpdateValues(veh);
177  }
178  if (reason == MSMoveReminder::NOTIFICATION_ARRIVED) {
179  ++nVehArrived;
180  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
181  ++nVehLaneChangeFrom;
182  } else if (myParent == nullptr || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
183  ++nVehLeft;
185  ++nVehVaporized;
186  }
187  }
188  }
190  return false;
191  }
192  return reason == MSMoveReminder::NOTIFICATION_JUNCTION;
193 }
194 
195 
196 bool
198 #ifdef DEBUG_NOTIFY_ENTER
199  std::cout << "\n" << SIMTIME << " MSMeanData_Net::MSLaneMeanDataValues: veh '" << veh.getID() << "' enters lane '" << enteredLane->getID() << "'" << std::endl;
200 #else
201  UNUSED_PARAMETER(enteredLane);
202 #endif
203  if (myParent == nullptr || myParent->vehicleApplies(veh)) {
204  if (getLane() == nullptr || !veh.isVehicle() || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
206  ++nVehDeparted;
207  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
208  ++nVehLaneChangeTo;
209  } else if (myParent == nullptr || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
210  ++nVehEntered;
211  }
212  }
213  return true;
214  }
215  return false;
216 }
217 
218 
219 bool
221  return sampleSeconds == 0 && nVehDeparted == 0 && nVehArrived == 0 && nVehEntered == 0
222  && nVehLeft == 0 && nVehVaporized == 0 && nVehLaneChangeFrom == 0 && nVehLaneChangeTo == 0;
223 }
224 
225 
226 void
228  const double numLanes, const double defaultTravelTime, const int numVehicles) const {
229 
230 #ifdef DEBUG_OCCUPANCY2
231  // tests #3264
232  double occupancy = occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100;
233  if (occupancy > 100) {
234  std::cout << SIMTIME << " Encountered bad occupancy: " << occupancy
235  << ", myLaneLength=" << myLaneLength << ", period=" << STEPS2TIME(period) << ", occupationSum=" << occupationSum
236  << std::endl;
237  }
238  // refs #3265
239  std::cout << SIMTIME << "ID: " << getDescription() << " minVehicleLength=" << minimalVehicleLength
240  << "\ndensity=" << MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1. / MAX2(minimalVehicleLength, NUMERICAL_EPS)) << std::endl;
241 #endif
242 
243  if (myParent == nullptr) {
244  if (sampleSeconds > 0) {
245  dev.writeAttr("density", MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1000. * numLanes / MAX2(minimalVehicleLength, NUMERICAL_EPS)))
246  .writeAttr("occupancy", occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
247  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
248  }
249  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft);
250  if (nVehVaporized > 0) {
251  dev.writeAttr("vaporized", nVehVaporized);
252  }
253  dev.closeTag();
254  return;
255  }
256  if (sampleSeconds > myParent->myMinSamples) {
257  double overlapTraveltime = myParent->myMaxTravelTime;
258  if (travelledDistance > 0.f) {
259  // one vehicle has to drive lane length + vehicle length before it has left the lane
260  // thus we need to scale with an extended length, approximated by lane length + average vehicle length
261  overlapTraveltime = MIN2(overlapTraveltime, (myLaneLength + vehLengthSum / sampleSeconds) * sampleSeconds / travelledDistance);
262  }
263  if (numVehicles > 0) {
264  dev.writeAttr("traveltime", sampleSeconds / numVehicles).writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
265  } else {
266  double traveltime = myParent->myMaxTravelTime;
267  if (frontTravelledDistance > NUMERICAL_EPS) {
268  traveltime = MIN2(traveltime, myLaneLength * frontSampleSeconds / frontTravelledDistance);
269  dev.writeAttr("traveltime", traveltime);
270  } else if (defaultTravelTime >= 0.) {
271  dev.writeAttr("traveltime", defaultTravelTime);
272  }
273  dev.writeAttr("overlapTraveltime", overlapTraveltime)
274  .writeAttr("density", MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1000. * numLanes / MAX2(minimalVehicleLength, NUMERICAL_EPS)))
275  .writeAttr("occupancy", occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
276  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
277  }
278  } else if (defaultTravelTime >= 0.) {
279  dev.writeAttr("traveltime", defaultTravelTime).writeAttr("speed", myLaneLength / defaultTravelTime);
280  }
281  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft)
282  .writeAttr("laneChangedFrom", nVehLaneChangeFrom).writeAttr("laneChangedTo", nVehLaneChangeTo);
283  if (nVehVaporized > 0) {
284  dev.writeAttr("vaporized", nVehVaporized);
285  }
286  dev.closeTag();
287 }
288 
289 // ---------------------------------------------------------------------------
290 // MSMeanData_Net - methods
291 // ---------------------------------------------------------------------------
292 MSMeanData_Net::MSMeanData_Net(const std::string& id,
293  const SUMOTime dumpBegin,
294  const SUMOTime dumpEnd, const bool useLanes,
295  const bool withEmpty, const bool printDefaults,
296  const bool withInternal,
297  const bool trackVehicles,
298  const int detectPersons,
299  const double maxTravelTime,
300  const double minSamples,
301  const double haltSpeed,
302  const std::string& vTypes)
303  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
304  withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes),
305  myHaltSpeed(haltSpeed) {
306 }
307 
308 
310 
311 
313 MSMeanData_Net::createValues(MSLane* const lane, const double length, const bool doAdd) const {
314  return new MSLaneMeanDataValues(lane, length, doAdd, this);
315 }
316 
317 
318 /****************************************************************************/
319 
MSMeanData_Net::MSLaneMeanDataValues::~MSLaneMeanDataValues
virtual ~MSLaneMeanDataValues()
Destructor.
Definition: MSMeanData_Net.cpp:69
MSMoveReminder::NOTIFICATION_LANE_CHANGE
The vehicle changes lanes (micro only)
Definition: MSMoveReminder.h:97
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:48
ToString.h
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
SUMOTime.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSMeanData_Net::MSLaneMeanDataValues::frontTravelledDistance
double frontTravelledDistance
The travelled distance regarding the vehicle front.
Definition: MSMeanData_Net.h:181
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
NUMERICAL_EPS
#define NUMERICAL_EPS
Definition: config.h:145
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
SUMOTrafficObject::isVehicle
virtual bool isVehicle() const =0
Get the vehicle's ID.
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
MSMeanData_Net::MSLaneMeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData_Net.h:67
MSMeanData_Net::MSLaneMeanDataValues::write
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.
Definition: MSMeanData_Net.cpp:227
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSMeanData_Net::MSLaneMeanDataValues::nVehArrived
int nVehArrived
The number of vehicles that finished on the lane.
Definition: MSMeanData_Net.h:156
MSMeanData_Net::MSLaneMeanDataValues::waitSeconds
double waitSeconds
The number of vehicle probes with small speed.
Definition: MSMeanData_Net.h:168
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:91
MSMeanData_Net::MSLaneMeanDataValues::nVehLeft
int nVehLeft
The number of vehicles that left this lane within the sample interval.
Definition: MSMeanData_Net.h:162
MSEdge.h
MSMeanData_Net::MSLaneMeanDataValues::addTo
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
Definition: MSMeanData_Net.cpp:94
MSMeanData_Net.h
MSMeanData_Net::MSLaneMeanDataValues::reset
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
Definition: MSMeanData_Net.cpp:74
MSMeanData_Net::MSLaneMeanDataValues::notifyLeave
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called if the vehicle leaves the reminder's lane.
Definition: MSMeanData_Net.cpp:172
MSVehicle.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
MSMoveReminder::NOTIFICATION_VAPORIZED
The vehicle got vaporized.
Definition: MSMoveReminder.h:107
MESegment.h
MSMeanData_Net::MSLaneMeanDataValues::nVehLaneChangeFrom
int nVehLaneChangeFrom
The number of vehicles that changed from this lane.
Definition: MSMeanData_Net.h:172
MSMeanData_Net::MSLaneMeanDataValues::occupationSum
double occupationSum
The sum of the occupation of the lane.
Definition: MSMeanData_Net.h:187
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:80
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
MSMeanData
Data collector for edges/lanes.
Definition: MSMeanData.h:60
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
MSMeanData_Net::MSLaneMeanDataValues::nVehVaporized
int nVehVaporized
The number of vehicles that left this lane within the sample interval.
Definition: MSMeanData_Net.h:165
TS
#define TS
Definition: SUMOTime.h:44
MSMeanData_Net::MSLaneMeanDataValues::isEmpty
bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData_Net.cpp:220
MSMeanData_Net::MSLaneMeanDataValues::frontSampleSeconds
double frontSampleSeconds
The number of vehicle probes regarding the vehicle front.
Definition: MSMeanData_Net.h:178
MSMeanData_Net::createValues
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
Definition: MSMeanData_Net.cpp:313
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
MSMeanData::MeanDataValues::travelledDistance
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:176
MSMeanData_Net::MSLaneMeanDataValues::notifyMoveInternal
void notifyMoveInternal(const SUMOTrafficObject &veh, const double frontOnLane, const double timeOnLane, const double, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane)
Internal notification about the vehicle moves.
Definition: MSMeanData_Net.cpp:119
OutputDevice.h
MSGlobals.h
MSMeanData_Net::MSLaneMeanDataValues::minimalVehicleLength
double minimalVehicleLength
minimal vehicle length in the current interval (used to determine a maximal density,...
Definition: MSMeanData_Net.h:190
MSMeanData_Net
Network state mean data collector for edges/lanes.
Definition: MSMeanData_Net.h:58
MSMoveReminder::NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSMoveReminder.h:91
DEBUG_COND
#define DEBUG_COND
Definition: MSMeanData_Net.cpp:46
MSMoveReminder::NOTIFICATION_SEGMENT
The vehicle changes the segment (meso only)
Definition: MSMoveReminder.h:95
MSMeanData::MeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:69
MSDetectorFileOutput::detectPersons
bool detectPersons() const
Definition: MSDetectorFileOutput.h:167
MSEdgeControl.h
MSMoveReminder::NOTIFICATION_ARRIVED
The vehicle arrived at its destination (is deleted)
Definition: MSMoveReminder.h:105
MSVehicleType::getLength
double getLength() const
Get vehicle's length [m].
Definition: MSVehicleType.h:110
MSMeanData::MeanDataValues::sampleSeconds
double sampleSeconds
Definition: MSMeanData.h:173
INVALID_DOUBLE
const double INVALID_DOUBLE
Definition: StdDefs.h:63
MSMeanData_Net::MSLaneMeanDataValues::nVehLaneChangeTo
int nVehLaneChangeTo
The number of vehicles that changed to this lane.
Definition: MSMeanData_Net.h:175
config.h
MSMeanData_Net::myHaltSpeed
const double myHaltSpeed
the minimum sample seconds
Definition: MSMeanData_Net.h:245
MSMeanData_Net::MSLaneMeanDataValues::MSLaneMeanDataValues
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Net *parent)
Constructor.
Definition: MSMeanData_Net.cpp:55
MELoop.h
MSMeanData_Net::MSLaneMeanDataValues::nVehEntered
int nVehEntered
The number of vehicles that entered this lane within the sample interval.
Definition: MSMeanData_Net.h:159
MSLane.h
MSMeanData_Net::MSMeanData_Net
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 int detectPersons, const double maxTravelTime, const double minSamples, const double haltSpeed, const std::string &vTypes)
Constructor.
Definition: MSMeanData_Net.cpp:292
MSMeanData_Net::MSLaneMeanDataValues::nVehDeparted
int nVehDeparted
Definition: MSMeanData_Net.h:153
MSMeanData_Net::MSLaneMeanDataValues::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
Definition: MSMeanData_Net.cpp:197
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:89
MSMoveReminder::NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:93
MSMeanData_Net::~MSMeanData_Net
virtual ~MSMeanData_Net()
Destructor.
Definition: MSMeanData_Net.cpp:309
MSMeanData_Net::MSLaneMeanDataValues::vehLengthSum
double vehLengthSum
The sum of the lengths the vehicles had.
Definition: MSMeanData_Net.h:184