View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental.ar;
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/wjschakel">Wouter Schakel</a>
19   */
20  public abstract class ArTaskHeadwayBased extends AbstractArTask
21  {
22  
23      /**
24       * Constructor.
25       * @param id id
26       */
27      public ArTaskHeadwayBased(final String id)
28      {
29          super(id);
30      }
31  
32      /** Current speed. */
33      private Speed speed;
34  
35      @Override
36      public double calculateTaskDemand(final LanePerception perception) throws ParameterException
37      {
38          LaneBasedGtu gtu = perception.getGtu();
39          Parameters parameters = gtu.getParameters();
40          double a = gtu.getAcceleration().si;
41          double b = parameters.getParameter(ParameterTypes.B).si;
42          double tMin = parameters.getParameter(ParameterTypes.TMIN).si;
43          double hMin = a < -b ? (1.0 - (a + b) / (8.0 - b)) * tMin : tMin;
44          EgoPerception<?, ?> ego = perception.getPerceptionCategoryOrNull(EgoPerception.class);
45          this.speed = ego.getSpeed();
46          Duration h = getHeadway(perception, gtu, parameters);
47          if (h == null)
48          {
49              return 0.0; // no task demand
50          }
51          return h.si <= hMin ? 1.0 : (h.si > 3.0 ? 0.5 : 1.0 - (1.0 - 0.5) * (h.si - hMin) / (3.0 - hMin));
52      }
53  
54      /**
55       * Returns the current speed to translate a distance headway to a time headway.
56       * @return speed
57       */
58      protected Speed getSpeed()
59      {
60          return this.speed;
61      }
62  
63      /**
64       * Returns a collector for the task demand.
65       * @param perception perception
66       * @param gtu gtu
67       * @param parameters parameters
68       * @return headway, {@code null} of none.
69       * @throws ParameterException on invalid parameter
70       */
71      protected abstract Duration getHeadway(LanePerception perception, LaneBasedGtu gtu, Parameters parameters)
72              throws ParameterException;
73  
74  }