Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSCFModel_IDM.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
20// The Intelligent Driver Model (IDM) car-following model
21/****************************************************************************/
22#include <config.h>
23
24#include "MSCFModel_IDM.h"
25#include <microsim/MSVehicle.h>
26
27//#define DEBUG_V
28//#define DEBUG_INSERTION_SPEED
29
30#define DEBUG_COND (veh->isSelected())
31//#define DEBUG_COND true
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
38 MSCFModel(vtype),
39 myIDMM(idmm),
40 myDelta(idmm ? 4.0 : vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_DELTA, 4.)),
41 myAdaptationFactor(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_FACTOR, 1.8) : 1.0),
42 myAdaptationTime(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_TIME, 600.0) : 0.0),
43 myIterations(MAX2(1, int(TS / vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_STEPPING, .25) + .5))),
44 myTwoSqrtAccelDecel(double(2 * sqrt(myAccel * myDecel))) {
45 // IDM does not drive very precise and may violate minGap on occasion
47}
48
50
51
52double
53MSCFModel_IDM::minNextSpeed(double speed, const MSVehicle* const /*veh*/) const {
54 // permit exceeding myDecel when approaching stops
55 const double decel = MAX2(myDecel, MIN2(myEmergencyDecel, 1.5));
57 return MAX2(speed - ACCEL2SPEED(decel), 0.);
58 } else {
59 // NOTE: ballistic update allows for negative speeds to indicate a stop within the next timestep
60 return speed - ACCEL2SPEED(decel);
61 }
62}
63
64
65
66double
67MSCFModel_IDM::finalizeSpeed(MSVehicle* const veh, double vPos) const {
68 const double vNext = MSCFModel::finalizeSpeed(veh, vPos);
69 if (myAdaptationFactor != 1.) {
71 vars->levelOfService += (vNext / veh->getLane()->getVehicleMaxSpeed(veh) - vars->levelOfService) / myAdaptationTime * TS;
72 }
73 return vNext;
74}
75
76
77double
78MSCFModel_IDM::freeSpeed(const MSVehicle* const veh, double speed, double seen, double maxSpeed, const bool /*onInsertion*/, const CalcReason /*usage*/) const {
79 if (maxSpeed < 0.) {
80 // can occur for ballistic update (in context of driving at red light)
81 return maxSpeed;
82 }
83 const double secGap = getSecureGap(veh, nullptr, maxSpeed, 0, myDecel);
84 double vSafe;
85 if (speed <= maxSpeed) {
86 // accelerate
87 vSafe = _v(veh, 1e6, speed, maxSpeed, veh->getLane()->getVehicleMaxSpeed(veh), false);
88 } else {
89 // decelerate
90 // @note relax gap to avoid emergency braking
91 // @note since the transition point does not move we set the leader speed to 0
92 vSafe = _v(veh, MAX2(seen, secGap), speed, 0, veh->getLane()->getVehicleMaxSpeed(veh), false);
93 }
94 if (seen < secGap) {
95 // avoid overshoot when close to change in speed limit
96 vSafe = MIN2(vSafe, maxSpeed);
97 }
98 //std::cout << SIMTIME << " speed=" << speed << " maxSpeed=" << maxSpeed << " seen=" << seen << " secGap=" << secGap << " vSafe=" << vSafe << "\n";
99 return vSafe;
100}
101
102
103double
104MSCFModel_IDM::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred, const CalcReason /*usage*/) const {
105 applyHeadwayAndSpeedDifferencePerceptionErrors(veh, speed, gap2pred, predSpeed, predMaxDecel, pred);
106 return _v(veh, gap2pred, speed, predSpeed, veh->getLane()->getVehicleMaxSpeed(veh));
107}
108
109
110double
111MSCFModel_IDM::insertionFollowSpeed(const MSVehicle* const v, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred) const {
112 // see definition of s in _v()
113 double s = MAX2(0., speed * myHeadwayTime + speed * (speed - predSpeed) / myTwoSqrtAccelDecel);
114 if (gap2pred >= s) {
115 // followSpeed always stays below speed because s*s / (gap2pred * gap2pred) > 0. This would prevent insertion with maximum speed at all distances
116 return speed;
117 } else {
118 // we cannot call follow speed directly because it assumes that 'speed'
119 // is the current speed rather than the desired insertion speed.
120 // If the safe speed is much lower than the desired speed, the
121 // followSpeed function would still return a new speed that involves
122 // reasonable braking rather than the actual safe speed (and cause
123 // emergency braking in a subsequent step)
124 const double speed2 = followSpeed(v, speed, gap2pred, predSpeed, predMaxDecel, pred, CalcReason::FUTURE);
125 const double speed3 = followSpeed(v, speed2, gap2pred, predSpeed, predMaxDecel, pred, CalcReason::FUTURE);
126 if (speed2 - speed3 < ACCEL2SPEED(1)) {
127 return speed2;
128 } else {
129#ifdef DEBUG_INSERTION_SPEED
130 std::cout << SIMTIME << " veh=" << v->getID() << " speed=" << speed << " gap2pred=" << gap2pred << " predSpeed=" << predSpeed << " predMaxDecel=" << predMaxDecel << " pred=" << Named::getIDSecure(pred) << " s=" << s << " speed2=" << speed2 << " speed3=" << speed3 << "\n";
131#endif
132 return insertionFollowSpeed(v, speed2, gap2pred, predSpeed, predMaxDecel, pred);
133 }
134 }
135}
136
137
138double
139MSCFModel_IDM::insertionStopSpeed(const MSVehicle* const veh, double speed, double gap) const {
140 // we want to insert the vehicle in an equilibrium state
141 double result = MSCFModel::insertionStopSpeed(veh, speed, gap);
142 int i = 0;
143 while (result - speed < -ACCEL2SPEED(myDecel) && ++i < 10) {
144 speed = result;
145 result = MSCFModel::insertionStopSpeed(veh, speed, gap);
146 }
147 return result;
148}
149
150
151double
152MSCFModel_IDM::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
153 applyHeadwayPerceptionError(veh, speed, gap);
154 if (gap < 0.01) {
155 return 0;
156 }
157 double result = _v(veh, gap, speed, 0, veh->getLane()->getVehicleMaxSpeed(veh), false);
158 //std::cout << SIMTIME << " stopSpeed speed=" << speed << " gap=" << gap << " decel=" << decel << " result=" << result << "\n";
159 if (gap > 0 && speed < NUMERICAL_EPS && result < NUMERICAL_EPS) {
160 // ensure that stops can be reached:
161 //std::cout << " switching to krauss: " << veh->getID() << " gap=" << gap << " speed=" << speed << " res1=" << result << " res2=" << maximumSafeStopSpeed(gap, speed, false, veh->getActionStepLengthSecs())<< "\n";
162 result = maximumSafeStopSpeed(gap, decel, speed, false, veh->getActionStepLengthSecs());
163 }
164 // avoid overshooting the stop location
165 if (gap >= 0) {
166 result = MIN2(result, DIST2SPEED(gap));
167 //if (result * TS > gap) {
168 // std::cout << "Maximum stop speed exceeded for gap=" << gap << " result=" << result << " veh=" << veh->getID() << " speed=" << speed << " t=" << SIMTIME << "\n";
169 //}
170 }
171
172 return result;
173}
174
175
177double
178MSCFModel_IDM::interactionGap(const MSVehicle* const veh, double vL) const {
179 // Resolve the IDM equation to gap. Assume predecessor has
180 // speed != 0 and that vsafe will be the current speed plus acceleration,
181 // i.e that with this gap there will be no interaction.
182 const double acc = myAccel * (1. - pow(veh->getSpeed() / veh->getLane()->getVehicleMaxSpeed(veh), myDelta));
183 const double vNext = veh->getSpeed() + acc;
184 const double gap = (vNext - vL) * (veh->getSpeed() + vL) / (2 * myDecel) + vL;
185
186 // Don't allow timeHeadWay < deltaT situations.
187 return MAX2(gap, SPEED2DIST(vNext));
188}
189
190double
191MSCFModel_IDM::getSecureGap(const MSVehicle* const /*veh*/, const MSVehicle* const /*pred*/, const double speed, const double leaderSpeed, const double /*leaderMaxDecel*/) const {
192 const double delta_v = speed - leaderSpeed;
193 return MAX2(0.0, speed * myHeadwayTime + speed * delta_v / myTwoSqrtAccelDecel);
194}
195
196
197double
198MSCFModel_IDM::_v(const MSVehicle* const veh, const double gap2pred, const double egoSpeed,
199 const double predSpeed, const double desSpeed, const bool respectMinGap) const {
200// this is more or less based on http://www.vwi.tu-dresden.de/~treiber/MicroApplet/IDM.html
201// and http://arxiv.org/abs/cond-mat/0304337
202// we assume however constant speed for the leader
203 double headwayTime = myHeadwayTime;
204 if (myAdaptationFactor != 1.) {
206 headwayTime *= myAdaptationFactor + vars->levelOfService * (1. - myAdaptationFactor);
207 }
208 double newSpeed = egoSpeed;
209 double gap = gap2pred;
210 if (respectMinGap) {
211 // gap2pred comes with minGap already subtracted so we need to add it here again
212 gap += myType->getMinGap();
213 }
214#ifdef DEBUG_V
215 if (DEBUG_COND) {
216 std::cout << SIMTIME << " veh=" << veh->getID() << " gap2pred=" << gap2pred << " egoSpeed=" << egoSpeed << " predSpeed=" << predSpeed << " desSpeed=" << desSpeed << " rMG=" << respectMinGap << " hw=" << headwayTime << "\n";
217 }
218#endif
219 for (int i = 0; i < myIterations; i++) {
220 const double delta_v = newSpeed - predSpeed;
221 double s = MAX2(0., newSpeed * headwayTime + newSpeed * delta_v / myTwoSqrtAccelDecel);
222 if (respectMinGap) {
223 s += myType->getMinGap();
224 }
225 gap = MAX2(NUMERICAL_EPS, gap); // avoid singularity
226 const double acc = myAccel * (1. - pow(newSpeed / MAX2(NUMERICAL_EPS, desSpeed), myDelta) - (s * s) / (gap * gap));
227#ifdef DEBUG_V
228 if (DEBUG_COND) {
229 std::cout << " i=" << i << " gap=" << gap << " t=" << myHeadwayTime << " t2=" << headwayTime << " s=" << s << " pow=" << pow(newSpeed / desSpeed, myDelta) << " gapDecel=" << (s * s) / (gap * gap) << " a=" << acc;
230 }
231#endif
232 newSpeed = MAX2(0.0, newSpeed + ACCEL2SPEED(acc) / myIterations);
233#ifdef DEBUG_V
234 if (DEBUG_COND) {
235 std::cout << " v2=" << newSpeed << " gLC=" << MSGlobals::gComputeLC << "\n";
236 }
237#endif
238 //TODO use more realistic position update which takes accelerated motion into account
239 gap -= MAX2(0., SPEED2DIST(newSpeed - predSpeed) / myIterations);
240 }
241 return MAX2(0., newSpeed);
242}
243
244
247 return new MSCFModel_IDM(vtype, myIDMM);
248}
#define SPEED2DIST(x)
Definition SUMOTime.h:45
#define ACCEL2SPEED(x)
Definition SUMOTime.h:51
#define TS
Definition SUMOTime.h:42
#define SIMTIME
Definition SUMOTime.h:62
#define DIST2SPEED(x)
Definition SUMOTime.h:47
@ SUMO_ATTR_CF_IDMM_ADAPT_TIME
@ SUMO_ATTR_CF_IDM_DELTA
@ SUMO_ATTR_CF_IDM_STEPPING
@ SUMO_ATTR_CF_IDMM_ADAPT_FACTOR
@ SUMO_ATTR_COLLISION_MINGAP_FACTOR
T MIN2(T a, T b)
Definition StdDefs.h:76
T MAX2(T a, T b)
Definition StdDefs.h:82
double levelOfService
state variable for remembering speed deviation history (lambda)
The Intelligent Driver Model (IDM) car-following model.
const int myIterations
The number of iterations in speed calculations.
double stopSpeed(const MSVehicle *const veh, const double speed, double gap, double decel, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
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...
double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed (no dawdling)
virtual double freeSpeed(const MSVehicle *const veh, double speed, double seen, double maxSpeed, const bool onInsertion=false, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed without a leader.
~MSCFModel_IDM()
Destructor.
const bool myIDMM
whether the model is IDMM or IDM
double getSecureGap(const MSVehicle *const veh, const MSVehicle *const pred, const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
MSCFModel_IDM(const MSVehicleType *vtype, bool idmm)
Constructor.
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....
const double myAdaptationTime
The IDMM adaptation time tau.
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
const double myTwoSqrtAccelDecel
A computational shortcut.
const double myAdaptationFactor
The IDMM adaptation factor beta.
const double myDelta
The IDM delta exponent.
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...
double _v(const MSVehicle *const veh, const double gap2pred, const double mySpeed, const double predSpeed, const double desSpeed, const bool respectMinGap=true) const
double interactionGap(const MSVehicle *const, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
The car-following model abstraction.
Definition MSCFModel.h:55
void applyHeadwayPerceptionError(const MSVehicle *const veh, double speed, double &gap) const
Overwrites gap by the perceived value obtained from the vehicle's driver state.
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
double myEmergencyDecel
The vehicle's maximum emergency deceleration [m/s^2].
Definition MSCFModel.h:703
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,...
CalcReason
What the return value of stop/follow/free-Speed is used for.
Definition MSCFModel.h:77
@ FUTURE
the return value is used for calculating future speeds
Definition MSCFModel.h:81
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition MSCFModel.h:707
double myDecel
The vehicle's maximum deceleration [m/s^2].
Definition MSCFModel.h:701
double maximumSafeStopSpeed(double gap, double decel, double currentSpeed, bool onInsertion=false, double headway=-1, bool relaxEmergency=true) const
Returns the maximum next velocity for stopping within gap.
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition MSCFModel.h:698
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition MSCFModel.h:695
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition MSCFModel.h:710
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...
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
static bool gComputeLC
whether the simulationLoop is in the lane changing phase
Definition MSGlobals.h:140
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition MSLane.h:574
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
double getActionStepLengthSecs() const
Returns the vehicle's action step length in secs, i.e. the interval between two action points.
Definition MSVehicle.h:533
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition MSVehicle.h:581
double getSpeed() const
Returns the vehicle's current speed.
Definition MSVehicle.h:490
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle's car following model variables.
Definition MSVehicle.h:986
The car-following model and parameter.
double getMinGap() const
Get the free space in front of vehicles of this class.
const SUMOVTypeParameter & getParameter() const
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:67
const std::string & getID() const
Returns the id.
Definition Named.h:74
double getCFParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
#define DEBUG_COND