Eclipse SUMO - Simulation of Urban MObility
MSCFModel.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 /****************************************************************************/
21 // The car-following model abstraction
22 /****************************************************************************/
23 #pragma once
24 #include <config.h>
25 
26 #include <cmath>
27 #include <string>
28 #include <utils/common/StdDefs.h>
30 
31 #define INVALID_SPEED 299792458 + 1 // nothing can go faster than the speed of light!
32 // Factor that the minimum emergency decel is increased by in corresponding situations
33 #define EMERGENCY_DECEL_AMPLIFIER 1.2
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class MSVehicleType;
39 class MSVehicle;
40 class MSLane;
41 class MSPerson;
42 class MSLink;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
55 class MSCFModel {
56 
57 public:
58 
60  public:
61  virtual ~VehicleVariables();
62  };
63 
67  MSCFModel(const MSVehicleType* vtype);
68 
69 
71  virtual ~MSCFModel();
72 
73 
76 
83  virtual double finalizeSpeed(MSVehicle* const veh, double vPos) const;
84 
85 
87  virtual double patchSpeedBeforeLC(const MSVehicle* veh, double vMin, double vMax) const {
88  UNUSED_PARAMETER(veh);
89  UNUSED_PARAMETER(vMin);
90  return vMax;
91  }
92 
93 
106  virtual double freeSpeed(const MSVehicle* const veh, double speed, double seen,
107  double maxSpeed, const bool onInsertion = false) const;
108 
109 
119  virtual double followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred = 0) const = 0;
120 
121 
134  virtual double insertionFollowSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred = 0) const;
135 
136 
146  inline double stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
147  return stopSpeed(veh, speed, gap, myDecel);
148  }
149 
160  virtual double stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel) const = 0;
161 
162 
172  virtual double insertionStopSpeed(const MSVehicle* const veh, double speed, double gap) const;
173 
184  virtual double followSpeedTransient(double duration, const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const;
185 
194  virtual double interactionGap(const MSVehicle* const veh, double vL) const;
195 
196 
201  virtual double maximumLaneSpeedCF(double maxSpeed, double maxSpeedLane) const {
202  return MIN2(maxSpeed, maxSpeedLane);
203  }
204 
205 
209  virtual int getModelID() const = 0;
210 
211 
216  virtual MSCFModel* duplicate(const MSVehicleType* vtype) const = 0;
217 
218 
223  return 0;
224  }
226 
227 
231  inline double getMaxAccel() const {
232  return myAccel;
233  }
234 
235 
239  inline double getMaxDecel() const {
240  return myDecel;
241  }
242 
243 
247  inline double getEmergencyDecel() const {
248  return myEmergencyDecel;
249  }
250 
251 
255  inline double getApparentDecel() const {
256  return myApparentDecel;
257  }
258 
261  inline double getCollisionMinGapFactor() const {
263  }
264 
265 
268 
272  virtual double getImperfection() const {
273  return -1;
274  }
275 
276 
280  virtual double getHeadwayTime() const {
281  return myHeadwayTime;
282  }
284 
285 
286 
287 
290 
303  virtual double maxNextSpeed(double speed, const MSVehicle* const veh) const;
304 
305 
315  virtual double minNextSpeed(double speed, const MSVehicle* const veh = 0) const;
316 
326  virtual double minNextSpeedEmergency(double speed, const MSVehicle* const veh = 0) const;
327 
328 
334  virtual double brakeGap(const double speed) const {
335  return brakeGap(speed, myDecel, myHeadwayTime);
336  }
337 
338  virtual double brakeGap(const double speed, const double decel, const double headwayTime) const;
339 
340  static double brakeGapEuler(const double speed, const double decel, const double headwayTime);
341 
342  static double freeSpeed(const double currentSpeed, const double decel, const double dist, const double maxSpeed, const bool onInsertion, const double actionStepLength);
343 
351  inline virtual double getSecureGap(const MSVehicle* const /*veh*/, const MSVehicle* const /*pred*/, const double speed, const double leaderSpeed, const double leaderMaxDecel) const {
352  // The solution approach leaderBrakeGap >= followerBrakeGap is not
353  // secure when the follower can brake harder than the leader because the paths may still cross.
354  // As a workaround we use a value of leaderDecel which errs on the side of caution
355  const double maxDecel = MAX2(myDecel, leaderMaxDecel);
356  double secureGap = MAX2((double) 0, brakeGap(speed, myDecel, myHeadwayTime) - brakeGap(leaderSpeed, maxDecel, 0));
357  return secureGap;
358  }
359 
360  virtual
364  inline double getSpeedAfterMaxDecel(double v) const {
365  return MAX2((double) 0, v - (double) ACCEL2SPEED(myDecel));
366  }
368 
374  SUMOTime getMinimalArrivalTime(double dist, double currentSpeed, double arrivalSpeed) const;
375 
376 
385  static double estimateArrivalTime(double dist, double speed, double maxSpeed, double accel);
386 
400  static double estimateArrivalTime(double dist, double initialSpeed, double arrivalSpeed, double maxSpeed, double accel, double decel);
401 
408  static double avoidArrivalAccel(double dist, double time, double speed, double maxDecel);
409 
410 
415  double getMinimalArrivalSpeed(double dist, double currentSpeed) const;
416 
421  double getMinimalArrivalSpeedEuler(double dist, double currentSpeed) const;
422 
423 
437  static double gapExtrapolation(const double duration, const double currentGap, double v1, double v2, double a1 = 0, double a2 = 0, const double maxV1 = std::numeric_limits<double>::max(), const double maxV2 = std::numeric_limits<double>::max());
438 
451  static double passingTime(const double lastPos, const double passedPos, const double currentPos, const double lastSpeed, const double currentSpeed);
452 
453 
454 
466  static double speedAfterTime(const double t, const double oldSpeed, const double dist);
467 
468 
470  virtual double distAfterTime(double t, double speed, double accel) const;
471 
472 
473 
474  /* @brief estimate speed while accelerating for the given distance
475  * @param[in] dist The distance during which accelerating takes place
476  * @param[in] v The initial speed
477  * @param[in] accel The acceleration
478  * XXX affected by ticket #860 (the formula is invalid for the Euler position update rule)
479  * XXX (Leo) Migrated estimateSpeedAfterDistance() to MSCFModel from MSVehicle as Jakob suggested (removed inline property, because myType is fw-declared)
480  */
481  double estimateSpeedAfterDistance(const double dist, const double v, const double accel) const;
482 
485 
489  virtual void setMaxAccel(double accel) {
490  myAccel = accel;
491  }
492 
493 
497  virtual void setMaxDecel(double decel) {
498  myDecel = decel;
499  }
500 
501 
505  virtual void setEmergencyDecel(double decel) {
506  myEmergencyDecel = decel;
507  }
508 
509 
513  virtual void setApparentDecel(double decel) {
514  myApparentDecel = decel;
515  }
516 
517 
521  virtual void setImperfection(double imperfection) {
522  UNUSED_PARAMETER(imperfection);
523  }
524 
525 
529  virtual void setHeadwayTime(double headwayTime) {
530  myHeadwayTime = headwayTime;
531  }
533 
542  double maximumSafeFollowSpeed(double gap, double egoSpeed, double predSpeed, double predMaxDecel, bool onInsertion = false) const;
543 
544 
556  double calculateEmergencyDeceleration(double gap, double egoSpeed, double predSpeed, double predMaxDecel) const;
557 
558 
566  double maximumSafeStopSpeed(double gap, double decel, double currentSpeed, bool onInsertion = false, double headway = -1) const;
567 
568 
576  double maximumSafeStopSpeedEuler(double gap, double decel, bool onInsertion, double headway) const;
577 
578 
591  double maximumSafeStopSpeedBallistic(double gap, double decel, double currentSpeed, bool onInsertion = false, double headway = -1) const;
592 
600  virtual std::string getParameter(const MSVehicle* veh, const std::string& key) const {
601  UNUSED_PARAMETER(veh);
602  UNUSED_PARAMETER(key);
603  return "";
604  }
605 
613  virtual void setParameter(MSVehicle* veh, const std::string& key, const std::string& value) const {
614  UNUSED_PARAMETER(veh);
615  UNUSED_PARAMETER(key);
616  UNUSED_PARAMETER(value);
617  }
618 
619 protected:
620 
629  void applyHeadwayAndSpeedDifferencePerceptionErrors(const MSVehicle* const veh, double speed, double& gap, double& predSpeed, double predMaxDecel, const MSVehicle* const pred) const;
630 
636  void applyHeadwayPerceptionError(const MSVehicle* const veh, double speed, double& gap) const;
637 
638 
639 protected:
642 
644  double myAccel;
645 
647  double myDecel;
654 
657 
658 
659 
660 };
661 
662 
663 
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:49
long long int SUMOTime
Definition: SUMOTime.h:32
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
T MIN2(T a, T b)
Definition: StdDefs.h:74
T MAX2(T a, T b)
Definition: StdDefs.h:80
The car-following model abstraction.
Definition: MSCFModel.h:55
double estimateSpeedAfterDistance(const double dist, const double v, const double accel) const
Definition: MSCFModel.cpp:707
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:237
virtual double getSecureGap(const MSVehicle *const, const MSVehicle *const, const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
Definition: MSCFModel.h:351
double stopSpeed(const MSVehicle *const veh, const double speed, double gap) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
Definition: MSCFModel.h:146
static double gapExtrapolation(const double duration, const double currentGap, double v1, double v2, double a1=0, double a2=0, const double maxV1=std::numeric_limits< double >::max(), const double maxV2=std::numeric_limits< double >::max())
return the resulting gap if, starting with gap currentGap, two vehicles continue with constant accele...
Definition: MSCFModel.cpp:506
virtual double minNextSpeedEmergency(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed after emergency braking, given the current speed (depends on the numerical ...
Definition: MSCFModel.cpp:254
virtual std::string getParameter(const MSVehicle *veh, const std::string &key) const
try to get the given parameter for this carFollowingModel
Definition: MSCFModel.h:600
virtual double followSpeedTransient(double duration, const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const
Computes the vehicle's follow speed that avoids a collision for the given amount of time.
Definition: MSCFModel.cpp:298
virtual void setImperfection(double imperfection)
Sets a new value for driver imperfection.
Definition: MSCFModel.h:521
double getCollisionMinGapFactor() const
Get the factor of minGap that must be maintained to avoid a collision event.
Definition: MSCFModel.h:261
double maximumSafeStopSpeed(double gap, double decel, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap.
Definition: MSCFModel.cpp:716
double getEmergencyDecel() const
Get the vehicle type's maximal phisically possible deceleration [m/s^2].
Definition: MSCFModel.h:247
static double brakeGapEuler(const double speed, const double decel, const double headwayTime)
Definition: MSCFModel.cpp:88
virtual double interactionGap(const MSVehicle *const veh, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
Definition: MSCFModel.cpp:222
static double avoidArrivalAccel(double dist, double time, double speed, double maxDecel)
Computes the acceleration needed to arrive not before the given time.
Definition: MSCFModel.cpp:463
double getMinimalArrivalSpeed(double dist, double currentSpeed) const
Computes the minimal possible arrival speed after covering a given distance.
Definition: MSCFModel.cpp:480
virtual void setHeadwayTime(double headwayTime)
Sets a new value for desired headway [s].
Definition: MSCFModel.h:529
virtual double patchSpeedBeforeLC(const MSVehicle *veh, double vMin, double vMax) const
apply custom speed adaptations within the given speed bounds
Definition: MSCFModel.h:87
virtual MSCFModel * duplicate(const MSVehicleType *vtype) const =0
Duplicates the car-following model.
virtual double minNextSpeed(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed given the current speed (depends on the numerical update scheme and its ste...
Definition: MSCFModel.cpp:243
virtual double insertionFollowSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const
Computes the vehicle's safe speed (no dawdling) This method is used during the insertion stage....
Definition: MSCFModel.cpp:277
virtual void setEmergencyDecel(double decel)
Sets a new value for maximal physically possible deceleration [m/s^2].
Definition: MSCFModel.h:505
SUMOTime getMinimalArrivalTime(double dist, double currentSpeed, double arrivalSpeed) const
Computes the minimal time needed to cover a distance given the desired speed at arrival.
Definition: MSCFModel.cpp:373
void applyHeadwayPerceptionError(const MSVehicle *const veh, double speed, double &gap) const
Overwrites gap by the perceived value obtained from the vehicle's driver state.
Definition: MSCFModel.cpp:1023
virtual void setMaxAccel(double accel)
Sets a new value for maximum acceleration [m/s^2].
Definition: MSCFModel.h:489
static double speedAfterTime(const double t, const double oldSpeed, const double dist)
Calculates the speed after a time t \in [0,TS] given the initial speed and the distance traveled in a...
Definition: MSCFModel.cpp:678
virtual double brakeGap(const double speed) const
Returns the distance the vehicle needs to halt including driver's reaction time tau (i....
Definition: MSCFModel.h:334
static double passingTime(const double lastPos, const double passedPos, const double currentPos, const double lastSpeed, const double currentSpeed)
Calculates the time at which the position passedPosition has been passed In case of a ballistic updat...
Definition: MSCFModel.cpp:600
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
Definition: MSCFModel.cpp:163
virtual double freeSpeed(const MSVehicle *const veh, double speed, double seen, double maxSpeed, const bool onInsertion=false) const
Computes the vehicle's safe speed without a leader.
Definition: MSCFModel.cpp:266
virtual ~MSCFModel()
Destructor.
Definition: MSCFModel.cpp:66
double maximumSafeStopSpeedEuler(double gap, double decel, bool onInsertion, double headway) const
Returns the maximum next velocity for stopping within gap when using the semi-implicit Euler update.
Definition: MSCFModel.cpp:766
virtual void setMaxDecel(double decel)
Sets a new value for maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:497
double myEmergencyDecel
The vehicle's maximum emergency deceleration [m/s^2].
Definition: MSCFModel.h:649
virtual VehicleVariables * createVehicleVariables() const
Returns model specific values which are stored inside a vehicle and must be used with casting.
Definition: MSCFModel.h:222
void applyHeadwayAndSpeedDifferencePerceptionErrors(const MSVehicle *const veh, double speed, double &gap, double &predSpeed, double predMaxDecel, const MSVehicle *const pred) const
Overwrites gap2pred and predSpeed by the perceived values obtained from the vehicle's driver state,...
Definition: MSCFModel.cpp:987
double maximumSafeFollowSpeed(double gap, double egoSpeed, double predSpeed, double predMaxDecel, bool onInsertion=false) const
Returns the maximum safe velocity for following the given leader.
Definition: MSCFModel.cpp:861
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const =0
Computes the vehicle's follow speed (no dawdling)
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition: MSCFModel.h:653
double calculateEmergencyDeceleration(double gap, double egoSpeed, double predSpeed, double predMaxDecel) const
Returns the minimal deceleration for following the given leader safely.
Definition: MSCFModel.cpp:933
virtual double stopSpeed(const MSVehicle *const veh, const double speed, double gap, double decel) const =0
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
virtual double maximumLaneSpeedCF(double maxSpeed, double maxSpeedLane) const
Returns the maximum velocity the CF-model wants to achieve in the next step.
Definition: MSCFModel.h:201
MSCFModel(const MSVehicleType *vtype)
Constructor.
Definition: MSCFModel.cpp:54
double getApparentDecel() const
Get the vehicle type's apparent deceleration [m/s^2] (the one regarded by its followers.
Definition: MSCFModel.h:255
double myDecel
The vehicle's maximum deceleration [m/s^2].
Definition: MSCFModel.h:647
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition: MSCFModel.h:231
virtual int getModelID() const =0
Returns the model's ID; the XML-Tag number is used.
virtual double getImperfection() const
Get the driver's imperfection.
Definition: MSCFModel.h:272
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition: MSCFModel.h:644
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition: MSCFModel.h:641
virtual double distAfterTime(double t, double speed, double accel) const
calculates the distance travelled after accelerating for time t
Definition: MSCFModel.cpp:348
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:239
virtual double getSpeedAfterMaxDecel(double v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:364
virtual void setApparentDecel(double decel)
Sets a new value for the apparent deceleration [m/s^2].
Definition: MSCFModel.h:513
virtual void setParameter(MSVehicle *veh, const std::string &key, const std::string &value) const
try to set the given parameter for this carFollowingModel
Definition: MSCFModel.h:613
double maximumSafeStopSpeedBallistic(double gap, double decel, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap when using the ballistic positional update.
Definition: MSCFModel.cpp:794
double getMinimalArrivalSpeedEuler(double dist, double currentSpeed) const
Computes the minimal possible arrival speed after covering a given distance for Euler update.
Definition: MSCFModel.cpp:487
static double estimateArrivalTime(double dist, double speed, double maxSpeed, double accel)
Computes the time needed to travel a distance dist given an initial speed and constant acceleration....
Definition: MSCFModel.cpp:392
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:656
double myApparentDecel
The vehicle's deceleration as expected by surrounding traffic [m/s^2].
Definition: MSCFModel.h:651
virtual double insertionStopSpeed(const MSVehicle *const veh, double speed, double gap) const
Computes the vehicle's safe speed for approaching an obstacle at insertion without constraints due to...
Definition: MSCFModel.cpp:288
virtual double getHeadwayTime() const
Get the driver's desired headway [s].
Definition: MSCFModel.h:280
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:75
The car-following model and parameter.
Definition: MSVehicleType.h:62