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 
42 #include <microsim/MSGlobals.h>
43 #include <mesosim/MELoop.h>
44 #include <mesosim/MESegment.h>
45 
46 // ===========================================================================
47 // debug constants
48 // ===========================================================================
49 //#define DEBUG_OCCUPANCY
50 //#define DEBUG_OCCUPANCY2
51 //#define DEBUG_NOTIFY_ENTER
52 //#define DEBUG_COND (veh.getLane()->getID() == "31to211_0")
53 #define DEBUG_COND (false)
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 // ---------------------------------------------------------------------------
60 // MSMeanData_Net::MSLaneMeanDataValues - methods
61 // ---------------------------------------------------------------------------
63  const double length,
64  const bool doAdd,
65  const MSMeanData_Net* parent)
66  : MSMeanData::MeanDataValues(lane, length, doAdd, parent),
67  nVehDeparted(0), nVehArrived(0), nVehEntered(0), nVehLeft(0),
68  nVehVaporized(0), waitSeconds(0),
69  nVehLaneChangeFrom(0), nVehLaneChangeTo(0),
70  frontSampleSeconds(0), frontTravelledDistance(0),
71  vehLengthSum(0), occupationSum(0),
72  minimalVehicleLength(INVALID_DOUBLE),
73  myParent(parent) {}
74 
75 
77 }
78 
79 
80 void
82  nVehDeparted = 0;
83  nVehArrived = 0;
84  nVehEntered = 0;
85  nVehLeft = 0;
86  nVehVaporized = 0;
88  nVehLaneChangeTo = 0;
89  sampleSeconds = 0.;
91  waitSeconds = 0;
94  vehLengthSum = 0;
95  occupationSum = 0;
97 }
98 
99 
100 void
106  v.nVehLeft += nVehLeft;
119  } else {
121  }
122 }
123 
124 
125 void
127  const SUMOVehicle& veh, const double frontOnLane,
128  const double timeOnLane, const double /* meanSpeedFrontOnLane */,
129  const double meanSpeedVehicleOnLane,
130  const double travelledDistanceFrontOnLane,
131  const double travelledDistanceVehicleOnLane,
132  const double meanLengthOnLane) {
133 #ifdef DEBUG_OCCUPANCY
134  if DEBUG_COND {
135  std::cout << SIMTIME << "\n MSMeanData_Net::MSLaneMeanDataValues::notifyMoveInternal()\n"
136  << " veh '" << veh.getID() << "' on lane '" << veh.getLane()->getID() << "'"
137  << ", timeOnLane=" << timeOnLane
138  << ", meanSpeedVehicleOnLane=" << meanSpeedVehicleOnLane
139  << ",\ntravelledDistanceFrontOnLane=" << travelledDistanceFrontOnLane
140  << ", travelledDistanceVehicleOnLane=" << travelledDistanceVehicleOnLane
141  << ", meanLengthOnLane=" << meanLengthOnLane
142  << std::endl;
143  }
144 #endif
145  sampleSeconds += timeOnLane;
146  travelledDistance += travelledDistanceVehicleOnLane;
147  vehLengthSum += veh.getVehicleType().getLength() * timeOnLane;
149  // For the mesosim case no information on whether the vehicle was occupying
150  // the lane with its whole length is available. We assume the whole length
151  // Therefore this increment is taken out with more information on the vehicle movement.
152  occupationSum += veh.getVehicleType().getLength() * timeOnLane;
153  } else {
154  // for the microsim case more elaborate calculation of the average length on the lane,
155  // is taken out in notifyMove(), refs #153
156  occupationSum += meanLengthOnLane * TS;
157  }
158  if (myParent != 0 && meanSpeedVehicleOnLane < myParent->myHaltSpeed) {
159  waitSeconds += timeOnLane;
160  }
161  frontSampleSeconds += frontOnLane;
162  frontTravelledDistance += travelledDistanceFrontOnLane;
165  } else {
167  }
168 #ifdef DEBUG_OCCUPANCY2
169  // refs #3265
170  std::cout << SIMTIME << "ID: " << getDescription() << " minVehicleLength=" << minimalVehicleLength << std::endl;
171 #endif
172 }
173 
174 
175 bool
176 MSMeanData_Net::MSLaneMeanDataValues::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
177  if ((myParent == 0 || myParent->vehicleApplies(veh)) && (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane())) {
180  }
181  if (reason == MSMoveReminder::NOTIFICATION_ARRIVED) {
182  ++nVehArrived;
183  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
185  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
186  ++nVehLeft;
188  ++nVehVaporized;
189  }
190  }
191  }
193  return false;
194  }
195  return reason == MSMoveReminder::NOTIFICATION_JUNCTION;
196 }
197 
198 
199 bool
201 #ifdef DEBUG_NOTIFY_ENTER
202  std::cout << "\n" << SIMTIME << " MSMeanData_Net::MSLaneMeanDataValues: veh '" << veh.getID() << "' enters lane '" << enteredLane->getID() << "'" << std::endl;
203 #else
204  UNUSED_PARAMETER(enteredLane);
205 #endif
206  if (myParent == 0 || myParent->vehicleApplies(veh)) {
207  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
209  ++nVehDeparted;
210  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
212  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
213  ++nVehEntered;
214  }
215  }
216  return true;
217  }
218  return false;
219 }
220 
221 
222 bool
224  return sampleSeconds == 0 && nVehDeparted == 0 && nVehArrived == 0 && nVehEntered == 0
225  && nVehLeft == 0 && nVehVaporized == 0 && nVehLaneChangeFrom == 0 && nVehLaneChangeTo == 0;
226 }
227 
228 
229 void
231  const double numLanes, const double defaultTravelTime, const int numVehicles) const {
232 
233 #ifdef DEBUG_OCCUPANCY2
234  // tests #3264
235  double occupancy = occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100;
236  if (occupancy > 100) {
237  std::cout << SIMTIME << " Encountered bad occupancy: " << occupancy
238  << ", myLaneLength=" << myLaneLength << ", period=" << STEPS2TIME(period) << ", occupationSum=" << occupationSum
239  << std::endl;
240  }
241  // refs #3265
242  std::cout << SIMTIME << "ID: " << getDescription() << " minVehicleLength=" << minimalVehicleLength
243  << "\ndensity=" << MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1. / MAX2(minimalVehicleLength, NUMERICAL_EPS)) << std::endl;
244 #endif
245 
246  if (myParent == 0) {
247  if (sampleSeconds > 0) {
248  dev.writeAttr("density", MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1000. / MAX2(minimalVehicleLength, NUMERICAL_EPS)))
249  .writeAttr("occupancy", occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
250  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
251  }
252  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft);
253  if (nVehVaporized > 0) {
254  dev.writeAttr("vaporized", nVehVaporized);
255  }
256  dev.closeTag();
257  return;
258  }
260  double overlapTraveltime = myParent->myMaxTravelTime;
261  if (travelledDistance > 0.f) {
262  // one vehicle has to drive lane length + vehicle length before it has left the lane
263  // thus we need to scale with an extended length, approximated by lane length + average vehicle length
264  overlapTraveltime = MIN2(overlapTraveltime, (myLaneLength + vehLengthSum / sampleSeconds) * sampleSeconds / travelledDistance);
265  }
266  if (numVehicles > 0) {
267  dev.writeAttr("traveltime", sampleSeconds / numVehicles).writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
268  } else {
269  double traveltime = myParent->myMaxTravelTime;
270  if (frontTravelledDistance > 0.f) {
271  traveltime = MIN2(traveltime, myLaneLength * frontSampleSeconds / frontTravelledDistance);
272  dev.writeAttr("traveltime", traveltime);
273  } else if (defaultTravelTime >= 0.) {
274  dev.writeAttr("traveltime", defaultTravelTime);
275  }
276  dev.writeAttr("overlapTraveltime", overlapTraveltime)
277  .writeAttr("density", MIN2(sampleSeconds / STEPS2TIME(period) * (double) 1000 / myLaneLength, 1000. / MAX2(minimalVehicleLength, NUMERICAL_EPS)))
278  .writeAttr("occupancy", occupationSum / STEPS2TIME(period) / myLaneLength / numLanes * (double) 100)
279  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
280  }
281  } else if (defaultTravelTime >= 0.) {
282  dev.writeAttr("traveltime", defaultTravelTime).writeAttr("speed", myLaneLength / defaultTravelTime);
283  }
284  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft)
285  .writeAttr("laneChangedFrom", nVehLaneChangeFrom).writeAttr("laneChangedTo", nVehLaneChangeTo);
286  if (nVehVaporized > 0) {
287  dev.writeAttr("vaporized", nVehVaporized);
288  }
289  dev.closeTag();
290 }
291 
292 // ---------------------------------------------------------------------------
293 // MSMeanData_Net - methods
294 // ---------------------------------------------------------------------------
295 MSMeanData_Net::MSMeanData_Net(const std::string& id,
296  const SUMOTime dumpBegin,
297  const SUMOTime dumpEnd, const bool useLanes,
298  const bool withEmpty, const bool printDefaults,
299  const bool withInternal,
300  const bool trackVehicles,
301  const double maxTravelTime,
302  const double minSamples,
303  const double haltSpeed,
304  const std::string& vTypes)
305  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
306  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
307  myHaltSpeed(haltSpeed) {
308 }
309 
310 
312 
313 
315 MSMeanData_Net::createValues(MSLane* const lane, const double length, const bool doAdd) const {
316  return new MSLaneMeanDataValues(lane, length, doAdd, this);
317 }
318 
319 
320 /****************************************************************************/
321 
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
Data collector for edges/lanes.
Definition: MSMeanData.h:67
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
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
const std::string & getDescription() const
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.
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.
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given type.
void removeFromVehicleUpdateValues(SUMOVehicle &veh)
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
Notification
Definition of a vehicle state.
const MSMeanData_Net * myParent
The meandata parent.
const double myHaltSpeed
the minimum sample seconds
T MAX2(T a, T b)
Definition: StdDefs.h:74
The vehicle got vaporized.
The vehicle changes the segment (meso only)
#define TS
Definition: SUMOTime.h:52
#define DEBUG_COND
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Net *parent)
Constructor.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
Data structure for mean (aggregated) edge/lane values.
const MSLane * getLane() const
Returns the lane the reminder works on.
#define SIMTIME
Definition: SUMOTime.h:70
const std::string & getID() const
Returns the id.
Definition: Named.h:66
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
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.
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
bool isEmpty() const
Returns whether any data was collected.
T MIN2(T a, T b)
Definition: StdDefs.h:68
void notifyMoveInternal(const SUMOVehicle &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.
const double myLaneLength
The length of the lane / edge the data collector is on.
Definition: MSMeanData.h:175
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
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.
double getLength() const
Get vehicle&#39;s length [m].
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.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
double occupationSum
The sum of the occupation of the lane.
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
#define NUMERICAL_EPS
Definition: config.h:152
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
const double INVALID_DOUBLE
Definition: StdDefs.h:60
double minimalVehicleLength
minimal vehicle length in the current interval (used to determine a maximal density, see #3265)
static bool gUseMesoSim
Definition: MSGlobals.h:98
Representation of a lane in the micro simulation.
Definition: MSLane.h:79