Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/Edge.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2023 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#include <config.h>
21
22#include <iterator>
23#include <microsim/MSEdge.h>
24#include <microsim/MSLane.h>
27#include <microsim/MSVehicle.h>
29#include <libsumo/Helper.h>
30#include <libsumo/TraCIDefs.h>
32#include <libsumo/Lane.h>
34#include "Edge.h"
35
36
37namespace libsumo {
38// ===========================================================================
39// static member initializations
40// ===========================================================================
41SubscriptionResults Edge::mySubscriptionResults;
42ContextSubscriptionResults Edge::myContextSubscriptionResults;
43
44
45// ===========================================================================
46// static member definitions
47// ===========================================================================
48std::vector<std::string>
49Edge::getIDList() {
50 std::vector<std::string> ids;
52 return ids;
53}
54
55
56int
57Edge::getIDCount() {
58 return (int)getIDList().size();
59}
60
61
62double
63Edge::getAdaptedTraveltime(const std::string& edgeID, double time) {
64 const MSEdge* e = getEdge(edgeID);
65 double value;
66 if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingTravelTime(e, time, value)) {
67 return -1.;
68 }
69 return value;
70}
71
72
73double
74Edge::getEffort(const std::string& edgeID, double time) {
75 const MSEdge* e = getEdge(edgeID);
76 double value;
77 if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingEffort(e, time, value)) {
78 return -1.;
79 }
80 return value;
81}
82
83
84double
85Edge::getTraveltime(const std::string& edgeID) {
86 return getEdge(edgeID)->getCurrentTravelTime();
87}
88
89
90MSEdge*
91Edge::getEdge(const std::string& edgeID) {
92 MSEdge* e = MSEdge::dictionary(edgeID);
93 if (e == nullptr) {
94 throw TraCIException("Edge '" + edgeID + "' is not known");
95 }
96 return e;
97}
98
99
100double
101Edge::getWaitingTime(const std::string& edgeID) {
102 return getEdge(edgeID)->getWaitingSeconds();
103}
104
105
106const std::vector<std::string>
107Edge::getLastStepPersonIDs(const std::string& edgeID) {
108 std::vector<std::string> personIDs;
109 std::vector<MSTransportable*> persons = getEdge(edgeID)->getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
110 personIDs.reserve(persons.size());
111 for (MSTransportable* p : persons) {
112 personIDs.push_back(p->getID());
113 }
114 return personIDs;
115}
116
117
118const std::vector<std::string>
119Edge::getLastStepVehicleIDs(const std::string& edgeID) {
120 std::vector<std::string> vehIDs;
121 for (const SUMOVehicle* veh : getEdge(edgeID)->getVehicles()) {
122 vehIDs.push_back(veh->getID());
123 }
124 return vehIDs;
125}
126
127
128double
129Edge::getCO2Emission(const std::string& edgeID) {
130 double sum = 0;
131 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
132 sum += lane->getEmissions<PollutantsInterface::CO2>();
133 }
134 return sum;
135}
136
137
138double
139Edge::getCOEmission(const std::string& edgeID) {
140 double sum = 0;
141 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
142 sum += lane->getEmissions<PollutantsInterface::CO>();
143 }
144 return sum;
145}
146
147
148double
149Edge::getHCEmission(const std::string& edgeID) {
150 double sum = 0;
151 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
152 sum += lane->getEmissions<PollutantsInterface::HC>();
153 }
154 return sum;
155}
156
157
158double
159Edge::getPMxEmission(const std::string& edgeID) {
160 double sum = 0;
161 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
162 sum += lane->getEmissions<PollutantsInterface::PM_X>();
163 }
164 return sum;
165}
166
167
168double
169Edge::getNOxEmission(const std::string& edgeID) {
170 double sum = 0;
171 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
172 sum += lane->getEmissions<PollutantsInterface::NO_X>();
173 }
174 return sum;
175}
176
177
178double
179Edge::getFuelConsumption(const std::string& edgeID) {
180 double sum = 0;
181 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
182 sum += lane->getEmissions<PollutantsInterface::FUEL>();
183 }
184 return sum;
185}
186
187
188double
189Edge::getNoiseEmission(const std::string& edgeID) {
190 double sum = 0;
191 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
192 sum += pow(10., (lane->getHarmonoise_NoiseEmissions() / 10.));
193 }
194 if (sum != 0) {
195 return HelpersHarmonoise::sum(sum);
196 }
197 return sum;
198}
199
200
201double
202Edge::getElectricityConsumption(const std::string& edgeID) {
203 double sum = 0;
204 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
205 sum += lane->getEmissions<PollutantsInterface::ELEC>();
206 }
207 return sum;
208}
209
210
211int
212Edge::getLastStepVehicleNumber(const std::string& edgeID) {
213 return getEdge(edgeID)->getVehicleNumber();
214}
215
216
217double
218Edge::getLastStepMeanSpeed(const std::string& edgeID) {
219 return getEdge(edgeID)->getMeanSpeed();
220}
221
222double
223Edge::getMeanFriction(const std::string& edgeID) {
224 return getEdge(edgeID)->getMeanFriction();
225}
226
227
228double
229Edge::getLastStepOccupancy(const std::string& edgeID) {
230 return getEdge(edgeID)->getOccupancy();
231}
232
233
234int
235Edge::getLastStepHaltingNumber(const std::string& edgeID) {
236 int result = 0;
237 for (const SUMOVehicle* veh : getEdge(edgeID)->getVehicles()) {
238 if (veh->getSpeed() < SUMO_const_haltingSpeed) {
239 result++;
240 }
241 }
242 return result;
243}
244
245
246double
247Edge::getLastStepLength(const std::string& edgeID) {
248 double lengthSum = 0;
249 int numVehicles = 0;
250 for (const SUMOVehicle* veh : getEdge(edgeID)->getVehicles()) {
251 numVehicles++;
252 lengthSum += dynamic_cast<const MSBaseVehicle*>(veh)->getVehicleType().getLength();
253 }
254 if (numVehicles == 0) {
255 return 0;
256 }
257 return lengthSum / numVehicles;
258}
259
260
261int
262Edge::getLaneNumber(const std::string& edgeID) {
263 return (int)getEdge(edgeID)->getLanes().size();
264}
265
266
267std::string
268Edge::getStreetName(const std::string& edgeID) {
269 return getEdge(edgeID)->getStreetName();
270}
271
272
273const std::vector<std::string>
274Edge::getPendingVehicles(const std::string& edgeID) {
275 getEdge(edgeID); // validate edgeID
276 std::vector<std::string> vehIDs;
278 if (veh->getEdge()->getID() == edgeID) {
279 vehIDs.push_back(veh->getID());
280 }
281 }
282 return vehIDs;
283}
284
285
286double
287Edge::getAngle(const std::string& edgeID, double relativePosition) {
288 const std::vector<MSLane*>& lanes = getEdge(edgeID)->getLanes();
289 return lanes.empty() ? libsumo::INVALID_DOUBLE_VALUE : Lane::getAngle(lanes.front()->getID(), relativePosition);
290}
291
292
293std::string
294Edge::getParameter(const std::string& edgeID, const std::string& param) {
295 return getEdge(edgeID)->getParameter(param, "");
296}
297
298
300
301
302void
303Edge::setAllowed(const std::string& edgeID, std::string allowedClasses) {
304 setAllowedSVCPermissions(edgeID, parseVehicleClasses(allowedClasses));
305}
306
307
308void
309Edge::setAllowed(const std::string& edgeID, std::vector<std::string> allowedClasses) {
310 setAllowedSVCPermissions(edgeID, parseVehicleClasses(allowedClasses));
311}
312
313
314void
315Edge::setDisallowed(const std::string& edgeID, std::string disallowedClasses) {
316 setAllowedSVCPermissions(edgeID, invertPermissions(parseVehicleClasses(disallowedClasses)));
317}
318
319
320void
321Edge::setDisallowed(const std::string& edgeID, std::vector<std::string> disallowedClasses) {
322 setAllowedSVCPermissions(edgeID, invertPermissions(parseVehicleClasses(disallowedClasses)));
323}
324
325
326void
327Edge::setAllowedSVCPermissions(const std::string& edgeID, int permissions) {
328 MSEdge* e = getEdge(edgeID);
329 for (MSLane* lane : e->getLanes()) {
330 lane->setPermissions(permissions, MSLane::CHANGE_PERMISSIONS_PERMANENT);
331 }
333}
334
335
336void
337Edge::adaptTraveltime(const std::string& edgeID, double time, double beginSeconds, double endSeconds) {
338 MSNet::getInstance()->getWeightsStorage().addTravelTime(getEdge(edgeID), beginSeconds, endSeconds, time);
339}
340
341
342void
343Edge::setEffort(const std::string& edgeID, double effort, double beginSeconds, double endSeconds) {
344 MSNet::getInstance()->getWeightsStorage().addEffort(getEdge(edgeID), beginSeconds, endSeconds, effort);
345}
346
347
348void
349Edge::setMaxSpeed(const std::string& edgeID, double speed) {
350 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
351 lane->setMaxSpeed(speed);
352 }
353}
354
355void
356Edge::setFriction(const std::string& edgeID, double value) {
357 for (MSLane* lane : getEdge(edgeID)->getLanes()) {
358 lane->setFrictionCoefficient(value);
359 }
360}
361
362void
363Edge::setParameter(const std::string& edgeID, const std::string& name, const std::string& value) {
364 getEdge(edgeID)->setParameter(name, value);
365}
366
367
369
370
371void
372Edge::storeShape(const std::string& edgeID, PositionVector& shape) {
373 const MSEdge* const e = getEdge(edgeID);
374 const std::vector<MSLane*>& lanes = e->getLanes();
375 shape = lanes.front()->getShape();
376 if (lanes.size() > 1) {
377 copy(lanes.back()->getShape().begin(), lanes.back()->getShape().end(), back_inserter(shape));
378 }
379}
380
381
382std::shared_ptr<VariableWrapper>
383Edge::makeWrapper() {
384 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
385}
386
387
388bool
389Edge::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
390 switch (variable) {
391 case TRACI_ID_LIST:
392 return wrapper->wrapStringList(objID, variable, getIDList());
393 case ID_COUNT:
394 return wrapper->wrapInt(objID, variable, getIDCount());
396 return wrapper->wrapDouble(objID, variable, getTraveltime(objID));
397 case VAR_WAITING_TIME:
398 return wrapper->wrapDouble(objID, variable, getWaitingTime(objID));
400 return wrapper->wrapStringList(objID, variable, getLastStepPersonIDs(objID));
402 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
403 case VAR_CO2EMISSION:
404 return wrapper->wrapDouble(objID, variable, getCO2Emission(objID));
405 case VAR_COEMISSION:
406 return wrapper->wrapDouble(objID, variable, getCOEmission(objID));
407 case VAR_HCEMISSION:
408 return wrapper->wrapDouble(objID, variable, getHCEmission(objID));
409 case VAR_PMXEMISSION:
410 return wrapper->wrapDouble(objID, variable, getPMxEmission(objID));
411 case VAR_NOXEMISSION:
412 return wrapper->wrapDouble(objID, variable, getNOxEmission(objID));
414 return wrapper->wrapDouble(objID, variable, getFuelConsumption(objID));
416 return wrapper->wrapDouble(objID, variable, getNoiseEmission(objID));
418 return wrapper->wrapDouble(objID, variable, getElectricityConsumption(objID));
420 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
422 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
423 case VAR_FRICTION:
424 return wrapper->wrapDouble(objID, variable, getMeanFriction(objID));
426 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
428 return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
429 case LAST_STEP_LENGTH:
430 return wrapper->wrapDouble(objID, variable, getLastStepLength(objID));
431 case VAR_LANE_INDEX:
432 return wrapper->wrapInt(objID, variable, getLaneNumber(objID));
433 case VAR_NAME:
434 return wrapper->wrapString(objID, variable, getStreetName(objID));
436 return wrapper->wrapStringList(objID, variable, getPendingVehicles(objID));
437 case VAR_ANGLE:
438 paramData->readUnsignedByte();
439 return wrapper->wrapDouble(objID, variable, getAngle(objID, paramData->readDouble()));
441 paramData->readUnsignedByte();
442 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
444 paramData->readUnsignedByte();
445 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
446 default:
447 return false;
448 }
449}
450
451}
452
453
454/****************************************************************************/
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition StdDefs.h:58
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:123
C++ TraCI client API implementation.
static double sum(double val)
Computes the resulting noise.
The base class for microscopic and mesoscopic vehicles.
double getLength() const
Returns the vehicle's length.
A road/street connecting two junctions.
Definition MSEdge.h:77
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:168
void rebuildAllowedLanes(const bool onInit=false)
Definition MSEdge.cpp:300
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary....
Definition MSEdge.cpp:945
static void insertIDs(std::vector< std::string > &into)
Inserts IDs of all known edges into the given vector.
Definition MSEdge.cpp:1000
double getWaitingSeconds() const
return accumated waiting time for all vehicles on this edges lanes or segments
Definition MSEdge.cpp:1424
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 addEffort(const MSEdge *const e, double begin, double end, double value)
Adds an effort information for an edge and a time span.
const MSVehicleContainer::VehicleVector & getPendingVehicles() const
retrieve vehicles waiting for insertion
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static const long CHANGE_PERMISSIONS_PERMANENT
Definition MSLane.h:1334
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:183
MSEdgeWeightsStorage & getWeightsStorage()
Returns the net's internal edge travel times/efforts container.
Definition MSNet.cpp:1195
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition MSNet.h:433
A list of positions.
Representation of a vehicle.
Definition SUMOVehicle.h:62
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual double readDouble()
Definition storage.cpp:362
TRACI_CONST double INVALID_DOUBLE_VALUE
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int VAR_NOXEMISSION
TRACI_CONST int VAR_NAME
TRACI_CONST int LAST_STEP_PERSON_ID_LIST
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_WAITING_TIME
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:338
TRACI_CONST int LAST_STEP_LENGTH
TRACI_CONST int VAR_ANGLE
TRACI_CONST int VAR_LANE_INDEX
TRACI_CONST int VAR_PMXEMISSION
TRACI_CONST int VAR_COEMISSION
TRACI_CONST int LAST_STEP_MEAN_SPEED
TRACI_CONST int VAR_CO2EMISSION
TRACI_CONST int VAR_PENDING_VEHICLES
TRACI_CONST int VAR_FUELCONSUMPTION
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:337
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
TRACI_CONST int VAR_HCEMISSION
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int VAR_NOISEEMISSION
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_FRICTION
TRACI_CONST int VAR_CURRENT_TRAVELTIME
TRACI_CONST int VAR_ELECTRICITYCONSUMPTION