Eclipse SUMO - Simulation of Urban MObility
libsumo/VehicleType.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-2024 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 <microsim/MSNet.h>
24 #include <microsim/MSVehicleType.h>
25 #include <libsumo/TraCIConstants.h>
28 #include "Helper.h"
29 #include "VehicleType.h"
30 
31 
32 namespace libsumo {
33 // ===========================================================================
34 // static member initializations
35 // ===========================================================================
36 SubscriptionResults VehicleType::mySubscriptionResults;
37 ContextSubscriptionResults VehicleType::myContextSubscriptionResults;
38 
39 
40 // ===========================================================================
41 // static member definitions
42 // ===========================================================================
43 std::vector<std::string>
44 VehicleType::getIDList() {
45  std::vector<std::string> ids;
47  return ids;
48 }
49 
50 
51 int
52 VehicleType::getIDCount() {
53  return (int)getIDList().size();
54 }
55 
56 
57 double
58 VehicleType::getLength(const std::string& typeID) {
59  return getVType(typeID)->getLength();
60 }
61 
62 
63 double
64 VehicleType::getMaxSpeed(const std::string& typeID) {
65  return getVType(typeID)->getMaxSpeed();
66 }
67 
68 
69 double
70 VehicleType::getActionStepLength(const std::string& typeID) {
71  return getVType(typeID)->getActionStepLengthSecs();
72 }
73 
74 
75 double
76 VehicleType::getSpeedFactor(const std::string& typeID) {
77  return getVType(typeID)->getSpeedFactor().getParameter()[0];
78 }
79 
80 
81 double
82 VehicleType::getSpeedDeviation(const std::string& typeID) {
83  return getVType(typeID)->getSpeedFactor().getParameter()[1];
84 }
85 
86 
87 double
88 VehicleType::getAccel(const std::string& typeID) {
89  return getVType(typeID)->getCarFollowModel().getMaxAccel();
90 }
91 
92 
93 double
94 VehicleType::getDecel(const std::string& typeID) {
95  return getVType(typeID)->getCarFollowModel().getMaxDecel();
96 }
97 
98 
99 double
100 VehicleType::getEmergencyDecel(const std::string& typeID) {
101  return getVType(typeID)->getCarFollowModel().getEmergencyDecel();
102 }
103 
104 
105 double
106 VehicleType::getApparentDecel(const std::string& typeID) {
107  return getVType(typeID)->getCarFollowModel().getApparentDecel();
108 }
109 
110 
111 double
112 VehicleType::getImperfection(const std::string& typeID) {
113  return getVType(typeID)->getCarFollowModel().getImperfection();
114 }
115 
116 
117 double
118 VehicleType::getTau(const std::string& typeID) {
119  return getVType(typeID)->getCarFollowModel().getHeadwayTime();
120 }
121 
122 
123 std::string
124 VehicleType::getVehicleClass(const std::string& typeID) {
125  return toString(getVType(typeID)->getVehicleClass());
126 }
127 
128 
129 std::string
130 VehicleType::getEmissionClass(const std::string& typeID) {
131  return PollutantsInterface::getName(getVType(typeID)->getEmissionClass());
132 }
133 
134 
135 std::string
136 VehicleType::getShapeClass(const std::string& typeID) {
137  return getVehicleShapeName(getVType(typeID)->getGuiShape());
138 }
139 
140 
141 double
142 VehicleType::getMinGap(const std::string& typeID) {
143  return getVType(typeID)->getMinGap();
144 }
145 
146 
147 double
148 VehicleType::getWidth(const std::string& typeID) {
149  return getVType(typeID)->getWidth();
150 }
151 
152 
153 double
154 VehicleType::getHeight(const std::string& typeID) {
155  return getVType(typeID)->getHeight();
156 }
157 
158 
159 TraCIColor
160 VehicleType::getColor(const std::string& typeID) {
161  return Helper::makeTraCIColor(getVType(typeID)->getColor());
162 }
163 
164 
165 double
166 VehicleType::getMinGapLat(const std::string& typeID) {
167  return getVType(typeID)->getMinGapLat();
168 }
169 
170 
171 double
172 VehicleType::getMaxSpeedLat(const std::string& typeID) {
173  return getVType(typeID)->getMaxSpeedLat();
174 }
175 
176 
177 std::string
178 VehicleType::getLateralAlignment(const std::string& typeID) {
179  if (getVType(typeID)->getPreferredLateralAlignment() != LatAlignmentDefinition::GIVEN) {
180  return toString(getVType(typeID)->getPreferredLateralAlignment());
181  } else {
182  return toString(getVType(typeID)->getPreferredLateralAlignmentOffset());
183  }
184 }
185 
186 
187 std::string
188 VehicleType::getParameter(const std::string& typeID, const std::string& key) {
189  if (StringUtils::startsWith(key, "junctionModel.")) {
190  const std::string attrName = key.substr(14);
191  if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
192  throw TraCIException("Invalid junctionModel parameter '" + key + "' for type '" + typeID + "'");
193  }
195  if (SUMOVTypeParameter::AllowedJMAttrs.count(attr) == 0) {
196  throw TraCIException("Invalid junctionModel parameter '" + key + "' for type '" + typeID + "'");
197  }
198  if (getVType(typeID)->getParameter().jmParameter.count(attr) != 0) {
199  return getVType(typeID)->getParameter().jmParameter.find(attr)->second;
200  } else {
201  return "";
202  }
203  } else {
204  return getVType(typeID)->getParameter().getParameter(key, "");
205  }
206 }
207 
209 
210 int
211 VehicleType::getPersonCapacity(const std::string& typeID) {
212  return getVType(typeID)->getPersonCapacity();
213 }
214 
215 double
216 VehicleType::getScale(const std::string& typeID) {
217  return getVType(typeID)->getParameter().scale;
218 }
219 
220 double
221 VehicleType::getBoardingDuration(const std::string& typeID) {
222  return STEPS2TIME(getVType(typeID)->getBoardingDuration(true));
223 }
224 
225 double
226 VehicleType::getImpatience(const std::string& typeID) {
227  return getVType(typeID)->getImpatience();
228 }
229 
230 void
231 VehicleType::setLength(const std::string& typeID, double length) {
232  getVType(typeID)->setLength(length);
233 }
234 
235 
236 void
237 VehicleType::setMaxSpeed(const std::string& typeID, double speed) {
238  getVType(typeID)->setMaxSpeed(speed);
239 }
240 
241 
242 void
243 VehicleType::setActionStepLength(const std::string& typeID, double actionStepLength, bool resetActionOffset) {
244  getVType(typeID)->setActionStepLength(SUMOVehicleParserHelper::processActionStepLength(actionStepLength), resetActionOffset);
245 }
246 
247 
248 void
249 VehicleType::setBoardingDuration(const std::string& typeID, double boardingDuration) {
250  getVType(typeID)->setBoardingDuration(TIME2STEPS(boardingDuration), true);
251 }
252 
253 
254 void
255 VehicleType::setImpatience(const std::string& typeID, double impatience) {
256  getVType(typeID)->setImpatience(impatience);
257 }
258 
259 
260 void
261 VehicleType::setVehicleClass(const std::string& typeID, const std::string& clazz) {
262  getVType(typeID)->setVClass(getVehicleClassID(clazz));
263 }
264 
265 
266 void
267 VehicleType::setSpeedFactor(const std::string& typeID, double factor) {
268  getVType(typeID)->setSpeedFactor(factor);
269 }
270 
271 
272 void
273 VehicleType::setSpeedDeviation(const std::string& typeID, double deviation) {
274  getVType(typeID)->setSpeedDeviation(deviation);
275 }
276 
277 
278 void
279 VehicleType::setEmissionClass(const std::string& typeID, const std::string& clazz) {
280  getVType(typeID)->setEmissionClass(PollutantsInterface::getClassByName(clazz));
281 }
282 
283 
284 void
285 VehicleType::setShapeClass(const std::string& typeID, const std::string& shapeClass) {
286  getVType(typeID)->setShape(getVehicleShapeID(shapeClass));
287 }
288 
289 
290 void
291 VehicleType::setWidth(const std::string& typeID, double width) {
292  getVType(typeID)->setWidth(width);
293 }
294 
295 
296 void
297 VehicleType::setHeight(const std::string& typeID, double height) {
298  getVType(typeID)->setHeight(height);
299 }
300 
301 
302 void
303 VehicleType::setMinGap(const std::string& typeID, double minGap) {
304  getVType(typeID)->setMinGap(minGap);
305 }
306 
307 
308 void
309 VehicleType::setAccel(const std::string& typeID, double accel) {
310  getVType(typeID)->setAccel(accel);
311 }
312 
313 
314 void
315 VehicleType::setDecel(const std::string& typeID, double decel) {
316  MSVehicleType* v = getVType(typeID);
317  v->setDecel(decel);
318  // automatically raise emergencyDecel to ensure it is at least as high as decel
319  if (decel > v->getCarFollowModel().getEmergencyDecel()) {
320  if (v->getParameter().cfParameter.count(SUMO_ATTR_EMERGENCYDECEL) > 0) {
321  // notify user only if emergencyDecel was previously specified
322  WRITE_WARNINGF(TL("Automatically setting emergencyDecel to % for vType '%' to match decel."), toString(decel), typeID);
323  }
324  v->setEmergencyDecel(decel);
325  }
326 }
327 
328 
329 void
330 VehicleType::setEmergencyDecel(const std::string& typeID, double decel) {
331  MSVehicleType* v = getVType(typeID);
332  v->setEmergencyDecel(decel);
333  if (decel < v->getCarFollowModel().getMaxDecel()) {
334  WRITE_WARNINGF(TL("New value of emergencyDecel (%) is lower than decel (%)"), toString(decel), toString(v->getCarFollowModel().getMaxDecel()));
335  }
336 }
337 
338 
339 void
340 VehicleType::setApparentDecel(const std::string& typeID, double decel) {
341  getVType(typeID)->setApparentDecel(decel);
342 }
343 
344 
345 void
346 VehicleType::setImperfection(const std::string& typeID, double imperfection) {
347  getVType(typeID)->setImperfection(imperfection);
348 }
349 
350 
351 void
352 VehicleType::setTau(const std::string& typeID, double tau) {
353  getVType(typeID)->setTau(tau);
354 }
355 
356 
357 void
358 VehicleType::setColor(const std::string& typeID, const TraCIColor& c) {
359  getVType(typeID)->setColor(Helper::makeRGBColor(c));
360 }
361 
362 
363 void
364 VehicleType::setMinGapLat(const std::string& typeID, double minGapLat) {
365  getVType(typeID)->setMinGapLat(minGapLat);
366 }
367 
368 
369 void
370 VehicleType::setMaxSpeedLat(const std::string& typeID, double speed) {
371  getVType(typeID)->setMaxSpeedLat(speed);
372 }
373 
374 
375 void
376 VehicleType::setLateralAlignment(const std::string& typeID, const std::string& latAlignment) {
377  double lao;
379  if (SUMOVTypeParameter::parseLatAlignment(latAlignment, lao, lad)) {
380  getVType(typeID)->setPreferredLateralAlignment(lad, lao);
381  } else {
382  throw TraCIException("Unknown value '" + latAlignment + "' when setting latAlignment for vType '" + typeID + "';\n must be one of (\"right\", \"center\", \"arbitrary\", \"nice\", \"compact\", \"left\" or a float)");
383  }
384 }
385 
386 void
387 VehicleType::setScale(const std::string& typeID, double value) {
388  getVType(typeID)->setScale(value);
389 }
390 
391 void
392 VehicleType::copy(const std::string& origTypeID, const std::string& newTypeID) {
393  getVType(origTypeID)->duplicateType(newTypeID, true);
394 }
395 
396 
397 void
398 VehicleType::setParameter(const std::string& typeID, const std::string& name, const std::string& value) {
399  if (StringUtils::startsWith(name, "junctionModel.")) {
400  const std::string attrName = name.substr(14);
401  if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
402  throw TraCIException("Invalid junctionModel parameter '" + name + "' for type '" + typeID + "'");
403  }
405  if (SUMOVTypeParameter::AllowedJMAttrs.count(attr) == 0) {
406  throw TraCIException("Invalid junctionModel parameter '" + name + "' for type '" + typeID + "'");
407  }
408  try {
409  StringUtils::toDouble(value); // check number format
410  ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).jmParameter[attr] = value;
411  } catch (NumberFormatException&) {
412  throw TraCIException("Invalid junctionModel parameter value '" + value + "' for type '" + typeID + " (should be numeric)'");
413  }
414  } else {
415  ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).setParameter(name, value);
416  }
417 }
418 
419 
421 
422 
424 VehicleType::getVType(std::string id) {
426  if (t == nullptr) {
427  throw TraCIException("Vehicle type '" + id + "' is not known");
428  }
429  return t;
430 }
431 
432 
433 std::shared_ptr<VariableWrapper>
434 VehicleType::makeWrapper() {
435  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
436 }
437 
438 
439 bool
440 VehicleType::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
441  return handleVariableWithID(objID, objID, variable, wrapper, paramData);
442 }
443 
444 
445 bool
446 VehicleType::handleVariableWithID(const std::string& objID, const std::string& typeID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
447  switch (variable) {
448  case TRACI_ID_LIST:
449  return wrapper->wrapStringList(objID, variable, getIDList());
450  case ID_COUNT:
451  return wrapper->wrapInt(objID, variable, getIDCount());
452  case VAR_LENGTH:
453  return wrapper->wrapDouble(objID, variable, getLength(typeID));
454  case VAR_HEIGHT:
455  return wrapper->wrapDouble(objID, variable, getHeight(typeID));
456  case VAR_MINGAP:
457  return wrapper->wrapDouble(objID, variable, getMinGap(typeID));
458  case VAR_MAXSPEED:
459  return wrapper->wrapDouble(objID, variable, getMaxSpeed(typeID));
460  case VAR_ACCEL:
461  return wrapper->wrapDouble(objID, variable, getAccel(typeID));
462  case VAR_DECEL:
463  return wrapper->wrapDouble(objID, variable, getDecel(typeID));
464  case VAR_EMERGENCY_DECEL:
465  return wrapper->wrapDouble(objID, variable, getEmergencyDecel(typeID));
466  case VAR_APPARENT_DECEL:
467  return wrapper->wrapDouble(objID, variable, getApparentDecel(typeID));
469  return wrapper->wrapDouble(objID, variable, getActionStepLength(typeID));
470  case VAR_IMPERFECTION:
471  return wrapper->wrapDouble(objID, variable, getImperfection(typeID));
472  case VAR_TAU:
473  return wrapper->wrapDouble(objID, variable, getTau(typeID));
474  case VAR_SPEED_FACTOR:
475  return wrapper->wrapDouble(objID, variable, getSpeedFactor(typeID));
476  case VAR_SPEED_DEVIATION:
477  return wrapper->wrapDouble(objID, variable, getSpeedDeviation(typeID));
478  case VAR_VEHICLECLASS:
479  return wrapper->wrapString(objID, variable, getVehicleClass(typeID));
480  case VAR_EMISSIONCLASS:
481  return wrapper->wrapString(objID, variable, getEmissionClass(typeID));
482  case VAR_SHAPECLASS:
483  return wrapper->wrapString(objID, variable, getShapeClass(typeID));
484  case VAR_WIDTH:
485  return wrapper->wrapDouble(objID, variable, getWidth(typeID));
486  case VAR_COLOR:
487  return wrapper->wrapColor(objID, variable, getColor(typeID));
488  case VAR_MINGAP_LAT:
489  return wrapper->wrapDouble(objID, variable, getMinGapLat(typeID));
490  case VAR_MAXSPEED_LAT:
491  return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(typeID));
492  case VAR_LATALIGNMENT:
493  return wrapper->wrapString(objID, variable, getLateralAlignment(typeID));
494  case VAR_PERSON_CAPACITY:
495  return wrapper->wrapInt(objID, variable, getPersonCapacity(typeID));
497  return wrapper->wrapDouble(objID, variable, getBoardingDuration(typeID));
498  case VAR_IMPATIENCE:
499  return wrapper->wrapDouble(objID, variable, getImpatience(typeID));
500  case VAR_SCALE:
501  return wrapper->wrapDouble(objID, variable, getScale(typeID));
503  paramData->readUnsignedByte();
504  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
506  paramData->readUnsignedByte();
507  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
508  default:
509  return false;
510  }
511 }
512 
513 }
514 
515 
516 /****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define TL(string)
Definition: MsgHandler.h:315
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
#define TIME2STEPS(x)
Definition: SUMOTime.h:57
LatAlignmentDefinition
Possible ways to choose the lateral alignment, i.e., how vehicles align themselves within their lane.
@ GIVEN
The alignment as offset is given.
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
SUMOVehicleShape getVehicleShapeID(const std::string &name)
Returns the class id of the shape class given by its name.
std::string getVehicleShapeName(SUMOVehicleShape id)
Returns the class name of the shape class given by its id.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_EMERGENCYDECEL
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
double getEmergencyDecel() const
Get the vehicle type's maximal phisically possible deceleration [m/s^2].
Definition: MSCFModel.h:272
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:264
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, SumoRNG *rng=nullptr, bool readOnly=false)
Returns the named vehicle type or a sample from the named distribution.
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector.
The car-following model and parameter.
Definition: MSVehicleType.h:63
void setEmergencyDecel(double emergencyDecel)
Set a new value for this type's emergency deceleration.
void setDecel(double decel)
Set a new value for this type's deceleration.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
const SUMOVTypeParameter & getParameter() const
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
Structure representing possible vehicle parameter.
SubParams cfParameter
Car-following parameter.
static std::set< SumoXMLAttr > AllowedJMAttrs
allowed attrs for the junction model
static bool parseLatAlignment(const std::string &val, double &lao, LatAlignmentDefinition &lad)
Parses and validates a given latAlignment value.
static SUMOTime processActionStepLength(double given)
Checks and converts given value for the action step length from seconds to miliseconds assuring it be...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
C++ TraCI client API implementation.
Definition: VehicleType.h:83
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:360
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:371
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
TRACI_CONST int VAR_VEHICLECLASS
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_IMPATIENCE
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_SCALE
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_SHAPECLASS
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:338
TRACI_CONST int VAR_ACTIONSTEPLENGTH
TRACI_CONST int VAR_SPEED_FACTOR
TRACI_CONST int VAR_TAU
TRACI_CONST int VAR_BOARDING_DURATION
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_PERSON_CAPACITY
TRACI_CONST int VAR_MAXSPEED
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:337
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_MAXSPEED_LAT
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_APPARENT_DECEL
TRACI_CONST int VAR_DECEL
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_EMERGENCY_DECEL
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_ACCEL
TRACI_CONST int VAR_SPEED_DEVIATION
@ key
the parser read a key of a value in an object
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.hpp:4451