SUMO - Simulation of Urban MObility
MSMeanData_Amitran.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Network state mean data collector for edges/lanes
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <microsim/MSEdgeControl.h>
33 #include <microsim/MSEdge.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSVehicle.h>
36 #include <utils/common/SUMOTime.h>
37 #include <utils/common/ToString.h>
39 #include "MSMeanData_Amitran.h"
40 #include <limits>
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
48 // ---------------------------------------------------------------------------
50  const double length,
51  const bool doAdd,
52  const MSMeanData_Amitran* parent)
53  : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
54 
55 
57 }
58 
59 
60 void
62  amount = 0;
63  typedAmount.clear();
64  typedSamples.clear();
65  typedTravelDistance.clear();
66 }
67 
68 
69 void
72  v.amount += amount;
75  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
76  v.typedAmount[it->first] += it->second;
77  }
78  for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
79  v.typedSamples[it->first] += it->second;
80  }
81  for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
82  v.typedTravelDistance[it->first] += it->second;
83  }
84 }
85 
86 
87 void
88 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) {
89  sampleSeconds += timeOnLane;
90  travelledDistance += travelledDistanceVehicleOnLane;
91  typedSamples[&veh.getVehicleType()] += timeOnLane;
92  typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
93 }
94 
95 
96 bool
98  if (myParent->vehicleApplies(veh)) {
99  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
101  ++amount;
102  typedAmount[&veh.getVehicleType()]++;
103  }
104  }
105  return true;
106  }
107  return false;
108 }
109 
110 
111 bool
113  return sampleSeconds == 0 && amount == 0;
114 }
115 
116 
117 void
119  const double /* numLanes */, const double defaultTravelTime, const int /* numVehicles */) const {
120  if (sampleSeconds > 0) {
121  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
122  } else if (defaultTravelTime >= 0.) {
123  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
124  } else {
125  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
126  }
127  if (myParent->isTyped()) {
128  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
129  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
130  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
131  dev.closeTag();
132  }
133  }
134  dev.closeTag();
135 }
136 
137 // ---------------------------------------------------------------------------
138 // MSMeanData_Amitran - methods
139 // ---------------------------------------------------------------------------
141  const SUMOTime dumpBegin,
142  const SUMOTime dumpEnd, const bool useLanes,
143  const bool withEmpty, const bool printDefaults,
144  const bool withInternal,
145  const bool trackVehicles,
146  const double maxTravelTime,
147  const double minSamples,
148  const double haltSpeed,
149  const std::string& vTypes)
150  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
151  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
152  myHaltSpeed(haltSpeed) {
153 }
154 
155 
157 
158 
159 void
161  dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
162 }
163 
164 
165 std::string
167  return toString(edge->getNumericalID());
168 }
169 
170 
171 void
172 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
173  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
174  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
175 }
176 
177 
178 bool
179 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
180  if (myDumpEmpty || !values.isEmpty()) {
181  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
182  return true;
183  }
184  return false;
185 }
186 
187 
189 MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
190  return new MSLaneMeanDataValues(lane, length, doAdd, this);
191 }
192 
193 
194 /****************************************************************************/
195 
Data collector for edges/lanes.
Definition: MSMeanData.h:67
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.
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.
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:171
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:275
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:183
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
Representation of a vehicle.
Definition: SUMOVehicle.h:67
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:76
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:65
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:175
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.
void notifyMoveInternal(const SUMOVehicle &veh, const double, const double timeOnLane, const double, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane)
Internal notification about the vehicle moves.
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:172
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:71
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:438
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:79
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.