Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSMeanData.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Tue, 17.11.2009
18 : ///
19 : // Data collector for edges/lanes
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 :
24 : #include <vector>
25 : #include <set>
26 : #include <list>
27 : #include <limits>
28 : #include <microsim/output/MSDetectorFileOutput.h>
29 : #include <microsim/MSMoveReminder.h>
30 : #include <utils/common/SUMOTime.h>
31 :
32 :
33 : // ===========================================================================
34 : // class declarations
35 : // ===========================================================================
36 : class OutputDevice;
37 : class MSEdge;
38 : class MSLane;
39 : class SUMOTrafficObject;
40 :
41 : typedef std::vector<MSEdge*> MSEdgeVector;
42 :
43 : // ===========================================================================
44 : // class definitions
45 : // ===========================================================================
46 : /**
47 : * @class MSMeanData
48 : * @brief Data collector for edges/lanes
49 : *
50 : * This structure does not contain the data itself, it is stored within
51 : * MeanDataValues-MoveReminder objects.
52 : * This class is used to build the output, optionally, in the case
53 : * of edge-based dump, aggregated over the edge's lanes.
54 : *
55 : * @todo consider error-handling on write (using IOError)
56 : */
57 : class MSMeanData : public MSDetectorFileOutput {
58 : public:
59 : /**
60 : * @class MeanDataValues
61 : * @brief Data structure for mean (aggregated) edge/lane values
62 : *
63 : * Structure holding values that describe the emissions (XXX: emissions?) aggregated, refs. #2579
64 : * over some seconds.
65 : */
66 : class MeanDataValues : public MSMoveReminder {
67 : public:
68 : /** @brief Constructor */
69 : MeanDataValues(MSLane* const lane, const double length, const bool doAdd, const MSMeanData* const parent);
70 :
71 : /** @brief Destructor */
72 : virtual ~MeanDataValues();
73 :
74 :
75 : /** @brief Resets values so they may be used for the next interval
76 : */
77 : virtual void reset(bool afterWrite = false) = 0;
78 :
79 : /** @brief Add the values of this to the given one and store them there
80 : *
81 : * @param[in] val The meandata to add to
82 : */
83 : virtual void addTo(MeanDataValues& val) const = 0;
84 :
85 :
86 : /** @brief Called if the vehicle enters the reminder's lane
87 : *
88 : * @param[in] veh The entering vehicle.
89 : * @param[in] reason how the vehicle enters the lane
90 : * @see MSMoveReminder
91 : * @see MSMoveReminder::notifyEnter
92 : * @see MSMoveReminder::Notification
93 : */
94 : virtual bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
95 :
96 :
97 : /** @brief Checks whether the reminder still has to be notified about the vehicle moves
98 : *
99 : * Indicator if the reminders is still active for the passed
100 : * vehicle/parameters. If false, the vehicle will erase this reminder
101 : * from its reminder-container.
102 : *
103 : * @param[in] veh Vehicle that asks this reminder.
104 : * @param[in] oldPos Position before move.
105 : * @param[in] newPos Position after move with newSpeed.
106 : * @param[in] newSpeed Moving speed.
107 : *
108 : * @return True if vehicle hasn't passed the reminder completely.
109 : */
110 : bool notifyMove(SUMOTrafficObject& veh, double oldPos,
111 : double newPos, double newSpeed);
112 :
113 :
114 : /** @brief Called if the vehicle leaves the reminder's lane
115 : *
116 : * @param veh The leaving vehicle.
117 : * @param[in] lastPos Position on the lane when leaving.
118 : * @param[in] reason how the vehicle leaves the lane
119 : * @see MSMoveReminder
120 : * @see MSMoveReminder::notifyLeave
121 : */
122 : virtual bool notifyLeave(SUMOTrafficObject& veh, double lastPos,
123 : MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
124 :
125 :
126 : /** @brief Returns whether any data was collected.
127 : *
128 : * @return whether no data was collected
129 : */
130 : virtual bool isEmpty() const;
131 :
132 :
133 : /** @brief Called if a per timestep update is needed. Default does nothing.
134 : */
135 : virtual void update();
136 :
137 : /** @brief Writes output values into the given stream
138 : *
139 : * @param[in] dev The output device to write the data into
140 : * @param[in] period Length of the period the data were gathered
141 : * @param[in] numLanes The total number of lanes for which the data was collected
142 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
143 : */
144 : virtual void write(OutputDevice& dev, const SumoXMLAttrMask& attributeMask, const SUMOTime period,
145 : const int numLanes, const double speedLimit, const double defaultTravelTime,
146 : const int numVehicles = -1) const = 0;
147 :
148 : /** @brief Returns the number of collected sample seconds.
149 : * @return the number of collected sample seconds
150 : */
151 : virtual double getSamples() const;
152 :
153 : /** @brief Returns the total travelled distance.
154 : * @return the total travelled distance
155 : */
156 : double getTravelledDistance() const {
157 493 : return travelledDistance;
158 : }
159 :
160 : SUMOTime getResetTime() const {
161 120 : return resetTime;
162 : }
163 :
164 : double getLaneLength() const {
165 51 : return myLaneLength;
166 : }
167 :
168 : /// @brief return attribute value
169 0 : virtual double getAttributeValue(SumoXMLAttr a, const SUMOTime period, const double numLanes, const double speedLimit) const {
170 : UNUSED_PARAMETER(a);
171 : UNUSED_PARAMETER(period);
172 : UNUSED_PARAMETER(numLanes);
173 : UNUSED_PARAMETER(speedLimit);
174 0 : return 0;
175 : }
176 :
177 : protected:
178 : /// @brief The meandata parent
179 : const MSMeanData* const myParent;
180 :
181 : /// @brief The length of the lane / edge the data collector is on
182 : const double myLaneLength;
183 :
184 : /// @name Collected values
185 : /// @{
186 : /// @brief The number of sampled vehicle movements (in s)
187 : double sampleSeconds;
188 :
189 : /// @brief The sum of the distances the vehicles travelled
190 : double travelledDistance;
191 : //@}
192 :
193 : /// @brief time at which collection was reset;
194 : SUMOTime resetTime;
195 : };
196 :
197 :
198 : /**
199 : * @class MeanDataValueTracker
200 : * @brief Data structure for mean (aggregated) edge/lane values for tracked vehicles
201 : */
202 : class MeanDataValueTracker : public MeanDataValues {
203 : public:
204 : /** @brief Constructor */
205 : MeanDataValueTracker(MSLane* const lane, const double length,
206 : const MSMeanData* const parent);
207 :
208 : /** @brief Destructor */
209 : virtual ~MeanDataValueTracker();
210 :
211 : /** @brief Resets values so they may be used for the next interval
212 : */
213 : void reset(bool afterWrite);
214 :
215 : /** @brief Add the values of this to the given one and store them there
216 : *
217 : * @param[in] val The meandata to add to
218 : */
219 : void addTo(MSMeanData::MeanDataValues& val) const;
220 :
221 : /// @name Methods inherited from MSMoveReminder
222 : /// @{
223 :
224 : /** @brief Internal notification about the vehicle moves
225 : * @see MSMoveReminder::notifyMoveInternal().
226 : */
227 : void notifyMoveInternal(const SUMOTrafficObject& veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane);
228 :
229 :
230 : /** @brief Called if the vehicle leaves the reminder's lane
231 : *
232 : * @param veh The leaving vehicle.
233 : * @param[in] lastPos Position on the lane when leaving.
234 : * @param[in] isArrival whether the vehicle arrived at its destination
235 : * @param[in] isLaneChange whether the vehicle changed from the lane
236 : * @see MSMoveReminder
237 : * @see MSMoveReminder::notifyLeave
238 : */
239 : bool notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
240 :
241 :
242 : /** @brief Computes current values and adds them to their sums
243 : *
244 : * The fraction of time the vehicle is on the lane is computed and
245 : * used as a weight for the vehicle's current values.
246 : * The "emitted" field is incremented, additionally.
247 : *
248 : * @param[in] veh The entering vehicle.
249 : * @param[in] reason how the vehicle enters the lane
250 : * @see MSMoveReminder::notifyEnter
251 : * @return Always true
252 : */
253 : bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
254 : //@}
255 :
256 : bool isEmpty() const;
257 :
258 : /** @brief Writes output values into the given stream
259 : *
260 : * @param[in] dev The output device to write the data into
261 : * @param[in] period Length of the period the data were gathered
262 : * @param[in] numLanes The total number of lanes for which the data was collected
263 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
264 : */
265 : void write(OutputDevice& dev, const SumoXMLAttrMask& attributeMask, const SUMOTime period,
266 : const int numLanes, const double speedLimit, const double defaultTravelTime,
267 : const int numVehicles = -1) const;
268 :
269 : int getNumReady() const;
270 :
271 : void clearFirst();
272 :
273 : double getSamples() const;
274 :
275 : private:
276 : class TrackerEntry {
277 : public:
278 : /** @brief Constructor */
279 : TrackerEntry(MeanDataValues* const values)
280 96688 : : myNumVehicleEntered(0), myNumVehicleLeft(0), myValues(values) {}
281 :
282 : /** @brief Constructor */
283 26328 : virtual ~TrackerEntry() {
284 26328 : delete myValues;
285 26328 : }
286 :
287 : /// @brief The number of vehicles which entered in the current interval
288 : int myNumVehicleEntered;
289 :
290 : /// @brief The number of vehicles which left in the current interval
291 : int myNumVehicleLeft;
292 :
293 : /// @brief The number of vehicles which left in the current interval
294 : MeanDataValues* myValues;
295 : };
296 :
297 : /// @brief The map of vehicles to data entries
298 : std::map<const SUMOTrafficObject*, TrackerEntry*> myTrackedData;
299 :
300 : /// @brief The currently active meandata "intervals"
301 : std::list<TrackerEntry*> myCurrentData;
302 :
303 : };
304 :
305 :
306 : public:
307 : /** @brief Constructor
308 : *
309 : * @param[in] id The id of the detector
310 : * @param[in] dumpBegin Begin time of dump
311 : * @param[in] dumpEnd End time of dump
312 : * @param[in] useLanes Information whether lane-based or edge-based dump shall be generated
313 : * @param[in] excludeEmpty Information if and which empty lanes/edges shall be written
314 : * @param[in] trackVehicles Information whether vehicles shall be tracked
315 : * @param[in] detectPersons Whether pedestrians shall be detected instead of vehicles
316 : * @param[in] maxTravelTime the maximum travel time to use when calculating per vehicle output
317 : * @param[in] defaultEffort the value to use when calculating defaults
318 : * @param[in] minSamples the minimum number of sample seconds before the values are valid
319 : * @param[in] vTypes the set of vehicle types to consider
320 : */
321 : MSMeanData(const std::string& id,
322 : const SUMOTime dumpBegin, const SUMOTime dumpEnd,
323 : const bool useLanes, const std::string& excludeEmpty, const bool withInternal,
324 : const bool trackVehicles, const int detectPersons,
325 : const double minSamples,
326 : const double maxTravelTime,
327 : const std::string& vTypes,
328 : const std::string& writeAttributes,
329 : const std::vector<MSEdge*>& edges,
330 : AggregateType aggregate);
331 :
332 :
333 : /// @brief Destructor
334 : virtual ~MSMeanData();
335 :
336 : /** @brief Adds the value collectors to all relevant edges.
337 : */
338 : void init();
339 :
340 : /// @name Methods inherited from MSDetectorFileOutput.
341 : /// @{
342 :
343 : /** @brief Writes collected values into the given stream
344 : *
345 : * At first, it is checked whether the values for the current interval shall be written.
346 : * If not, a reset is performed, only, using "resetOnly". Otherwise,
347 : * both the list of single-lane edges and the list of multi-lane edges
348 : * are gone through and each edge is written using "writeEdge".
349 : *
350 : * @param[in] dev The output device to write the data into
351 : * @param[in] startTime First time step the data were gathered
352 : * @param[in] stopTime Last time step the data were gathered
353 : * @see MSDetectorFileOutput::writeXMLOutput
354 : * @see write
355 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
356 : */
357 : void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
358 :
359 : /** @brief Opens the XML-output using "netstats" as root element
360 : *
361 : * @param[in] dev The output device to write the root into
362 : * @see MSDetectorFileOutput::writeXMLDetectorProlog
363 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
364 : */
365 : virtual void writeXMLDetectorProlog(OutputDevice& dev) const;
366 : /// @}
367 :
368 : /** @brief Updates the detector
369 : */
370 : virtual void detectorUpdate(const SUMOTime step);
371 :
372 : double getMinSamples() const {
373 39645 : return myMinSamples;
374 : }
375 :
376 : double getMaxTravelTime() const {
377 32204 : return myMaxTravelTime;
378 : }
379 :
380 : bool isEdgeData() const {
381 : return myAmEdgeBased;
382 : }
383 :
384 : /// @brief return all attributes that are (potentially) written by this output
385 0 : virtual std::vector<std::string> getAttributeNames() const {
386 0 : return std::vector<std::string>();
387 : }
388 :
389 : /// @brief return attribute value for the given lane
390 0 : virtual double getAttributeValue(const MSLane* lane, SumoXMLAttr a, double defaultValue) const {
391 : UNUSED_PARAMETER(lane);
392 : UNUSED_PARAMETER(a);
393 0 : return defaultValue;
394 : }
395 :
396 : /// @brief retrieve all MeanDataValues
397 : const std::vector<MSMoveReminder*> getReminders() const;
398 :
399 : protected:
400 : /** @brief Create an instance of MeanDataValues
401 : *
402 : * @param[in] lane The lane to create for
403 : * @param[in] doAdd whether to add the values as reminder to the lane
404 : */
405 : virtual MSMeanData::MeanDataValues* createValues(MSLane* const lane, const double length, const bool doAdd) const = 0;
406 :
407 : /** @brief Resets network value in order to allow processing of the next interval
408 : *
409 : * Goes through the lists of edges and starts "resetOnly" for each edge.
410 : * @param[in] edge The last time step that is reported
411 : */
412 : void resetOnly(SUMOTime stopTime);
413 :
414 : /** @brief Return the relevant edge id
415 : *
416 : * @param[in] edge The edge to retrieve the id for
417 : */
418 : virtual std::string getEdgeID(const MSEdge* const edge);
419 :
420 : /** @brief Writes edge values into the given stream
421 : *
422 : * microsim: It is checked whether the dump shall be generated edge-
423 : * or lane-wise. In the first case, the lane-data are collected
424 : * and aggregated and written directly. In the second case, "writeLane"
425 : * is used to write each lane's state.
426 : *
427 : * @param[in] dev The output device to write the data into
428 : * @param[in] edgeValues List of this edge's value collectors
429 : * @param[in] edge The edge to write the dump of
430 : * @param[in] startTime First time step the data were gathered
431 : * @param[in] stopTime Last time step the data were gathered
432 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
433 : */
434 : void writeEdge(OutputDevice& dev, const std::vector<MeanDataValues*>& edgeValues,
435 : const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime);
436 :
437 :
438 : /** @brief Writes aggregated data of all edge values into the given stream
439 : *
440 : * microsim: It is checked whether the dump shall be generated edge-
441 : * or lane-wise. In the first case, the lane-data are collected
442 : * and aggregated and written directly. In the second case, "writeLane"
443 : * is used to write each lane's state.
444 : *
445 : * @param[in] dev The output device to write the data into
446 : * @param[in] startTime First time step the data were gathered
447 : * @param[in] stopTime Last time step the data were gathered
448 : */
449 : void writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
450 :
451 : /** @brief Writes aggregated data for each TAZ into the given stream
452 : *
453 : * microsim: It is checked whether the dump shall be generated edge-
454 : * or lane-wise. In the first case, the lane-data are collected
455 : * and aggregated and written directly. In the second case, "writeLane"
456 : * is used to write each lane's state.
457 : *
458 : * @param[in] dev The output device to write the data into
459 : * @param[in] startTime First time step the data were gathered
460 : * @param[in] stopTime Last time step the data were gathered
461 : */
462 : void writeAggregatedTAZ(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
463 :
464 : /** @brief Writes the interval opener
465 : *
466 : * @param[in] dev The output device to write the data into
467 : * @param[in] startTime First time step the data were gathered
468 : * @param[in] stopTime Last time step the data were gathered
469 : */
470 : virtual void openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime);
471 :
472 : /** @brief Writes the surrounding element into the given stream
473 : *
474 : * @param[in] dev The output device to write the data into
475 : * @param[in] values The values to check for emptiness
476 : * @param[in] tag The xml tag to write (lane / edge)
477 : * @param[in] id The id for the lane / edge to write
478 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
479 : */
480 : virtual void writePrefix(OutputDevice& dev, const MeanDataValues& values,
481 : const SumoXMLTag tag, const std::string id) const;
482 :
483 :
484 : protected:
485 : const std::vector<MeanDataValues*>* getEdgeValues(const MSEdge* edge) const;
486 :
487 : protected:
488 : /// @brief the minimum sample seconds
489 : const double myMinSamples;
490 :
491 : /// @brief the maximum travel time to write
492 : const double myMaxTravelTime;
493 :
494 : /// @brief Value collectors; sorted by edge, then by lane
495 : std::vector<std::vector<MeanDataValues*> > myMeasures;
496 :
497 : /// @brief Whether empty lanes/edges shall be written
498 : bool myDumpEmpty = true;
499 :
500 : /// @brief Information whether the output shall be edge-based (not lane-based)
501 : const bool myAmEdgeBased;
502 :
503 : private:
504 : /// @brief The first and the last time step to write information (-1 indicates always)
505 : const SUMOTime myDumpBegin, myDumpEnd;
506 :
507 : /// @brief time at which init was called();
508 : SUMOTime myInitTime;
509 :
510 : /// @brief The corresponding first edges
511 : MSEdgeVector myEdges;
512 :
513 : ConstMSEdgeVector myTAZ;
514 :
515 : /// @brief The index in myEdges / myMeasures
516 : std::map<const MSEdge*, int> myEdgeIndex;
517 :
518 : /// @brief Whether empty lanes/edges shall be written with default values
519 : bool myPrintDefaults = false;
520 :
521 : /// @brief Whether only empty lanes/edges which have been modified shall be written
522 : bool myPrintModified = false;
523 :
524 : /// @brief Whether internal lanes/edges shall be written
525 : const bool myDumpInternal;
526 :
527 : /// @brief Whether vehicles are tracked
528 : const bool myTrackVehicles;
529 :
530 : /// @brief bit mask for checking attributes to be written
531 : const SumoXMLAttrMask myWrittenAttributes;
532 :
533 : /// @brief whether the data for all edges shall be aggregated
534 : const AggregateType myAggregate;
535 :
536 : /// @brief The intervals for which output still has to be generated (only in the tracking case)
537 : std::list< std::pair<SUMOTime, SUMOTime> > myPendingIntervals;
538 :
539 : private:
540 : /// @brief Invalidated copy constructor.
541 : MSMeanData(const MSMeanData&);
542 :
543 : /// @brief Invalidated assignment operator.
544 : MSMeanData& operator=(const MSMeanData&);
545 :
546 : };
|