1 package org.opentrafficsim.ahfe;
2
3 import java.util.LinkedHashMap;
4 import java.util.LinkedHashSet;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.SortedSet;
8
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.djunits.value.vdouble.scalar.Length;
11 import org.djunits.value.vdouble.scalar.Time;
12 import org.opentrafficsim.base.TimeStampedObject;
13 import org.opentrafficsim.base.parameters.ParameterException;
14 import org.opentrafficsim.base.parameters.ParameterTypeDuration;
15 import org.opentrafficsim.base.parameters.ParameterTypes;
16 import org.opentrafficsim.base.parameters.Parameters;
17 import org.opentrafficsim.core.gtu.GTUException;
18 import org.opentrafficsim.core.gtu.perception.PerceptionException;
19 import org.opentrafficsim.core.network.LateralDirectionality;
20 import org.opentrafficsim.core.network.NetworkException;
21 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
22 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
23 import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
24 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
25 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.DirectNeighborsPerception;
26 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.HeadwayGtuType;
27 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
28 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
29
30 import nl.tudelft.simulation.dsol.SimRuntimeException;
31
32
33
34
35
36
37
38
39
40
41
42 @Deprecated
43 public abstract class AbstractDelayedNeighborsPerception extends AbstractDelayedPerceptionCategory
44 implements NeighborsPerception
45 {
46
47
48 private static final long serialVersionUID = 20170217L;
49
50
51 protected static final ParameterTypeDuration TR = ParameterTypes.TR;
52
53
54 protected static final ParameterTypeDuration DT = ParameterTypes.DT;
55
56
57 private Time initialTime = null;
58
59
60 private final DirectNeighborsPerception direct;
61
62
63 private Duration reactionTime = null;
64
65
66 private Duration plannerTimeStep = null;
67
68
69 private Duration remainder = null;
70
71
72 public static final String FIRSTLEADERS = "firstLeaders";
73
74
75 public static final String FIRSTFOLLOWERS = "firstFollower";
76
77
78 public static final String GTUALONGSIDE = "gtuAlongside";
79
80
81 public static final String LEADERS = "leaders";
82
83
84 public static final String FOLLOWERS = "followers";
85
86
87 public static final NeighborsInfoType<SortedSet<RelativeLane>> CROSSSECTION = new NeighborsInfoType<>("cross-section");
88
89
90 public static final NeighborsInfoType<Length> ODOMETER = new NeighborsInfoType<>("odometer");
91
92
93 private boolean gtuAlongsideLeftOverride = false;
94
95
96 private boolean gtuAlongsideRightOverride = false;
97
98
99
100
101
102 public AbstractDelayedNeighborsPerception(final LanePerception perception)
103 {
104 super(perception);
105 this.direct = new DirectNeighborsPerception(perception, HeadwayGtuType.COPY);
106 }
107
108
109 @Override
110 public final void updateAll() throws GTUException, NetworkException, ParameterException
111 {
112
113 if (this.remainder == null)
114 {
115 try
116 {
117
118 Parameters params = getPerception().getGtu().getParameters();
119 this.reactionTime = params.getParameter(TR);
120 this.plannerTimeStep = params.getParameter(DT);
121 double rem;
122 if (this.reactionTime.eq0())
123 {
124 rem = 0;
125 }
126 else if (this.reactionTime.gt(this.plannerTimeStep))
127 {
128 rem = this.reactionTime.si % this.plannerTimeStep.si;
129 }
130 else
131 {
132 rem = this.plannerTimeStep.si - this.reactionTime.si;
133 }
134 this.remainder = Duration.instantiateSI(rem);
135 }
136 catch (ParameterException | GTUException exception)
137 {
138 throw new RuntimeException("Exception while setting up delayed neighors perception.", exception);
139 }
140 }
141
142
143 Time now = getPerception().getGtu().getSimulator().getSimulatorTime();
144 if (this.initialTime == null)
145 {
146 this.initialTime = now;
147 }
148 if (now.minus(this.initialTime).le(this.reactionTime))
149 {
150 updateAllDelayed();
151 return;
152 }
153
154 if (this.remainder.eq0())
155 {
156
157 updateAllDelayed();
158 }
159 else
160 {
161
162 Time scheduledTime = now.plus(this.remainder);
163 try
164 {
165 getPerception().getGtu().getSimulator().scheduleEventAbs(scheduledTime, this, this, "updateAllDelayed", null);
166 }
167 catch (SimRuntimeException exception)
168 {
169 throw new RuntimeException("Scheduling perception update in the past.", exception);
170 }
171 }
172
173
174
175
176
177
178
179
180 if (getPerception().getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
181 {
182 this.gtuAlongsideLeftOverride = newFirstLeaderOrFollower(getFollowers(RelativeLane.LEFT),
183 this.direct.getFirstFollowers(LateralDirectionality.LEFT))
184 || newFirstLeaderOrFollower(getLeaders(RelativeLane.LEFT),
185 this.direct.getFirstLeaders(LateralDirectionality.LEFT))
186 || this.direct.isGtuAlongside(LateralDirectionality.LEFT);
187 }
188 if (getPerception().getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT))
189 {
190 this.gtuAlongsideRightOverride = newFirstLeaderOrFollower(getFollowers(RelativeLane.RIGHT),
191 this.direct.getFirstFollowers(LateralDirectionality.RIGHT))
192 || newFirstLeaderOrFollower(getLeaders(RelativeLane.RIGHT),
193 this.direct.getFirstLeaders(LateralDirectionality.RIGHT))
194 || this.direct.isGtuAlongside(LateralDirectionality.RIGHT);
195 }
196
197 }
198
199
200
201
202
203
204
205 private boolean newFirstLeaderOrFollower(final Iterable<? extends HeadwayGTU> delayedSet,
206 final Set<? extends HeadwayGTU> currentSet)
207 {
208 Set<String> set = new LinkedHashSet<>();
209 for (HeadwayGTU gtu : delayedSet)
210 {
211 set.add(gtu.getId());
212 }
213 for (HeadwayGTU gtu : currentSet)
214 {
215 if (!set.contains(gtu.getId()) && gtu.getDistance().si < 50)
216 {
217 return true;
218 }
219 }
220 return false;
221 }
222
223
224
225
226
227
228 public final boolean isGtuAlongsideOverride(final LateralDirectionality lat)
229 {
230 return lat.isLeft() ? this.gtuAlongsideLeftOverride : this.gtuAlongsideRightOverride;
231 }
232
233
234
235
236
237
238
239
240 protected void updateAllDelayed() throws GTUException, NetworkException, ParameterException
241 {
242
243 try
244 {
245 getGtu().getReferencePosition();
246 }
247 catch (GTUException exception)
248 {
249
250 return;
251 }
252
253 this.direct.updateAll();
254
255 setInfo(CROSSSECTION,
256 new TimeStampedObject<>(getPerception().getLaneStructure().getExtendedCrossSection(), getTimestamp()));
257 setInfo(ODOMETER, new TimeStampedObject<>(getGtu().getOdometer(), getTimestamp()));
258 }
259
260
261
262
263
264 public final SortedSet<RelativeLane> getDelayedCrossSection()
265 {
266 try
267 {
268 return getInfo(CROSSSECTION).getObject();
269 }
270 catch (PerceptionException exception)
271 {
272 throw new RuntimeException("Crosssection was not perceived.", exception);
273 }
274 }
275
276
277
278
279
280 public static final class NeighborsInfoType<T> extends DelayedInfoType<T>
281 {
282
283
284 private static final Map<String, NeighborsInfoType<?>> LANEINFOTYPES = new LinkedHashMap<>();
285
286
287
288
289
290 public NeighborsInfoType(final String id)
291 {
292 super(id, TR);
293 }
294
295
296
297
298
299
300 @SuppressWarnings("unchecked")
301 public static NeighborsInfoType<SortedSet<HeadwayGTU>> getSortedSetType(final String id)
302 {
303 if (!LANEINFOTYPES.containsKey(id))
304 {
305 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
306 }
307 return (NeighborsInfoType<SortedSet<HeadwayGTU>>) LANEINFOTYPES.get(id);
308 }
309
310
311
312
313
314
315 @SuppressWarnings("unchecked")
316 public static NeighborsInfoType<PerceptionCollectable<HeadwayGTU, LaneBasedGTU>> getIterableType(final String id)
317 {
318 if (!LANEINFOTYPES.containsKey(id))
319 {
320 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
321 }
322 return (NeighborsInfoType<PerceptionCollectable<HeadwayGTU, LaneBasedGTU>>) LANEINFOTYPES.get(id);
323 }
324
325
326
327
328
329
330 @SuppressWarnings("unchecked")
331 public static NeighborsInfoType<Boolean> getBooleanType(final String id)
332 {
333 if (!LANEINFOTYPES.containsKey(id))
334 {
335 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
336 }
337 return (NeighborsInfoType<Boolean>) LANEINFOTYPES.get(id);
338 }
339
340
341 @Override
342 public String toString()
343 {
344 return "NeighborsInfoType []";
345 }
346
347 }
348
349 }