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 std::set<std::string>* const vTypes,
57  const MSMeanData_Amitran* /* parent */)
58  : MSMeanData::MeanDataValues(lane, length, doAdd, vTypes), amount(0) {}
59 
60 
62 }
63 
64 
65 void
67  amount = 0;
68  typedAmount.clear();
69  typedSamples.clear();
70  typedTravelDistance.clear();
71 }
72 
73 
74 void
77  v.amount += amount;
80  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
81  v.typedAmount[it->first] += it->second;
82  }
83  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
84  v.typedSamples[it->first] += it->second;
85  }
86  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
87  v.typedTravelDistance[it->first] += it->second;
88  }
89 }
90 
91 
92 void
94  sampleSeconds += timeOnLane;
95  travelledDistance += speed * timeOnLane;
96  typedSamples[&veh.getVehicleType()] += timeOnLane;
97  typedTravelDistance[&veh.getVehicleType()] += speed * timeOnLane;
98 }
99 
100 
101 bool
103  if (vehicleApplies(veh)) {
104  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
106  ++amount;
107  typedAmount[&veh.getVehicleType()]++;
108  }
109  }
110  return true;
111  }
112  return false;
113 }
114 
115 
116 bool
118  return sampleSeconds == 0 && amount == 0;
119 }
120 
121 
122 void
124  const SUMOReal /* numLanes */, const SUMOReal defaultTravelTime, const int /* numVehicles */) const {
125  if (sampleSeconds > 0) {
126  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
127  } else if (defaultTravelTime >= 0.) {
128  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
129  } else {
130  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
131  }
132  if (myVehicleTypes != 0 && !myVehicleTypes->empty()) {
133  for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
134  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
135  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
136  dev.closeTag();
137  }
138  }
139  dev.closeTag();
140 }
141 
142 // ---------------------------------------------------------------------------
143 // MSMeanData_Amitran - methods
144 // ---------------------------------------------------------------------------
146  const SUMOTime dumpBegin,
147  const SUMOTime dumpEnd, const bool useLanes,
148  const bool withEmpty, const bool printDefaults,
149  const bool withInternal,
150  const bool trackVehicles,
151  const SUMOReal maxTravelTime,
152  const SUMOReal minSamples,
153  const SUMOReal haltSpeed,
154  const std::set<std::string> vTypes)
155  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
156  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
157  myHaltSpeed(haltSpeed) {
158 }
159 
160 
162 
163 
164 void
166  dev.writeXMLHeader("linkData", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/amitran/linkdata.xsd\"");
167 }
168 
169 
170 std::string
172  return toString(edge->getNumericalID());
173 }
174 
175 
176 void
177 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
178  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
179  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
180 }
181 
182 
183 bool
184 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
185  if (myDumpEmpty || !values.isEmpty()) {
186  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
187  return true;
188  }
189  return false;
190 }
191 
192 
194 MSMeanData_Amitran::createValues(MSLane* const lane, const SUMOReal length, const bool doAdd) const {
195  return new MSLaneMeanDataValues(lane, length, doAdd, &myVehicleTypes, this);
196 }
197 
198 
199 /****************************************************************************/
200 
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
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:173
long long int SUMOTime
Definition: SUMOTime.h:43
Network state mean data collector for edges/lanes.
void notifyMoveInternal(SUMOVehicle &veh, SUMOReal timeOnLane, SUMOReal speed)
Internal notification about the vehicle moves.
const std::set< std::string > *const myVehicleTypes
The vehicle types to look for (0 or empty means all)
Definition: MSMeanData.h:186
The vehicle arrived at a junction.
SUMOReal travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:181
Notification
Definition of a vehicle state.
MSLaneMeanDataValues(MSLane *const lane, const SUMOReal length, const bool doAdd, const std::set< std::string > *const vTypes=0, const MSMeanData_Amitran *parent=0)
Constructor.
const std::set< std::string > myVehicleTypes
The vehicle types to look for (empty means all)
Definition: MSMeanData.h:440
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
std::map< const MSVehicleType *, SUMOReal > typedSamples
The number of sampled vehicle movements by type (in s)
bool isEmpty() const
Returns whether any data was collected.
const MSLane * getLane() const
Returns the lane the reminder works on.
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
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const SUMOReal length, const bool doAdd) const
Create an instance of MeanDataValues.
virtual ~MSMeanData_Amitran()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:76
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:127
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.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
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::set< std::string > vTypes)
Constructor.
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.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
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.
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)
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:271
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
bool vehicleApplies(const SUMOVehicle &veh) const
Tests whether the vehicles type is to be regarded.
Definition: MSMeanData.cpp:120
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:446
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.