View Javadoc
1   package org.opentrafficsim.road.gtu.following;
2   
3   import org.djunits.unit.AccelerationUnit;
4   
5   /**
6    * The Intelligent Driver Model by Treiber, Hennecke and Helbing.
7    * <p>
8    * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10   * <p>
11   * @version $Revision: 1408 $, $LastChangedDate: 2015-09-24 15:17:25 +0200 (Thu, 24 Sep 2015) $, by $Author: pknoppers $,
12   *          initial version 19 nov. 2014 <br>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   */
15  public class IDM extends AbstractGTUFollowingModel
16  {
17      /** Preferred net longitudinal distance when stopped [m]. */
18      private final Length.Rel s0;
19  
20      /** Maximum longitudinal acceleration [m/s^2]. */
21      private final Acceleration.Abs a;
22  
23      /** Longitudinal deceleration [m/s^2]. (Should be a positive value even though it is a <b>de</b>celeration.) */
24      private final Acceleration.Abs b;
25  
26      /** Safe time headway. */
27      private final Time.Rel tSafe;
28  
29      /**
30       * Time slot size used by IDM (not defined in the paper, but 0.5s is a reasonable trade-off between computational speed and
31       * accuracy).
32       */
33      private final Time.Rel stepSize = new Time.Rel(0.5, SECOND);
34  
35      /**
36       * Mean speed limit adherence (1.0: mean free speed equals the speed limit; 1.1: mean speed limit equals 110% of the speed
37       * limit, etc.).
38       */
39      private final double delta;
40  
41      /**
42       * Construct a new IDM car following model with reasonable values (reasonable for passenger cars).
43       */
44      public IDM()
45      {
46          this.a = new Acceleration.Abs(1.56, METER_PER_SECOND_2);
47          this.b = new Acceleration.Abs(2.09, METER_PER_SECOND_2);
48          this.s0 = new Length.Rel(3, METER);
49          this.tSafe = new Time.Rel(1.2, SECOND);
50          this.delta = 1d;
51      }
52  
53      /**
54       * Construct a new IDM car following model.
55       * @param a DoubleScalar.Abs&lt;AccelerationUnit&gt;; the maximum acceleration of a stationary vehicle (normal value is 1
56       *            m/s/s)
57       * @param b DoubleScalar.Abs&lt;AccelerationUnit&gt;; the maximum deemed-safe deceleration (this is a positive value).
58       *            Normal value is 1.5 m/s/s.
59       * @param s0 DoubleScalar.Rel&lt;LengthUnit&gt;; the minimum stationary headway (normal value is 2 m)
60       * @param tSafe DoubleScalar.Rel&lt;TimeUnit&gt;; the minimum time-headway (normal value is 1s)
61       * @param delta double; the speed limit adherence (1.0; mean free speed equals the speed limit; 1.1: mean free speed equals
62       *            110% of the speed limit; etc.)
63       */
64      public IDM(final Acceleration.Abs a, final Acceleration.Abs b, final Length.Rel s0, final Time.Rel tSafe, final double delta)
65      {
66          this.a = a;
67          this.b = b;
68          this.s0 = s0;
69          this.tSafe = tSafe;
70          this.delta = delta;
71      }
72  
73      /**
74       * Desired speed (taking into account the urge to drive a little faster or slower than the posted speed limit).
75       * @param speedLimit DoubleScalarAbs&lt;SpeedUnit&gt;; the speed limit
76       * @param followerMaximumSpeed DoubleScalar.Abs&lt;SpeedUnit&gt;; the maximum speed that the follower can drive
77       * @return DoubleScalarRel&lt;SpeedUnit&gt;; the desired speed
78       */
79      private Speed.Rel vDes(final Speed.Abs speedLimit, final Speed.Abs followerMaximumSpeed)
80      {
81          return new Speed.Rel(Math.min(this.delta * speedLimit.getSI(), followerMaximumSpeed.getSI()), METER_PER_SECOND);
82      }
83  
84      /** {@inheritDoc} */
85      public final Acceleration.Abs computeAcceleration(final Speed.Abs followerSpeed, final Speed.Abs followerMaximumSpeed,
86              final Speed.Abs leaderSpeed, final Length.Rel headway, final Speed.Abs speedLimit)
87      {
88          // System.out.println("Applying IDM for " + follower + " headway is " + headway);
89          // dV is the approach speed
90          Speed.Rel dV = followerSpeed.minus(leaderSpeed);
91          Acceleration.Abs aFree =
92                  new Acceleration.Abs(this.a.getSI()
93                          * (1 - Math.pow(followerSpeed.getSI() / vDes(speedLimit, followerMaximumSpeed).getSI(), 4)),
94                          METER_PER_SECOND_2);
95          if (Double.isNaN(aFree.getSI()))
96          {
97              aFree = new Acceleration.Abs(0, AccelerationUnit.SI);
98          }
99          Acceleration.Rel logWeightedAccelerationTimes2 =
100                 new Acceleration.Rel(Math.sqrt(this.a.getSI() * this.b.getSI()), METER_PER_SECOND_2).multiplyBy(2); 
101         // don't forget the times 2
102         
103         // TODO compute logWeightedAccelerationTimes2 only once per run
104         Length.Rel right =
105                 followerSpeed.toRel().multiplyBy(this.tSafe)
106                         .plus(dV.multiplyBy(followerSpeed.toRel().divideBy(logWeightedAccelerationTimes2)));
107         /*-
108             Calc.speedTimesTime(followerSpeed, this.tSafe).plus(
109                 Calc.speedTimesTime(dV, Calc.speedDividedByAcceleration(followerSpeed, logWeightedAccelerationTimes2)));
110          */
111         if (right.getSI() < 0)
112         {
113             // System.out.println("Fixing negative right");
114             right = new Length.Rel(0, METER);
115         }
116         Length.Rel sStar = this.s0.plus(right);
117         if (sStar.getSI() < 0) // Negative value should be treated as 0
118         {
119             System.out.println("sStar is negative");
120             sStar = new Length.Rel(0, METER);
121         }
122         // System.out.println("s* is " + sStar);
123         Acceleration.Rel aInteraction =
124                 new Acceleration.Rel(-Math.pow(this.a.getSI() * sStar.getSI() / headway.getSI(), 2), METER_PER_SECOND_2);
125         Acceleration.Abs newAcceleration = aFree.plus(aInteraction);
126         if (newAcceleration.getSI() * this.stepSize.getSI() + followerSpeed.getSI() < 0)
127         {
128             // System.out.println("Limiting deceleration to prevent moving backwards");
129             newAcceleration = new Acceleration.Abs(-followerSpeed.getSI() / this.stepSize.getSI(), METER_PER_SECOND_2);
130         }
131         // System.out.println("newAcceleration is " + newAcceleration);
132         return newAcceleration;
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public final Time.Rel getStepSize()
138     {
139         return new Time.Rel(this.stepSize);
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public final Acceleration.Abs maximumSafeDeceleration()
145     {
146         return this.b;
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     public final String getName()
152     {
153         return "IDM";
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     public final String getLongName()
159     {
160         return String.format("%s (a=%.1fm/s\u00b2, b=%.1fm/s\u00b2, s0=%.1fm, tSafe=%.1fs, delta=%.2f)", getName(),
161                 this.a.getSI(), this.b.getSI(), this.s0.getSI(), this.tSafe.getSI(), this.delta);
162     }
163 
164 }