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 122 : return resetTime;
162 : }
163 :
164 : double getLaneLength() const {
165 53 : 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 : MeanDataValues* getSubData() {
178 1025729 : return mySubData;
179 : }
180 :
181 : void setSubData(MeanDataValues* subData) {
182 42 : mySubData = subData;
183 280 : }
184 :
185 : protected:
186 : /// @brief The meandata parent
187 : const MSMeanData* const myParent;
188 :
189 : /// @brief The length of the lane / edge the data collector is on
190 : const double myLaneLength;
191 :
192 : /// @name Collected values
193 : /// @{
194 : /// @brief The number of sampled vehicle movements (in s)
195 : double sampleSeconds;
196 :
197 : /// @brief The sum of the distances the vehicles travelled
198 : double travelledDistance;
199 : //@}
200 :
201 : /// @brief time at which collection was reset;
202 : SUMOTime resetTime;
203 :
204 : /// @brief additional data to collect and write
205 : MeanDataValues* mySubData = nullptr;
206 : };
207 :
208 :
209 : /**
210 : * @class MeanDataValueTracker
211 : * @brief Data structure for mean (aggregated) edge/lane values for tracked vehicles
212 : */
213 : class MeanDataValueTracker : public MeanDataValues {
214 : public:
215 : /** @brief Constructor */
216 : MeanDataValueTracker(MSLane* const lane, const double length,
217 : const MSMeanData* const parent);
218 :
219 : /** @brief Destructor */
220 : virtual ~MeanDataValueTracker();
221 :
222 : /** @brief Resets values so they may be used for the next interval
223 : */
224 : void reset(bool afterWrite);
225 :
226 : /** @brief Add the values of this to the given one and store them there
227 : *
228 : * @param[in] val The meandata to add to
229 : */
230 : void addTo(MSMeanData::MeanDataValues& val) const;
231 :
232 : /// @name Methods inherited from MSMoveReminder
233 : /// @{
234 :
235 : /** @brief Internal notification about the vehicle moves
236 : * @see MSMoveReminder::notifyMoveInternal().
237 : */
238 : 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);
239 :
240 :
241 : /** @brief Called if the vehicle leaves the reminder's lane
242 : *
243 : * @param veh The leaving vehicle.
244 : * @param[in] lastPos Position on the lane when leaving.
245 : * @param[in] isArrival whether the vehicle arrived at its destination
246 : * @param[in] isLaneChange whether the vehicle changed from the lane
247 : * @see MSMoveReminder
248 : * @see MSMoveReminder::notifyLeave
249 : */
250 : bool notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
251 :
252 :
253 : /** @brief Computes current values and adds them to their sums
254 : *
255 : * The fraction of time the vehicle is on the lane is computed and
256 : * used as a weight for the vehicle's current values.
257 : * The "emitted" field is incremented, additionally.
258 : *
259 : * @param[in] veh The entering vehicle.
260 : * @param[in] reason how the vehicle enters the lane
261 : * @see MSMoveReminder::notifyEnter
262 : * @return Always true
263 : */
264 : bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
265 : //@}
266 :
267 : bool isEmpty() const;
268 :
269 : /** @brief Writes output values into the given stream
270 : *
271 : * @param[in] dev The output device to write the data into
272 : * @param[in] period Length of the period the data were gathered
273 : * @param[in] numLanes The total number of lanes for which the data was collected
274 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
275 : */
276 : void write(OutputDevice& dev, const SumoXMLAttrMask& attributeMask, const SUMOTime period,
277 : const int numLanes, const double speedLimit, const double defaultTravelTime,
278 : const int numVehicles = -1) const;
279 :
280 : int getNumReady() const;
281 :
282 : void clearFirst();
283 :
284 : double getSamples() const;
285 :
286 : private:
287 : class TrackerEntry {
288 : public:
289 : /** @brief Constructor */
290 : TrackerEntry(MeanDataValues* const values)
291 96688 : : myNumVehicleEntered(0), myNumVehicleLeft(0), myValues(values) {}
292 :
293 : /** @brief Constructor */
294 96688 : virtual ~TrackerEntry() {
295 96688 : delete myValues;
296 96688 : }
297 :
298 : /// @brief The number of vehicles which entered in the current interval
299 : int myNumVehicleEntered;
300 :
301 : /// @brief The number of vehicles which left in the current interval
302 : int myNumVehicleLeft;
303 :
304 : /// @brief The number of vehicles which left in the current interval
305 : MeanDataValues* myValues;
306 : };
307 :
308 : /// @brief The map of vehicles to data entries
309 : std::map<const SUMOTrafficObject*, std::shared_ptr<TrackerEntry> > myTrackedData;
310 :
311 : /// @brief The currently active meandata "intervals"
312 : std::list<std::shared_ptr<TrackerEntry> > myCurrentData;
313 :
314 : };
315 :
316 :
317 : public:
318 : /** @brief Constructor
319 : *
320 : * @param[in] id The id of the detector
321 : * @param[in] dumpBegin Begin time of dump
322 : * @param[in] dumpEnd End time of dump
323 : * @param[in] useLanes Information whether lane-based or edge-based dump shall be generated
324 : * @param[in] excludeEmpty Information if and which empty lanes/edges shall be written
325 : * @param[in] trackVehicles Information whether vehicles shall be tracked
326 : * @param[in] detectPersons Whether pedestrians shall be detected instead of vehicles
327 : * @param[in] maxTravelTime the maximum travel time to use when calculating per vehicle output
328 : * @param[in] defaultEffort the value to use when calculating defaults
329 : * @param[in] minSamples the minimum number of sample seconds before the values are valid
330 : * @param[in] vTypes the set of vehicle types to consider
331 : */
332 : MSMeanData(const std::string& id,
333 : const SUMOTime dumpBegin, const SUMOTime dumpEnd,
334 : const bool useLanes, const std::string& excludeEmpty, const bool withInternal,
335 : const bool trackVehicles, const int detectPersons,
336 : const double minSamples,
337 : const double maxTravelTime,
338 : const std::string& vTypes,
339 : const std::string& writeAttributes,
340 : const std::vector<MSEdge*>& edges,
341 : AggregateType aggregate);
342 :
343 :
344 : /// @brief Destructor
345 : virtual ~MSMeanData();
346 :
347 : /** @brief Adds the value collectors to all relevant edges.
348 : */
349 : void init();
350 :
351 : /// @name Methods inherited from MSDetectorFileOutput.
352 : /// @{
353 :
354 : /** @brief Writes collected values into the given stream
355 : *
356 : * At first, it is checked whether the values for the current interval shall be written.
357 : * If not, a reset is performed, only, using "resetOnly". Otherwise,
358 : * both the list of single-lane edges and the list of multi-lane edges
359 : * are gone through and each edge is written using "writeEdge".
360 : *
361 : * @param[in] dev The output device to write the data into
362 : * @param[in] startTime First time step the data were gathered
363 : * @param[in] stopTime Last time step the data were gathered
364 : * @see MSDetectorFileOutput::writeXMLOutput
365 : * @see write
366 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
367 : */
368 : void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
369 :
370 : /** @brief Opens the XML-output using "netstats" as root element
371 : *
372 : * @param[in] dev The output device to write the root into
373 : * @see MSDetectorFileOutput::writeXMLDetectorProlog
374 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
375 : */
376 : virtual void writeXMLDetectorProlog(OutputDevice& dev) const;
377 : /// @}
378 :
379 : /** @brief Updates the detector
380 : */
381 : virtual void detectorUpdate(const SUMOTime step);
382 :
383 : double getMinSamples() const {
384 39710 : return myMinSamples;
385 : }
386 :
387 : double getMaxTravelTime() const {
388 32269 : return myMaxTravelTime;
389 : }
390 :
391 : bool isEdgeData() const {
392 : return myAmEdgeBased;
393 : }
394 :
395 : /// @brief return all attributes that are (potentially) written by this output
396 : virtual std::vector<std::string> getAttributeNames() const = 0;
397 :
398 : /// @brief return attribute value for the given lane
399 : double getAttributeValue(const MSLane* lane, SumoXMLAttr a, double defaultValue) const;
400 :
401 : /// @brief retrieve all MeanDataValues
402 : const std::vector<MSMoveReminder*> getReminders() const;
403 :
404 : /// @brief set a sub collector
405 : void setSubData(MSMeanData* subData) {
406 6 : mySubData = subData;
407 6 : }
408 :
409 : protected:
410 : /** @brief Create an instance of MeanDataValues
411 : *
412 : * @param[in] lane The lane to create for
413 : * @param[in] length The lane length (or total length for aggregated data)
414 : * @param[in] doAdd whether to add the values as reminder to the lane
415 : */
416 : virtual MSMeanData::MeanDataValues* createValues(MSLane* const lane, const double length, const bool doAdd) const = 0;
417 :
418 : /** @brief Resets network value in order to allow processing of the next interval
419 : *
420 : * Goes through the lists of edges and starts "resetOnly" for each edge.
421 : * @param[in] edge The last time step that is reported
422 : */
423 : void resetOnly(SUMOTime stopTime);
424 :
425 : /** @brief Return the relevant edge id
426 : *
427 : * @param[in] edge The edge to retrieve the id for
428 : */
429 : virtual std::string getEdgeID(const MSEdge* const edge);
430 :
431 : /** @brief Writes edge values into the given stream
432 : *
433 : * microsim: It is checked whether the dump shall be generated edge-
434 : * or lane-wise. In the first case, the lane-data are collected
435 : * and aggregated and written directly. In the second case, "writeLane"
436 : * is used to write each lane's state.
437 : *
438 : * @param[in] dev The output device to write the data into
439 : * @param[in] edgeValues List of this edge's value collectors
440 : * @param[in] edge The edge to write the dump of
441 : * @param[in] startTime First time step the data were gathered
442 : * @param[in] stopTime Last time step the data were gathered
443 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
444 : */
445 : void writeEdge(OutputDevice& dev, const std::vector<MeanDataValues*>& edgeValues,
446 : const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime);
447 :
448 :
449 : /** @brief Writes aggregated data of all edge values into the given stream
450 : *
451 : * microsim: It is checked whether the dump shall be generated edge-
452 : * or lane-wise. In the first case, the lane-data are collected
453 : * and aggregated and written directly. In the second case, "writeLane"
454 : * is used to write each lane's state.
455 : *
456 : * @param[in] dev The output device to write the data into
457 : * @param[in] startTime First time step the data were gathered
458 : * @param[in] stopTime Last time step the data were gathered
459 : */
460 : void writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
461 :
462 : /** @brief Writes aggregated data for each TAZ into the given stream
463 : *
464 : * microsim: It is checked whether the dump shall be generated edge-
465 : * or lane-wise. In the first case, the lane-data are collected
466 : * and aggregated and written directly. In the second case, "writeLane"
467 : * is used to write each lane's state.
468 : *
469 : * @param[in] dev The output device to write the data into
470 : * @param[in] startTime First time step the data were gathered
471 : * @param[in] stopTime Last time step the data were gathered
472 : */
473 : void writeAggregatedTAZ(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
474 :
475 : /** @brief Writes the interval opener
476 : *
477 : * @param[in] dev The output device to write the data into
478 : * @param[in] startTime First time step the data were gathered
479 : * @param[in] stopTime Last time step the data were gathered
480 : */
481 : virtual void openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime);
482 :
483 : /** @brief Writes the surrounding element into the given stream
484 : *
485 : * @param[in] dev The output device to write the data into
486 : * @param[in] values The values to check for emptiness
487 : * @param[in] tag The xml tag to write (lane / edge)
488 : * @param[in] id The id for the lane / edge to write
489 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
490 : */
491 : virtual void writePrefix(OutputDevice& dev, const MeanDataValues& values,
492 : const SumoXMLTag tag, const std::string id) const;
493 :
494 : const std::vector<MeanDataValues*>* getEdgeValues(const MSEdge* edge) const;
495 :
496 : private:
497 : /** @brief Create an instance of MeanDataValues with subdata
498 : *
499 : * @param[in] lane The lane to create for
500 : * @param[in] length The lane length (or total length for aggregated data)
501 : * @param[in] doAdd whether to add the values as reminder to the lane
502 : */
503 : MSMeanData::MeanDataValues* createValuesSubData(MSLane* const lane, const double length, const bool doAdd) const;
504 :
505 : protected:
506 : /// @brief the minimum sample seconds
507 : const double myMinSamples;
508 :
509 : /// @brief the maximum travel time to write
510 : const double myMaxTravelTime;
511 :
512 : /// @brief Value collectors; sorted by edge, then by lane
513 : std::vector<std::vector<MeanDataValues*> > myMeasures;
514 :
515 : /// @brief Whether empty lanes/edges shall be written
516 : bool myDumpEmpty = true;
517 :
518 : /// @brief Information whether the output shall be edge-based (not lane-based)
519 : const bool myAmEdgeBased;
520 :
521 : private:
522 : /// @brief The first and the last time step to write information (-1 indicates always)
523 : const SUMOTime myDumpBegin, myDumpEnd;
524 :
525 : /// @brief time at which init was called();
526 : SUMOTime myInitTime;
527 :
528 : /// @brief The corresponding first edges
529 : MSEdgeVector myEdges;
530 :
531 : ConstMSEdgeVector myTAZ;
532 :
533 : /// @brief The index in myEdges / myMeasures
534 : std::map<const MSEdge*, int> myEdgeIndex;
535 :
536 : /// @brief Whether empty lanes/edges shall be written with default values
537 : bool myPrintDefaults = false;
538 :
539 : /// @brief Whether only empty lanes/edges which have been modified shall be written
540 : bool myPrintModified = false;
541 :
542 : /// @brief Whether internal lanes/edges shall be written
543 : const bool myDumpInternal;
544 :
545 : /// @brief Whether vehicles are tracked
546 : const bool myTrackVehicles;
547 :
548 : /// @brief bit mask for checking attributes to be written
549 : const SumoXMLAttrMask myWrittenAttributes;
550 :
551 : /// @brief whether the data for all edges shall be aggregated
552 : const AggregateType myAggregate;
553 :
554 : /// @brief more data collectors which write to the same output
555 : MSMeanData* mySubData = nullptr;
556 :
557 : /// @brief The intervals for which output still has to be generated (only in the tracking case)
558 : std::list< std::pair<SUMOTime, SUMOTime> > myPendingIntervals;
559 :
560 : private:
561 : /// @brief Invalidated copy constructor.
562 : MSMeanData(const MSMeanData&);
563 :
564 : /// @brief Invalidated assignment operator.
565 : MSMeanData& operator=(const MSMeanData&);
566 :
567 : };
|