SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSMeanData_Net.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Network state mean data collector for edges/lanes
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <microsim/MSEdgeControl.h>
33 #include <microsim/MSEdge.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSVehicle.h>
36 #include <utils/common/SUMOTime.h>
37 #include <utils/common/ToString.h>
39 #include "MSMeanData_Net.h"
40 #include <limits>
41 
42 #ifdef HAVE_INTERNAL
43 #include <microsim/MSGlobals.h>
44 #include <mesosim/MELoop.h>
45 #include <mesosim/MESegment.h>
46 #endif
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // MSMeanData_Net::MSLaneMeanDataValues - methods
58 // ---------------------------------------------------------------------------
60  const SUMOReal length,
61  const bool doAdd,
62  const std::set<std::string>* const vTypes,
63  const MSMeanData_Net* parent)
64  : MSMeanData::MeanDataValues(lane, length, doAdd, vTypes),
65  nVehDeparted(0), nVehArrived(0), nVehEntered(0), nVehLeft(0),
66  nVehVaporized(0), waitSeconds(0),
67  nVehLaneChangeFrom(0), nVehLaneChangeTo(0),
68  vehLengthSum(0), myParent(parent) {}
69 
70 
72 }
73 
74 
75 void
77  nVehDeparted = 0;
78  nVehArrived = 0;
79  nVehEntered = 0;
80  nVehLeft = 0;
81  nVehVaporized = 0;
82  nVehLaneChangeFrom = 0;
83  nVehLaneChangeTo = 0;
84  sampleSeconds = 0.;
85  travelledDistance = 0;
86  waitSeconds = 0;
87  vehLengthSum = 0;
88 }
89 
90 
91 void
94  v.nVehDeparted += nVehDeparted;
95  v.nVehArrived += nVehArrived;
96  v.nVehEntered += nVehEntered;
97  v.nVehLeft += nVehLeft;
98  v.nVehVaporized += nVehVaporized;
99  v.nVehLaneChangeFrom += nVehLaneChangeFrom;
100  v.nVehLaneChangeTo += nVehLaneChangeTo;
101  v.sampleSeconds += sampleSeconds;
102  v.travelledDistance += travelledDistance;
103  v.waitSeconds += waitSeconds;
104  v.vehLengthSum += vehLengthSum;
105 }
106 
107 
108 void
110  sampleSeconds += timeOnLane;
111  travelledDistance += speed * timeOnLane;
112  vehLengthSum += veh.getVehicleType().getLength() * timeOnLane;
113  if (myParent != 0 && speed < myParent->myHaltSpeed) {
114  waitSeconds += timeOnLane;
115  }
116 }
117 
118 
119 bool
121  if (vehicleApplies(veh) && (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane())) {
122 #ifdef HAVE_INTERNAL
124  myLastVehicleUpdateValues.erase(&veh);
125  }
126 #endif
127  if (reason == MSMoveReminder::NOTIFICATION_ARRIVED) {
128  ++nVehArrived;
129  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
130  ++nVehLaneChangeFrom;
131  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
132  ++nVehLeft;
134  ++nVehVaporized;
135  }
136  }
137  }
138 #ifdef HAVE_INTERNAL
140  return false;
141  }
142 #endif
143  return reason == MSMoveReminder::NOTIFICATION_JUNCTION;
144 }
145 
146 
147 bool
149  if (vehicleApplies(veh)) {
150  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
152  ++nVehDeparted;
153  } else if (reason == MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
154  ++nVehLaneChangeTo;
155  } else if (myParent == 0 || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
156  ++nVehEntered;
157  }
158  }
159  return true;
160  }
161  return false;
162 }
163 
164 
165 bool
167  return sampleSeconds == 0 && nVehDeparted == 0 && nVehArrived == 0 && nVehEntered == 0
168  && nVehLeft == 0 && nVehVaporized == 0 && nVehLaneChangeFrom == 0 && nVehLaneChangeTo == 0;
169 }
170 
171 
172 void
174  const SUMOReal numLanes, const SUMOReal defaultTravelTime, const int numVehicles) const {
175  if (myParent == 0) {
176  if (sampleSeconds > 0) {
177  dev.writeAttr("density", sampleSeconds / STEPS2TIME(period) * (SUMOReal) 1000 / myLaneLength)
178  .writeAttr("occupancy", vehLengthSum / STEPS2TIME(period) / myLaneLength / numLanes * (SUMOReal) 100)
179  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
180  }
181  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft);
182  if (nVehVaporized > 0) {
183  dev.writeAttr("vaporized", nVehVaporized);
184  }
185  dev.closeTag();
186  return;
187  }
188  if (sampleSeconds > myParent->myMinSamples) {
189  SUMOReal traveltime = myParent->myMaxTravelTime;
190  if (travelledDistance > 0.f) {
191  traveltime = MIN2(traveltime, myLaneLength * sampleSeconds / travelledDistance);
192  }
193  if (numVehicles > 0) {
194  dev.writeAttr("traveltime", sampleSeconds / numVehicles).writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
195  } else {
196  dev.writeAttr("traveltime", traveltime)
197  .writeAttr("density", sampleSeconds / STEPS2TIME(period) * (SUMOReal) 1000 / myLaneLength)
198  .writeAttr("occupancy", vehLengthSum / STEPS2TIME(period) / myLaneLength / numLanes * (SUMOReal) 100)
199  .writeAttr("waitingTime", waitSeconds).writeAttr("speed", travelledDistance / sampleSeconds);
200  }
201  } else if (defaultTravelTime >= 0.) {
202  dev.writeAttr("traveltime", defaultTravelTime).writeAttr("speed", myLaneLength / defaultTravelTime);
203  }
204  dev.writeAttr("departed", nVehDeparted).writeAttr("arrived", nVehArrived).writeAttr("entered", nVehEntered).writeAttr("left", nVehLeft)
205  .writeAttr("laneChangedFrom", nVehLaneChangeFrom).writeAttr("laneChangedTo", nVehLaneChangeTo);
206  if (nVehVaporized > 0) {
207  dev.writeAttr("vaporized", nVehVaporized);
208  }
209  dev.closeTag();
210 }
211 
212 // ---------------------------------------------------------------------------
213 // MSMeanData_Net - methods
214 // ---------------------------------------------------------------------------
215 MSMeanData_Net::MSMeanData_Net(const std::string& id,
216  const SUMOTime dumpBegin,
217  const SUMOTime dumpEnd, const bool useLanes,
218  const bool withEmpty, const bool printDefaults,
219  const bool withInternal,
220  const bool trackVehicles,
221  const SUMOReal maxTravelTime,
222  const SUMOReal minSamples,
223  const SUMOReal haltSpeed,
224  const std::set<std::string> vTypes)
225  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
226  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
227  myHaltSpeed(haltSpeed) {
228 }
229 
230 
232 
233 
235 MSMeanData_Net::createValues(MSLane* const lane, const SUMOReal length, const bool doAdd) const {
236  return new MSLaneMeanDataValues(lane, length, doAdd, &myVehicleTypes, this);
237 }
238 
239 
240 /****************************************************************************/
241 
Data collector for edges/lanes.
Definition: MSMeanData.h:66
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
virtual ~MSLaneMeanDataValues()
Destructor.
MSLaneMeanDataValues(MSLane *const lane, const SUMOReal length, const bool doAdd, const std::set< std::string > *const vTypes=0, const MSMeanData_Net *parent=0)
Constructor.
void write(OutputDevice &dev, const SUMOTime period, const SUMOReal numLanes, const SUMOReal defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
unsigned nVehVaporized
The number of vehicles that left this lane within the sample interval.
The vehicle arrived at a junction.
SUMOReal travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:180
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
Notification
Definition of a vehicle state.
SUMOReal getLength() const
Get vehicle&#39;s length [m].
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 SUMOReal maxTravelTime, const SUMOReal minSamples, const SUMOReal haltSpeed, const std::set< std::string > vTypes)
Constructor.
The vehicle got vaporized.
The vehicle changes the segment (meso only)
unsigned nVehLaneChangeFrom
The number of vehicles that changed from this lane.
const std::set< std::string > myVehicleTypes
The vehicle types to look for (empty means all)
Definition: MSMeanData.h:420
Data structure for mean (aggregated) edge/lane values.
SUMOReal waitSeconds
The number of vehicle probes with small speed.
The vehicle changes lanes (micro only)
SUMOReal vehLengthSum
The sum of the lengths the vehicles had.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Computes current values and adds them to their sums.
Representation of a vehicle.
Definition: SUMOVehicle.h:63
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:75
void notifyMoveInternal(SUMOVehicle &veh, SUMOReal timeOnLane, SUMOReal speed)
Internal notification about the vehicle moves.
unsigned nVehArrived
The number of vehicles that finished on the lane.
unsigned nVehLaneChangeTo
The number of vehicles that changed to this lane.
unsigned nVehEntered
The number of vehicles that entered this lane within the sample interval.
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:57
const SUMOReal myHaltSpeed
the minimum sample seconds
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Called if the vehicle leaves the reminder&#39;s lane.
The vehicle has departed (was inserted into the network)
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const SUMOReal length, const bool doAdd) const
Create an instance of MeanDataValues.
unsigned nVehLeft
The number of vehicles that left this lane within the sample interval.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
Network state mean data collector for edges/lanes.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
static const bool gUseMesoSim
Definition: MSGlobals.h:98
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.