IDMOld.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.following;

  2. import java.io.Serializable;

  3. import org.djunits.unit.AccelerationUnit;
  4. import org.djunits.unit.DurationUnit;
  5. import org.djunits.unit.LengthUnit;
  6. import org.djunits.unit.SpeedUnit;
  7. import org.djunits.value.vdouble.scalar.Acceleration;
  8. import org.djunits.value.vdouble.scalar.Duration;
  9. import org.djunits.value.vdouble.scalar.Length;
  10. import org.djunits.value.vdouble.scalar.Speed;
  11. import org.opentrafficsim.base.parameters.ParameterException;
  12. import org.opentrafficsim.base.parameters.Parameters;
  13. import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
  14. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
  15. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
  16. import org.opentrafficsim.road.network.speed.SpeedLimitTypes;

  17. /**
  18.  * The Intelligent Driver Model by Treiber, Hennecke and Helbing.
  19.  * <p>
  20.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  21.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * <p>
  23.  * @version $Revision: 1408 $, $LastChangedDate: 2015-09-24 15:17:25 +0200 (Thu, 24 Sep 2015) $, by $Author: pknoppers $,
  24.  *          initial version 19 nov. 2014 <br>
  25.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  26.  */
  27. public class IDMOld extends AbstractGTUFollowingModelMobil implements Serializable
  28. {
  29.     /** */
  30.     private static final long serialVersionUID = 20141119L;

  31.     /** Preferred net longitudinal distance when stopped [m]. */
  32.     private final Length s0;

  33.     /** Maximum longitudinal acceleration [m/s^2]. */
  34.     private Acceleration a;

  35.     /** Longitudinal deceleration [m/s^2]. (Should be a positive value even though it is a <b>de</b>celeration.) */
  36.     private final Acceleration b;

  37.     /** Safe time headway. */
  38.     private Duration tSafe;

  39.     /**
  40.      * Default step size used by IDM (not defined in the paper, but 0.5s is a reasonable trade-off between computational speed
  41.      * and accuracy).
  42.      */
  43.     private static final Duration DEFAULT_STEP_SIZE = new Duration(0.5, DurationUnit.SECOND);

  44.     /**
  45.      * Mean speed limit adherence (1.0: mean free speed equals the speed limit; 1.1: mean speed limit equals 110% of the speed
  46.      * limit, etc.).
  47.      */
  48.     private double delta;

  49.     /**
  50.      * Construct a new IDM car following model with reasonable values (reasonable for passenger cars).
  51.      */
  52.     public IDMOld()
  53.     {
  54.         this.a = new Acceleration(1.56, AccelerationUnit.METER_PER_SECOND_2);
  55.         this.b = new Acceleration(2.09, AccelerationUnit.METER_PER_SECOND_2);
  56.         this.s0 = new Length(3, LengthUnit.METER);
  57.         this.tSafe = new Duration(1.2, DurationUnit.SECOND);
  58.         this.delta = 1.0;
  59.     }

  60.     /**
  61.      * Construct a new IDM car following model.
  62.      * @param a Acceleration; the maximum acceleration of a stationary vehicle (normal value is 1.56 m/s/s)
  63.      * @param b Acceleration; the maximum deemed-safe deceleration (this is a positive value). Normal value is 2.09 m/s/s.
  64.      * @param s0 Length; the minimum stationary headway (normal value is 3 m)
  65.      * @param tSafe Duration; the minimum time-headway (normal value is 1.2 s)
  66.      * @param delta double; the speed limit adherence (1.0; mean free speed equals the speed limit; 1.1: mean free speed equals
  67.      *            110% of the speed limit; etc.)
  68.      */
  69.     public IDMOld(final Acceleration a, final Acceleration b, final Length s0, final Duration tSafe, final double delta)
  70.     {
  71.         this.a = a;
  72.         this.b = b;
  73.         this.s0 = s0;
  74.         this.tSafe = tSafe;
  75.         this.delta = delta;
  76.     }

  77.     /**
  78.      * Desired speed (taking into account the urge to drive a little faster or slower than the posted speed limit).
  79.      * @param speedLimit Speed; the speed limit
  80.      * @param followerMaximumSpeed Speed; the maximum speed that the follower can drive
  81.      * @return DoubleScalarRel&lt;SpeedUnit&gt;; the desired speed
  82.      */
  83.     private Speed vDes(final Speed speedLimit, final Speed followerMaximumSpeed)
  84.     {
  85.         return new Speed(Math.min(this.delta * speedLimit.getSI(), followerMaximumSpeed.getSI()), SpeedUnit.SI);
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
  90.             final Speed leaderSpeed, final Length headway, final Speed speedLimit)
  91.     {
  92.         return computeAcceleration(followerSpeed, followerMaximumSpeed, leaderSpeed, headway, speedLimit, DEFAULT_STEP_SIZE);
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
  97.             final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
  98.     {
  99.         // TODO maxDistance
  100.         // dV is the approach speed
  101.         Speed dV = followerSpeed.minus(leaderSpeed);
  102.         double sStar = this.s0.si + followerSpeed.si * this.tSafe.si
  103.                 + dV.si * followerSpeed.si / (2.0 * Math.sqrt(this.a.si * this.b.si));
  104.         if (sStar < 0.0 && headway.si < 0.0)
  105.         {
  106.             return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
  107.         }
  108.         sStar = sStar >= 0.0 ? sStar : 0.0;
  109.         double s = headway.si > 0.0 ? headway.si : 1E-99;
  110.         Acceleration aInteraction = new Acceleration(this.a.si * (sStar / s) * (sStar / s), AccelerationUnit.SI);
  111.         Acceleration aFree =
  112.                 new Acceleration(this.a.si * (1.0 - Math.pow(followerSpeed.si / vDes(speedLimit, followerMaximumSpeed).si, 4)),
  113.                         AccelerationUnit.SI);
  114.         // limit deceleration for free term (= aFree)
  115.         if (aFree.si < -0.5)
  116.         {
  117.             aFree = new Acceleration(-0.5, AccelerationUnit.SI);
  118.         }
  119.         Acceleration newAcceleration = aFree.minus(aInteraction);
  120.         if (newAcceleration.si * stepSize.si + followerSpeed.si < 0)
  121.         {
  122.             newAcceleration = new Acceleration(-followerSpeed.si / stepSize.si, AccelerationUnit.SI);
  123.         }
  124.         return newAcceleration;
  125.     }

  126.     /** {@inheritDoc} */
  127.     @Override
  128.     public final Duration getStepSize()
  129.     {
  130.         return DEFAULT_STEP_SIZE;
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public final Acceleration getMaximumSafeDeceleration()
  135.     {
  136.         return this.b;
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     public final String getName()
  141.     {
  142.         return "IDM";
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public final String getLongName()
  147.     {
  148.         return String.format("%s (a=%.1fm/s\u00b2, b=%.1fm/s\u00b2, s0=%.1fm, tSafe=%.1fs, delta=%.2f)", getName(),
  149.                 this.a.getSI(), this.b.getSI(), this.s0.getSI(), this.tSafe.getSI(), this.delta);
  150.     }

  151.     /** {@inheritDoc} */
  152.     @Override
  153.     public final void setA(final Acceleration a)
  154.     {
  155.         this.a = a;
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     public final void setT(final Duration t)
  160.     {
  161.         this.tSafe = t;
  162.     }

  163.     /** {@inheritDoc} */
  164.     @Override
  165.     public final void setFspeed(final double fSpeed)
  166.     {
  167.         this.delta = fSpeed;
  168.     }

  169.     // The following is inherited from CarFollowingModel

  170.     /** {@inheritDoc} */
  171.     @Override
  172.     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
  173.     {
  174.         throw new UnsupportedOperationException("Old car-following model does not support desired speed.");
  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
  179.     {
  180.         throw new UnsupportedOperationException("Old car-following model does not support desired headway.");
  181.     }

  182.     /** {@inheritDoc} */
  183.     @Override
  184.     public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
  185.             final SpeedLimitInfo speedInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
  186.     {
  187.         Length headway;
  188.         Speed leaderSpeed;
  189.         if (leaders.isEmpty())
  190.         {
  191.             headway = new Length(Double.MAX_VALUE, LengthUnit.SI);
  192.             leaderSpeed = speed;
  193.         }
  194.         else
  195.         {
  196.             Headway leader = leaders.first();
  197.             headway = leader.getDistance();
  198.             leaderSpeed = leader.getSpeed();
  199.         }
  200.         return this.computeAcceleration(speed, speedInfo.getSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED), leaderSpeed, headway,
  201.                 speedInfo.getSpeedInfo(SpeedLimitTypes.FIXED_SIGN));
  202.     }

  203.     /** {@inheritDoc} */
  204.     @Override
  205.     public final String toString()
  206.     {
  207.         return "IDMOld [s0=" + this.s0 + ", a=" + this.a + ", b=" + this.b + ", tSafe=" + this.tSafe + ", stepSize="
  208.                 + DEFAULT_STEP_SIZE + ", delta=" + this.delta + "]";
  209.     }

  210. }