SUMO - Simulation of Urban MObility
MSCFModel_IDM.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The Intelligent Driver Model (IDM) car-following model
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include "MSCFModel_IDM.h"
27 #include <microsim/MSVehicle.h>
28 
29 //#define DEBUG_V
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
34 MSCFModel_IDM::MSCFModel_IDM(const MSVehicleType* vtype, bool idmm) :
35  MSCFModel(vtype),
36  myIDMM(idmm),
37  myDelta(idmm ? 4.0 : vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_DELTA, 4.)),
38  myAdaptationFactor(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_FACTOR, 1.8) : 1.0),
39  myAdaptationTime(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_TIME, 600.0) : 0.0),
40  myIterations(MAX2(1, int(TS / vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_STEPPING, .25) + .5))),
41  myTwoSqrtAccelDecel(double(2 * sqrt(myAccel * myDecel))) {
42  // IDM does not drive very precise and may violate minGap on occasion
44 }
45 
47 
48 
49 double
50 MSCFModel_IDM::finalizeSpeed(MSVehicle* const veh, double vPos) const {
51  const double vNext = MSCFModel::finalizeSpeed(veh, vPos);
52  if (myAdaptationFactor != 1.) {
54  vars->levelOfService += (vNext / veh->getLane()->getVehicleMaxSpeed(veh) - vars->levelOfService) / myAdaptationTime * TS;
55  }
56  return vNext;
57 }
58 
59 
60 double
61 MSCFModel_IDM::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const /*pred*/) const {
62 #ifdef DEBUG_V
63  gDebugFlag1 = veh->isSelected();
64 #endif
65  return _v(veh, gap2pred, speed, predSpeed, veh->getLane()->getVehicleMaxSpeed(veh));
66 }
67 
68 
69 double
70 MSCFModel_IDM::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
71  if (gap < 0.01) {
72  return 0;
73  }
74  double result = _v(veh, gap, speed, 0, veh->getLane()->getVehicleMaxSpeed(veh), false);
75  if (gap > 0 && speed < NUMERICAL_EPS) {
76  // ensure that stops can be reached:
77  result = maximumSafeStopSpeed(gap, speed, false, veh->getActionStepLengthSecs());
78  }
79  return result;
80 }
81 
82 
84 double
85 MSCFModel_IDM::interactionGap(const MSVehicle* const veh, double vL) const {
86  // Resolve the IDM equation to gap. Assume predecessor has
87  // speed != 0 and that vsafe will be the current speed plus acceleration,
88  // i.e that with this gap there will be no interaction.
89  const double acc = myAccel * (1. - pow(veh->getSpeed() / veh->getLane()->getVehicleMaxSpeed(veh), myDelta));
90  const double vNext = veh->getSpeed() + acc;
91  const double gap = (vNext - vL) * (veh->getSpeed() + vL) / (2 * myDecel) + vL;
92 
93  // Don't allow timeHeadWay < deltaT situations.
94  return MAX2(gap, SPEED2DIST(vNext));
95 }
96 
97 double
98 MSCFModel_IDM::getSecureGap(const double speed, const double leaderSpeed, const double /*leaderMaxDecel*/) const {
99  const double delta_v = speed - leaderSpeed;
100  return MAX2(0.0, speed * myHeadwayTime + speed * delta_v / myTwoSqrtAccelDecel);
101 }
102 
103 
104 double
105 MSCFModel_IDM::_v(const MSVehicle* const veh, const double gap2pred, const double egoSpeed,
106  const double predSpeed, const double desSpeed, const bool respectMinGap) const {
107 // this is more or less based on http://www.vwi.tu-dresden.de/~treiber/MicroApplet/IDM.html
108 // and http://arxiv.org/abs/cond-mat/0304337
109 // we assume however constant speed for the leader
110  double headwayTime = myHeadwayTime;
111  if (myAdaptationFactor != 1.) {
113  headwayTime *= myAdaptationFactor + vars->levelOfService * (1. - myAdaptationFactor);
114  }
115  double newSpeed = egoSpeed;
116  double gap = gap2pred;
117  if (respectMinGap) {
118  // gap2pred comes with minGap already subtracted so we need to add it here again
119  gap += myType->getMinGap();
120  }
121  for (int i = 0; i < myIterations; i++) {
122  const double delta_v = newSpeed - predSpeed;
123  double s = MAX2(0., newSpeed * headwayTime + newSpeed * delta_v / myTwoSqrtAccelDecel);
124  if (respectMinGap) {
125  s += myType->getMinGap();
126  }
127  gap = MAX2(NUMERICAL_EPS, gap); // avoid singularity
128  const double acc = myAccel * (1. - pow(newSpeed / desSpeed, myDelta) - (s * s) / (gap * gap));
129 #ifdef DEBUG_V
130  if (gDebugFlag1) {
131  std::cout << " gap=" << gap << " t=" << myHeadwayTime << " t2=" << headwayTime << " s=" << s << " pow=" << pow(newSpeed / desSpeed, myDelta) << " gapDecel=" << (s * s) / (gap * gap) << " a=" << acc;
132  }
133 #endif
134  newSpeed += ACCEL2SPEED(acc) / myIterations;
135 #ifdef DEBUG_V
136  if (gDebugFlag1) {
137  std::cout << " v2=" << newSpeed << "\n";
138  }
139 #endif
140  //TODO use more realistic position update which takes accelerated motion into account
141  gap -= MAX2(0., SPEED2DIST(newSpeed - predSpeed) / myIterations);
142  }
143  return MAX2(0., newSpeed);
144 }
145 
146 
147 MSCFModel*
149  return new MSCFModel_IDM(vtype, myIDMM);
150 }
bool gDebugFlag1
global utility flags for debugging
Definition: StdDefs.cpp:32
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane&#39;s maximum speed, given a vehicle&#39;s speed limit adaptation.
Definition: MSLane.h:492
~MSCFModel_IDM()
Destructor.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
double interactionGap(const MSVehicle *const, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle&#39;s car following model variables.
Definition: MSVehicle.h:909
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition: MSCFModel.h:587
#define SPEED2DIST(x)
Definition: SUMOTime.h:48
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:54
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:565
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
const bool myIDMM
whether the model is IDMM or IDM
const double myDelta
The IDM delta exponent.
The car-following model abstraction.
Definition: MSCFModel.h:57
const double myAdaptationFactor
The IDMM adaptation factor beta.
double myAccel
The vehicle&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:590
T MAX2(T a, T b)
Definition: StdDefs.h:76
double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
MSCFModel_IDM(const MSVehicleType *vtype, bool idmm)
Constructor.
#define TS
Definition: SUMOTime.h:45
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:165
The car-following model and parameter.
Definition: MSVehicleType.h:66
virtual bool isSelected() const
whether this vehicle is selected in the GUI
double stopSpeed(const MSVehicle *const veh, const double speed, double gap) const
Computes the vehicle&#39;s safe speed for approaching a non-moving obstacle (no dawdling) ...
double getSecureGap(const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
double getActionStepLengthSecs() const
Returns the vehicle&#39;s action step length in secs, i.e. the interval between two action points...
Definition: MSVehicle.h:517
const double myAdaptationTime
The IDMM adaptation time tau.
double levelOfService
state variable for remembering speed deviation history (lambda)
double _v(const MSVehicle *const veh, const double gap2pred, const double mySpeed, const double predSpeed, const double desSpeed, const bool respectMinGap=true) const
double getMinGap() const
Get the free space in front of vehicles of this class.
const double myTwoSqrtAccelDecel
A computational shortcut.
double maximumSafeStopSpeed(double gap, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap.
Definition: MSCFModel.cpp:709
const SUMOVTypeParameter & getParameter() const
double myDecel
The vehicle&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:593
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.
const int myIterations
The number of iterations in speed calculations.
double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const
Computes the vehicle&#39;s safe speed (no dawdling)
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition: MSCFModel.h:599
#define NUMERICAL_EPS
Definition: config.h:148
double myHeadwayTime
The driver&#39;s desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:602
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:483