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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Network state mean data collector for edges/lanes
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <microsim/MSEdgeControl.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSLane.h>
34 #include <microsim/MSVehicle.h>
35 #include <utils/common/SUMOTime.h>
36 #include <utils/common/ToString.h>
38 #include "MSMeanData_Amitran.h"
39 #include <limits>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 // ---------------------------------------------------------------------------
46 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
47 // ---------------------------------------------------------------------------
49  const double length,
50  const bool doAdd,
51  const MSMeanData_Amitran* parent)
52  : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
53 
54 
56 }
57 
58 
59 void
61  amount = 0;
62  typedAmount.clear();
63  typedSamples.clear();
64  typedTravelDistance.clear();
65 }
66 
67 
68 void
71  v.amount += amount;
74  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
75  v.typedAmount[it->first] += it->second;
76  }
77  for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
78  v.typedSamples[it->first] += it->second;
79  }
80  for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
81  v.typedTravelDistance[it->first] += it->second;
82  }
83 }
84 
85 
86 void
87 MSMeanData_Amitran::MSLaneMeanDataValues::notifyMoveInternal(const SUMOVehicle& veh, const double /* frontOnLane */, const double timeOnLane, const double /*meanSpeedFrontOnLane*/, const double /*meanSpeedVehicleOnLane*/, const double /*travelledDistanceFrontOnLane*/, const double travelledDistanceVehicleOnLane, const double /* meanLengthOnLane */) {
88  sampleSeconds += timeOnLane;
89  travelledDistance += travelledDistanceVehicleOnLane;
90  typedSamples[&veh.getVehicleType()] += timeOnLane;
91  typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
92 }
93 
94 
95 bool
97  if (myParent->vehicleApplies(veh)) {
98  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
100  ++amount;
101  typedAmount[&veh.getVehicleType()]++;
102  }
103  }
104  return true;
105  }
106  return false;
107 }
108 
109 
110 bool
112  return sampleSeconds == 0 && amount == 0;
113 }
114 
115 
116 void
118  const double /* numLanes */, const double defaultTravelTime, const int /* numVehicles */) const {
119  if (sampleSeconds > 0) {
120  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
121  } else if (defaultTravelTime >= 0.) {
122  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
123  } else {
124  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
125  }
126  if (myParent->isTyped()) {
127  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
128  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
129  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
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 double maxTravelTime,
146  const double minSamples,
147  const double haltSpeed,
148  const std::string& vTypes)
149  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
150  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
151  myHaltSpeed(haltSpeed) {
152 }
153 
154 
156 
157 
158 void
160  dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
161 }
162 
163 
164 std::string
166  return toString(edge->getNumericalID());
167 }
168 
169 
170 void
171 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
172  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
173  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
174 }
175 
176 
177 bool
178 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
179  if (myDumpEmpty || !values.isEmpty()) {
180  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
181  return true;
182  }
183  return false;
184 }
185 
186 
188 MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
189  return new MSLaneMeanDataValues(lane, length, doAdd, this);
190 }
191 
192 
193 /****************************************************************************/
194 
Data collector for edges/lanes.
Definition: MSMeanData.h:66
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:260
SumoXMLTag
Numbers representing SUMO-XML - element names.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given 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.
void notifyMoveInternal(const SUMOVehicle &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.
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:270
std::map< const MSVehicleType *, double > typedSamples
The number of sampled vehicle movements by type (in s)
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 double maxTravelTime, const double minSamples, const double haltSpeed, const std::string &vTypes)
Constructor.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:259
Data structure for mean (aggregated) edge/lane values.
A road/street connecting two junctions.
Definition: MSEdge.h:80
virtual ~MSMeanData_Amitran()
Destructor.
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:182
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
Representation of a vehicle.
Definition: SUMOVehicle.h:66
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:75
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:64
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:174
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:171
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:70
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:51
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:437
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
bool isEmpty() const
Returns whether any data was collected.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
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.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.