View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.following;
2   
3   import static org.opentrafficsim.core.gtu.behavioralcharacteristics.AbstractParameterType.Check.POSITIVE;
4   
5   import java.util.SortedMap;
6   
7   import org.djunits.unit.AccelerationUnit;
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.value.vdouble.scalar.Acceleration;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.djunits.value.vdouble.scalar.Speed;
12  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
13  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
14  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterTypeDouble;
15  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterTypes;
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-2016 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      /** Speed limit adherence factor. */
35      public static final ParameterTypeDouble DELTA = new ParameterTypeDouble("delta",
36              "Acceleration flattening exponent towards desired speed.", 4.0, POSITIVE);
37  
38      /** {@inheritDoc} */
39      @Override
40      public final Speed desiredSpeed(final BehavioralCharacteristics behavioralCharacteristics, final SpeedLimitInfo speedInfo)
41              throws ParameterException
42      {
43          Speed consideredSpeed =
44              SpeedLimitUtil.getLegalSpeedLimit(speedInfo).multiplyBy(behavioralCharacteristics.getParameter(ParameterTypes.FSPEED));
45          Speed maxVehicleSpeed = SpeedLimitUtil.getMaximumVehicleSpeed(speedInfo);
46          return consideredSpeed.le(maxVehicleSpeed) ? consideredSpeed : maxVehicleSpeed;
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public final Length desiredHeadway(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed)
52              throws ParameterException
53      {
54          return behavioralCharacteristics.getParameter(ParameterTypes.S0).plus(
55                  speed.multiplyBy(behavioralCharacteristics.getParameter(ParameterTypes.T)));
56      }
57  
58      /**
59       * Determination of car-following acceleration, possibly based on multiple leaders. This implementation calculates the IDM
60       * free term, which is returned if there are no leaders. If there are leaders <tt>combineInteractionTerm()</tt> is invoked
61       * to combine the free term with some implementation specific interaction term. The IDM free term is limited by a
62       * deceleration of <tt>B0</tt> for cases where the current speed is above the desired speed. This method can be overridden
63       * if the free term needs to be redefined.
64       * @param behavioralCharacteristics Behavioral characteristics.
65       * @param speed Current speed.
66       * @param desiredSpeed Desired speed.
67       * @param desiredHeadway Desired headway.
68       * @param leaders Set of leader headways (guaranteed positive) and speeds, ordered by headway (closest first).
69       * @throws ParameterException If parameter exception occurs.
70       * @return Car-following acceleration.
71       */
72      @Override
73      @SuppressWarnings("checkstyle:designforextension")
74      protected Acceleration followingAcceleration(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed,
75              final Speed desiredSpeed, final Length desiredHeadway, final SortedMap<Length, Speed> leaders)
76              throws ParameterException
77      {
78          Acceleration a = behavioralCharacteristics.getParameter(ParameterTypes.A);
79          Acceleration b0 = behavioralCharacteristics.getParameter(ParameterTypes.B0);
80          double delta = behavioralCharacteristics.getParameter(DELTA);
81          double aFree = a.si * (1 - Math.pow(speed.si / desiredSpeed.si, delta));
82          // limit deceleration in free term (occurs if speed > desired speed)
83          aFree = aFree > -b0.si ? aFree : -b0.si;
84          // return free term if there are no leaders
85          if (leaders.isEmpty())
86          {
87              return new Acceleration(aFree, AccelerationUnit.SI);
88          }
89          // return combined acceleration
90          return combineInteractionTerm(new Acceleration(aFree, AccelerationUnit.SI), behavioralCharacteristics, speed,
91                  desiredSpeed, desiredHeadway, leaders);
92      }
93  
94      /**
95       * Combines an interaction term with the free term. There should be at least 1 leader for this method.
96       * @param aFree Free term of acceleration.
97       * @param behavioralCharacteristics Behavioral characteristics.
98       * @param speed Current speed.
99       * @param desiredSpeed Desired speed.
100      * @param desiredHeadway Desired headway.
101      * @param leaders Set of leader headways (guaranteed positive) and speeds, ordered by headway (closest first).
102      * @return Combination of terms into a single acceleration.
103      * @throws ParameterException In case of parameter exception.
104      */
105     protected abstract Acceleration combineInteractionTerm(Acceleration aFree,
106             BehavioralCharacteristics behavioralCharacteristics, Speed speed, Speed desiredSpeed, Length desiredHeadway,
107             SortedMap<Length, Speed> leaders) throws ParameterException;
108 
109     /**
110      * Determines the dynamic desired headway, which is non-negative.
111      * @param behavioralCharacteristics Behavioral characteristics.
112      * @param speed Current speed.
113      * @param desiredHeadway Desired headway.
114      * @param leaderSpeed Speed of the leading vehicle.
115      * @return Dynamic desired headway.
116      * @throws ParameterException In case of parameter exception.
117      */
118     protected final Length dynamicDesiredHeadway(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed,
119             final Length desiredHeadway, final Speed leaderSpeed) throws ParameterException
120     {
121         double sStar = desiredHeadway.si + dynamicHeadwayTerm(behavioralCharacteristics, speed, leaderSpeed).si;
122         /*
123          * Due to a power of 2 in the IDM, negative values of sStar are not allowed. A negative sStar means that the leader is
124          * faster to such an extent, that the equilibrium headway (s0+vT) is completely compensated by the dynamic part in
125          * sStar. This might occur if a much faster leader changes lane closely in front. The compensation is limited to the
126          * equilibrium headway (i.e. sStar = 0), which means the driver wants to follow with acceleration. Note that usually the
127          * free term determines acceleration in such cases.
128          */
129         return new Length(sStar >= 0 ? sStar : 0, LengthUnit.SI);
130     }
131 
132     /**
133      * Determines the dynamic headway term. May be used on individual leaders for multi-anticipative following.
134      * @param behavioralCharacteristics Behavioral characteristics.
135      * @param speed Current speed.
136      * @param leaderSpeed Speed of the leading vehicle.
137      * @return Dynamic headway term.
138      * @throws ParameterException In case of parameter exception.
139      */
140     protected final Length dynamicHeadwayTerm(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed,
141             final Speed leaderSpeed) throws ParameterException
142     {
143         Acceleration a = behavioralCharacteristics.getParameter(ParameterTypes.A);
144         Acceleration b = behavioralCharacteristics.getParameter(ParameterTypes.B);
145         return new Length(speed.si * (speed.si - leaderSpeed.si) / (2 * Math.sqrt(a.si * b.si)), LengthUnit.SI);
146     }
147 
148 }