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