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-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 /****************************************************************************/
19 // Network state mean data collector for edges/lanes
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <microsim/MSEdgeControl.h>
24 #include <microsim/MSEdge.h>
25 #include <microsim/MSLane.h>
26 #include <microsim/MSVehicle.h>
27 #include <utils/common/SUMOTime.h>
28 #include <utils/common/ToString.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 
51 void
53  amount = 0;
54  typedAmount.clear();
55  typedSamples.clear();
56  typedTravelDistance.clear();
57  resetTime = SIMSTEP;
58 }
59 
60 
61 void
64  v.amount += amount;
65  v.sampleSeconds += sampleSeconds;
66  v.travelledDistance += travelledDistance;
67  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
68  v.typedAmount[it->first] += it->second;
69  }
70  for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
71  v.typedSamples[it->first] += it->second;
72  }
73  for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
74  v.typedTravelDistance[it->first] += it->second;
75  }
76 }
77 
78 
79 void
80 MSMeanData_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 */) {
81  sampleSeconds += timeOnLane;
82  travelledDistance += travelledDistanceVehicleOnLane;
83  typedSamples[&veh.getVehicleType()] += timeOnLane;
84  typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
85 }
86 
87 
88 bool
90  if (myParent->vehicleApplies(veh)) {
91  if (getLane() == nullptr || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
93  ++amount;
94  typedAmount[&veh.getVehicleType()]++;
95  }
96  }
97  return true;
98  }
99  return false;
100 }
101 
102 
103 bool
105  return sampleSeconds == 0 && amount == 0;
106 }
107 
108 
109 void
110 MSMeanData_Amitran::MSLaneMeanDataValues::write(OutputDevice& dev, long long int attributeMask, const SUMOTime /* period */,
111  const int /* numLanes */, const double /*speedLimit*/, const double defaultTravelTime, const int /* numVehicles */) const {
112  int averageSpeed;
113  if (sampleSeconds > 0) {
114  averageSpeed = int(100 * travelledDistance / sampleSeconds);
115  } else if (defaultTravelTime >= 0.) {
116  averageSpeed = int(100 * myLaneLength / defaultTravelTime);
117  } else {
118  averageSpeed = -1;
119  }
120  dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, amount, attributeMask);
121  dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, averageSpeed, attributeMask);
122 
123  if (myParent->isTyped()) {
124  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
125  dev.openTag("actorConfig");
126  const int typedAvgSpeed = int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second);
127  dev.writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
128  dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, it->second, attributeMask);
129  dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, typedAvgSpeed, attributeMask);
130  dev.closeTag();
131  }
132  }
133  dev.closeTag();
134 }
135 
136 // ---------------------------------------------------------------------------
137 // MSMeanData_Amitran - methods
138 // ---------------------------------------------------------------------------
140  const SUMOTime dumpBegin,
141  const SUMOTime dumpEnd, const bool useLanes,
142  const bool withEmpty, const bool printDefaults,
143  const bool withInternal,
144  const bool trackVehicles,
145  const int detectPersons,
146  const double maxTravelTime,
147  const double minSamples,
148  const double haltSpeed,
149  const std::string& vTypes,
150  const std::string& writeAttributes,
151  const std::vector<MSEdge*>& edges,
152  bool aggregate) :
153  MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
154  withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes, writeAttributes, edges, aggregate),
155  myHaltSpeed(haltSpeed) {
156 }
157 
158 
160 
161 
162 void
164  dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
165 }
166 
167 
168 std::string
170  return toString(edge->getNumericalID());
171 }
172 
173 
174 void
175 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
176  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
177  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
178 }
179 
180 
181 bool
182 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
183  if (myDumpEmpty || !values.isEmpty()) {
184  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
185  return true;
186  }
187  return false;
188 }
189 
190 
192 MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
193  return new MSLaneMeanDataValues(lane, length, doAdd, this);
194 }
195 
196 
197 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
#define SIMSTEP
Definition: SUMOTime.h:61
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:190
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.
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.
void write(OutputDevice &dev, long long int attributeMask, const SUMOTime period, const int numLanes, const double speedLimit, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
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:485
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 & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, long long int attributeMask)
writes a named attribute unless filtered
Definition: OutputDevice.h:271
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
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.
Representation of a vehicle, person, or container.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.