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 /****************************************************************************/
15 // Network state mean data collector for edges/lanes
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <microsim/MSEdgeControl.h>
25 #include <microsim/MSEdge.h>
26 #include <microsim/MSLane.h>
27 #include <microsim/MSVehicle.h>
28 #include <utils/common/SUMOTime.h>
29 #include <utils/common/ToString.h>
31 #include "MSMeanData_Amitran.h"
32 #include <limits>
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 // ---------------------------------------------------------------------------
39 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
40 // ---------------------------------------------------------------------------
42  const double length,
43  const bool doAdd,
44  const MSMeanData_Amitran* parent)
45  : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
46 
47 
49 }
50 
51 
52 void
54  amount = 0;
55  typedAmount.clear();
56  typedSamples.clear();
57  typedTravelDistance.clear();
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
111  const double /* numLanes */, const double defaultTravelTime, const int /* numVehicles */) const {
112  if (sampleSeconds > 0) {
113  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
114  } else if (defaultTravelTime >= 0.) {
115  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
116  } else {
117  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
118  }
119  if (myParent->isTyped()) {
120  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
121  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
122  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
123  dev.closeTag();
124  }
125  }
126  dev.closeTag();
127 }
128 
129 // ---------------------------------------------------------------------------
130 // MSMeanData_Amitran - methods
131 // ---------------------------------------------------------------------------
133  const SUMOTime dumpBegin,
134  const SUMOTime dumpEnd, const bool useLanes,
135  const bool withEmpty, const bool printDefaults,
136  const bool withInternal,
137  const bool trackVehicles,
138  const int detectPersons,
139  const double maxTravelTime,
140  const double minSamples,
141  const double haltSpeed,
142  const std::string& vTypes)
143  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
144  withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes),
145  myHaltSpeed(haltSpeed) {
146 }
147 
148 
150 
151 
152 void
154  dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
155 }
156 
157 
158 std::string
160  return toString(edge->getNumericalID());
161 }
162 
163 
164 void
165 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
166  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
167  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
168 }
169 
170 
171 bool
172 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
173  if (myDumpEmpty || !values.isEmpty()) {
174  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
175  return true;
176  }
177  return false;
178 }
179 
180 
182 MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
183  return new MSLaneMeanDataValues(lane, length, doAdd, this);
184 }
185 
186 
187 /****************************************************************************/
188 
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:182
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
ToString.h
MSMeanData_Amitran::openInterval
virtual void openInterval(OutputDevice &dev, const SUMOTime startTime, const SUMOTime stopTime)
Writes the interval opener.
Definition: MSMeanData_Amitran.cpp:165
MSMeanData::myDumpEmpty
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:432
MSEdge::getNumericalID
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:265
MSMeanData_Amitran.h
SUMOTime.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
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:80
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:53
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSMeanData::MeanDataValues::isEmpty
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:266
SUMO_ATTR_ID
@ SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
MSEdge.h
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:41
MSMeanData_Amitran::~MSMeanData_Amitran
virtual ~MSMeanData_Amitran()
Destructor.
Definition: MSMeanData_Amitran.cpp:149
MSVehicle.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
MSMeanData
Data collector for edges/lanes.
Definition: MSMeanData.h:59
SUMO_TAG_TIMESLICE
@ SUMO_TAG_TIMESLICE
Definition: SUMOXMLDefinitions.h:321
MSMeanData_Amitran
Network state mean data collector for edges/lanes.
Definition: MSMeanData_Amitran.h:56
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
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:132
MSMeanData::MeanDataValues::travelledDistance
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:175
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:110
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
MSMeanData_Amitran::MSLaneMeanDataValues::amount
int amount
Definition: MSMeanData_Amitran.h:128
MSMoveReminder::NOTIFICATION_DEPARTED
@ NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSMoveReminder.h:93
MSMeanData_Amitran::MSLaneMeanDataValues::MSLaneMeanDataValues
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Amitran *parent)
Constructor.
Definition: MSMeanData_Amitran.cpp:41
SUMO_ATTR_STARTTIME
@ SUMO_ATTR_STARTTIME
Definition: SUMOXMLDefinitions.h:889
MSMeanData_Amitran::MSLaneMeanDataValues::~MSLaneMeanDataValues
virtual ~MSLaneMeanDataValues()
Destructor.
Definition: MSMeanData_Amitran.cpp:48
MSMeanData::MeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:68
MSMeanData_Amitran::MSLaneMeanDataValues::typedSamples
std::map< const MSVehicleType *, double > typedSamples
The number of sampled vehicle movements by type (in s)
Definition: MSMeanData_Amitran.h:134
MSDetectorFileOutput::detectPersons
bool detectPersons() const
Definition: MSDetectorFileOutput.h:166
MSEdgeControl.h
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
MSMeanData_Amitran::MSLaneMeanDataValues::typedTravelDistance
std::map< const MSVehicleType *, double > typedTravelDistance
The sum of the distances the vehicles travelled by type.
Definition: MSMeanData_Amitran.h:137
SUMO_ATTR_DURATION
@ SUMO_ATTR_DURATION
Definition: SUMOXMLDefinitions.h:667
MSMeanData_Amitran::myHaltSpeed
const double myHaltSpeed
the minimum sample seconds
Definition: MSMeanData_Amitran.h:226
MSMeanData::MeanDataValues::sampleSeconds
double sampleSeconds
Definition: MSMeanData.h:172
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:131
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:62
MSMeanData_Amitran::MSLaneMeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData_Amitran.h:65
MSMeanData_Amitran::writeXMLDetectorProlog
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
Definition: MSMeanData_Amitran.cpp:153
MSMeanData_Amitran::MSLaneMeanDataValues::isEmpty
bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData_Amitran.cpp:104
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:172
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:89
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:227
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91
MSMoveReminder::NOTIFICATION_JUNCTION
@ NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:95
MSMeanData_Amitran::getEdgeID
virtual std::string getEdgeID(const MSEdge *const edge)
Return the relevant edge id.
Definition: MSMeanData_Amitran.cpp:159
MSVehicle
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79