View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental.sdm;
2   
3   import org.djunits.unit.DurationUnit;
4   import org.djunits.value.vdouble.scalar.Duration;
5   import org.djunits.value.vdouble.scalar.Frequency;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
8   
9   import nl.tudelft.simulation.jstats.distributions.DistLogNormal;
10  import nl.tudelft.simulation.jstats.streams.StreamInterface;
11  
12  /**
13   * Task as seen by the Stochastic Distraction Model.
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://github.com/peter-knoppers">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public class Distraction
23  {
24  
25      /** Id. */
26      private final String id;
27  
28      /** Description. */
29      private final String description;
30  
31      /** Frequency. */
32      private final Frequency frequency;
33  
34      /** Exposure (value in range [0...1]). */
35      private final double exposure;
36  
37      /** Random number stream. */
38      private final StreamInterface stream;
39  
40      /** Distribution of duration. */
41      private final ContinuousDistDoubleScalar.Rel<Duration, DurationUnit> dist;
42  
43      /** Level of task demand. */
44      private final double taskDemand;
45  
46      /**
47       * Constructor.
48       * @param id id
49       * @param description description
50       * @param frequency frequency per exposed driver
51       * @param exposure exposure (value in range [0...1])
52       * @param averageDuration average duration
53       * @param stdDuration standard deviation of duration
54       * @param taskDemand task demand
55       * @param stream random number stream
56       */
57      public Distraction(final String id, final String description, final Frequency frequency, final double exposure,
58              final Duration averageDuration, final Duration stdDuration, final double taskDemand, final StreamInterface stream)
59      {
60          Throw.whenNull(id, "Id may not be null.");
61          Throw.whenNull(description, "Description may not be null.");
62          Throw.whenNull(frequency, "Frequency may not be null.");
63          Throw.whenNull(averageDuration, "Average duration may not be null.");
64          Throw.whenNull(stream, "Random stream may not be null.");
65          Throw.when(exposure < 0.0 || exposure > 1.0, IllegalArgumentException.class, "Exposure should be in the range [0...1]");
66          Throw.when(taskDemand < 0.0 || taskDemand > 1.0, IllegalArgumentException.class,
67                  "Task demand should be in the range [0...1]");
68          this.id = id;
69          this.description = description;
70          this.frequency = frequency;
71          this.exposure = exposure;
72          this.stream = stream;
73          this.taskDemand = taskDemand;
74  
75          double var = stdDuration.si * stdDuration.si;
76          double avgSqrd = averageDuration.si * averageDuration.si;
77          double mu = Math.log(avgSqrd / Math.sqrt(var + avgSqrd));
78          double sigma = Math.sqrt(Math.log(var / avgSqrd + 1.0));
79          this.dist = new ContinuousDistDoubleScalar.Rel<>(new DistLogNormal(stream, mu, sigma), DurationUnit.SECOND);
80      }
81  
82      /**
83       * Returns the id.
84       * @return id
85       */
86      public String getId()
87      {
88          return this.id;
89      }
90  
91      /**
92       * Returns the description.
93       * @return description
94       */
95      public String getDescription()
96      {
97          return this.description;
98      }
99  
100     /**
101      * Returns the level of task demand.
102      * @return taskDemand level of task demand
103      */
104     public double getTaskDemand()
105     {
106         return this.taskDemand;
107     }
108 
109     /**
110      * Returns the next exposure.
111      * @return next exposure
112      */
113     public boolean nextExposure()
114     {
115         return this.stream.nextDouble() <= this.exposure;
116     }
117 
118     /**
119      * Returns the next inter-arrival time of this secondary task.
120      * @return next inter-arrival time of this secondary task
121      */
122     public Duration nextInterArrival()
123     {
124         return Duration.ofSI(-Math.log(this.stream.nextDouble()) / this.frequency.si);
125     }
126 
127     /**
128      * Returns the next duration of this secondary task.
129      * @return next duration of this secondary task
130      */
131     public Duration nextDuration()
132     {
133         return this.dist.get();
134     }
135 
136     @Override
137     public String toString()
138     {
139         return "Distraction [id=" + this.id + ", description=" + this.description + ", frequency=" + this.frequency
140                 + ", exposure=" + this.exposure + "]";
141     }
142 
143 }