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