SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
11 // Copyright (C) 2001-2014 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 HAVE_INTERNAL
43 #include <microsim/MSGlobals.h>
44 #include <mesosim/MELoop.h>
45 #include <mesosim/MESegment.h>
46 #endif
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // MSMeanData_Amitran::MSLaneMeanDataValues - methods
58 // ---------------------------------------------------------------------------
60  const SUMOReal length,
61  const bool doAdd,
62  const std::set<std::string>* const vTypes,
63  const MSMeanData_Amitran* /* parent */)
64  : MSMeanData::MeanDataValues(lane, length, doAdd, vTypes), amount(0) {}
65 
66 
68 }
69 
70 
71 void
73  amount = 0;
74  typedAmount.clear();
75  typedSamples.clear();
76  typedTravelDistance.clear();
77 }
78 
79 
80 void
83  v.amount += amount;
84  v.sampleSeconds += sampleSeconds;
85  v.travelledDistance += travelledDistance;
86  for (std::map<const MSVehicleType*, unsigned>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
87  v.typedAmount[it->first] += it->second;
88  }
89  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
90  v.typedSamples[it->first] += it->second;
91  }
92  for (std::map<const MSVehicleType*, SUMOReal>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
93  v.typedTravelDistance[it->first] += it->second;
94  }
95 }
96 
97 
98 void
100  sampleSeconds += timeOnLane;
101  travelledDistance += speed * timeOnLane;
102  typedSamples[&veh.getVehicleType()] += timeOnLane;
103  typedTravelDistance[&veh.getVehicleType()] += speed * timeOnLane;
104 }
105 
106 
107 bool
109  if (vehicleApplies(veh)) {
110  if (getLane() == 0 || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
112  ++amount;
113  typedAmount[&veh.getVehicleType()]++;
114  }
115  }
116  return true;
117  }
118  return false;
119 }
120 
121 
122 bool
124  return sampleSeconds == 0 && amount == 0;
125 }
126 
127 
128 void
130  const SUMOReal /* numLanes */, const SUMOReal defaultTravelTime, const int /* numVehicles */) const {
131  if (sampleSeconds > 0) {
132  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * travelledDistance / sampleSeconds));
133  } else if (defaultTravelTime >= 0.) {
134  dev.writeAttr("amount", amount).writeAttr("averageSpeed", int(100 * myLaneLength / defaultTravelTime));
135  } else {
136  dev.writeAttr("amount", amount).writeAttr("averageSpeed", "-1");
137  }
138  if (myVehicleTypes != 0 && !myVehicleTypes->empty()) {
139  for (std::map<const MSVehicleType*, unsigned>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
140  dev.openTag("actorConfig").writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
141  dev.writeAttr("amount", it->second).writeAttr("averageSpeed", int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second));
142  dev.closeTag();
143  }
144  }
145  dev.closeTag();
146 }
147 
148 // ---------------------------------------------------------------------------
149 // MSMeanData_Amitran - methods
150 // ---------------------------------------------------------------------------
152  const SUMOTime dumpBegin,
153  const SUMOTime dumpEnd, const bool useLanes,
154  const bool withEmpty, const bool printDefaults,
155  const bool withInternal,
156  const bool trackVehicles,
157  const SUMOReal maxTravelTime,
158  const SUMOReal minSamples,
159  const SUMOReal haltSpeed,
160  const std::set<std::string> vTypes)
161  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
162  withInternal, trackVehicles, maxTravelTime, minSamples, vTypes),
163  myHaltSpeed(haltSpeed) {
164 }
165 
166 
168 
169 
170 void
172  dev.writeXMLHeader("linkData", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/amitran/linkdata.xsd\"");
173 }
174 
175 
176 std::string
178  return toString(edge->getNumericalID());
179 }
180 
181 
182 void
183 MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
184  const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
185  dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
186 }
187 
188 
189 bool
190 MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag /* tag */, const std::string id) const {
191  if (myDumpEmpty || !values.isEmpty()) {
192  dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
193  return true;
194  }
195  return false;
196 }
197 
198 
200 MSMeanData_Amitran::createValues(MSLane* const lane, const SUMOReal length, const bool doAdd) const {
201  return new MSLaneMeanDataValues(lane, length, doAdd, &myVehicleTypes, this);
202 }
203 
204 
205 /****************************************************************************/
206