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