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-2016 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 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
52 // ---------------------------------------------------------------------------
54  const SUMOReal length,
55  const bool doAdd,
56  const MSMeanData_Amitran* parent)
57  : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
58 
59 
61 }
62 
63 
64 void
66  amount = 0;
67  typedAmount.clear();
68  typedSamples.clear();
69  typedTravelDistance.clear();
70 }
71 
72 
73 void
76  v.amount += amount;
79  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
80  v.typedAmount[it->first] += it->second;
81  }
82  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
83  v.typedSamples[it->first] += it->second;
84  }
85  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
86  v.typedTravelDistance[it->first] += it->second;
87  }
88 }
89 
90 
91 void
92 MSMeanData_Amitran::MSLaneMeanDataValues::notifyMoveInternal(const SUMOVehicle& veh, const SUMOReal /* frontOnLane */, const SUMOReal timeOnLane, const SUMOReal /*meanSpeedFrontOnLane*/, const SUMOReal /*meanSpeedVehicleOnLane*/, const SUMOReal /*travelledDistanceFrontOnLane*/, const SUMOReal travelledDistanceVehicleOnLane) {
93  sampleSeconds += timeOnLane;
94  travelledDistance += travelledDistanceVehicleOnLane;
95  typedSamples[&veh.getVehicleType()] += timeOnLane;
96  typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
97 }
98 
99 
100 bool
102  if (myParent->vehicleApplies(veh)) {
103  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
105  ++amount;
106  typedAmount[&veh.getVehicleType()]++;
107  }
108  }
109  return true;
110  }
111  return false;
112 }
113 
114 
115 bool
117  return sampleSeconds == 0 && amount == 0;
118 }
119 
120 
121 void
123  const SUMOReal /* numLanes */, const SUMOReal defaultTravelTime, const int /* numVehicles */) const {
124  if (sampleSeconds > 0) {
125  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
126  } else if (defaultTravelTime >= 0.) {
127  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
128  } else {
129  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
130  }
131  if (myParent->isTyped()) {
132  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
133  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
134  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
135  dev.closeTag();
136  }
137  }
138  dev.closeTag();
139 }
140 
141 // ---------------------------------------------------------------------------
142 // MSMeanData_Amitran - methods
143 // ---------------------------------------------------------------------------
145  const SUMOTime dumpBegin,
146  const SUMOTime dumpEnd, const bool useLanes,
147  const bool withEmpty, const bool printDefaults,
148  const bool withInternal,
149  const bool trackVehicles,
150  const SUMOReal maxTravelTime,
151  const SUMOReal minSamples,
152  const SUMOReal haltSpeed,
153  const std::string& vTypes)
154  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
155  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
156  myHaltSpeed(haltSpeed) {
157 }
158 
159 
161 
162 
163 void
165  dev.writeXMLHeader("linkData", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/amitran/linkdata.xsd\"");
166 }
167 
168 
169 std::string
171  return toString(edge->getNumericalID());
172 }
173 
174 
175 void
176 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
177  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
178  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
179 }
180 
181 
182 bool
183 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
184  if (myDumpEmpty || !values.isEmpty()) {
185  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
186  return true;
187  }
188  return false;
189 }
190 
191 
193 MSMeanData_Amitran::createValues(MSLane* const lane, const SUMOReal length, const bool doAdd) const {
194  return new MSLaneMeanDataValues(lane, length, doAdd, this);
195 }
196 
197 
198 /****************************************************************************/
199 
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Computes current values and adds them to their sums.
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:257
SumoXMLTag
Numbers representing SUMO-XML - element names.
const SUMOReal myLaneLength
The length of the lane / edge the data collector is on.
Definition: MSMeanData.h:175
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:82
long long int SUMOTime
Definition: SUMOTime.h:43
void write(OutputDevice &dev, const SUMOTime period, const SUMOReal numLanes, const SUMOReal defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const SUMOReal length, const bool doAdd) const
Create an instance of MeanDataValues.
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given type.
Network state mean data collector for edges/lanes.
void notifyMoveInternal(const SUMOVehicle &veh, const SUMOReal, const SUMOReal timeOnLane, const SUMOReal, const SUMOReal meanSpeedVehicleOnLane, const SUMOReal travelledDistanceFrontOnLane, const SUMOReal travelledDistanceVehicleOnLane)
Internal notification about the vehicle moves.
The vehicle arrived at a junction.
SUMOReal travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:183
Notification
Definition of a vehicle state.
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:175
std::map< const MSVehicleType *, SUMOReal > typedSamples
The number of sampled vehicle movements by type (in s)
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:275
Data structure for mean (aggregated) edge/lane values.
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
const SUMOReal myHaltSpeed
the minimum sample seconds
A road/street connecting two junctions.
Definition: MSEdge.h:80
virtual ~MSMeanData_Amitran()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:76
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
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 SUMOReal maxTravelTime, const SUMOReal minSamples, const SUMOReal haltSpeed, const std::string &vTypes)
Constructor.
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.
MSLaneMeanDataValues(MSLane *const lane, const SUMOReal length, const bool doAdd, const MSMeanData_Amitran *parent)
Constructor.
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)
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.
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.
#define SUMOReal
Definition: config.h:213
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:438
bool isEmpty() const
Returns whether any data was collected.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
std::map< const MSVehicleType *, SUMOReal > typedTravelDistance
The sum of the distances the vehicles travelled by type.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.