1 package org.opentrafficsim.road.gtu.generator;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import nl.tudelft.simulation.dsol.SimRuntimeException;
9
10 import org.djunits.unit.LengthUnit;
11 import org.djunits.unit.SpeedUnit;
12 import org.djunits.unit.TimeUnit;
13 import org.djunits.value.vdouble.scalar.Length;
14 import org.djunits.value.vdouble.scalar.Speed;
15 import org.djunits.value.vdouble.scalar.Time;
16 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
17 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
18 import org.opentrafficsim.core.gtu.GTUDirectionality;
19 import org.opentrafficsim.core.gtu.GTUException;
20 import org.opentrafficsim.core.gtu.GTUType;
21 import org.opentrafficsim.core.gtu.RelativePosition;
22 import org.opentrafficsim.core.gtu.animation.GTUColorer;
23 import org.opentrafficsim.core.network.NetworkException;
24 import org.opentrafficsim.core.network.OTSNetwork;
25 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
26 import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
27 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
28 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
29 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU.LaneBasedIndividualCarBuilder;
30 import org.opentrafficsim.road.gtu.lane.perception.LanePerceptionFull;
31 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
32 import org.opentrafficsim.road.gtu.lane.tactical.following.HeadwayGTU;
33 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
34 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
35 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
36 import org.opentrafficsim.road.network.lane.Lane;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public abstract class AbstractGTUGenerator
54 {
55
56 private final String name;
57
58
59 private final GTUType gtuType;
60
61
62 private final Class<?> gtuClass;
63
64
65 private final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist;
66
67
68 private final ContinuousDistDoubleScalar.Rel<Time.Rel, TimeUnit> interarrivelTimeDist;
69
70
71 private long generatedGTUs = 0;
72
73
74 private final long maxGTUs;
75
76
77 private final Time.Abs startTime;
78
79
80 private final Time.Abs endTime;
81
82
83 private final Lane lane;
84
85
86 private final Length.Rel position;
87
88
89 private final GTUDirectionality direction;
90
91
92 private final GTUColorer gtuColorer;
93
94
95 private final LaneBasedStrategicalPlanner strategicalPlanner;
96
97
98 private final Class<LanePerceptionFull> perceptionClass;
99
100
101 private final OTSNetwork network;
102
103
104 private List<LaneBasedIndividualCarBuilder> carBuilderList = new ArrayList<>();
105
106
107 @SuppressWarnings("checkstyle:visibilitymodifier")
108 protected long numberGTUs = 0;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 @SuppressWarnings("checkstyle:parameternumber")
130 public AbstractGTUGenerator(final String name, final OTSDEVSSimulatorInterface simulator, final GTUType gtuType,
131 final Class<?> gtuClass, final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist,
132 final ContinuousDistDoubleScalar.Rel<Time.Rel, TimeUnit> interarrivelTimeDist, final long maxGTUs,
133 final Time.Abs startTime, final Time.Abs endTime, final Lane lane, final Length.Rel position,
134 final GTUDirectionality direction, final GTUColorer gtuColorer,
135 final LaneBasedStrategicalPlanner strategicalPlanner, final Class<LanePerceptionFull> perceptionClass,
136 final OTSNetwork network) throws SimRuntimeException
137 {
138 super();
139 this.name = name;
140 this.gtuType = gtuType;
141 this.gtuClass = gtuClass;
142 this.initialSpeedDist = initialSpeedDist;
143 this.interarrivelTimeDist = interarrivelTimeDist;
144 this.maxGTUs = maxGTUs;
145 this.startTime = startTime;
146 this.endTime = endTime;
147 this.lane = lane;
148 this.position = position;
149 this.direction = direction;
150 this.gtuColorer = gtuColorer;
151 this.strategicalPlanner = strategicalPlanner;
152 this.perceptionClass = perceptionClass;
153 this.network = network;
154
155 simulator.scheduleEventAbs(startTime, this, this, "generate", null);
156 }
157
158
159
160
161
162 protected final void generate() throws Exception
163 {
164
165 if (getSimulator().getSimulatorTime().getTime().gt(this.endTime))
166 {
167 return;
168 }
169
170
171 if (this.generatedGTUs >= this.maxGTUs)
172 {
173 return;
174 }
175
176
177 this.numberGTUs++;
178 String id = this.name + ":" + this.numberGTUs;
179
180
181 if (LaneBasedIndividualGTU.class.isAssignableFrom(getGtuClass()))
182 {
183 LaneBasedIndividualCarBuilder carBuilder = new LaneBasedIndividualCarBuilder();
184 carBuilder.setId(id);
185 carBuilder.setGtuType(getGtuType());
186 Length.Rel carLength = getLengthDist().draw();
187 carBuilder.setLength(carLength);
188 carBuilder.setWidth(getWidthDist().draw());
189 carBuilder.setMaximumVelocity(getMaximumSpeedDist().draw());
190 carBuilder.setInitialSpeed(getInitialSpeedDist().draw());
191 carBuilder.setSimulator(getSimulator());
192 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
193 initialLongitudinalPositions.add(new DirectedLanePosition(this.lane, this.position, this.direction));
194 carBuilder.setInitialLongitudinalPositions(initialLongitudinalPositions);
195 carBuilder.setAnimationClass(DefaultCarAnimation.class);
196 carBuilder.setStrategicalPlanner(getStrategicalPlanner());
197 carBuilder.setPerception(this.perceptionClass.newInstance());
198 carBuilder.setGtuColorer(this.gtuColorer);
199 carBuilder.setNetwork(this.network);
200 this.generatedGTUs++;
201
202 if (enoughSpace(carBuilder))
203 {
204 carBuilder.build();
205 }
206 else
207 {
208
209 this.carBuilderList.add(carBuilder);
210
211 if (this.carBuilderList.size() == 1)
212 {
213
214 getSimulator().scheduleEventRel(new Time.Rel(0.1, TimeUnit.SECOND), this, this,
215 "checkCarBuilderList", null);
216 }
217 }
218 }
219 else
220 {
221 throw new GTUException("GTU class " + getGtuClass().getName() + ": cannot instantiate, no builder.");
222 }
223
224
225 OTSSimTimeDouble nextTime = getSimulator().getSimulatorTime().plus(this.interarrivelTimeDist.draw());
226 if (nextTime.get().le(this.endTime))
227 {
228 getSimulator().scheduleEventAbs(nextTime, this, this, "generate", null);
229 }
230 }
231
232
233
234
235
236
237
238
239
240 protected final boolean enoughSpace(final LaneBasedIndividualCarBuilder carBuilder) throws NetworkException,
241 GTUException
242 {
243 DirectedLanePosition directedLanePosition = carBuilder.getInitialLongitudinalPositions().iterator().next();
244 Lane generatorLane = directedLanePosition.getLane();
245 double genPosSI = directedLanePosition.getPosition().getSI();
246 GTUDirectionality direction = directedLanePosition.getGtuDirection();
247
248 double lengthSI = generatorLane.getLength().getSI();
249 double frontNew = (genPosSI + carBuilder.getLength().getSI()) / lengthSI;
250 double rearNew = genPosSI / lengthSI;
251
252
253 for (LaneBasedGTU gtu : generatorLane.getGtuList())
254 {
255 double frontGTU = gtu.fractionalPosition(generatorLane, gtu.getFront());
256 double rearGTU = gtu.fractionalPosition(generatorLane, gtu.getRear());
257 if ((frontNew >= rearGTU && frontNew <= frontGTU) || (rearNew >= rearGTU && rearNew <= frontGTU)
258 || (frontGTU >= rearNew && frontGTU <= frontNew) || (rearGTU >= rearNew && rearGTU <= frontNew))
259 {
260
261 return false;
262 }
263 }
264
265
266 GTUFollowingModelOld followingModel = new IDMPlusOld();
267
268
269 HeadwayGTU headwayGTU = headway(new Length.Rel(250.0, LengthUnit.METER), generatorLane);
270 Length.Rel minimumHeadway = new Length.Rel(0.0, LengthUnit.METER);
271 if (headwayGTU.getGtuId() != null)
272 {
273 minimumHeadway =
274 followingModel.minimumHeadway(carBuilder.getInitialSpeed(), headwayGTU.getGtuSpeed(), new Length.Rel(
275 1.0, LengthUnit.CENTIMETER), new Length.Rel(250.0, LengthUnit.METER), generatorLane
276 .getSpeedLimit(carBuilder.getGtuType()), carBuilder.getMaximumVelocity());
277 double acc =
278 followingModel.computeAcceleration(carBuilder.getInitialSpeed(), carBuilder.getMaximumVelocity(),
279 headwayGTU.getGtuSpeed(), minimumHeadway, carBuilder.getMaximumVelocity()).getSI();
280 if (acc < 0)
281 {
282 System.err.println(getSimulator().getSimulatorTime() + ", generator headway for GTU "
283 + headwayGTU.getGtuId() + ", distance " + headwayGTU.getDistance().si + " m, max " + minimumHeadway
284 + ", has to brake with a=" + acc + " m/s^2");
285 return false;
286 }
287 }
288
289
290
291 return headwayGTU.getDistance().ge(minimumHeadway);
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306 private HeadwayGTU headwayRecursiveForwardSI(final Lane theLane, final double lanePositionSI,
307 final double cumDistanceSI, final double maxDistanceSI, final Time.Abs when) throws GTUException
308 {
309
310 LaneBasedGTU otherGTU =
311 theLane.getGtuAhead(new Length.Rel(lanePositionSI, LengthUnit.METER), GTUDirectionality.DIR_PLUS,
312 RelativePosition.REAR, when);
313 if (otherGTU != null)
314 {
315 double distanceM =
316 cumDistanceSI + otherGTU.position(theLane, otherGTU.getRear(), when).getSI() - lanePositionSI;
317 if (distanceM > 0 && distanceM <= maxDistanceSI)
318 {
319 return new HeadwayGTU(otherGTU.getId(), otherGTU.getVelocity(), distanceM, otherGTU.getGTUType());
320 }
321 return new HeadwayGTU(null, null, Double.MAX_VALUE, null);
322 }
323
324
325 if (cumDistanceSI + theLane.getLength().getSI() - lanePositionSI < maxDistanceSI)
326 {
327
328 if (theLane.nextLanes(this.gtuType).size() > 0)
329 {
330 HeadwayGTU foundMaxGTUDistanceSI = new HeadwayGTU(null, null, Double.MAX_VALUE, null);
331 for (Lane nextLane : theLane.nextLanes(this.gtuType).keySet())
332 {
333
334
335
336
337 {
338 double traveledDistanceSI = cumDistanceSI + theLane.getLength().getSI() - lanePositionSI;
339 HeadwayGTU closest =
340 headwayRecursiveForwardSI(nextLane, 0.0, traveledDistanceSI, maxDistanceSI, when);
341 if (closest.getDistance().si < maxDistanceSI
342 && closest.getDistance().si < foundMaxGTUDistanceSI.getDistance().si)
343 {
344 foundMaxGTUDistanceSI = closest;
345 }
346 }
347 }
348 return foundMaxGTUDistanceSI;
349 }
350 }
351
352
353 return new HeadwayGTU(null, null, Double.MAX_VALUE, null);
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
368 private HeadwayGTU headwayRecursiveBackwardSI(final Lane theLane, final double lanePositionSI,
369 final double cumDistanceSI, final double maxDistanceSI, final Time.Abs when) throws GTUException
370 {
371
372 LaneBasedGTU otherGTU =
373 theLane.getGtuBehind(new Length.Rel(lanePositionSI, LengthUnit.METER), GTUDirectionality.DIR_PLUS,
374 RelativePosition.FRONT, when);
375 if (otherGTU != null)
376 {
377 double distanceM =
378 cumDistanceSI + otherGTU.position(theLane, otherGTU.getFront(), when).getSI() - lanePositionSI;
379 if (distanceM > 0 && distanceM <= maxDistanceSI)
380 {
381 return new HeadwayGTU(otherGTU.getId(), otherGTU.getVelocity(), distanceM, otherGTU.getGTUType());
382 }
383 return new HeadwayGTU(null, null, Double.MAX_VALUE, null);
384 }
385
386
387 if (cumDistanceSI + theLane.getLength().getSI() - lanePositionSI < maxDistanceSI)
388 {
389
390 if (theLane.prevLanes(this.gtuType).size() > 0)
391 {
392 HeadwayGTU foundMaxGTUDistanceSI = new HeadwayGTU(null, null, Double.MAX_VALUE, null);
393 for (Lane prevLane : theLane.prevLanes(this.gtuType).keySet())
394 {
395
396
397
398
399 {
400 double traveledDistanceSI = cumDistanceSI + theLane.getLength().getSI() - lanePositionSI;
401 HeadwayGTU closest =
402 headwayRecursiveBackwardSI(prevLane, 0.0, traveledDistanceSI, maxDistanceSI, when);
403 if (closest.getDistance().si < maxDistanceSI
404 && closest.getDistance().si < foundMaxGTUDistanceSI.getDistance().si)
405 {
406 foundMaxGTUDistanceSI = closest;
407 }
408 }
409 }
410 return foundMaxGTUDistanceSI;
411 }
412 }
413
414
415 return new HeadwayGTU(null, null, Double.MAX_VALUE, null);
416 }
417
418
419
420
421
422
423
424
425
426 private HeadwayGTU headwayGTUSIForward(final double maxDistanceSI, final Lane generatorLane) throws GTUException
427 {
428 Time.Abs when = getSimulator().getSimulatorTime().getTime();
429 HeadwayGTU foundMaxGTUDistanceSI = new HeadwayGTU(null, null, Double.MAX_VALUE, null);
430
431 HeadwayGTU closest;
432 if (this.direction.equals(GTUDirectionality.DIR_PLUS))
433 {
434 closest = headwayRecursiveForwardSI(this.lane, generatorLane.getLength().getSI(), 0.0, maxDistanceSI, when);
435 }
436 else
437 {
438 closest =
439 headwayRecursiveBackwardSI(this.lane, generatorLane.getLength().getSI(), 0.0, maxDistanceSI, when);
440 }
441 if (closest.getDistance().si < maxDistanceSI && closest.getDistance().si < foundMaxGTUDistanceSI.getDistance().si)
442 {
443 foundMaxGTUDistanceSI = closest;
444 }
445 return foundMaxGTUDistanceSI;
446 }
447
448
449
450
451
452
453
454
455 public final HeadwayGTU headway(final Length.Rel maxDistance, final Lane generatorLane) throws GTUException
456 {
457 return headwayGTUSIForward(maxDistance.getSI(), generatorLane);
458 }
459
460
461
462
463
464 protected final void checkCarBuilderList() throws Exception
465 {
466 if (!this.carBuilderList.isEmpty())
467 {
468 LaneBasedIndividualCarBuilder carBuilder = this.carBuilderList.get(0);
469 if (enoughSpace(carBuilder))
470 {
471 this.carBuilderList.remove(0);
472 carBuilder.build();
473 }
474 }
475
476
477 if (!this.carBuilderList.isEmpty())
478 {
479 getSimulator()
480 .scheduleEventRel(new Time.Rel(0.1, TimeUnit.SECOND), this, this, "checkCarBuilderList", null);
481 }
482 }
483
484
485 public abstract OTSDEVSSimulatorInterface getSimulator();
486
487
488 public abstract ContinuousDistDoubleScalar.Rel<Length.Rel, LengthUnit> getLengthDist();
489
490
491 public abstract ContinuousDistDoubleScalar.Rel<Length.Rel, LengthUnit> getWidthDist();
492
493
494 public abstract ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> getMaximumSpeedDist();
495
496
497
498
499 public final String getName()
500 {
501 return this.name;
502 }
503
504
505
506
507 public final GTUType getGtuType()
508 {
509 return this.gtuType;
510 }
511
512
513
514
515 public final Class<?> getGtuClass()
516 {
517 return this.gtuClass;
518 }
519
520
521
522
523 public final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> getInitialSpeedDist()
524 {
525 return this.initialSpeedDist;
526 }
527
528
529
530
531 public final ContinuousDistDoubleScalar.Rel<Time.Rel, TimeUnit> getInterarrivelTimeDist()
532 {
533 return this.interarrivelTimeDist;
534 }
535
536
537
538
539 public final long getMaxGTUs()
540 {
541 return this.maxGTUs;
542 }
543
544
545
546
547 public final Time.Abs getStartTime()
548 {
549 return this.startTime;
550 }
551
552
553
554
555 public final Time.Abs getEndTime()
556 {
557 return this.endTime;
558 }
559
560
561
562
563 public final GTUColorer getGtuColorer()
564 {
565 return this.gtuColorer;
566 }
567
568
569
570
571 public final LaneBasedStrategicalPlanner getStrategicalPlanner()
572 {
573 return this.strategicalPlanner;
574 }
575
576
577
578
579 public final Class<LanePerceptionFull> getPerceptionClass()
580 {
581 return this.perceptionClass;
582 }
583
584 }