View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVE;
4   import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVEZERO;
5   
6   import java.util.Set;
7   
8   import org.djutils.exceptions.Throw;
9   import org.djutils.exceptions.Try;
10  import org.djutils.immutablecollections.Immutable;
11  import org.djutils.immutablecollections.ImmutableLinkedHashSet;
12  import org.djutils.immutablecollections.ImmutableSet;
13  import org.opentrafficsim.base.Identifiable;
14  import org.opentrafficsim.base.parameters.ParameterException;
15  import org.opentrafficsim.base.parameters.ParameterTypeDouble;
16  import org.opentrafficsim.base.parameters.Parameters;
17  import org.opentrafficsim.core.gtu.GTUException;
18  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
19  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
20  
21  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22  
23  /**
24   * Task-capability interface in accordance to Fuller (2011). Task demand is the sum of demands described by individual
25   * {@code Task}s. These take exogenous information to describe the workload in fundamental relations. Task demand is divided by
26   * task capability to arrive at a task saturation. Task saturation is input to {@code BehavioralAdaptation}s which alter
27   * parameters describing personal traits, such as desired headway and desired speed. In this way, task demand is kept at an
28   * equilibrium as described by Fuller.
29   * <p>
30   * A {@code BehavioralAdaptation} may also determine what the level of situational awareness is, which includes determining
31   * reaction time. Both situational awareness and reaction time parameters can be used in perception to model deteriorated
32   * perception due to a task demand imbalance.
33   * <p>
34   * Fuller, R., Driver control theory: From task difficulty homeostasis to risk allostasis, in Handbook of Traffic Psychology.
35   * 2011. p. 13-26
36   * <p>
37   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
39   * <p>
40   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
41   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
42   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
43   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
44   */
45  public class Fuller implements Mental
46  {
47  
48      // Parameters
49  
50      /** Task capability in nominal task capability units, i.e. mean is 1. */
51      public static final ParameterTypeDouble TC = new ParameterTypeDouble("TC", "Task capability", 1.0, POSITIVE);
52  
53      /** Critical task saturation. */
54      public static final ParameterTypeDouble TS_CRIT =
55              new ParameterTypeDouble("TScrit", "Critical task saturation", 0.8, POSITIVEZERO)
56              {
57                  /** */
58                  private static final long serialVersionUID = 20180403L;
59  
60                  /** {@inheritDoc} */
61                  @Override
62                  public void check(final Double value, final Parameters params) throws ParameterException
63                  {
64                      Double tsMax = params.getParameterOrNull(TS_MAX);
65                      Throw.when(tsMax != null && value > tsMax, ParameterException.class,
66                              "Value for TS_CRIT should not be larger than TS_MAX.");
67                  }
68              };
69  
70      /** Maximum task saturation, pertaining to maximum deterioration. */
71      public static final ParameterTypeDouble TS_MAX =
72              new ParameterTypeDouble("TSmax", "Maximum task saturation", 2.0, POSITIVEZERO)
73              {
74                  /** */
75                  private static final long serialVersionUID = 20180403L;
76  
77                  /** {@inheritDoc} */
78                  @Override
79                  public void check(final Double value, final Parameters params) throws ParameterException
80                  {
81                      Double tsCrit = params.getParameterOrNull(TS_CRIT);
82                      Throw.when(tsCrit != null && value < tsCrit, ParameterException.class,
83                              "Value for TS_MAX should not be smaller than TS_CRIT.");
84                  }
85              };
86  
87      /** Task saturation. */
88      public static final ParameterTypeDouble TS = new ParameterTypeDouble("TS", "Task saturation", 0.0, POSITIVEZERO);
89  
90      // Properties
91  
92      /** Tasks causing task demand. */
93      private final Set<Task> tasks;
94  
95      /** Behavioral adaptations depending on task saturation. */
96      private final Set<BehavioralAdaptation> behavioralAdapatations;
97  
98      /**
99       * Constructor with custom situational awareness.
100      * @param tasks Set&lt;Task&gt;; tasks
101      * @param behavioralAdapatations Set&lt;BehavioralAdaptation&gt;; behavioralAdapatations
102      */
103     public Fuller(final Set<Task> tasks, final Set<BehavioralAdaptation> behavioralAdapatations)
104     {
105         Throw.whenNull(tasks, "Tasks may not be null.");
106         Throw.whenNull(behavioralAdapatations, "Behavioral adaptations may not be null.");
107         this.tasks = tasks;
108         this.behavioralAdapatations = behavioralAdapatations;
109     }
110 
111     /**
112      * Adds a task.
113      * @param task Task; task to add
114      */
115     public void addTask(final Task task)
116     {
117         this.tasks.add(task);
118     }
119 
120     /**
121      * Removes a task.
122      * @param task Task; task to remove
123      */
124     public void removeTask(final Task task)
125     {
126         this.tasks.remove(task);
127     }
128 
129     /**
130      * Returns the tasks.
131      * @return ImmutableSet&lt;Task&gt; tasks
132      */
133     public ImmutableSet<Task> getTasks()
134     {
135         return new ImmutableLinkedHashSet<>(this.tasks, Immutable.WRAP);
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public void apply(final LanePerception perception) throws ParameterException, GTUException
141     {
142         LaneBasedGTU gtu = Try.assign(() -> perception.getGtu(), "Could not obtain GTU.");
143         Parameters parameters = gtu.getParameters();
144         double taskDemand = 0.0;
145         // a) the fundamental diagrams of task workload are defined in the tasks
146         // b) sum task demand
147         for (Task task : this.tasks)
148         {
149             taskDemand += task.demand(perception, gtu, parameters);
150         }
151         double taskSaturation = taskDemand / parameters.getParameter(TC);
152         parameters.setParameter(TS, taskSaturation);
153         // c) behavioral adaptation
154         for (BehavioralAdaptation behavioralAdapatation : this.behavioralAdapatations)
155         {
156             behavioralAdapatation.adapt(parameters, taskSaturation);
157         }
158         // d) situational awareness can be implemented by one of the behavioral responses
159         // e) perception errors from situational awareness are included in the perception step
160         // f) reaction time from situational awareness are included in the perception step
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     public String toString()
166     {
167         return "Fuller [tasks=" + this.tasks + ", behavioralAdapatations=" + this.behavioralAdapatations + "]";
168     }
169 
170     /**
171      * Interface for tasks, where each describes a fundamental relation between exogenous inputs causing a mental demand.
172      * <p>
173      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
174      * <br>
175      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
176      * <p>
177      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
178      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
179      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
180      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
181      */
182     @FunctionalInterface
183     public interface Task
184     {
185         /**
186          * Returns the demand of this task.
187          * @param perception LanePerception; perception
188          * @param gtu LaneBasedGTU; gtu
189          * @param parameters Parameters; parameters
190          * @return double; demand of this task
191          * @throws ParameterException if a parameter is missing or out of bounds
192          * @throws GTUException exceptions pertaining to the GTU
193          */
194         double demand(LanePerception perception, LaneBasedGTU gtu, Parameters parameters)
195                 throws ParameterException, GTUException;
196 
197         /**
198          * Class for constant demand.
199          */
200         class Constant extends IdentifiableTask
201         {
202             /** Task demand. */
203             private final double taskDemand;
204 
205             /**
206              * Constructor.
207              * @param id String; id
208              * @param taskDemand double; task demand
209              */
210             public Constant(final String id, final double taskDemand)
211             {
212                 super(id);
213                 this.taskDemand = taskDemand;
214             }
215 
216             /** {@inheritDoc} */
217             @Override
218             public double demand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
219                     throws ParameterException, GTUException
220             {
221                 return this.taskDemand;
222             }
223 
224         }
225 
226         /**
227          * Class for exponential demand.
228          */
229         class Exponential extends IdentifiableTask
230         {
231             /** Initial level of task demand. */
232             private final double initialTaskDemand;
233 
234             /** Additional task demand. */
235             private final double additionalTaskDemand;
236 
237             /** Time scale at which task demand changes from the initial to the final value. */
238             private final double tau;
239 
240             /** Start time of the distraction. */
241             private final double start;
242 
243             /**
244              * Constructor.
245              * @param id String; id
246              * @param initialTaskDemand double; initial level of task demand
247              * @param finalTaskDemand double; final level of task demand
248              * @param tau double; time scale at which task demand changes from the initial to the final value
249              * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
250              */
251             public Exponential(final String id, final double initialTaskDemand, final double finalTaskDemand, final double tau,
252                     final SimulatorInterface.TimeDoubleUnit simulator)
253             {
254                 super(id);
255                 this.initialTaskDemand = initialTaskDemand;
256                 this.additionalTaskDemand = finalTaskDemand - initialTaskDemand;
257                 this.tau = tau;
258                 this.start = simulator.getSimulatorTime().si;
259             }
260 
261             /** {@inheritDoc} */
262             @Override
263             public double demand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
264                     throws ParameterException, GTUException
265             {
266                 double t = gtu.getSimulator().getSimulatorTime().si - this.start;
267                 return this.initialTaskDemand + this.additionalTaskDemand * (1.0 - Math.exp(-t / this.tau));
268             }
269 
270         }
271     }
272 
273     /**
274      * Task that makes itself identifiable. This is intended for inspection and visualization.
275      * <p>
276      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
277      * <br>
278      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
279      * <p>
280      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 nov. 2018 <br>
281      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
282      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
283      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
284      */
285     public abstract static class IdentifiableTask implements Task, Identifiable
286     {
287         /** Id. */
288         private final String id;
289 
290         /**
291          * Constructor.
292          * @param id String; id
293          */
294         public IdentifiableTask(final String id)
295         {
296             this.id = id;
297         }
298 
299         /** {@inheritDoc} */
300         @Override
301         public String getId()
302         {
303             return this.id;
304         }
305 
306         /** {@inheritDoc} */
307         @Override
308         public String toString()
309         {
310             return "IdentifiableTask [id=" + getId() + "]";
311         }
312     }
313 
314     /**
315      * Behavioral adaptation by changing parameter values.
316      * <p>
317      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
318      * <br>
319      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
320      * <p>
321      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
322      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
323      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
324      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
325      */
326     @FunctionalInterface
327     public interface BehavioralAdaptation
328     {
329         /**
330          * Adapt to task saturation by changing parameter values.
331          * @param parameters Parameters; parameters
332          * @param taskSaturation double; task saturation
333          * @throws ParameterException if a parameter is missing or out of bounds
334          */
335         void adapt(Parameters parameters, double taskSaturation) throws ParameterException;
336     }
337 
338 }