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 1026698 : 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 39705 : return myMinSamples;
385 : }
386 :
387 : double getMaxTravelTime() const {
388 32264 : return myMaxTravelTime;
389 : }
390 :
391 : bool isEdgeData() const {
392 122 : 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 : const MSEdgeVector& getEdges() const {
410 : return myEdges;
411 : }
412 :
413 :
414 : protected:
415 : /** @brief Create an instance of MeanDataValues
416 : *
417 : * @param[in] lane The lane to create for
418 : * @param[in] length The lane length (or total length for aggregated data)
419 : * @param[in] doAdd whether to add the values as reminder to the lane
420 : */
421 : virtual MSMeanData::MeanDataValues* createValues(MSLane* const lane, const double length, const bool doAdd) const = 0;
422 :
423 : /** @brief Resets network value in order to allow processing of the next interval
424 : *
425 : * Goes through the lists of edges and starts "resetOnly" for each edge.
426 : * @param[in] edge The last time step that is reported
427 : */
428 : void resetOnly(SUMOTime stopTime);
429 :
430 : /** @brief Return the relevant edge id
431 : *
432 : * @param[in] edge The edge to retrieve the id for
433 : */
434 : virtual std::string getEdgeID(const MSEdge* const edge);
435 :
436 : /** @brief Writes edge values into the given stream
437 : *
438 : * microsim: It is checked whether the dump shall be generated edge-
439 : * or lane-wise. In the first case, the lane-data are collected
440 : * and aggregated and written directly. In the second case, "writeLane"
441 : * is used to write each lane's state.
442 : *
443 : * @param[in] dev The output device to write the data into
444 : * @param[in] edgeValues List of this edge's value collectors
445 : * @param[in] edge The edge to write the dump of
446 : * @param[in] startTime First time step the data were gathered
447 : * @param[in] stopTime Last time step the data were gathered
448 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
449 : */
450 : void writeEdge(OutputDevice& dev, const std::vector<MeanDataValues*>& edgeValues,
451 : const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime);
452 :
453 :
454 : /** @brief Writes aggregated data of all edge values into the given stream
455 : *
456 : * microsim: It is checked whether the dump shall be generated edge-
457 : * or lane-wise. In the first case, the lane-data are collected
458 : * and aggregated and written directly. In the second case, "writeLane"
459 : * is used to write each lane's state.
460 : *
461 : * @param[in] dev The output device to write the data into
462 : * @param[in] startTime First time step the data were gathered
463 : * @param[in] stopTime Last time step the data were gathered
464 : */
465 : void writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
466 :
467 : /** @brief Writes aggregated data for each TAZ into the given stream
468 : *
469 : * microsim: It is checked whether the dump shall be generated edge-
470 : * or lane-wise. In the first case, the lane-data are collected
471 : * and aggregated and written directly. In the second case, "writeLane"
472 : * is used to write each lane's state.
473 : *
474 : * @param[in] dev The output device to write the data into
475 : * @param[in] startTime First time step the data were gathered
476 : * @param[in] stopTime Last time step the data were gathered
477 : */
478 : void writeAggregatedTAZ(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
479 :
480 : /** @brief Writes the interval opener
481 : *
482 : * @param[in] dev The output device to write the data into
483 : * @param[in] startTime First time step the data were gathered
484 : * @param[in] stopTime Last time step the data were gathered
485 : */
486 : virtual void openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime);
487 :
488 : /** @brief Writes the surrounding element into the given stream
489 : *
490 : * @param[in] dev The output device to write the data into
491 : * @param[in] values The values to check for emptiness
492 : * @param[in] tag The xml tag to write (lane / edge)
493 : * @param[in] id The id for the lane / edge to write
494 : * @exception IOError If an error on writing occurs (!!! not yet implemented)
495 : */
496 : virtual void writePrefix(OutputDevice& dev, const MeanDataValues& values,
497 : const SumoXMLTag tag, const std::string id) const;
498 :
499 : const std::vector<MeanDataValues*>* getEdgeValues(const MSEdge* edge) const;
500 :
501 : private:
502 : /** @brief Create an instance of MeanDataValues with subdata
503 : *
504 : * @param[in] lane The lane to create for
505 : * @param[in] length The lane length (or total length for aggregated data)
506 : * @param[in] doAdd whether to add the values as reminder to the lane
507 : */
508 : MSMeanData::MeanDataValues* createValuesSubData(MSLane* const lane, const double length, const bool doAdd) const;
509 :
510 : protected:
511 : /// @brief the minimum sample seconds
512 : const double myMinSamples;
513 :
514 : /// @brief the maximum travel time to write
515 : const double myMaxTravelTime;
516 :
517 : /// @brief Value collectors; sorted by edge, then by lane
518 : std::vector<std::vector<MeanDataValues*> > myMeasures;
519 :
520 : /// @brief Whether empty lanes/edges shall be written
521 : bool myDumpEmpty = true;
522 :
523 : /// @brief Information whether the output shall be edge-based (not lane-based)
524 : const bool myAmEdgeBased;
525 :
526 : private:
527 : /// @brief The first and the last time step to write information (-1 indicates always)
528 : const SUMOTime myDumpBegin, myDumpEnd;
529 :
530 : /// @brief time at which init was called();
531 : SUMOTime myInitTime;
532 :
533 : /// @brief time at which reset() was called
534 : SUMOTime myLastResetTime;
535 :
536 : /// @brief The corresponding first edges
537 : MSEdgeVector myEdges;
538 :
539 : ConstMSEdgeVector myTAZ;
540 :
541 : /// @brief The index in myEdges / myMeasures
542 : std::map<const MSEdge*, int> myEdgeIndex;
543 :
544 : /// @brief Whether empty lanes/edges shall be written with default values
545 : bool myPrintDefaults = false;
546 :
547 : /// @brief Whether only empty lanes/edges which have been modified shall be written
548 : bool myPrintModified = false;
549 :
550 : /// @brief Whether internal lanes/edges shall be written
551 : const bool myDumpInternal;
552 :
553 : /// @brief Whether vehicles are tracked
554 : const bool myTrackVehicles;
555 :
556 : /// @brief bit mask for checking attributes to be written
557 : const SumoXMLAttrMask myWrittenAttributes;
558 :
559 : /// @brief whether the data for all edges shall be aggregated
560 : const AggregateType myAggregate;
561 :
562 : /// @brief more data collectors which write to the same output
563 : MSMeanData* mySubData = nullptr;
564 :
565 : /// @brief The intervals for which output still has to be generated (only in the tracking case)
566 : std::list< std::pair<SUMOTime, SUMOTime> > myPendingIntervals;
567 :
568 : private:
569 : /// @brief Invalidated copy constructor.
570 : MSMeanData(const MSMeanData&);
571 :
572 : /// @brief Invalidated assignment operator.
573 : MSMeanData& operator=(const MSMeanData&);
574 :
575 : };
|