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.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Network state mean data collector for edges/lanes
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <microsim/MSEdgeControl.h>
26 #include <microsim/MSEdge.h>
27 #include <microsim/MSLane.h>
28 #include <microsim/MSVehicle.h>
29 #include <utils/common/SUMOTime.h>
30 #include <utils/common/ToString.h>
32 #include "MSMeanData_Amitran.h"
33 #include <limits>
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 // ---------------------------------------------------------------------------
40 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
41 // ---------------------------------------------------------------------------
43  const double length,
44  const bool doAdd,
45  const MSMeanData_Amitran* parent)
46  : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
47 
48 
50 }
51 
52 
53 void
55  amount = 0;
56  typedAmount.clear();
57  typedSamples.clear();
58  typedTravelDistance.clear();
59 }
60 
61 
62 void
65  v.amount += amount;
68  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
69  v.typedAmount[it->first] += it->second;
70  }
71  for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
72  v.typedSamples[it->first] += it->second;
73  }
74  for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
75  v.typedTravelDistance[it->first] += it->second;
76  }
77 }
78 
79 
80 void
81 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 */) {
82  sampleSeconds += timeOnLane;
83  travelledDistance += travelledDistanceVehicleOnLane;
84  typedSamples[&veh.getVehicleType()] += timeOnLane;
85  typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
86 }
87 
88 
89 bool
91  if (myParent->vehicleApplies(veh)) {
92  if (getLane() == nullptr || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
94  ++amount;
95  typedAmount[&veh.getVehicleType()]++;
96  }
97  }
98  return true;
99  }
100  return false;
101 }
102 
103 
104 bool
106  return sampleSeconds == 0 && amount == 0;
107 }
108 
109 
110 void
112  const double /* numLanes */, const double defaultTravelTime, const int /* numVehicles */) const {
113  if (sampleSeconds > 0) {
114  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
115  } else if (defaultTravelTime >= 0.) {
116  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
117  } else {
118  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
119  }
120  if (myParent->isTyped()) {
121  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
122  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
123  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
124  dev.closeTag();
125  }
126  }
127  dev.closeTag();
128 }
129 
130 // ---------------------------------------------------------------------------
131 // MSMeanData_Amitran - methods
132 // ---------------------------------------------------------------------------
134  const SUMOTime dumpBegin,
135  const SUMOTime dumpEnd, const bool useLanes,
136  const bool withEmpty, const bool printDefaults,
137  const bool withInternal,
138  const bool trackVehicles,
139  const int detectPersons,
140  const double maxTravelTime,
141  const double minSamples,
142  const double haltSpeed,
143  const std::string& vTypes)
144  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
145  withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes),
146  myHaltSpeed(haltSpeed) {
147 }
148 
149 
151 
152 
153 void
155  dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
156 }
157 
158 
159 std::string
161  return toString(edge->getNumericalID());
162 }
163 
164 
165 void
166 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
167  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
168  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
169 }
170 
171 
172 bool
173 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
174  if (myDumpEmpty || !values.isEmpty()) {
175  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
176  return true;
177  }
178  return false;
179 }
180 
181 
183 MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
184  return new MSLaneMeanDataValues(lane, length, doAdd, this);
185 }
186 
187 
188 /****************************************************************************/
189 
Data collector for edges/lanes.
Definition: MSMeanData.h:60
const MSLane * getLane() const
Returns the lane the reminder works on.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
SumoXMLTag
Numbers representing SUMO-XML - element names.
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:35
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
Network state mean data collector for edges/lanes.
The vehicle arrived at a junction.
Notification
Definition of a vehicle state.
void write(OutputDevice &dev, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:264
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.
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)
Constructor.
std::map< const MSVehicleType *, double > typedSamples
The number of sampled vehicle movements by type (in s)
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:263
Data structure for mean (aggregated) edge/lane values.
A road/street connecting two junctions.
Definition: MSEdge.h:76
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
virtual ~MSMeanData_Amitran()
Destructor.
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:176
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:69
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
const double myHaltSpeed
the minimum sample seconds
const double myLaneLength
The length of the lane / edge the data collector is on.
Definition: MSMeanData.h:168
virtual void openInterval(OutputDevice &dev, const SUMOTime startTime, const SUMOTime stopTime)
Writes the interval opener.
virtual std::string getEdgeID(const MSEdge *const edge)
Return the relevant edge id.
bool isTyped() const
Checks whether the detector is type specific.
std::map< const MSVehicleType *, int > typedAmount
The number of vehicles that entered this lane within the sample interval by type. ...
The vehicle has departed (was inserted into the network)
std::map< const MSVehicleType *, double > typedTravelDistance
The sum of the distances the vehicles travelled by type.
const MSMeanData *const myParent
The meandata parent.
Definition: MSMeanData.h:165
Representation of a vehicle or person.
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Amitran *parent)
Constructor.
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
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.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:433
bool isEmpty() const
Returns whether any data was collected.
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.