View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import org.djunits.value.vdouble.scalar.Duration;
4   import org.djunits.value.vdouble.scalar.Speed;
5   import org.opentrafficsim.base.parameters.ParameterException;
6   import org.opentrafficsim.base.parameters.ParameterTypes;
7   import org.opentrafficsim.base.parameters.Parameters;
8   import org.opentrafficsim.core.gtu.perception.EgoPerception;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
10  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
11  
12  /**
13   * Task class that translates a (composite) headway in to a task demand.
14   * <p>
15   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public abstract class TaskHeadwayBased extends AbstractTask
23  {
24  
25      /**
26       * Constructor.
27       * @param id String; id
28       */
29      public TaskHeadwayBased(final String id)
30      {
31          super(id);
32      }
33  
34      /** Current speed. */
35      private Speed speed;
36  
37      /** {@inheritDoc} */
38      @Override
39      public double calculateTaskDemand(final LanePerception perception, final LaneBasedGtu gtu, final Parameters parameters)
40              throws ParameterException
41      {
42          double a = gtu.getAcceleration().si;
43          double b = parameters.getParameter(ParameterTypes.B).si;
44          double tMin = parameters.getParameter(ParameterTypes.TMIN).si;
45          double hMin = a < -b ? (1.0 - (a + b) / (8.0 - b)) * tMin : tMin;
46          EgoPerception<?, ?> ego = perception.getPerceptionCategoryOrNull(EgoPerception.class);
47          this.speed = ego.getSpeed();
48          Duration h = getHeadway(perception, gtu, parameters);
49          if (h == null)
50          {
51              return 0.0; // no task demand
52          }
53          return h.si <= hMin ? 1.0 : (h.si > 3.0 ? 0.5 : 1.0 - (1.0 - 0.5) * (h.si - hMin) / (3.0 - hMin));
54      }
55  
56      /**
57       * Returns the current speed to translate a distance headway to a time headway.
58       * @return Speed; speed
59       */
60      protected Speed getSpeed()
61      {
62          return this.speed;
63      }
64  
65      /**
66       * Returns a collector for the task demand.
67       * @param perception LanePerception; perception
68       * @param gtu LaneBasedGtu; gtu
69       * @param parameters Parameters; parameters
70       * @return Duration; headway, {@code null} of none.
71       * @throws ParameterException on invalid parameter
72       */
73      protected abstract Duration getHeadway(LanePerception perception, LaneBasedGtu gtu, Parameters parameters)
74              throws ParameterException;
75  
76  }