View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.following;
2   
3   import java.io.Serializable;
4   import java.util.SortedMap;
5   
6   import org.djunits.unit.AccelerationUnit;
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.unit.SpeedUnit;
10  import org.djunits.value.vdouble.scalar.Acceleration;
11  import org.djunits.value.vdouble.scalar.Duration;
12  import org.djunits.value.vdouble.scalar.Length;
13  import org.djunits.value.vdouble.scalar.Speed;
14  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
15  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
16  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
17  import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
18  
19  /**
20   * The Intelligent Driver Model by Treiber, Hennecke and Helbing.
21   * <p>
22   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision: 1408 $, $LastChangedDate: 2015-09-24 15:17:25 +0200 (Thu, 24 Sep 2015) $, by $Author: pknoppers $,
26   *          initial version 19 nov. 2014 <br>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   */
29  public class IDMOld extends AbstractGTUFollowingModelMobil implements Serializable
30  {
31      /** */
32      private static final long serialVersionUID = 20141119L;
33  
34      /** Preferred net longitudinal distance when stopped [m]. */
35      private final Length s0;
36  
37      /** Maximum longitudinal acceleration [m/s^2]. */
38      private Acceleration a;
39  
40      /** Longitudinal deceleration [m/s^2]. (Should be a positive value even though it is a <b>de</b>celeration.) */
41      private final Acceleration b;
42  
43      /** Safe time headway. */
44      private Duration tSafe;
45  
46      /**
47       * Default step size used by IDM (not defined in the paper, but 0.5s is a reasonable trade-off between computational speed
48       * and accuracy).
49       */
50      private static final Duration DEFAULT_STEP_SIZE = new Duration(0.5, DurationUnit.SECOND);
51  
52      /**
53       * Mean speed limit adherence (1.0: mean free speed equals the speed limit; 1.1: mean speed limit equals 110% of the speed
54       * limit, etc.).
55       */
56      private double delta;
57  
58      /**
59       * Construct a new IDM car following model with reasonable values (reasonable for passenger cars).
60       */
61      public IDMOld()
62      {
63          this.a = new Acceleration(1.56, AccelerationUnit.METER_PER_SECOND_2);
64          this.b = new Acceleration(2.09, AccelerationUnit.METER_PER_SECOND_2);
65          this.s0 = new Length(3, LengthUnit.METER);
66          this.tSafe = new Duration(1.2, DurationUnit.SECOND);
67          this.delta = 1.0;
68      }
69  
70      /**
71       * Construct a new IDM car following model.
72       * @param a Acceleration; the maximum acceleration of a stationary vehicle (normal value is 1.56 m/s/s)
73       * @param b Acceleration; the maximum deemed-safe deceleration (this is a positive value). Normal value is 2.09 m/s/s.
74       * @param s0 Length; the minimum stationary headway (normal value is 3 m)
75       * @param tSafe Duration; the minimum time-headway (normal value is 1.2 s)
76       * @param delta double; the speed limit adherence (1.0; mean free speed equals the speed limit; 1.1: mean free speed equals
77       *            110% of the speed limit; etc.)
78       */
79      public IDMOld(final Acceleration a, final Acceleration b, final Length s0, final Duration tSafe, final double delta)
80      {
81          this.a = a;
82          this.b = b;
83          this.s0 = s0;
84          this.tSafe = tSafe;
85          this.delta = delta;
86      }
87  
88      /**
89       * Desired speed (taking into account the urge to drive a little faster or slower than the posted speed limit).
90       * @param speedLimit DoubleScalarAbs&lt;SpeedUnit&gt;; the speed limit
91       * @param followerMaximumSpeed Speed; the maximum speed that the follower can drive
92       * @return DoubleScalarRel&lt;SpeedUnit&gt;; the desired speed
93       */
94      private Speed vDes(final Speed speedLimit, final Speed followerMaximumSpeed)
95      {
96          return new Speed(Math.min(this.delta * speedLimit.getSI(), followerMaximumSpeed.getSI()), SpeedUnit.SI);
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
102             final Speed leaderSpeed, final Length headway, final Speed speedLimit)
103     {
104         return computeAcceleration(followerSpeed, followerMaximumSpeed, leaderSpeed, headway, speedLimit, DEFAULT_STEP_SIZE);
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
110             final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
111     {
112         // TODO maxDistance
113         // dV is the approach speed
114         Speed dV = followerSpeed.minus(leaderSpeed);
115         double sStar = this.s0.si + followerSpeed.si * this.tSafe.si
116                 + dV.si * followerSpeed.si / (2.0 * Math.sqrt(this.a.si * this.b.si));
117         if (sStar < 0.0 && headway.si < 0.0)
118         {
119             return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
120         }
121         sStar = sStar >= 0.0 ? sStar : 0.0;
122         double s = headway.si > 0.0 ? headway.si : 1E-99;
123         Acceleration aInteraction = new Acceleration(this.a.si * (sStar / s) * (sStar / s), AccelerationUnit.SI);
124         Acceleration aFree =
125                 new Acceleration(this.a.si * (1.0 - Math.pow(followerSpeed.si / vDes(speedLimit, followerMaximumSpeed).si, 4)),
126                         AccelerationUnit.SI);
127         // limit deceleration for free term (= aFree)
128         if (aFree.si < -0.5)
129         {
130             aFree = new Acceleration(-0.5, AccelerationUnit.SI);
131         }
132         Acceleration newAcceleration = aFree.minus(aInteraction);
133         if (newAcceleration.si * stepSize.si + followerSpeed.si < 0)
134         {
135             newAcceleration = new Acceleration(-followerSpeed.si / stepSize.si, AccelerationUnit.SI);
136         }
137         return newAcceleration;
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     public final Duration getStepSize()
143     {
144         return DEFAULT_STEP_SIZE;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public final Acceleration getMaximumSafeDeceleration()
150     {
151         return this.b;
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     public final String getName()
157     {
158         return "IDM";
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final String getLongName()
164     {
165         return String.format("%s (a=%.1fm/s\u00b2, b=%.1fm/s\u00b2, s0=%.1fm, tSafe=%.1fs, delta=%.2f)", getName(),
166                 this.a.getSI(), this.b.getSI(), this.s0.getSI(), this.tSafe.getSI(), this.delta);
167     }
168 
169     /** {@inheritDoc} */
170     @Override
171     public final void setA(final Acceleration a)
172     {
173         this.a = a;
174     }
175 
176     /** {@inheritDoc} */
177     @Override
178     public final void setT(final Duration t)
179     {
180         this.tSafe = t;
181     }
182 
183     /** {@inheritDoc} */
184     @Override
185     public final void setFspeed(final double fSpeed)
186     {
187         this.delta = fSpeed;
188     }
189 
190     // The following is inherited from CarFollowingModel
191 
192     /** {@inheritDoc} */
193     @Override
194     public final Speed desiredSpeed(final BehavioralCharacteristics behavioralCharacteristics, final SpeedLimitInfo speedInfo)
195             throws ParameterException
196     {
197         throw new UnsupportedOperationException("Old car-following model does not support desired speed.");
198     }
199 
200     /** {@inheritDoc} */
201     @Override
202     public final Length desiredHeadway(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed)
203             throws ParameterException
204     {
205         throw new UnsupportedOperationException("Old car-following model does not support desired headway.");
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     public final Acceleration followingAcceleration(final BehavioralCharacteristics behavioralCharacteristics,
211             final Speed speed, final SpeedLimitInfo speedInfo, final SortedMap<Length, Speed> leaders) throws ParameterException
212     {
213         Length headway;
214         Speed leaderSpeed;
215         if (leaders.isEmpty())
216         {
217             headway = new Length(Double.MAX_VALUE, LengthUnit.SI);
218             leaderSpeed = speed;
219         }
220         else
221         {
222             headway = leaders.firstKey();
223             leaderSpeed = leaders.get(headway);
224         }
225         return this.computeAcceleration(speed, speedInfo.getSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED), leaderSpeed, headway,
226                 speedInfo.getSpeedInfo(SpeedLimitTypes.FIXED_SIGN));
227     }
228 
229     /** {@inheritDoc} */
230     @Override
231     public final String toString()
232     {
233         return "IDMOld [s0=" + this.s0 + ", a=" + this.a + ", b=" + this.b + ", tSafe=" + this.tSafe + ", stepSize="
234                 + DEFAULT_STEP_SIZE + ", delta=" + this.delta + "]";
235     }
236 
237 }