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
23
24
25
26
27
28
29
30
31
32 public abstract class TaskHeadwayBased implements Task
33 {
34
35
36 private Speed speed;
37
38
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;
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
60
61
62 protected Speed getSpeed()
63 {
64 return this.speed;
65 }
66
67
68
69
70
71
72
73
74
75 protected abstract Duration getHeadway(LanePerception perception, LaneBasedGTU gtu, Parameters parameters)
76 throws ParameterException;
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static class TaskHeadwayCollector implements PerceptionCollector<Duration, LaneBasedGTU, Duration>
91 {
92
93
94 private final Speed speed;
95
96
97
98
99
100 public TaskHeadwayCollector(final Speed speed)
101 {
102 this.speed = speed;
103 }
104
105
106 @Override
107 public Supplier<Duration> getIdentity()
108 {
109 return new Supplier<Duration>()
110 {
111 @Override
112 public Duration get()
113 {
114 return null;
115 }
116 };
117 }
118
119
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();
132 return intermediate;
133 }
134 };
135 }
136
137
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 }