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-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 jun. 2018 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class Distraction
26  {
27  
28      /** Id. */
29      private final String id;
30  
31      /** Description. */
32      private final String description;
33  
34      /** Frequency. */
35      private final Frequency frequency;
36  
37      /** Exposure (value in range [0...1]). */
38      private final double exposure;
39  
40      /** Random number stream. */
41      private final StreamInterface stream;
42  
43      /** Task supplier. */
44      private final TaskSupplier taskSupplier;
45  
46      /** Distribution of duration. */
47      private final ContinuousDistDoubleScalar.Rel<Duration, DurationUnit> dist;
48  
49      /**
50       * Constructor.
51       * @param id String; id
52       * @param description String; description
53       * @param frequency Frequency; frequency per exposed driver
54       * @param exposure double; exposure (value in range [0...1])
55       * @param averageDuration Duration; average duration
56       * @param stdDuration Duration; standard deviation of duration
57       * @param stream StreamInterface; random number stream
58       * @param taskSupplier TaskSupplier; task supplier
59       */
60      public Distraction(final String id, final String description, final Frequency frequency, final double exposure,
61              final Duration averageDuration, final Duration stdDuration, final StreamInterface stream,
62              final TaskSupplier taskSupplier)
63      {
64          Throw.whenNull(id, "Id may not be null.");
65          Throw.whenNull(description, "Description may not be null.");
66          Throw.whenNull(frequency, "Frequency may not be null.");
67          Throw.whenNull(averageDuration, "Average duration may not be null.");
68          Throw.whenNull(stream, "Random stream may not be null.");
69          Throw.when(exposure < 0.0 || exposure > 1.0, IllegalArgumentException.class, "Exposure should be in the range [0...1]");
70          this.id = id;
71          this.description = description;
72          this.frequency = frequency;
73          this.exposure = exposure;
74          this.stream = stream;
75          this.taskSupplier = taskSupplier;
76  
77          double var = stdDuration.si * stdDuration.si;
78          double avgSqrd = averageDuration.si * averageDuration.si;
79          double mu = Math.log(avgSqrd / Math.sqrt(var + avgSqrd));
80          double sigma = Math.sqrt(Math.log(var / avgSqrd + 1.0));
81          this.dist = new ContinuousDistDoubleScalar.Rel<>(new DistLogNormal(stream, mu, sigma), DurationUnit.SECOND);
82      }
83  
84      /**
85       * Returns the id.
86       * @return String; id
87       */
88      public final String getId()
89      {
90          return this.id;
91      }
92  
93      /**
94       * Returns the description.
95       * @return String; description
96       */
97      public final String getDescription()
98      {
99          return this.description;
100     }
101 
102     /**
103      * Returns the next exposure.
104      * @return boolean; next exposure
105      */
106     public final boolean nextExposure()
107     {
108         return this.stream.nextDouble() <= this.exposure;
109     }
110 
111     /**
112      * Returns the next inter-arrival time of this secondary task.
113      * @return Duration; next inter-arrival time of this secondary task
114      */
115     public final Duration nextInterArrival()
116     {
117         return Duration.createSI(-Math.log(this.stream.nextDouble()) / this.frequency.si);
118     }
119 
120     /**
121      * Returns the next duration of this secondary task.
122      * @return Duration; next duration of this secondary task
123      */
124     public final Duration nextDuration()
125     {
126         return this.dist.draw();
127     }
128 
129     /**
130      * Returns a task for the given GTU.
131      * @param gtu LaneBasedGTU; gtu
132      * @return Task; task for given GTU
133      */
134     public final Task getTask(final LaneBasedGTU gtu)
135     {
136         return this.taskSupplier.getTask(gtu);
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public String toString()
142     {
143         return "Distraction [id=" + this.id + ", description=" + this.description + ", frequency=" + this.frequency
144                 + ", exposure=" + this.exposure + "]";
145     }
146 
147 }