Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 
21 #include <iterator>
22 #include <microsim/MSEdge.h>
23 #include <microsim/MSLane.h>
26 #include <microsim/MSVehicle.h>
27 #include <libsumo/TraCIDefs.h>
28 #include <libsumo/TraCIConstants.h>
30 #include "Edge.h"
31 
32 
33 namespace libsumo {
34 // ===========================================================================
35 // static member initializations
36 // ===========================================================================
39 
40 
41 // ===========================================================================
42 // static member definitions
43 // ===========================================================================
44 std::vector<std::string>
46  std::vector<std::string> ids;
47  MSEdge::insertIDs(ids);
48  return ids;
49 }
50 
51 
52 int
54  return (int)getIDList().size();
55 }
56 
57 
58 double
59 Edge::getAdaptedTraveltime(const std::string& id, double time) {
60  const MSEdge* e = getEdge(id);
61  double value;
62  if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingTravelTime(e, time, value)) {
63  return -1.;
64  }
65  return value;
66 }
67 
68 
69 double
70 Edge::getEffort(const std::string& id, double time) {
71  const MSEdge* e = getEdge(id);
72  double value;
73  if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingEffort(e, time, value)) {
74  return -1.;
75  }
76  return value;
77 }
78 
79 
80 double
81 Edge::getTraveltime(const std::string& id) {
82  return getEdge(id)->getCurrentTravelTime();
83 }
84 
85 
86 MSEdge*
87 Edge::getEdge(const std::string& id) {
88  MSEdge* e = MSEdge::dictionary(id);
89  if (e == nullptr) {
90  throw TraCIException("Edge '" + id + "' is not known");
91  }
92  return e;
93 }
94 
95 
96 double
97 Edge::getWaitingTime(const std::string& id) {
98  return getEdge(id)->getWaitingSeconds();
99 }
100 
101 
102 const std::vector<std::string>
103 Edge::getLastStepPersonIDs(const std::string& id) {
104  std::vector<std::string> personIDs;
105  std::vector<MSTransportable*> persons = getEdge(id)->getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
106  personIDs.reserve(persons.size());
107  for (MSTransportable* p : persons) {
108  personIDs.push_back(p->getID());
109  }
110  return personIDs;
111 }
112 
113 
114 const std::vector<std::string>
115 Edge::getLastStepVehicleIDs(const std::string& id) {
116  std::vector<std::string> vehIDs;
117  for (const SUMOVehicle* veh : getEdge(id)->getVehicles()) {
118  vehIDs.push_back(veh->getID());
119  }
120  return vehIDs;
121 }
122 
123 
124 double
125 Edge::getCO2Emission(const std::string& id) {
126  double sum = 0;
127  for (MSLane* lane : getEdge(id)->getLanes()) {
128  sum += lane->getCO2Emissions();
129  }
130  return sum;
131 }
132 
133 
134 double
135 Edge::getCOEmission(const std::string& id) {
136  double sum = 0;
137  for (MSLane* lane : getEdge(id)->getLanes()) {
138  sum += lane->getCOEmissions();
139  }
140  return sum;
141 }
142 
143 
144 double
145 Edge::getHCEmission(const std::string& id) {
146  double sum = 0;
147  for (MSLane* lane : getEdge(id)->getLanes()) {
148  sum += lane->getHCEmissions();
149  }
150  return sum;
151 }
152 
153 
154 double
155 Edge::getPMxEmission(const std::string& id) {
156  double sum = 0;
157  for (MSLane* lane : getEdge(id)->getLanes()) {
158  sum += lane->getPMxEmissions();
159  }
160  return sum;
161 }
162 
163 
164 double
165 Edge::getNOxEmission(const std::string& id) {
166  double sum = 0;
167  for (MSLane* lane : getEdge(id)->getLanes()) {
168  sum += lane->getNOxEmissions();
169  }
170  return sum;
171 }
172 
173 
174 double
175 Edge::getFuelConsumption(const std::string& id) {
176  double sum = 0;
177  for (MSLane* lane : getEdge(id)->getLanes()) {
178  sum += lane->getFuelConsumption();
179  }
180  return sum;
181 }
182 
183 
184 double
185 Edge::getNoiseEmission(const std::string& id) {
186  double sum = 0;
187  for (MSLane* lane : getEdge(id)->getLanes()) {
188  sum += pow(10., (lane->getHarmonoise_NoiseEmissions() / 10.));
189  }
190  if (sum != 0) {
191  return HelpersHarmonoise::sum(sum);
192  }
193  return sum;
194 }
195 
196 
197 double
198 Edge::getElectricityConsumption(const std::string& id) {
199  double sum = 0;
200  for (MSLane* lane : getEdge(id)->getLanes()) {
201  sum += lane->getElectricityConsumption();
202  }
203  return sum;
204 }
205 
206 
207 int
208 Edge::getLastStepVehicleNumber(const std::string& id) {
209  return getEdge(id)->getVehicleNumber();
210 }
211 
212 
213 double
214 Edge::getLastStepMeanSpeed(const std::string& id) {
215  return getEdge(id)->getMeanSpeed();
216 }
217 
218 
219 double
220 Edge::getLastStepOccupancy(const std::string& id) {
221  return getEdge(id)->getOccupancy();
222 }
223 
224 
225 int
226 Edge::getLastStepHaltingNumber(const std::string& id) {
227  int result = 0;
228  for (const SUMOVehicle* veh : getEdge(id)->getVehicles()) {
229  if (veh->getSpeed() < SUMO_const_haltingSpeed) {
230  result++;
231  }
232  }
233  return result;
234 }
235 
236 
237 double
238 Edge::getLastStepLength(const std::string& id) {
239  double lengthSum = 0;
240  int numVehicles = 0;
241  for (const SUMOVehicle* veh : getEdge(id)->getVehicles()) {
242  numVehicles++;
243  lengthSum += dynamic_cast<const MSBaseVehicle*>(veh)->getVehicleType().getLength();
244  }
245  if (numVehicles == 0) {
246  return 0;
247  }
248  return lengthSum / numVehicles;
249 }
250 
251 
252 int
253 Edge::getLaneNumber(const std::string& id) {
254  return (int)getEdge(id)->getLanes().size();
255 }
256 
257 
258 std::string
259 Edge::getStreetName(const std::string& id) {
260  return getEdge(id)->getStreetName();
261 }
262 
263 
264 std::string
265 Edge::getParameter(const std::string& id, const std::string& paramName) {
266  return getEdge(id)->getParameter(paramName, "");
267 }
268 
269 
271 
272 
273 void
274 Edge::setAllowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
275  SVCPermissions permissions = parseVehicleClasses(classes);
276  setAllowedSVCPermissions(id, permissions);
277 }
278 
279 
280 void
281 Edge::setDisallowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
282  SVCPermissions permissions = invertPermissions(parseVehicleClasses(classes));
283  setAllowedSVCPermissions(id, permissions);
284 }
285 
286 
287 void
288 Edge::setAllowedSVCPermissions(const std::string& id, int permissions) {
289  MSEdge* e = getEdge(id);
290  for (MSLane* lane : e->getLanes()) {
291  lane->setPermissions(permissions, MSLane::CHANGE_PERMISSIONS_PERMANENT);
292  }
293  e->rebuildAllowedLanes();
294  for (MSEdge* const pred : e->getPredecessors()) {
295  pred->rebuildAllowedTargets();
296  }
297 }
298 
299 
300 void
301 Edge::adaptTraveltime(const std::string& id, double value, double begTime, double endTime) {
302  MSNet::getInstance()->getWeightsStorage().addTravelTime(getEdge(id), begTime, endTime, value);
303 }
304 
305 
306 void
307 Edge::setEffort(const std::string& id, double value, double begTime, double endTime) {
308  MSNet::getInstance()->getWeightsStorage().addEffort(getEdge(id), begTime, endTime, value);
309 }
310 
311 
312 void
313 Edge::setMaxSpeed(const std::string& id, double value) {
314  for (MSLane* lane : getEdge(id)->getLanes()) {
315  lane->setMaxSpeed(value);
316  }
317 }
318 
319 
320 void
321 Edge::setParameter(const std::string& id, const std::string& name, const std::string& value) {
322  getEdge(id)->setParameter(name, value);
323 }
324 
325 
327 
328 
329 void
330 Edge::storeShape(const std::string& id, PositionVector& shape) {
331  const MSEdge* const e = getEdge(id);
332  const std::vector<MSLane*>& lanes = e->getLanes();
333  shape = lanes.front()->getShape();
334  if (lanes.size() > 1) {
335  copy(lanes.back()->getShape().begin(), lanes.back()->getShape().end(), back_inserter(shape));
336  }
337 }
338 
339 
340 std::shared_ptr<VariableWrapper>
342  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
343 }
344 
345 
346 bool
347 Edge::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
348  switch (variable) {
349  case TRACI_ID_LIST:
350  return wrapper->wrapStringList(objID, variable, getIDList());
351  case ID_COUNT:
352  return wrapper->wrapInt(objID, variable, getIDCount());
354  return wrapper->wrapDouble(objID, variable, getTraveltime(objID));
355  case VAR_WAITING_TIME:
356  return wrapper->wrapDouble(objID, variable, getWaitingTime(objID));
358  return wrapper->wrapStringList(objID, variable, getLastStepPersonIDs(objID));
360  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
361  case VAR_CO2EMISSION:
362  return wrapper->wrapDouble(objID, variable, getCO2Emission(objID));
363  case VAR_COEMISSION:
364  return wrapper->wrapDouble(objID, variable, getCOEmission(objID));
365  case VAR_HCEMISSION:
366  return wrapper->wrapDouble(objID, variable, getHCEmission(objID));
367  case VAR_PMXEMISSION:
368  return wrapper->wrapDouble(objID, variable, getPMxEmission(objID));
369  case VAR_NOXEMISSION:
370  return wrapper->wrapDouble(objID, variable, getNOxEmission(objID));
371  case VAR_FUELCONSUMPTION:
372  return wrapper->wrapDouble(objID, variable, getFuelConsumption(objID));
373  case VAR_NOISEEMISSION:
374  return wrapper->wrapDouble(objID, variable, getNoiseEmission(objID));
376  return wrapper->wrapDouble(objID, variable, getElectricityConsumption(objID));
378  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
380  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
381  case LAST_STEP_OCCUPANCY:
382  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
384  return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
385  case LAST_STEP_LENGTH:
386  return wrapper->wrapDouble(objID, variable, getLastStepLength(objID));
387  case VAR_LANE_INDEX:
388  return wrapper->wrapInt(objID, variable, getLaneNumber(objID));
389  case VAR_NAME:
390  return wrapper->wrapString(objID, variable, getStreetName(objID));
391  default:
392  return false;
393  }
394 }
395 
396 }
397 
398 
399 /****************************************************************************/
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:241
static void setEffort(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:307
static double getNOxEmission(const std::string &id)
Definition: Edge.cpp:165
double getLength() const
Returns the vehicle&#39;s length.
static int getIDCount()
Definition: Edge.cpp:53
static void insertIDs(std::vector< std::string > &into)
Inserts IDs of all known edges into the given vector.
Definition: MSEdge.cpp:852
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
TRACI_CONST int VAR_COEMISSION
double getWaitingSeconds() const
return accumated waiting time for all vehicles on this edges lanes or segments
Definition: MSEdge.cpp:1218
static double getCO2Emission(const std::string &id)
Definition: Edge.cpp:125
TRACI_CONST int LAST_STEP_LENGTH
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:98
TRACI_CONST int VAR_WAITING_TIME
static ContextSubscriptionResults myContextSubscriptionResults
Definition: Edge.h:102
static double getFuelConsumption(const std::string &id)
Definition: Edge.cpp:175
static double getLastStepMeanSpeed(const std::string &id)
Definition: Edge.cpp:214
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:383
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
static double getHCEmission(const std::string &id)
Definition: Edge.cpp:145
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
static double getAdaptedTraveltime(const std::string &id, double time)
Definition: Edge.cpp:59
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:166
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
double getMeanSpeed() const
get the mean speed
Definition: MSEdge.cpp:732
TRACI_CONST int VAR_FUELCONSUMPTION
static void setDisallowedVehicleClasses(const std::string &id, std::vector< std::string > classes)
Definition: Edge.cpp:281
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:803
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:242
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:51
TRACI_CONST int VAR_NOXEMISSION
std::vector< MSTransportable * > getSortedPersons(SUMOTime timestep, bool includeRiding=false) const
Returns this edge&#39;s persons sorted by pos.
Definition: MSEdge.cpp:950
static double getElectricityConsumption(const std::string &id)
Definition: Edge.cpp:198
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
static std::string getParameter(const std::string &id, const std::string &paramName)
Definition: Edge.cpp:265
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:787
static double getLastStepOccupancy(const std::string &id)
Definition: Edge.cpp:220
TRACI_CONST int TRACI_ID_LIST
A road/street connecting two junctions.
Definition: MSEdge.h:77
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int VAR_CURRENT_TRAVELTIME
void rebuildAllowedLanes()
Definition: MSEdge.cpp:239
TRACI_CONST int VAR_NOISEEMISSION
static SubscriptionResults mySubscriptionResults
Definition: Edge.h:101
static LIBSUMO_GET_PARAMETER_WITH_KEY_API void setAllowedVehicleClasses(const std::string &id, std::vector< std::string > vector)
Definition: Edge.cpp:274
TRACI_CONST int ID_COUNT
static std::string getStreetName(const std::string &id)
Definition: Edge.cpp:259
static double getTraveltime(const std::string &id)
Definition: Edge.cpp:81
Representation of a vehicle.
Definition: SUMOVehicle.h:58
static double getNoiseEmission(const std::string &id)
Definition: Edge.cpp:185
int getVehicleNumber() const
return total number of vehicles on this edges lanes or segments
Definition: MSEdge.cpp:1212
A list of positions.
void addTravelTime(const MSEdge *const e, double begin, double end, double value)
Adds a travel time information for an edge and a time span.
TRACI_CONST int VAR_PMXEMISSION
static double getEffort(const std::string &id, double time)
Definition: Edge.cpp:70
static double getCOEmission(const std::string &id)
Definition: Edge.cpp:135
static int getLastStepHaltingNumber(const std::string &id)
Definition: Edge.cpp:226
static const std::vector< std::string > getLastStepVehicleIDs(const std::string &id)
Definition: Edge.cpp:115
static void setAllowedSVCPermissions(const std::string &id, int permissions)
Definition: Edge.cpp:288
virtual 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...
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:55
const std::string & getStreetName() const
Returns the street name of the edge.
Definition: MSEdge.h:301
static double getWaitingTime(const std::string &id)
Definition: Edge.cpp:97
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
static void setMaxSpeed(const std::string &id, double value)
Definition: Edge.cpp:313
static void adaptTraveltime(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:301
TRACI_CONST int LAST_STEP_MEAN_SPEED
static MSEdge * getEdge(const std::string &id)
Definition: Edge.cpp:87
static std::shared_ptr< VariableWrapper > makeWrapper()
Definition: Edge.cpp:341
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int LAST_STEP_PERSON_ID_LIST
static double getPMxEmission(const std::string &id)
Definition: Edge.cpp:155
static int getLaneNumber(const std::string &id)
Definition: Edge.cpp:253
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
static double getLastStepLength(const std::string &id)
Definition: Edge.cpp:238
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:60
static const std::vector< std::string > getLastStepPersonIDs(const std::string &id)
Definition: Edge.cpp:103
double getOccupancy() const
return mean occupancy on this edges lanes or segments
Definition: MSEdge.cpp:1234
TRACI_CONST int VAR_CO2EMISSION
static std::vector< std::string > getIDList()
Definition: Edge.cpp:45
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Edge.cpp:347
static LIBSUMO_SUBSCRIPTION_API void storeShape(const std::string &id, PositionVector &shape)
Saves the shape of the requested object in the given container.
Definition: Edge.cpp:330
static int getLastStepVehicleNumber(const std::string &id)
Definition: Edge.cpp:208
TRACI_CONST int VAR_LANE_INDEX
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
Definition: Edge.cpp:321
TRACI_CONST int VAR_HCEMISSION
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1239
static double sum(double val)
Computes the resulting noise.
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
TRACI_CONST int VAR_ELECTRICITYCONSUMPTION
TRACI_CONST int VAR_NAME
MSEdgeWeightsStorage & getWeightsStorage()
Returns the net&#39;s internal edge travel times/efforts container.
Definition: MSNet.cpp:984
void addEffort(const MSEdge *const e, double begin, double end, double value)
Adds an effort information for an edge and a time span.