View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental.ar;
2   
3   import java.util.Set;
4   
5   import org.djutils.exceptions.Throw;
6   import org.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.ParameterTypeDouble;
8   import org.opentrafficsim.base.parameters.Parameters;
9   import org.opentrafficsim.base.parameters.constraint.DualBound;
10  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
11  import org.opentrafficsim.road.gtu.lane.perception.mental.BehavioralAdaptation;
12  import org.opentrafficsim.road.gtu.lane.perception.mental.SumFuller;
13  
14  /**
15   * Extends Fuller with the concept of anticipation reliance (AR).
16   * <p>
17   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   */
24  public class ArFuller extends SumFuller<ArTask>
25  {
26  
27      /** Fraction of primary task that can be reduced by anticipation reliance. */
28      public static final ParameterTypeDouble ALPHA = new ParameterTypeDouble("alpha",
29              "Fraction of primary task that can be reduced by anticipation reliance.", 0.8, DualBound.UNITINTERVAL);
30  
31      /** Fraction of auxiliary tasks that can be reduced by anticipation reliance. */
32      public static final ParameterTypeDouble BETA = new ParameterTypeDouble("beta",
33              "Fraction of auxiliary tasks that can be reduced by anticipation reliance.", 0.6, DualBound.UNITINTERVAL);
34  
35      /** Primary task id. */
36      private final String primaryTaskId;
37  
38      /**
39       * Constructor.
40       * @param tasks tasks
41       * @param behavioralAdapatations behavioralAdapatations
42       * @param primaryTaskId String; primary task id
43       */
44      public ArFuller(final Set<ArTask> tasks, final Set<BehavioralAdaptation> behavioralAdapatations, final String primaryTaskId)
45      {
46          super(tasks, behavioralAdapatations);
47          this.primaryTaskId = primaryTaskId;
48      }
49  
50      @Override
51      protected double getTotalTaskDemand(final LanePerception perception) throws ParameterException
52      {
53          manage(perception);
54          return getTasks().stream().mapToDouble((t) -> t.getTaskDemand() - t.getAnticipationReliance()).sum();
55      }
56  
57      /**
58       * Manage task demand and anticipation reliance levels.
59       * @param perception perception
60       * @throws ParameterException if a parameter is missing or out of bounds
61       */
62      // protected such that it may be overridden
63      protected void manage(final LanePerception perception) throws ParameterException
64      {
65          ArTask primary = null;
66          for (ArTask task : getTasks())
67          {
68              if (task.getId().equals(this.primaryTaskId))
69              {
70                  primary = task;
71                  break;
72              }
73          }
74          Throw.whenNull(primary, "There is no primary task with id '%s'.", this.primaryTaskId);
75          double primaryTaskDemand = primary.getTaskDemand(perception);
76          // max AR is alpha of TD, actual AR approaches 0 for increasing TD
77          Parameters parameters = perception.getGtu().getParameters();
78          double a = parameters.getParameter(ALPHA);
79          double b = parameters.getParameter(BETA);
80          primary.setAnticipationReliance(a * primaryTaskDemand * (1.0 - primaryTaskDemand));
81          for (ArTask auxiliary : getTasks())
82          {
83              if (!auxiliary.getId().equals(this.primaryTaskId))
84              {
85                  double auxiliaryTaskLoad = auxiliary.getTaskDemand(perception);
86                  // max AR is beta of TD, actual AR approaches 0 as primary TD approaches 0
87                  auxiliary.setAnticipationReliance(b * auxiliaryTaskLoad * primaryTaskDemand);
88              }
89          }
90      }
91  
92      @Override
93      public String toString()
94      {
95          return "ArFuller [tasks=" + getTasks() + "]";
96      }
97  
98  }