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