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;
66  v.sampleSeconds += sampleSeconds;
67  v.travelledDistance += travelledDistance;
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 
MSMeanData_Amitran::createValues
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
Definition: MSMeanData_Amitran.cpp:183
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:48
ToString.h
MSMeanData_Amitran::openInterval
virtual void openInterval(OutputDevice &dev, const SUMOTime startTime, const SUMOTime stopTime)
Writes the interval opener.
Definition: MSMeanData_Amitran.cpp:166
MSMeanData::myDumpEmpty
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:433
MSEdge::getNumericalID
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:263
MSMeanData_Amitran.h
SUMOTime.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MSMeanData_Amitran::MSLaneMeanDataValues::notifyMoveInternal
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.
Definition: MSMeanData_Amitran.cpp:81
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
MSMeanData_Amitran::MSLaneMeanDataValues::reset
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
Definition: MSMeanData_Amitran.cpp:54
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSMeanData::MeanDataValues::isEmpty
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:264
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
MSEdge.h
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:42
MSMeanData_Amitran::~MSMeanData_Amitran
virtual ~MSMeanData_Amitran()
Destructor.
Definition: MSMeanData_Amitran.cpp:150
MSVehicle.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
MSMeanData
Data collector for edges/lanes.
Definition: MSMeanData.h:60
SUMO_TAG_TIMESLICE
Definition: SUMOXMLDefinitions.h:322
MSMeanData_Amitran
Network state mean data collector for edges/lanes.
Definition: MSMeanData_Amitran.h:57
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
MSMeanData_Amitran::MSMeanData_Amitran
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.
Definition: MSMeanData_Amitran.cpp:133
MSMeanData::MeanDataValues::travelledDistance
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:176
OutputDevice.h
MSMeanData_Amitran::MSLaneMeanDataValues::write
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.
Definition: MSMeanData_Amitran.cpp:111
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:76
MSMeanData_Amitran::MSLaneMeanDataValues::amount
int amount
Definition: MSMeanData_Amitran.h:129
MSMoveReminder::NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSMoveReminder.h:91
MSMeanData_Amitran::MSLaneMeanDataValues::MSLaneMeanDataValues
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Amitran *parent)
Constructor.
Definition: MSMeanData_Amitran.cpp:42
SUMO_ATTR_STARTTIME
Definition: SUMOXMLDefinitions.h:886
MSMeanData_Amitran::MSLaneMeanDataValues::~MSLaneMeanDataValues
virtual ~MSLaneMeanDataValues()
Destructor.
Definition: MSMeanData_Amitran.cpp:49
MSMeanData::MeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:69
MSMeanData_Amitran::MSLaneMeanDataValues::typedSamples
std::map< const MSVehicleType *, double > typedSamples
The number of sampled vehicle movements by type (in s)
Definition: MSMeanData_Amitran.h:135
MSDetectorFileOutput::detectPersons
bool detectPersons() const
Definition: MSDetectorFileOutput.h:167
MSEdgeControl.h
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:240
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
MSMeanData_Amitran::MSLaneMeanDataValues::typedTravelDistance
std::map< const MSVehicleType *, double > typedTravelDistance
The sum of the distances the vehicles travelled by type.
Definition: MSMeanData_Amitran.h:138
SUMO_ATTR_DURATION
Definition: SUMOXMLDefinitions.h:665
MSMeanData_Amitran::myHaltSpeed
const double myHaltSpeed
the minimum sample seconds
Definition: MSMeanData_Amitran.h:227
MSMeanData::MeanDataValues::sampleSeconds
double sampleSeconds
Definition: MSMeanData.h:173
MSMeanData_Amitran::MSLaneMeanDataValues::typedAmount
std::map< const MSVehicleType *, int > typedAmount
The number of vehicles that entered this lane within the sample interval by type.
Definition: MSMeanData_Amitran.h:132
MSMeanData_Amitran::MSLaneMeanDataValues::addTo
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
Definition: MSMeanData_Amitran.cpp:63
MSMeanData_Amitran::MSLaneMeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData_Amitran.h:66
MSMeanData_Amitran::writeXMLDetectorProlog
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
Definition: MSMeanData_Amitran.cpp:154
MSMeanData_Amitran::MSLaneMeanDataValues::isEmpty
bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData_Amitran.cpp:105
MSMeanData_Amitran::writePrefix
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.
Definition: MSMeanData_Amitran.cpp:173
config.h
MSMeanData_Amitran::MSLaneMeanDataValues::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
Definition: MSMeanData_Amitran.cpp:90
MSLane.h
OutputDevice::writeXMLHeader
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.
Definition: OutputDevice.cpp:228
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:89
MSMoveReminder::NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:93
MSMeanData_Amitran::getEdgeID
virtual std::string getEdgeID(const MSEdge *const edge)
Return the relevant edge id.
Definition: MSMeanData_Amitran.cpp:160