TaskHeadwayBased.java

  1. package org.opentrafficsim.road.gtu.lane.perception.mental;

  2. import org.djunits.value.vdouble.scalar.Duration;
  3. import org.djunits.value.vdouble.scalar.Speed;
  4. import org.opentrafficsim.base.parameters.ParameterException;
  5. import org.opentrafficsim.base.parameters.ParameterTypes;
  6. import org.opentrafficsim.base.parameters.Parameters;
  7. import org.opentrafficsim.core.gtu.perception.EgoPerception;
  8. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  9. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;

  10. /**
  11.  * Task class that translates a (composite) headway in to a task demand.
  12.  * <p>
  13.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * </p>
  16.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  18.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  19.  */
  20. public abstract class TaskHeadwayBased extends AbstractTask
  21. {

  22.     /**
  23.      * Constructor.
  24.      * @param id id
  25.      */
  26.     public TaskHeadwayBased(final String id)
  27.     {
  28.         super(id);
  29.     }

  30.     /** Current speed. */
  31.     private Speed speed;

  32.     @Override
  33.     public double calculateTaskDemand(final LanePerception perception, final LaneBasedGtu gtu, final Parameters parameters)
  34.             throws ParameterException
  35.     {
  36.         double a = gtu.getAcceleration().si;
  37.         double b = parameters.getParameter(ParameterTypes.B).si;
  38.         double tMin = parameters.getParameter(ParameterTypes.TMIN).si;
  39.         double hMin = a < -b ? (1.0 - (a + b) / (8.0 - b)) * tMin : tMin;
  40.         EgoPerception<?, ?> ego = perception.getPerceptionCategoryOrNull(EgoPerception.class);
  41.         this.speed = ego.getSpeed();
  42.         Duration h = getHeadway(perception, gtu, parameters);
  43.         if (h == null)
  44.         {
  45.             return 0.0; // no task demand
  46.         }
  47.         return h.si <= hMin ? 1.0 : (h.si > 3.0 ? 0.5 : 1.0 - (1.0 - 0.5) * (h.si - hMin) / (3.0 - hMin));
  48.     }

  49.     /**
  50.      * Returns the current speed to translate a distance headway to a time headway.
  51.      * @return speed
  52.      */
  53.     protected Speed getSpeed()
  54.     {
  55.         return this.speed;
  56.     }

  57.     /**
  58.      * Returns a collector for the task demand.
  59.      * @param perception perception
  60.      * @param gtu gtu
  61.      * @param parameters parameters
  62.      * @return headway, {@code null} of none.
  63.      * @throws ParameterException on invalid parameter
  64.      */
  65.     protected abstract Duration getHeadway(LanePerception perception, LaneBasedGtu gtu, Parameters parameters)
  66.             throws ParameterException;

  67. }