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