1 package org.opentrafficsim.demo.conflict;
2
3 import java.net.URL;
4 import java.rmi.RemoteException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import javax.naming.NamingException;
13 import javax.swing.SwingUtilities;
14
15 import org.djunits.unit.AccelerationUnit;
16 import org.djunits.unit.DurationUnit;
17 import org.djunits.unit.FrequencyUnit;
18 import org.djunits.unit.LengthUnit;
19 import org.djunits.unit.SpeedUnit;
20 import org.djunits.value.vdouble.scalar.Acceleration;
21 import org.djunits.value.vdouble.scalar.Duration;
22 import org.djunits.value.vdouble.scalar.Frequency;
23 import org.djunits.value.vdouble.scalar.Length;
24 import org.djunits.value.vdouble.scalar.Speed;
25 import org.djunits.value.vdouble.scalar.Time;
26 import org.opentrafficsim.base.modelproperties.Property;
27 import org.opentrafficsim.base.modelproperties.PropertyException;
28 import org.opentrafficsim.base.parameters.ParameterException;
29 import org.opentrafficsim.base.parameters.ParameterSet;
30 import org.opentrafficsim.base.parameters.ParameterTypes;
31 import org.opentrafficsim.base.parameters.Parameters;
32 import org.opentrafficsim.core.distributions.Generator;
33 import org.opentrafficsim.core.distributions.ProbabilityException;
34 import org.opentrafficsim.core.dsol.OTSModelInterface;
35 import org.opentrafficsim.core.gtu.GTUCharacteristics;
36 import org.opentrafficsim.core.gtu.GTUDirectionality;
37 import org.opentrafficsim.core.gtu.GTUException;
38 import org.opentrafficsim.core.gtu.GTUType;
39 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterFactory;
40 import org.opentrafficsim.core.idgenerator.IdGenerator;
41 import org.opentrafficsim.core.network.Node;
42 import org.opentrafficsim.core.network.OTSLink;
43 import org.opentrafficsim.core.network.OTSNetwork;
44 import org.opentrafficsim.core.network.OTSNode;
45 import org.opentrafficsim.core.network.route.Route;
46 import org.opentrafficsim.road.animation.AnimationToggles;
47 import org.opentrafficsim.road.gtu.generator.GeneratorPositions;
48 import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator;
49 import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
50 import org.opentrafficsim.road.gtu.generator.TTCRoomChecker;
51 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
52 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristicsGenerator;
53 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
54 import org.opentrafficsim.road.gtu.lane.VehicleModel;
55 import org.opentrafficsim.road.gtu.lane.perception.categories.DirectBusStopPerception;
56 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
57 import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractIDM;
58 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlus;
59 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.AccelerationBusStop;
60 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
61 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveBusStop;
62 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRS;
63 import org.opentrafficsim.road.gtu.lane.tactical.pt.BusSchedule;
64 import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil;
65 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Cooperation;
66 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.GapAcceptance;
67 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
68 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Synchronization;
69 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Tailgating;
70 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
71 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
72 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
73 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
74 import org.opentrafficsim.road.network.lane.CrossSectionLink;
75 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
76 import org.opentrafficsim.road.network.lane.Lane;
77 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
78 import org.opentrafficsim.road.network.lane.object.BusStop;
79 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
80 import org.opentrafficsim.simulationengine.OTSSimulationException;
81
82 import nl.tudelft.simulation.dsol.SimRuntimeException;
83 import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
84 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
85 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
86 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
87 import nl.tudelft.simulation.jstats.streams.StreamInterface;
88 import nl.tudelft.simulation.language.io.URLResource;
89
90
91
92
93
94
95
96
97
98
99
100 public class BusStreetDemo extends AbstractWrappableAnimation
101 {
102
103
104 private static final long serialVersionUID = 20161211L;
105
106
107 @Override
108 protected final OTSModelInterface makeModel() throws OTSSimulationException
109 {
110 return new BusStreetModel();
111 }
112
113
114 @Override
115 protected final void addAnimationToggles()
116 {
117 AnimationToggles.setIconAnimationTogglesFull(this);
118 hideAnimationClass(OTSLink.class);
119 hideAnimationClass(OTSNode.class);
120 }
121
122
123 @Override
124 public final String shortName()
125 {
126 return "Bus street demonstration";
127 }
128
129
130 @Override
131 public final String description()
132 {
133 return "Bus street demonstration";
134 }
135
136
137
138
139 class BusStreetModel implements OTSModelInterface
140 {
141
142
143 private static final long serialVersionUID = 20161211L;
144
145
146 private OTSNetwork network;
147
148
149 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
150
151
152 @Override
153 public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0) throws SimRuntimeException
154 {
155 this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) arg0;
156 Map<String, StreamInterface> streams = new HashMap<>();
157 streams.put("generation", new MersenneTwister(100L));
158 this.simulator.getReplication().setStreams(streams);
159 try
160 {
161 URL url = URLResource.getResource("/conflict/BusStreet.xml");
162 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
163 this.network = nlp.build(url, false);
164 ConflictBuilder.buildConflicts(this.network, GTUType.VEHICLE, this.simulator,
165 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
166
167
168 Lane lane = ((CrossSectionLink) this.network.getLink("B1B2")).getLanes().get(0);
169 BusStop stop = new BusStop("Cafe Boszicht.1", lane, lane.getLength(), "Cafe Boszicht", this.simulator);
170 Set<String> lines1 = new HashSet<>();
171 lines1.add("1");
172 stop.setLines(lines1);
173
174 lane = ((CrossSectionLink) this.network.getLink("C1C2")).getLanes().get(0);
175 stop = new BusStop("Cafe Boszicht.2", lane, lane.getLength(), "Cafe Boszicht", this.simulator);
176 Set<String> lines2 = new HashSet<>();
177 lines2.add("2");
178 stop.setLines(lines2);
179
180 lane = ((CrossSectionLink) this.network.getLink("EF")).getLanes().get(0);
181 stop = new BusStop("Herberg De Deugd", lane, new Length(75.0, LengthUnit.SI), "Herberg De Deugd",
182 this.simulator);
183 stop.setLines(lines1);
184
185 lane = ((CrossSectionLink) this.network.getLink("FG")).getLanes().get(1);
186 stop = new BusStop("De Vleeshoeve", lane, new Length(75.0, LengthUnit.SI), "De Vleeshoeve", this.simulator);
187 Set<String> lines12 = new HashSet<>();
188 lines12.add("1");
189 lines12.add("2");
190 stop.setLines(lines12);
191
192 lane = ((CrossSectionLink) this.network.getLink("GH")).getLanes().get(2);
193 stop = new BusStop("Kippenboerderij De Scharrelaar", lane, new Length(50.0, LengthUnit.SI),
194 "Kippenboerderij De Scharrelaar", this.simulator);
195 stop.setLines(lines2);
196
197 lane = ((CrossSectionLink) this.network.getLink("I1I2")).getLanes().get(0);
198 stop = new BusStop("Dorpshuys", lane, lane.getLength(), "Dorpshuys", this.simulator);
199 stop.setLines(lines1);
200
201 lane = ((CrossSectionLink) this.network.getLink("K1K2")).getLanes().get(0);
202 stop = new BusStop("De verkeerde afslag", lane, lane.getLength(), "De verkeerde afslag", this.simulator);
203 stop.setLines(lines12);
204
205 makeGenerator(streams.get("generation"));
206
207 }
208 catch (Exception exception)
209 {
210 exception.printStackTrace();
211 }
212 }
213
214
215 @Override
216 public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
217 {
218 return this.simulator;
219 }
220
221
222 @Override
223 public OTSNetwork getNetwork()
224 {
225 return this.network;
226 }
227
228
229
230
231
232
233
234
235
236 private void makeGenerator(final StreamInterface stream)
237 throws GTUException, SimRuntimeException, ProbabilityException, ParameterException
238 {
239 Lane lane = ((CrossSectionLink) this.network.getLink("AB")).getLanes().get(0);
240 String id = lane.getId();
241 Set<DirectedLanePosition> initialLongitudinalPositions = new HashSet<>();
242 initialLongitudinalPositions
243 .add(new DirectedLanePosition(lane, new Length(10.0, LengthUnit.SI), GTUDirectionality.DIR_PLUS));
244 Generator<Duration> headwayGenerator =
245 new HeadwayGenerator(new Frequency(800, FrequencyUnit.PER_HOUR), this.simulator);
246 LaneBasedGTUCharacteristicsGenerator characteristicsGenerator =
247 new CharacteristicsGenerator(this.simulator, new double[] { 0.9, 0.06, 0.04 }, this.network);
248 RoomChecker roomChecker = new TTCRoomChecker(new Duration(10.0, DurationUnit.SI));
249 new LaneBasedGTUGenerator(id, headwayGenerator, getColorer(), characteristicsGenerator,
250 GeneratorPositions.create(initialLongitudinalPositions, stream), this.network, this.simulator, roomChecker,
251 new IdGenerator(""));
252 }
253
254 }
255
256
257
258
259
260
261 public static void main(final String[] args) throws SimRuntimeException
262 {
263 SwingUtilities.invokeLater(new Runnable()
264 {
265 @Override
266 public void run()
267 {
268 try
269 {
270 BusStreetDemo animation = new BusStreetDemo();
271
272 animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
273 new ArrayList<Property<?>>(), null, true);
274
275 }
276 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
277 {
278 exception.printStackTrace();
279 }
280 }
281 });
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295 private class HeadwayGenerator implements Generator<Duration>
296 {
297
298
299 private final Frequency demand;
300
301
302 private final SimulatorInterface.TimeDoubleUnit simulator;
303
304
305
306
307
308 HeadwayGenerator(final Frequency demand, final SimulatorInterface.TimeDoubleUnit simulator)
309 {
310 this.demand = demand;
311 this.simulator = simulator;
312 }
313
314
315 @Override
316 public Duration draw() throws ProbabilityException, ParameterException
317 {
318 return new Duration(
319 -Math.log(this.simulator.getReplication().getStream("generation").nextDouble()) / this.demand.si,
320 DurationUnit.SI);
321 }
322
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336 public class CharacteristicsGenerator implements LaneBasedGTUCharacteristicsGenerator
337 {
338
339
340 private final DEVSSimulatorInterface.TimeDoubleUnit simulator;
341
342
343 private final double[] probabilities;
344
345
346 private final LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> plannerFactory;
347
348
349 private final Route carRouteN;
350
351
352 private final Route carRouteO;
353
354
355 private final List<Node> busNodes1;
356
357
358 private final List<Node> busNodes2;
359
360
361 private final Duration shortDwellTime = new Duration(15.0, DurationUnit.SI);
362
363
364 private final Duration longDwellTime = new Duration(60.0, DurationUnit.SI);
365
366
367
368
369
370
371 public CharacteristicsGenerator(final DEVSSimulatorInterface.TimeDoubleUnit simulator, final double[] probabilities,
372 final OTSNetwork network)
373 {
374 this.simulator = simulator;
375 this.probabilities = probabilities;
376 List<Node> carNodesN = new ArrayList<>();
377 carNodesN.add(network.getNode("A"));
378 carNodesN.add(network.getNode("B"));
379 carNodesN.add(network.getNode("C"));
380 carNodesN.add(network.getNode("D"));
381 carNodesN.add(network.getNode("E"));
382 carNodesN.add(network.getNode("F"));
383 carNodesN.add(network.getNode("G"));
384 carNodesN.add(network.getNode("H"));
385 carNodesN.add(network.getNode("I"));
386 carNodesN.add(network.getNode("J"));
387 carNodesN.add(network.getNode("K"));
388 carNodesN.add(network.getNode("L"));
389 carNodesN.add(network.getNode("M"));
390 List<Node> carNodesO = new ArrayList<>(carNodesN);
391 carNodesN.add(network.getNode("N"));
392 carNodesO.add(network.getNode("O"));
393 this.carRouteN = new Route("carN", carNodesN);
394 this.carRouteO = new Route("carO", carNodesO);
395 this.busNodes1 = new ArrayList<>();
396 this.busNodes1.add(network.getNode("A"));
397 this.busNodes1.add(network.getNode("B"));
398 this.busNodes1.add(network.getNode("B1"));
399 this.busNodes1.add(network.getNode("B2"));
400 this.busNodes1.add(network.getNode("D"));
401 this.busNodes1.add(network.getNode("E"));
402 this.busNodes1.add(network.getNode("F"));
403 this.busNodes1.add(network.getNode("G"));
404 this.busNodes1.add(network.getNode("H"));
405 this.busNodes1.add(network.getNode("I"));
406 this.busNodes1.add(network.getNode("I1"));
407 this.busNodes1.add(network.getNode("I2"));
408 this.busNodes1.add(network.getNode("J"));
409 this.busNodes1.add(network.getNode("K"));
410 this.busNodes1.add(network.getNode("K1"));
411 this.busNodes1.add(network.getNode("K2"));
412 this.busNodes1.add(network.getNode("L"));
413 this.busNodes1.add(network.getNode("M"));
414 this.busNodes1.add(network.getNode("N"));
415
416 this.busNodes2 = new ArrayList<>();
417 this.busNodes2.add(network.getNode("A"));
418 this.busNodes2.add(network.getNode("B"));
419 this.busNodes2.add(network.getNode("C"));
420 this.busNodes2.add(network.getNode("C1"));
421 this.busNodes2.add(network.getNode("C2"));
422 this.busNodes2.add(network.getNode("E"));
423 this.busNodes2.add(network.getNode("F"));
424 this.busNodes2.add(network.getNode("G"));
425 this.busNodes2.add(network.getNode("H"));
426 this.busNodes2.add(network.getNode("I"));
427 this.busNodes2.add(network.getNode("J"));
428 this.busNodes2.add(network.getNode("K"));
429 this.busNodes2.add(network.getNode("K1"));
430 this.busNodes2.add(network.getNode("K2"));
431 this.busNodes2.add(network.getNode("K3"));
432 this.busNodes2.add(network.getNode("L"));
433 this.busNodes2.add(network.getNode("M"));
434 this.busNodes2.add(network.getNode("O"));
435
436 this.plannerFactory =
437 new LaneBasedStrategicalRoutePlannerFactory(new LMRSFactoryCarBus(), new ParameterFactoryCarBus());
438 }
439
440
441 @Override
442 public LaneBasedGTUCharacteristics draw() throws ProbabilityException, ParameterException, GTUException
443 {
444
445 double r = this.simulator.getReplication().getStream("generation").nextDouble();
446 int classNum = r < this.probabilities[0] ? 0 : r < this.probabilities[0] + this.probabilities[1] ? 1 : 2;
447 r = this.simulator.getReplication().getStream("generation").nextDouble();
448 GTUType gtuType;
449 Length length;
450 Length width;
451 Speed maximumSpeed;
452 Route route;
453 switch (classNum)
454 {
455 case 0:
456 {
457 gtuType = new GTUType("CAR", GTUType.CAR);
458 length = new Length(4.0, LengthUnit.SI);
459 width = new Length(1.8, LengthUnit.SI);
460 maximumSpeed = new Speed(200.0, SpeedUnit.KM_PER_HOUR);
461 route = r < 0.5 ? this.carRouteN : this.carRouteO;
462 break;
463 }
464 case 1:
465 {
466 gtuType = new GTUType("BUS1", GTUType.SCHEDULED_BUS);
467 length = new Length(8.0, LengthUnit.SI);
468 width = new Length(2.0, LengthUnit.SI);
469 maximumSpeed = new Speed(100.0, SpeedUnit.KM_PER_HOUR);
470 BusSchedule schedule = new BusSchedule("bus1." + this.simulator.getSimulatorTime(), this.busNodes1, "1");
471 Time now = this.simulator.getSimulatorTime();
472 schedule.addBusStop("Cafe Boszicht.1", now.plus(new Duration(70.0, DurationUnit.SI)), this.longDwellTime,
473 true);
474 schedule.addBusStop("Herberg De Deugd", now.plus(new Duration(100.0, DurationUnit.SI)), this.shortDwellTime,
475 false);
476 schedule.addBusStop("De Vleeshoeve", now.plus(new Duration(120.0, DurationUnit.SI)), this.shortDwellTime,
477 false);
478 schedule.addBusStop("Dorpshuys", now.plus(new Duration(200.0, DurationUnit.SI)), this.longDwellTime, true);
479 schedule.addBusStop("De verkeerde afslag", now.plus(new Duration(270.0, DurationUnit.SI)),
480 this.longDwellTime, true);
481 route = schedule;
482 break;
483 }
484 case 2:
485 {
486 gtuType = new GTUType("BUS2", GTUType.SCHEDULED_BUS);
487 length = new Length(12.0, LengthUnit.SI);
488 width = new Length(2.0, LengthUnit.SI);
489 maximumSpeed = new Speed(100.0, SpeedUnit.KM_PER_HOUR);
490 BusSchedule schedule = new BusSchedule("bus2." + this.simulator.getSimulatorTime(), this.busNodes2, "2");
491 Time now = this.simulator.getSimulatorTime();
492 schedule.addBusStop("Cafe Boszicht.2", now.plus(new Duration(80.0, DurationUnit.SI)), this.longDwellTime,
493 true);
494 schedule.addBusStop("De Vleeshoeve", now.plus(new Duration(110.0, DurationUnit.SI)), this.shortDwellTime,
495 false);
496 schedule.addBusStop("Kippenboerderij De Scharrelaar", now.plus(new Duration(180.0, DurationUnit.SI)),
497 this.longDwellTime, false);
498 schedule.addBusStop("De verkeerde afslag", now.plus(new Duration(260.0, DurationUnit.SI)),
499 this.longDwellTime, true);
500 route = schedule;
501 break;
502 }
503 default:
504 throw new RuntimeException("Reaching default of switch case.");
505 }
506
507 GTUCharacteristics gtuCharacteristics = new GTUCharacteristics(gtuType, length, width, maximumSpeed,
508 Acceleration.createSI(3.0), Acceleration.createSI(-8.0), length.multiplyBy(0.5));
509
510 return new LaneBasedGTUCharacteristics(gtuCharacteristics, this.plannerFactory, route, null, null,
511 VehicleModel.MINMAX);
512 }
513
514 }
515
516
517
518
519
520
521
522
523
524
525
526
527 private class LMRSFactoryCarBus implements LaneBasedTacticalPlannerFactory<LMRS>
528 {
529
530
531 LMRSFactoryCarBus()
532 {
533 }
534
535
536 @Override
537 public final Parameters getParameters()
538 {
539 ParameterSet parameters = new ParameterSet();
540 parameters.setDefaultParameters(ParameterTypes.class);
541 parameters.setDefaultParameters(LmrsParameters.class);
542 parameters.setDefaultParameters(ConflictUtil.class);
543 parameters.setDefaultParameters(AbstractIDM.class);
544 return parameters;
545 }
546
547
548 @Override
549 public final LMRS create(final LaneBasedGTU gtu) throws GTUException
550 {
551 DefaultLMRSPerceptionFactory pFac = new DefaultLMRSPerceptionFactory();
552 LMRS lmrs = new LMRS(new IDMPlus(), gtu, pFac.generatePerception(gtu), Synchronization.PASSIVE, Cooperation.PASSIVE,
553 GapAcceptance.INFORMED, Tailgating.NONE);
554 lmrs.setDefaultIncentives();
555 if (gtu.getGTUType().isOfType(GTUType.SCHEDULED_BUS))
556 {
557 lmrs.addMandatoryIncentive(new IncentiveBusStop());
558 lmrs.addAccelerationIncentive(new AccelerationBusStop());
559 lmrs.getPerception().addPerceptionCategory(new DirectBusStopPerception(lmrs.getPerception()));
560 }
561 return lmrs;
562 }
563
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577 private class ParameterFactoryCarBus implements ParameterFactory
578 {
579
580
581 ParameterFactoryCarBus()
582 {
583 }
584
585
586 @Override
587 public void setValues(final Parameters parameters, final GTUType gtuType) throws ParameterException
588 {
589
590 parameters.setParameter(ParameterTypes.LOOKAHEAD, new Length(100.0, LengthUnit.METER));
591 if (gtuType.isOfType(GTUType.CAR))
592 {
593 parameters.setParameter(LmrsParameters.VGAIN, new Speed(3.0, SpeedUnit.METER_PER_SECOND));
594 }
595 else if (gtuType.isOfType(GTUType.SCHEDULED_BUS))
596 {
597 parameters.setParameter(ParameterTypes.A, new Acceleration(0.8, AccelerationUnit.METER_PER_SECOND_2));
598 }
599 else
600 {
601 throw new RuntimeException("Unable to determine characteristics for GTU of type " + gtuType);
602 }
603
604 }
605
606 }
607
608 }