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.opentrafficsim.base.parameters.ParameterException;
  7. import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
  8. import org.opentrafficsim.base.parameters.ParameterTypes;
  9. import org.opentrafficsim.base.parameters.Parameters;
  10. import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
  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. import nl.tudelft.simulation.language.Throw;

  16. /**
  17.  * Static methods regarding traffic lights for composition in tactical planners.
  18.  * <p>
  19.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  21.  * <p>
  22.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version May 13, 2016 <br>
  23.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  24.  */
  25. public final class TrafficLightUtil
  26. {
  27.     /** Maximum deceleration for stopping for yellow traffic light. */
  28.     public static final ParameterTypeAcceleration B_YELLOW =
  29.             new ParameterTypeAcceleration("bYellow", "Maximum deceleration for stopping for yellow traffic light.",
  30.                     new Acceleration(3.5, AccelerationUnit.SI), ConstraintInterface.POSITIVE);

  31.     /**
  32.      * Do not instantiate.
  33.      */
  34.     private TrafficLightUtil()
  35.     {
  36.         //
  37.     }

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

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

  120. }