View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.opentrafficsim.base.parameters.ParameterException;
9   import org.opentrafficsim.base.parameters.ParameterTypes;
10  import org.opentrafficsim.base.parameters.Parameters;
11  import org.opentrafficsim.core.gtu.Try;
12  import org.opentrafficsim.core.gtu.perception.EgoPerception;
13  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
14  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
16  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
17  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
18  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;
19  import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.Task;
20  
21  /**
22   * Task class that translates a (composite) headway in to a task demand.
23   * <p>
24   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
26   * <p>
27   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 25 jun. 2018 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
31   */
32  public abstract class TaskHeadwayBased implements Task
33  {
34  
35      /** Current speed. */
36      private Speed speed;
37  
38      /** {@inheritDoc} */
39      @Override
40      public double demand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
41              throws ParameterException
42      {
43          double a = gtu.getAcceleration().si;
44          double b = parameters.getParameter(ParameterTypes.B).si;
45          double tMin = parameters.getParameter(ParameterTypes.TMIN).si;
46          double hMin = a < -b ? (1.0 - (a + b) / (8.0 - b)) * tMin : tMin;
47          EgoPerception ego = perception.getPerceptionCategoryOrNull(EgoPerception.class);
48          Try.execute(() -> ego.updateSpeed(), "Could not update perception of ego speed.");
49          this.speed = ego.getSpeed();
50          Duration h = getHeadway(perception, gtu, parameters);
51          if (h == null)
52          {
53              return 0.0; // no task demand
54          }
55          return h.si <= hMin ? 1.0 : (h.si > 3.0 ? 0.5 : 1.0 - (1.0 - 0.5) * (h.si - hMin) / (3.0 - hMin));
56      }
57  
58      /**
59       * Returns the current speed to translate a distance headway to a time headway.
60       * @return Speed; speed
61       */
62      protected Speed getSpeed()
63      {
64          return this.speed;
65      }
66  
67      /**
68       * Returns a collector for the task demand.
69       * @param perception LanePerception; perception
70       * @param gtu LaneBasedGTU; gtu
71       * @param parameters Parameters; parameters
72       * @return Duration; headway, {@code null} of none.
73       * @throws ParameterException on invalid parameter
74       */
75      protected abstract Duration getHeadway(LanePerception perception, LaneBasedGTU gtu, Parameters parameters)
76              throws ParameterException;
77  
78      /**
79       * Simple collector implementation to obtain time headway.
80       * <p>
81       * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
82       * <br>
83       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
84       * <p>
85       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
86       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
87       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
88       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
89       */
90      public static class TaskHeadwayCollector implements PerceptionCollector<Duration, LaneBasedGTU, Duration>
91      {
92  
93          /** Own speed. */
94          private final Speed speed;
95  
96          /**
97           * Constructor.
98           * @param speed Speed; speed
99           */
100         public TaskHeadwayCollector(final Speed speed)
101         {
102             this.speed = speed;
103         }
104 
105         /** {@inheritDoc} */
106         @Override
107         public Supplier<Duration> getIdentity()
108         {
109             return new Supplier<Duration>()
110             {
111                 @Override
112                 public Duration get()
113                 {
114                     return null; // if no leader
115                 }
116             };
117         }
118 
119         /** {@inheritDoc} */
120         @Override
121         public PerceptionAccumulator<LaneBasedGTU, Duration> getAccumulator()
122         {
123             return new PerceptionAccumulator<LaneBasedGTU, Duration>()
124             {
125                 @SuppressWarnings("synthetic-access")
126                 @Override
127                 public Intermediate<Duration> accumulate(final Intermediate<Duration> intermediate, final LaneBasedGTU object,
128                         final Length distance)
129                 {
130                     intermediate.setObject(distance.divideBy(TaskHeadwayCollector.this.speed));
131                     intermediate.stop(); // need only 1 leader
132                     return intermediate;
133                 }
134             };
135         }
136 
137         /** {@inheritDoc} */
138         @Override
139         public PerceptionFinalizer<Duration, Duration> getFinalizer()
140         {
141             return new PerceptionFinalizer<Duration, Duration>()
142             {
143                 @Override
144                 public Duration collect(final Duration intermediate)
145                 {
146                     return intermediate == null ? intermediate : (intermediate.gt0() ? intermediate : Duration.ZERO);
147                 }
148             };
149         }
150 
151     }
152 
153 }