SUMO - Simulation of Urban MObility
Edge.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-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 /****************************************************************************/
17 // C++ TraCI client API implementation
18 /****************************************************************************/
19 
20 
21 #include <microsim/MSEdge.h>
22 #include <microsim/MSLane.h>
25 #include <microsim/MSVehicle.h>
26 #include <libsumo/TraCIDefs.h>
28 #include "Edge.h"
29 
30 
31 namespace libsumo {
32 std::vector<std::string>
34  std::vector<std::string> ids;
35  MSEdge::insertIDs(ids);
36  return ids;
37 }
38 
39 int
41  return (int)getIDList().size();
42 }
43 
44 double Edge::retrieveExistingTravelTime(const std::string& id, double time) {
45  const MSEdge* e = getEdge(id);
46  double value;
47  if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingTravelTime(e, time, value)) {
48  return -1.;
49  }
50  return value;
51 }
52 
53 double
54 Edge::retrieveExistingEffort(const std::string& id, double time) {
55  const MSEdge* e = getEdge(id);
56  double value;
57  if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingEffort(e, time, value)) {
58  return -1.;
59  }
60  return value;
61 }
62 
63 double
64 Edge::getCurrentTravelTime(const std::string& id) {
65  return getEdge(id)->getCurrentTravelTime();
66 }
67 
68 MSEdge*
69 Edge::getEdge(const std::string& id) {
70  MSEdge* e = MSEdge::dictionary(id);
71  if (e == nullptr) {
72  throw TraCIException("Edge '" + id + "' is not known");
73  }
74  return e;
75 }
76 
77 double
78 Edge::getWaitingSeconds(const std::string& id) {
79  double wtime = 0;
80  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
81  for (auto lane : lanes) {
82  wtime += lane->getWaitingSeconds();
83  }
84  return wtime;
85 }
86 
87 const std::vector<std::string>
88 Edge::getPersonIDs(const std::string& id) {
89  std::vector<std::string> personIDs;
90  std::vector<MSTransportable*> persons = getEdge(id)->getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
91  personIDs.reserve(persons.size());
92  for (MSTransportable* p : persons) {
93  personIDs.push_back(p->getID());
94  }
95  return personIDs;
96 }
97 
98 const std::vector<std::string>
99 Edge::getVehicleIDs(const std::string& id) {
100  std::vector<std::string> vehIDs;
101  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
102  for (auto lane : lanes) {
103  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
104  for (auto veh : vehs) {
105  vehIDs.push_back(veh->getID());
106  }
107  lane->releaseVehicles();
108  }
109  return vehIDs;
110 }
111 
112 
113 double
114 Edge::getCO2Emissions(const std::string& id) {
115  double sum = 0;
116  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
117  for (auto lane : lanes) {
118  sum += lane->getCO2Emissions();
119  }
120  return sum;
121 }
122 
123 double
124 Edge::getCOEmissions(const std::string& id) {
125  double sum = 0;
126  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
127  for (auto lane : lanes) {
128  sum += lane->getCOEmissions();
129  }
130  return sum;
131 }
132 
133 double
134 Edge::getHCEmissions(const std::string& id) {
135  double sum = 0;
136  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
137  for (auto lane : lanes) {
138  sum += lane->getHCEmissions();
139  }
140  return sum;
141 }
142 
143 double
144 Edge::getPMxEmissions(const std::string& id) {
145  double sum = 0;
146  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
147  for (auto lane : lanes) {
148  sum += lane->getPMxEmissions();
149  }
150  return sum;
151 }
152 
153 double
154 Edge::getNOxEmissions(const std::string& id) {
155  double sum = 0;
156  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
157  for (auto lane : lanes) {
158  sum += lane->getNOxEmissions();
159  }
160  return sum;
161 }
162 
163 double
164 Edge::getFuelConsumption(const std::string& id) {
165  double sum = 0;
166  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
167  for (auto lane : lanes) {
168  sum += lane->getFuelConsumption();
169  }
170  return sum;
171 }
172 
173 double
174 Edge::getNoiseEmissions(const std::string& id) {
175  double sum = 0;
176  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
177  for (auto lane : lanes) {
178  sum += pow(10., (lane->getHarmonoise_NoiseEmissions() / 10.));
179  }
180  if (sum != 0) {
181  return HelpersHarmonoise::sum(sum);
182  }
183  return sum;
184 }
185 
186 double
187 Edge::getElectricityConsumption(const std::string& id) {
188  double sum = 0;
189  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
190  for (auto lane : lanes) {
191  sum += lane->getElectricityConsumption();
192  }
193  return sum;
194 }
195 
196 int
197 Edge::getVehicleNumber(const std::string& id) {
198  int sum = 0;
199  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
200  for (auto lane : lanes) {
201  sum += lane->getVehicleNumber();
202  }
203  return sum;
204 }
205 
206 double
207 Edge::getMeanSpeed(const std::string& id) {
208  return getEdge(id)->getMeanSpeed();
209 }
210 
211 double
212 Edge::getOccupancy(const std::string& id) {
213  double sum = 0;
214  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
215  for (auto lane : lanes) {
216  sum += lane->getNettoOccupancy();
217  }
218  return sum / (double)lanes.size();
219 }
220 
221 int
222 Edge::getVehicleHaltingNumber(const std::string& id) {
223  int halting = 0;
224  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
225  for (auto lane : lanes) {
226  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
227  for (auto veh : vehs) {
228  if (veh->getSpeed() < SUMO_const_haltingSpeed) {
229  ++halting;
230  }
231  }
232  lane->releaseVehicles();
233  }
234  return halting;
235 }
236 
237 double
238 Edge::getVehicleAverageLength(const std::string& id) {
239  double lengthSum = 0;
240  int noVehicles = 0;
241  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
242  for (auto lane : lanes) {
243  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
244  for (auto veh : vehs) {
245  lengthSum += veh->getVehicleType().getLength();
246  }
247  noVehicles += (int)vehs.size();
248  lane->releaseVehicles();
249  }
250  if (noVehicles == 0) {
251  return 0;
252  }
253  return lengthSum / (double)noVehicles;
254 }
255 
256 std::string
257 Edge::getParameter(const std::string& id, const std::string& paramName) {
258  return getEdge(id)->getParameter(paramName, "");
259 }
260 
261 void
262 Edge::setAllowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
263  SVCPermissions permissions = parseVehicleClasses(classes);
264  setAllowedSVCPermissions(id, permissions);
265 }
266 
267 void
268 Edge::setDisallowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
269  SVCPermissions permissions = invertPermissions(parseVehicleClasses(classes));
270  setAllowedSVCPermissions(id, permissions);
271 }
272 
273 void
274 Edge::setAllowedSVCPermissions(const std::string& id, SVCPermissions permissions) {
275  MSEdge* e = getEdge(id);
276  const std::vector<MSLane*>& lanes = e->getLanes();
277  for (auto lane : lanes) {
278  lane->setPermissions(permissions, MSLane::CHANGE_PERMISSIONS_PERMANENT);
279  }
280  e->rebuildAllowedLanes();
281 }
282 
283 void
284 Edge::addTravelTime(const std::string& id, double begTime, double endTime, double value) {
285  MSNet::getInstance()->getWeightsStorage().addTravelTime(getEdge(id), begTime, endTime, value);
286 }
287 
288 void
289 Edge::addEffort(const std::string& id, double begTime, double endTime, double value) {
290  MSNet::getInstance()->getWeightsStorage().addEffort(getEdge(id), begTime, endTime, value);
291 }
292 
293 void
294 Edge::setMaxSpeed(const std::string& id, double value) {
295  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
296  for (auto lane : lanes) {
297  lane->setMaxSpeed(value);
298  }
299 }
300 
301 void
302 Edge::setParameter(const std::string& id, const std::string& name, const std::string& value) {
303  getEdge(id)->setParameter(name, value);
304 }
305 
306 void
307 Edge::getShape(const std::string& id, PositionVector& shape) {
308  const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
309  shape = lanes.front()->getShape();
310  if (lanes.size() > 1) {
311  copy(lanes.back()->getShape().begin(), lanes.back()->getShape().end(), back_inserter(shape));
312  }
313 }
314 }
315 
316 
317 /****************************************************************************/
318 
319 
320 
static void setAllowedVehicleClasses(const std::string &id, std::vector< std::string > vector)
Definition: Edge.cpp:262
static void setAllowedSVCPermissions(const std::string &id, SVCPermissions permissions)
Definition: Edge.cpp:274
static std::vector< std::string > getIDList()
Definition: Edge.cpp:33
static void insertIDs(std::vector< std::string > &into)
Inserts IDs of all known edges into the given vector.
Definition: MSEdge.cpp:793
static double retrieveExistingEffort(const std::string &id, double time)
Definition: Edge.cpp:54
static double getOccupancy(const std::string &id)
Definition: Edge.cpp:212
static double getVehicleAverageLength(const std::string &id)
Definition: Edge.cpp:238
static void addTravelTime(const std::string &id, double begTime, double endTime, double value)
Definition: Edge.cpp:284
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
static double getFuelConsumption(const std::string &id)
Definition: Edge.cpp:164
static void addEffort(const std::string &id, double begTime, double endTime, double value)
Definition: Edge.cpp:289
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
double getMeanSpeed() const
get the mean speed
Definition: MSEdge.cpp:700
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:744
static double getWaitingSeconds(const std::string &id)
Definition: Edge.cpp:78
static double getCurrentTravelTime(const std::string &id)
Definition: Edge.cpp:64
static double retrieveExistingTravelTime(const std::string &id, double time)
Definition: Edge.cpp:44
static double getNOxEmissions(const std::string &id)
Definition: Edge.cpp:154
std::vector< MSTransportable * > getSortedPersons(SUMOTime timestep, bool includeRiding=false) const
Returns this edge&#39;s persons sorted by pos.
Definition: MSEdge.cpp:876
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:727
static int getIDCount()
Definition: Edge.cpp:40
static const std::vector< std::string > getPersonIDs(const std::string &id)
Definition: Edge.cpp:88
static int getVehicleNumber(const std::string &id)
Definition: Edge.cpp:197
A road/street connecting two junctions.
Definition: MSEdge.h:80
void rebuildAllowedLanes()
Definition: MSEdge.cpp:246
A list of positions.
static double getNoiseEmissions(const std::string &id)
Definition: Edge.cpp:174
void addTravelTime(const MSEdge *const e, double begin, double end, double value)
Adds a travel time information for an edge and a time span.
static double getCOEmissions(const std::string &id)
Definition: Edge.cpp:124
static double getPMxEmissions(const std::string &id)
Definition: Edge.cpp:144
static void setMaxSpeed(const std::string &id, double value)
Definition: Edge.cpp:294
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static double getElectricityConsumption(const std::string &id)
Definition: Edge.cpp:187
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
Definition: Edge.cpp:302
static std::string getParameter(const std::string &id, const std::string &paramName)
Definition: Edge.cpp:257
static MSEdge * getEdge(const std::string &id)
Definition: Edge.cpp:69
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:89
Definition: Edge.cpp:31
static void getShape(const std::string &id, PositionVector &shape)
Definition: Edge.cpp:307
static double getHCEmissions(const std::string &id)
Definition: Edge.cpp:134
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static int getVehicleHaltingNumber(const std::string &id)
Definition: Edge.cpp:222
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
static double getCO2Emissions(const std::string &id)
Definition: Edge.cpp:114
static void setDisallowedVehicleClasses(const std::string &id, std::vector< std::string > classes)
Definition: Edge.cpp:268
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1073
static double getMeanSpeed(const std::string &id)
Definition: Edge.cpp:207
static double sum(double val)
Computes the resulting noise.
static const std::vector< std::string > getVehicleIDs(const std::string &id)
Definition: Edge.cpp:99
MSEdgeWeightsStorage & getWeightsStorage()
Returns the net&#39;s internal edge travel times/efforts container.
Definition: MSNet.cpp:785
void addEffort(const MSEdge *const e, double begin, double end, double value)
Adds an effort information for an edge and a time span.