View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.following;
2   
3   import org.djunits.value.vdouble.scalar.Acceleration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
8   import org.opentrafficsim.base.parameters.ParameterTypeDouble;
9   import org.opentrafficsim.base.parameters.ParameterTypeDuration;
10  import org.opentrafficsim.base.parameters.ParameterTypeLength;
11  import org.opentrafficsim.base.parameters.ParameterTypes;
12  import org.opentrafficsim.base.parameters.Parameters;
13  import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
14  import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
15  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
16  import org.opentrafficsim.road.gtu.lane.tactical.util.SpeedLimitUtil;
17  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
18  
19  /**
20   * Implementation of the IDM. See <a
21   * href=https://en.wikipedia.org/wiki/Intelligent_driver_model>https://en.wikipedia.org/wiki/Intelligent_driver_model</a>
22   * <p>
23   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 22, 2016 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public abstract class AbstractIDM extends AbstractCarFollowingModel
32  {
33  
34      /** Acceleration parameter type. */
35      protected static final ParameterTypeAcceleration A = ParameterTypes.A;
36  
37      /** Comfortable deceleration parameter type. */
38      protected static final ParameterTypeAcceleration B = ParameterTypes.B;
39  
40      /** Desired headway parameter type. */
41      protected static final ParameterTypeDuration T = ParameterTypes.T;
42  
43      /** Stopping distance parameter type. */
44      protected static final ParameterTypeLength S0 = ParameterTypes.S0;
45  
46      /** Adjustment deceleration parameter type. */
47      protected static final ParameterTypeAcceleration B0 = ParameterTypes.B0;
48  
49      /** Speed limit adherence factor parameter type. */
50      protected static final ParameterTypeDouble FSPEED = ParameterTypes.FSPEED;
51  
52      /** Acceleration flattening. */
53      public static final ParameterTypeDouble DELTA = new ParameterTypeDouble("delta",
54              "Acceleration flattening exponent towards desired speed.", 4.0, ConstraintInterface.POSITIVE);
55  
56      /** Default IDM desired headway model. */
57      public static final DesiredHeadwayModel HEADWAY = new DesiredHeadwayModel()
58      {
59          @Override
60          public Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
61          {
62              return Length.createSI(parameters.getParameter(S0).si + speed.si * parameters.getParameter(T).si);
63          }
64      };
65  
66      /** Default IDM desired speed model. */
67      public static final DesiredSpeedModel DESIRED_SPEED = new DesiredSpeedModel()
68      {
69          @Override
70          public Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
71          {
72              Speed consideredSpeed = SpeedLimitUtil.getLegalSpeedLimit(speedInfo).multiplyBy(parameters.getParameter(FSPEED));
73              Speed maxVehicleSpeed = SpeedLimitUtil.getMaximumVehicleSpeed(speedInfo);
74              return consideredSpeed.le(maxVehicleSpeed) ? consideredSpeed : maxVehicleSpeed;
75          }
76      };
77  
78      /**
79       * Constructor with modular models for desired headway and desired speed.
80       * @param desiredHeadwayModel DesiredHeadwayModel; desired headway model
81       * @param desiredSpeedModel DesiredSpeedModel; desired speed model
82       */
83      public AbstractIDM(final DesiredHeadwayModel desiredHeadwayModel, final DesiredSpeedModel desiredSpeedModel)
84      {
85          super(desiredHeadwayModel, desiredSpeedModel);
86      }
87  
88      /**
89       * Determination of car-following acceleration, possibly based on multiple leaders. This implementation calculates the IDM
90       * free term, which is returned if there are no leaders. If there are leaders <tt>combineInteractionTerm()</tt> is invoked
91       * to combine the free term with some implementation specific interaction term. The IDM free term is limited by a
92       * deceleration of <tt>B0</tt> for cases where the current speed is above the desired speed. This method can be overridden
93       * if the free term needs to be redefined.
94       * @param parameters Parameters; Parameters.
95       * @param speed Speed; Current speed.
96       * @param desiredSpeed Speed; Desired speed.
97       * @param desiredHeadway Length; Desired headway.
98       * @param leaders PerceptionIterable&lt;? extends Headway&gt;; Set of leader headways (guaranteed positive) and speeds,
99       *            ordered by headway (closest first).
100      * @throws ParameterException If parameter exception occurs.
101      * @return Car-following acceleration.
102      */
103     @Override
104     @SuppressWarnings("checkstyle:designforextension")
105     protected Acceleration followingAcceleration(final Parameters parameters, final Speed speed, final Speed desiredSpeed,
106             final Length desiredHeadway, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
107     {
108         Acceleration a = parameters.getParameter(A);
109         Acceleration b0 = parameters.getParameter(B0);
110         double delta = parameters.getParameter(DELTA);
111         double aFree = a.si * (1 - Math.pow(speed.si / desiredSpeed.si, delta));
112         // limit deceleration in free term (occurs if speed > desired speed)
113         aFree = aFree > -b0.si ? aFree : -b0.si;
114         // return free term if there are no leaders
115         if (leaders.isEmpty())
116         {
117             return Acceleration.createSI(aFree);
118         }
119         // return combined acceleration
120         return combineInteractionTerm(Acceleration.createSI(aFree), parameters, speed, desiredSpeed, desiredHeadway, leaders);
121     }
122 
123     /**
124      * Combines an interaction term with the free term. There should be at least 1 leader for this method.
125      * @param aFree Acceleration; Free term of acceleration.
126      * @param parameters Parameters; Parameters.
127      * @param speed Speed; Current speed.
128      * @param desiredSpeed Speed; Desired speed.
129      * @param desiredHeadway Length; Desired headway.
130      * @param leaders PerceptionIterable&lt;? extends Headway&gt;; Set of leader headways (guaranteed positive) and speeds,
131      *            ordered by headway (closest first).
132      * @return Combination of terms into a single acceleration.
133      * @throws ParameterException In case of parameter exception.
134      */
135     protected abstract Acceleration combineInteractionTerm(Acceleration aFree, Parameters parameters, Speed speed,
136             Speed desiredSpeed, Length desiredHeadway, PerceptionIterable<? extends Headway> leaders) throws ParameterException;
137 
138     /**
139      * Determines the dynamic desired headway, which is non-negative.
140      * @param parameters Parameters; Parameters.
141      * @param speed Speed; Current speed.
142      * @param desiredHeadway Length; Desired headway.
143      * @param leaderSpeed Speed; Speed of the leading vehicle.
144      * @return Dynamic desired headway.
145      * @throws ParameterException In case of parameter exception.
146      */
147     protected final Length dynamicDesiredHeadway(final Parameters parameters, final Speed speed, final Length desiredHeadway,
148             final Speed leaderSpeed) throws ParameterException
149     {
150         double sStar = desiredHeadway.si + dynamicHeadwayTerm(parameters, speed, leaderSpeed).si;
151         /*
152          * Due to a power of 2 in the IDM, negative values of sStar are not allowed. A negative sStar means that the leader is
153          * faster to such an extent, that the equilibrium headway (s0+vT) is completely compensated by the dynamic part in
154          * sStar. This might occur if a much faster leader changes lane closely in front. The compensation is limited to the
155          * equilibrium headway minus the stopping distance (i.e. sStar > s0), which means the driver wants to follow with
156          * acceleration. Note that usually the free term determines acceleration in such cases.
157          */
158         Length s0 = parameters.getParameter(S0);
159         /*
160          * Limit used to be 0, but the IDM is very sensitive there. With a decelerating leader, an ok acceleration in one time
161          * step, may results in acceleration < -10 in the next.
162          */
163         return Length.createSI(sStar >= s0.si ? sStar : s0.si);
164     }
165 
166     /**
167      * Determines the dynamic headway term. May be used on individual leaders for multi-anticipative following.
168      * @param parameters Parameters; Parameters.
169      * @param speed Speed; Current speed.
170      * @param leaderSpeed Speed; Speed of the leading vehicle.
171      * @return Dynamic headway term.
172      * @throws ParameterException In case of parameter exception.
173      */
174     protected final Length dynamicHeadwayTerm(final Parameters parameters, final Speed speed, final Speed leaderSpeed)
175             throws ParameterException
176     {
177         Acceleration a = parameters.getParameter(A);
178         Acceleration b = parameters.getParameter(B);
179         return Length.createSI(speed.si * (speed.si - leaderSpeed.si) / (2 * Math.sqrt(a.si * b.si)));
180     }
181 
182 }