TrafficLightUtil.java

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

  2. import org.djunits.unit.AccelerationUnit;
  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.djutils.exceptions.Throw;
  7. import org.opentrafficsim.base.parameters.ParameterException;
  8. import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
  9. import org.opentrafficsim.base.parameters.ParameterTypes;
  10. import org.opentrafficsim.base.parameters.Parameters;
  11. import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterableSet;
  12. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayTrafficLight;
  13. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  14. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;

  15. /**
  16.  * Static methods regarding traffic lights for composition in tactical planners.
  17.  * <p>
  18.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  19.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  20.  * </p>
  21.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  22.  */
  23. public final class TrafficLightUtil
  24. {
  25.     /** Maximum deceleration for stopping for yellow traffic light. */
  26.     public static final ParameterTypeAcceleration BCRIT = ParameterTypes.BCRIT;

  27.     /**
  28.      * Do not instantiate.
  29.      */
  30.     private TrafficLightUtil()
  31.     {
  32.         //
  33.     }

  34.     /**
  35.      * Returns an acceleration as response to a set of traffic lights, being positive infinity if ignored. The response is
  36.      * governed by the car-following model in case a traffic light is yellow or red. A constant deceleration to stop is also
  37.      * calculated, and the highest acceleration of both is used. If this value is below -bYellow (B_YELLOW), the traffic light
  38.      * is ignored, which usually occurs only during the yellow phase. By using the highest acceleration of the car-following
  39.      * model and the constant deceleration, it is ensured that comfortable deceleration is applied if approaching a red traffic
  40.      * light from far away, while strong deceleration is only applied if required and appropriately represents stopping for
  41.      * yellow.
  42.      * @param parameters parameters
  43.      * @param headwayTrafficLights set of headway traffic lights
  44.      * @param carFollowingModel car following model
  45.      * @param speed speed
  46.      * @param speedLimitInfo speed limit info
  47.      * @return acceleration as response to a traffic light, being positive infinity if ignored
  48.      * @throws ParameterException if a parameter is not defined
  49.      * @throws NullPointerException if any input is null
  50.      * @throws IllegalArgumentException if the traffic light is not downstream
  51.      */
  52.     public static Acceleration respondToTrafficLights(final Parameters parameters,
  53.             final Iterable<HeadwayTrafficLight> headwayTrafficLights, final CarFollowingModel carFollowingModel,
  54.             final Speed speed, final SpeedLimitInfo speedLimitInfo) throws ParameterException
  55.     {
  56.         Throw.whenNull(headwayTrafficLights, "Traffic light set may not be null.");
  57.         Acceleration a = new Acceleration(Double.POSITIVE_INFINITY, AccelerationUnit.SI);
  58.         for (HeadwayTrafficLight headwayTrafficLight : headwayTrafficLights)
  59.         {
  60.             Acceleration aLight =
  61.                     respondToTrafficLight(parameters, headwayTrafficLight, carFollowingModel, speed, speedLimitInfo);
  62.             a = Acceleration.min(a, aLight);
  63.         }
  64.         return a;
  65.     }

  66.     /**
  67.      * Returns an acceleration as response to a traffic light, being positive infinity if ignored. The response is governed by
  68.      * the car-following model in case the traffic light is yellow or red. A constant deceleration to stop is also calculated,
  69.      * and the highest acceleration of both is used. If this value is below -bYellow (B_YELLOW), the traffic light is ignored,
  70.      * which usually occurs only during the yellow phase. By using the highest acceleration of the car-following model and the
  71.      * constant deceleration, it is ensured that comfortable deceleration is applied if approaching a red traffic light from far
  72.      * away, while strong deceleration is only applied if required and appropriately represents stopping for yellow.
  73.      * @param parameters parameters
  74.      * @param headwayTrafficLight headway traffic light
  75.      * @param carFollowingModel car following model
  76.      * @param speed speed
  77.      * @param speedLimitInfo speed limit info
  78.      * @return acceleration as response to a traffic light, being positive infinity if ignored
  79.      * @throws ParameterException if a parameter is not defined
  80.      * @throws NullPointerException if any input is null
  81.      * @throws IllegalArgumentException if the traffic light is not downstream
  82.      */
  83.     // @docs/06-behavior/tactical-planner/#modular-utilities
  84.     public static Acceleration respondToTrafficLight(final Parameters parameters, final HeadwayTrafficLight headwayTrafficLight,
  85.             final CarFollowingModel carFollowingModel, final Speed speed, final SpeedLimitInfo speedLimitInfo)
  86.             throws ParameterException
  87.     {
  88.         Throw.whenNull(parameters, "Parameters may not be null.");
  89.         Throw.whenNull(headwayTrafficLight, "Traffic light may not be null.");
  90.         Throw.whenNull(carFollowingModel, "Car-following model may not be null.");
  91.         Throw.whenNull(speed, "Speed may not be null.");
  92.         Throw.whenNull(speedLimitInfo, "Speed limit info may not be null.");
  93.         Throw.when(!headwayTrafficLight.isAhead(), IllegalArgumentException.class, "Traffic light must be downstream.");
  94.         if ((headwayTrafficLight.getTrafficLightColor().isRed() || headwayTrafficLight.getTrafficLightColor().isYellow())
  95.                 && !headwayTrafficLight.canTurnOnRed())
  96.         {
  97.             // deceleration from car-following model
  98.             Acceleration a = carFollowingModel.followingAcceleration(parameters, speed, speedLimitInfo,
  99.                     new PerceptionIterableSet<>(headwayTrafficLight));
  100.             // compare to constant deceleration
  101.             Length s0 = parameters.getParameter(ParameterTypes.S0);
  102.             if (headwayTrafficLight.getDistance().gt(s0)) // constant acceleration not applicable if within s0
  103.             {
  104.                 // constant acceleration is -.5*v^2/s, where s = distance-s0 > 0
  105.                 Acceleration aConstant = CarFollowingUtil.constantAccelerationStop(carFollowingModel, parameters, speed,
  106.                         headwayTrafficLight.getDistance());
  107.                 a = Acceleration.max(a, aConstant);
  108.             }
  109.             // return a if a > -b
  110.             if (a.gt(parameters.getParameter(BCRIT).neg()))
  111.             {
  112.                 return a;
  113.             }
  114.         }
  115.         // ignore traffic light
  116.         return new Acceleration(Double.POSITIVE_INFINITY, AccelerationUnit.SI);
  117.     }

  118. }