LCOV - code coverage report
Current view: top level - src/microsim/output - MSMeanData.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 9 15 60.0 %
Date: 2024-05-05 15:31:14 Functions: 1 5 20.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2001-2024 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, long long int 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         506 :             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, long long int 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       96544 :                 : myNumVehicleEntered(0), myNumVehicleLeft(0), myValues(values) {}
     281             : 
     282             :             /** @brief Constructor */
     283       26256 :             virtual ~TrackerEntry() {
     284       26256 :                 delete myValues;
     285       26256 :             }
     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] withEmpty Information whether empty lanes/edges shall be written
     314             :      * @param[in] withInternal Information whether internal lanes/edges shall be written
     315             :      * @param[in] trackVehicles Information whether vehicles shall be tracked
     316             :      * @param[in] detectPersons Whether pedestrians shall be detected instead of vehicles
     317             :      * @param[in] maxTravelTime the maximum travel time to use when calculating per vehicle output
     318             :      * @param[in] defaultEffort the value to use when calculating defaults
     319             :      * @param[in] minSamples the minimum number of sample seconds before the values are valid
     320             :      * @param[in] vTypes the set of vehicle types to consider
     321             :      */
     322             :     MSMeanData(const std::string& id,
     323             :                const SUMOTime dumpBegin, const SUMOTime dumpEnd,
     324             :                const bool useLanes, const bool withEmpty,
     325             :                const bool printDefaults, const bool withInternal,
     326             :                const bool trackVehicles, const int detectPersons,
     327             :                const double minSamples,
     328             :                const double maxTravelTime,
     329             :                const std::string& vTypes,
     330             :                const std::string& writeAttributes,
     331             :                const std::vector<MSEdge*>& edges,
     332             :                bool aggregate);
     333             : 
     334             : 
     335             :     /// @brief Destructor
     336             :     virtual ~MSMeanData();
     337             : 
     338             :     /** @brief Adds the value collectors to all relevant edges.
     339             :      */
     340             :     void init();
     341             : 
     342             :     /// @name Methods inherited from MSDetectorFileOutput.
     343             :     /// @{
     344             : 
     345             :     /** @brief Writes collected values into the given stream
     346             :      *
     347             :      * At first, it is checked whether the values for the current interval shall be written.
     348             :      *  If not, a reset is performed, only, using "resetOnly". Otherwise,
     349             :      *  both the list of single-lane edges and the list of multi-lane edges
     350             :      *  are gone through and each edge is written using "writeEdge".
     351             :      *
     352             :      * @param[in] dev The output device to write the data into
     353             :      * @param[in] startTime First time step the data were gathered
     354             :      * @param[in] stopTime Last time step the data were gathered
     355             :      * @see MSDetectorFileOutput::writeXMLOutput
     356             :      * @see write
     357             :      * @exception IOError If an error on writing occurs (!!! not yet implemented)
     358             :      */
     359             :     void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
     360             : 
     361             :     /** @brief Opens the XML-output using "netstats" as root element
     362             :      *
     363             :      * @param[in] dev The output device to write the root into
     364             :      * @see MSDetectorFileOutput::writeXMLDetectorProlog
     365             :      * @exception IOError If an error on writing occurs (!!! not yet implemented)
     366             :      */
     367             :     virtual void writeXMLDetectorProlog(OutputDevice& dev) const;
     368             :     /// @}
     369             : 
     370             :     /** @brief Updates the detector
     371             :      */
     372             :     virtual void detectorUpdate(const SUMOTime step);
     373             : 
     374             :     double getMinSamples() const {
     375       45097 :         return myMinSamples;
     376             :     }
     377             : 
     378             :     double getMaxTravelTime() const {
     379       37225 :         return myMaxTravelTime;
     380             :     }
     381             : 
     382             :     bool isEdgeData() const {
     383             :         return myAmEdgeBased;
     384             :     }
     385             : 
     386             :     /// @brief return all attributes that are (potentially) written by this output
     387           0 :     virtual std::vector<std::string> getAttributeNames() const {
     388           0 :         return std::vector<std::string>();
     389             :     }
     390             : 
     391             :     /// @brief return attribute value for the given lane
     392           0 :     virtual double getAttributeValue(const MSLane* lane, SumoXMLAttr a, double defaultValue) const {
     393             :         UNUSED_PARAMETER(lane);
     394             :         UNUSED_PARAMETER(a);
     395           0 :         return defaultValue;
     396             :     }
     397             : 
     398             : protected:
     399             :     /** @brief Create an instance of MeanDataValues
     400             :      *
     401             :      * @param[in] lane The lane to create for
     402             :      * @param[in] doAdd whether to add the values as reminder to the lane
     403             :      */
     404             :     virtual MSMeanData::MeanDataValues* createValues(MSLane* const lane, const double length, const bool doAdd) const = 0;
     405             : 
     406             :     /** @brief Resets network value in order to allow processing of the next interval
     407             :      *
     408             :      * Goes through the lists of edges and starts "resetOnly" for each edge.
     409             :      * @param[in] edge The last time step that is reported
     410             :      */
     411             :     void resetOnly(SUMOTime stopTime);
     412             : 
     413             :     /** @brief Return the relevant edge id
     414             :      *
     415             :      * @param[in] edge The edge to retrieve the id for
     416             :      */
     417             :     virtual std::string getEdgeID(const MSEdge* const edge);
     418             : 
     419             :     /** @brief Writes edge values into the given stream
     420             :      *
     421             :      * microsim: It is checked whether the dump shall be generated edge-
     422             :      *  or lane-wise. In the first case, the lane-data are collected
     423             :      *  and aggregated and written directly. In the second case, "writeLane"
     424             :      *  is used to write each lane's state.
     425             :      *
     426             :      * @param[in] dev The output device to write the data into
     427             :      * @param[in] edgeValues List of this edge's value collectors
     428             :      * @param[in] edge The edge to write the dump of
     429             :      * @param[in] startTime First time step the data were gathered
     430             :      * @param[in] stopTime Last time step the data were gathered
     431             :      * @exception IOError If an error on writing occurs (!!! not yet implemented)
     432             :      */
     433             :     void writeEdge(OutputDevice& dev, const std::vector<MeanDataValues*>& edgeValues,
     434             :                    const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime);
     435             : 
     436             : 
     437             :     /** @brief Writes aggregate of all edge values into the given stream
     438             :      *
     439             :      * microsim: It is checked whether the dump shall be generated edge-
     440             :      *  or lane-wise. In the first case, the lane-data are collected
     441             :      *  and aggregated and written directly. In the second case, "writeLane"
     442             :      *  is used to write each lane's state.
     443             :      *
     444             :      * @param[in] dev The output device to write the data into
     445             :      * @param[in] startTime First time step the data were gathered
     446             :      * @param[in] stopTime Last time step the data were gathered
     447             :      */
     448             :     void writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
     449             : 
     450             :     /** @brief Writes the interval opener
     451             :      *
     452             :      * @param[in] dev The output device to write the data into
     453             :      * @param[in] startTime First time step the data were gathered
     454             :      * @param[in] stopTime Last time step the data were gathered
     455             :      */
     456             :     virtual void openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime);
     457             : 
     458             :     /** @brief Checks for emptiness and writes prefix into the given stream
     459             :      *
     460             :      * @param[in] dev The output device to write the data into
     461             :      * @param[in] values The values to check for emptiness
     462             :      * @param[in] tag The xml tag to write (lane / edge)
     463             :      * @param[in] id The id for the lane / edge to write
     464             :      * @return whether further output should be generated
     465             :      * @exception IOError If an error on writing occurs (!!! not yet implemented)
     466             :      */
     467             :     virtual bool writePrefix(OutputDevice& dev, const MeanDataValues& values,
     468             :                              const SumoXMLTag tag, const std::string id) const;
     469             : 
     470             : 
     471             : protected:
     472             :     const std::vector<MeanDataValues*>* getEdgeValues(const MSEdge* edge) const;
     473             : 
     474             : protected:
     475             :     /// @brief the minimum sample seconds
     476             :     const double myMinSamples;
     477             : 
     478             :     /// @brief the maximum travel time to write
     479             :     const double myMaxTravelTime;
     480             : 
     481             :     /// @brief Value collectors; sorted by edge, then by lane
     482             :     std::vector<std::vector<MeanDataValues*> > myMeasures;
     483             : 
     484             :     /// @brief Whether empty lanes/edges shall be written
     485             :     const bool myDumpEmpty;
     486             : 
     487             :     /// @brief Information whether the output shall be edge-based (not lane-based)
     488             :     const bool myAmEdgeBased;
     489             : 
     490             : private:
     491             :     static long long int initWrittenAttributes(const std::string writeAttributes, const std::string& id);
     492             : 
     493             :     /// @brief The first and the last time step to write information (-1 indicates always)
     494             :     const SUMOTime myDumpBegin, myDumpEnd;
     495             : 
     496             :     /// @brief time at which init was called();
     497             :     SUMOTime myInitTime;
     498             : 
     499             :     /// @brief The corresponding first edges
     500             :     MSEdgeVector myEdges;
     501             : 
     502             :     /// @brief The index in myEdges / myMeasures
     503             :     std::map<const MSEdge*, int> myEdgeIndex;
     504             : 
     505             :     /// @brief Whether empty lanes/edges shall be written
     506             :     const bool myPrintDefaults;
     507             : 
     508             :     /// @brief Whether internal lanes/edges shall be written
     509             :     const bool myDumpInternal;
     510             : 
     511             :     /// @brief Whether vehicles are tracked
     512             :     const bool myTrackVehicles;
     513             : 
     514             :     /// @brief bit mask for checking attributes to be written
     515             :     const long long int myWrittenAttributes;
     516             : 
     517             :     /// @brief whether the data for all edges shall be aggregated
     518             :     const bool myAggregate;
     519             : 
     520             :     /// @brief The intervals for which output still has to be generated (only in the tracking case)
     521             :     std::list< std::pair<SUMOTime, SUMOTime> > myPendingIntervals;
     522             : 
     523             : private:
     524             :     /// @brief Invalidated copy constructor.
     525             :     MSMeanData(const MSMeanData&);
     526             : 
     527             :     /// @brief Invalidated assignment operator.
     528             :     MSMeanData& operator=(const MSMeanData&);
     529             : 
     530             : };

Generated by: LCOV version 1.14