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