Eclipse SUMO - Simulation of Urban MObility
MSMeanData_Amitran.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2023 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/****************************************************************************/
19// Network state mean data collector for edges/lanes
20/****************************************************************************/
21#include <config.h>
22
24#include <microsim/MSEdge.h>
25#include <microsim/MSLane.h>
26#include <microsim/MSVehicle.h>
30#include "MSMeanData_Amitran.h"
31#include <limits>
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
37// ---------------------------------------------------------------------------
38// MSMeanData_Amitran::MSLaneMeanDataValues - methods
39// ---------------------------------------------------------------------------
41 const double length,
42 const bool doAdd,
43 const MSMeanData_Amitran* parent)
44 : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
45
46
48}
49
50
51void
53 amount = 0;
54 typedAmount.clear();
55 typedSamples.clear();
56 typedTravelDistance.clear();
57}
58
59
60void
63 v.amount += amount;
64 v.sampleSeconds += sampleSeconds;
65 v.travelledDistance += travelledDistance;
66 for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
67 v.typedAmount[it->first] += it->second;
68 }
69 for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
70 v.typedSamples[it->first] += it->second;
71 }
72 for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
73 v.typedTravelDistance[it->first] += it->second;
74 }
75}
76
77
78void
79MSMeanData_Amitran::MSLaneMeanDataValues::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 */) {
80 sampleSeconds += timeOnLane;
81 travelledDistance += travelledDistanceVehicleOnLane;
82 typedSamples[&veh.getVehicleType()] += timeOnLane;
83 typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
84}
85
86
87bool
89 if (myParent->vehicleApplies(veh)) {
90 if (getLane() == nullptr || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
92 ++amount;
93 typedAmount[&veh.getVehicleType()]++;
94 }
95 }
96 return true;
97 }
98 return false;
99}
100
101
102bool
104 return sampleSeconds == 0 && amount == 0;
105}
106
107
108void
109MSMeanData_Amitran::MSLaneMeanDataValues::write(OutputDevice& dev, long long int attributeMask, const SUMOTime /* period */,
110 const double /* numLanes */, const double /*speedLimit*/, const double defaultTravelTime, const int /* numVehicles */) const {
111 int averageSpeed;
112 if (sampleSeconds > 0) {
113 averageSpeed = int(100 * travelledDistance / sampleSeconds);
114 } else if (defaultTravelTime >= 0.) {
115 averageSpeed = int(100 * myLaneLength / defaultTravelTime);
116 } else {
117 averageSpeed = -1;
118 }
119 dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, amount, attributeMask);
120 dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, averageSpeed, attributeMask);
121
122 if (myParent->isTyped()) {
123 for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
124 dev.openTag("actorConfig");
125 const int typedAvgSpeed = int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second);
126 dev.writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
127 dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, it->second, attributeMask);
128 dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, typedAvgSpeed, attributeMask);
129 dev.closeTag();
130 }
131 }
132 dev.closeTag();
133}
134
135// ---------------------------------------------------------------------------
136// MSMeanData_Amitran - methods
137// ---------------------------------------------------------------------------
139 const SUMOTime dumpBegin,
140 const SUMOTime dumpEnd, const bool useLanes,
141 const bool withEmpty, const bool printDefaults,
142 const bool withInternal,
143 const bool trackVehicles,
144 const int detectPersons,
145 const double maxTravelTime,
146 const double minSamples,
147 const double haltSpeed,
148 const std::string& vTypes,
149 const std::string& writeAttributes,
150 const std::vector<MSEdge*>& edges,
151 bool aggregate) :
152 MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
153 withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes, writeAttributes, edges, aggregate),
154 myHaltSpeed(haltSpeed) {
155}
156
157
159
160
161void
163 dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
164}
165
166
167std::string
169 return toString(edge->getNumericalID());
170}
171
172
173void
174MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
175 const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
177}
178
179
180bool
181MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
182 if (myDumpEmpty || !values.isEmpty()) {
183 dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
184 return true;
185 }
186 return false;
187}
188
189
191MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
192 return new MSLaneMeanDataValues(lane, length, doAdd, this);
193}
194
195
196/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_TIMESLICE
@ SUMO_ATTR_AVERAGESPEED
@ SUMO_ATTR_STARTTIME
@ SUMO_ATTR_ID
@ SUMO_ATTR_DURATION
@ SUMO_ATTR_AMOUNT
MSMeanData_Amitran.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A road/street connecting two junctions.
Definition: MSEdge.h:77
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:303
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:66
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:182
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:269
Data structure for mean (aggregated) edge/lane values.
void notifyMoveInternal(const SUMOTrafficObject &veh, const double, const double timeOnLane, const double, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double)
Internal notification about the vehicle moves.
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Amitran *parent)
Constructor.
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
void write(OutputDevice &dev, long long int attributeMask, const SUMOTime period, const double numLanes, const double speedLimit, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
bool isEmpty() const
Returns whether any data was collected.
std::map< const MSVehicleType *, double > typedTravelDistance
The sum of the distances the vehicles travelled by type.
std::map< const MSVehicleType *, int > typedAmount
The number of vehicles that entered this lane within the sample interval by type.
std::map< const MSVehicleType *, double > typedSamples
The number of sampled vehicle movements by type (in s)
Network state mean data collector for edges/lanes.
virtual void openInterval(OutputDevice &dev, const SUMOTime startTime, const SUMOTime stopTime)
Writes the interval opener.
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
virtual bool writePrefix(OutputDevice &dev, const MeanDataValues &values, const SumoXMLTag tag, const std::string id) const
Checks for emptiness and writes prefix into the given stream.
virtual std::string getEdgeID(const MSEdge *const edge)
Return the relevant edge id.
virtual ~MSMeanData_Amitran()
Destructor.
MSMeanData_Amitran(const std::string &id, const SUMOTime dumpBegin, const SUMOTime dumpEnd, const bool useLanes, const bool withEmpty, const bool printDefaults, const bool withInternal, const bool trackVehicles, const int detectPersons, const double maxTravelTime, const double minSamples, const double haltSpeed, const std::string &vTypes, const std::string &writeAttributes, const std::vector< MSEdge * > &edges, bool aggregate)
Constructor.
const double myHaltSpeed
the minimum sample seconds
Data collector for edges/lanes.
Definition: MSMeanData.h:57
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:475
Notification
Definition of a vehicle state.
@ NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
@ NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, long long int attributeMask)
writes a named attribute unless filtered
Definition: OutputDevice.h:271
Representation of a vehicle, person, or container.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.