1 package org.opentrafficsim.demo.carFollowing;
2
3 import static org.opentrafficsim.core.gtu.GTUType.CAR;
4
5 import java.awt.Frame;
6 import java.rmi.RemoteException;
7 import java.util.ArrayList;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Random;
11 import java.util.Set;
12
13 import javax.naming.NamingException;
14 import javax.swing.SwingUtilities;
15
16 import org.djunits.unit.TimeUnit;
17 import org.djunits.unit.UNITS;
18 import org.djunits.value.vdouble.scalar.Acceleration;
19 import org.djunits.value.vdouble.scalar.Duration;
20 import org.djunits.value.vdouble.scalar.Length;
21 import org.djunits.value.vdouble.scalar.Speed;
22 import org.djunits.value.vdouble.scalar.Time;
23 import org.opentrafficsim.base.modelproperties.ProbabilityDistributionProperty;
24 import org.opentrafficsim.base.modelproperties.Property;
25 import org.opentrafficsim.base.modelproperties.PropertyException;
26 import org.opentrafficsim.base.modelproperties.SelectionProperty;
27 import org.opentrafficsim.base.parameters.Parameters;
28 import org.opentrafficsim.core.compatibility.Compatible;
29 import org.opentrafficsim.core.dsol.OTSModelInterface;
30 import org.opentrafficsim.core.geometry.OTSGeometryException;
31 import org.opentrafficsim.core.geometry.OTSPoint3D;
32 import org.opentrafficsim.core.gtu.GTUDirectionality;
33 import org.opentrafficsim.core.gtu.GTUException;
34 import org.opentrafficsim.core.gtu.GTUType;
35 import org.opentrafficsim.core.network.NetworkException;
36 import org.opentrafficsim.core.network.OTSNetwork;
37 import org.opentrafficsim.core.network.OTSNode;
38 import org.opentrafficsim.graphs.FundamentalDiagramLane;
39 import org.opentrafficsim.road.animation.AnimationToggles;
40 import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
41 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
42 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner;
43 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
44 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld;
45 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
46 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
47 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
48 import org.opentrafficsim.road.network.factory.LaneFactory;
49 import org.opentrafficsim.road.network.lane.CrossSectionLink;
50 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
51 import org.opentrafficsim.road.network.lane.Lane;
52 import org.opentrafficsim.road.network.lane.LaneType;
53 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
54 import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
55 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
56 import org.opentrafficsim.simulationengine.OTSSimulationException;
57 import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
58
59 import nl.tudelft.simulation.dsol.SimRuntimeException;
60 import nl.tudelft.simulation.dsol.gui.swing.TablePanel;
61 import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
62 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
63 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
64
65
66
67
68
69
70
71
72
73
74
75 public class FundamentalDiagramsLane extends AbstractWrappableAnimation implements UNITS
76 {
77
78 private static final long serialVersionUID = 1L;
79
80
81 private FundamentalDiagramLanePlotsModel model;
82
83
84 public FundamentalDiagramsLane()
85 {
86 try
87 {
88 this.properties.add(new SelectionProperty("CarFollowingModel", "Car following model",
89 "<html>The car following model determines "
90 + "the acceleration that a vehicle will make taking into account nearby vehicles, "
91 + "infrastructural restrictions (e.g. speed limit, curvature of the road) "
92 + "capabilities of the vehicle and personality of the driver.</html>",
93 new String[] { "IDM", "IDM+" }, 1, false, 500));
94 this.properties.add(new ProbabilityDistributionProperty("TrafficComposition", "Traffic composition",
95 "<html>Mix of passenger cars and trucks</html>", new String[] { "passenger car", "truck" },
96 new Double[] { 0.8, 0.2 }, false, 10));
97 }
98 catch (PropertyException exception)
99 {
100 exception.printStackTrace();
101 }
102 }
103
104
105 @Override
106 public final void stopTimersThreads()
107 {
108 super.stopTimersThreads();
109 this.model = null;
110 }
111
112
113
114
115
116
117 public static void main(final String[] args) throws SimRuntimeException
118 {
119
120 SwingUtilities.invokeLater(new Runnable()
121 {
122 @Override
123 public void run()
124 {
125 try
126 {
127 FundamentalDiagramsLane fundamentalDiagramsLane = new FundamentalDiagramsLane();
128 fundamentalDiagramsLane.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600.0, SECOND),
129 fundamentalDiagramsLane.getProperties(), null, true);
130 }
131 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
132 {
133 exception.printStackTrace();
134 }
135 }
136 });
137 }
138
139
140 @Override
141 protected final OTSModelInterface makeModel()
142 {
143 this.model = new FundamentalDiagramLanePlotsModel(this.savedUserModifiedProperties);
144 return this.model;
145 }
146
147
148 @Override
149 protected final void addAnimationToggles()
150 {
151 AnimationToggles.setTextAnimationTogglesStandard(this);
152 }
153
154
155 @Override
156 protected final void addTabs(final SimpleSimulatorInterface simulator) throws OTSSimulationException
157 {
158 final int panelsPerRow = 3;
159 TablePanel charts = new TablePanel(3, panelsPerRow);
160 for (int plotNumber = 0; plotNumber < 9; plotNumber++)
161 {
162 FundamentalDiagramLane fd;
163 try
164 {
165 Lane lane = this.model.getLane(plotNumber);
166 int xs = (int) lane.getParentLink().getStartNode().getPoint().x;
167 int xe = (int) lane.getParentLink().getEndNode().getPoint().x;
168 fd = new FundamentalDiagramLane("Fundamental Diagram for [" + xs + ", " + xe + "] m", new Duration(1.0, SECOND),
169 lane, Compatible.EVERYTHING, (DEVSSimulatorInterface.TimeDoubleUnit) this.model.getSimulator());
170 fd.setTitle("Fundamental Diagram Graph");
171 fd.setExtendedState(Frame.MAXIMIZED_BOTH);
172 this.model.getFundamentalDiagrams().add(fd);
173 charts.setCell(fd.getContentPane(), plotNumber / panelsPerRow, plotNumber % panelsPerRow);
174 }
175 catch (NetworkException | SimRuntimeException exception)
176 {
177 exception.printStackTrace();
178 }
179 }
180 addTab(getTabCount(), "statistics", charts);
181 }
182
183
184 @Override
185 public final String shortName()
186 {
187 return "Fundamental Diagrams";
188 }
189
190
191 @Override
192 public final String description()
193 {
194 return "<html><h1>Fundamental Diagram Plots</H1>"
195 + "Simulation of a single lane road of 5 km length. Vechicles are generated at a constant rate of "
196 + "1500 veh/hour. At time 300s a blockade is inserted at position 4km; this blockade is removed at time "
197 + "500s. This blockade simulates a bridge opening.<br>"
198 + "The blockade causes a traffic jam that slowly dissolves after the blockade is removed.<br>"
199 + "Output is a set of Diagrams that plot observed density, flow and speed plots against each other.</html>";
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 class FundamentalDiagramLanePlotsModel implements OTSModelInterface, UNITS
218 {
219
220 private static final long serialVersionUID = 20140820L;
221
222
223 private OTSNetwork network = new OTSNetwork("network");
224
225
226 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
227
228
229 private Duration headway;
230
231
232 private int carsCreated = 0;
233
234
235 private GTUType gtuType = CAR;
236
237
238 private GTUFollowingModelOld carFollowingModelCars;
239
240
241 private GTUFollowingModelOld carFollowingModelTrucks;
242
243
244 private double carProbability;
245
246
247 private LaneBasedIndividualGTU block = null;
248
249
250 private Length startX = new Length(0, METER);
251
252
253 private Length laneLength = new Length(500, METER);
254
255
256 private List<Lane> lanes = new ArrayList<>();
257
258
259 private Speed speedLimit = new Speed(100, KM_PER_HOUR);
260
261
262 private List<FundamentalDiagramLane> fundamentalDiagramsLane = new ArrayList<>();
263
264
265 private List<Property<?>> fundamentalDiagramsLaneProperties = null;
266
267
268 private Random randomGenerator = new Random(12345);
269
270
271
272
273 FundamentalDiagramLanePlotsModel(final List<Property<?>> properties)
274 {
275 this.fundamentalDiagramsLaneProperties = properties;
276 }
277
278
279 @Override
280 public final void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> theSimulator)
281 throws SimRuntimeException
282 {
283 this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator;
284 try
285 {
286 LaneType laneType = LaneType.TWO_WAY_LANE;
287 OTSNode node = new OTSNode(this.network, "Node 0", new OTSPoint3D(this.startX.getSI(), 0, 0));
288 for (int laneNr = 0; laneNr < 10; laneNr++)
289 {
290 OTSNode next = new OTSNode(this.network, "Node " + (laneNr + 1),
291 new OTSPoint3D(node.getPoint().x + this.laneLength.si, 0, 0));
292 Lane lane = LaneFactory.makeLane(this.network, "Lane", node, next, null, laneType, this.speedLimit,
293 this.simulator);
294 this.lanes.add(lane);
295 node = next;
296 }
297
298 OTSNode end = new OTSNode(this.network, "End", new OTSPoint3D(node.getPoint().x + 50.0, 0, 0));
299 CrossSectionLink endLink = LaneFactory.makeLink(this.network, "endLink", node, end, null, this.simulator);
300 int last = this.lanes.size() - 1;
301 Lane sinkLane = new Lane(endLink, "sinkLane", this.lanes.get(last).getLateralCenterPosition(1.0),
302 this.lanes.get(last).getLateralCenterPosition(1.0), this.lanes.get(last).getWidth(1.0),
303 this.lanes.get(last).getWidth(1.0), laneType, this.speedLimit, new OvertakingConditions.None());
304 new SinkSensor(sinkLane, new Length(10.0, METER), this.simulator);
305 }
306 catch (NamingException | NetworkException | OTSGeometryException exception)
307 {
308 exception.printStackTrace();
309 }
310
311 for (Property<?> p : this.fundamentalDiagramsLaneProperties)
312 {
313 if (p instanceof SelectionProperty)
314 {
315 SelectionProperty sp = (SelectionProperty) p;
316 if ("CarGollowingModel".equals(sp.getKey()))
317 {
318 String modelName = sp.getValue();
319 if (modelName.equals("IDM"))
320 {
321 this.carFollowingModelCars = new IDMOld(new Acceleration(1, METER_PER_SECOND_2),
322 new Acceleration(1.5, METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND),
323 1d);
324 this.carFollowingModelTrucks = new IDMOld(new Acceleration(0.5, METER_PER_SECOND_2),
325 new Acceleration(1.5, METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND),
326 1d);
327 }
328 else if (modelName.equals("IDM+"))
329 {
330 this.carFollowingModelCars = new IDMPlusOld(new Acceleration(1, METER_PER_SECOND_2),
331 new Acceleration(1.5, METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND),
332 1d);
333 this.carFollowingModelTrucks = new IDMPlusOld(new Acceleration(0.5, METER_PER_SECOND_2),
334 new Acceleration(1.5, METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND),
335 1d);
336 }
337 else
338 {
339 throw new Error("Car following model " + modelName + " not implemented");
340 }
341 }
342 else
343 {
344 throw new Error("Unhandled SelectionProperty " + p.getKey());
345 }
346 }
347 else if (p instanceof ProbabilityDistributionProperty)
348 {
349 ProbabilityDistributionProperty pdp = (ProbabilityDistributionProperty) p;
350 String modelName = p.getKey();
351 if (modelName.equals("TrafficComposition"))
352 {
353 this.carProbability = pdp.getValue()[0];
354 }
355 else
356 {
357 throw new Error("Unhandled ProbabilityDistributionProperty " + p.getKey());
358 }
359 }
360 else
361 {
362 throw new Error("Unhandled property: " + p);
363 }
364 }
365
366
367 this.headway = new Duration(3600.0 / 1500.0, SECOND);
368
369 try
370 {
371
372 this.simulator.scheduleEventAbs(Time.ZERO, this, this, "generateCar", null);
373
374 this.simulator.scheduleEventAbs(new Time(1000, TimeUnit.BASE_SECOND), this, this, "createBlock", null);
375
376 this.simulator.scheduleEventAbs(new Time(1200, TimeUnit.BASE_SECOND), this, this, "removeBlock", null);
377
378 for (int t = 1; t <= this.simulator.getReplication().getTreatment().getRunLength().si / 25; t++)
379 {
380 this.simulator.scheduleEventAbs(new Time(25 * t - 0.001, TimeUnit.BASE_SECOND), this, this, "drawGraphs",
381 null);
382 }
383 }
384 catch (SimRuntimeException exception)
385 {
386 exception.printStackTrace();
387 }
388 }
389
390
391
392
393
394 protected final void createBlock() throws RemoteException
395 {
396 Length initialPosition = new Length(200, METER);
397 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
398 try
399 {
400 initialPositions.add(new DirectedLanePosition(this.lanes.get(this.lanes.size() - 1), initialPosition,
401 GTUDirectionality.DIR_PLUS));
402 Parameters parameters = DefaultsFactory.getDefaultParameters();
403 this.block = new LaneBasedIndividualGTU("999999", this.gtuType, new Length(4, METER), new Length(1.8, METER),
404 Speed.ZERO, Length.createSI(2.0), this.simulator, this.network);
405 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
406 new LaneBasedGTUFollowingTacticalPlanner(this.carFollowingModelCars, this.block), this.block);
407 this.block.setParameters(parameters);
408 this.block.initWithAnimation(strategicalPlanner, initialPositions, Speed.ZERO, DefaultCarAnimation.class,
409 FundamentalDiagramsLane.this.getColorer());
410 }
411 catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
412 {
413 exception.printStackTrace();
414 }
415 }
416
417
418
419
420 protected final void removeBlock()
421 {
422 this.block.destroy();
423 this.block = null;
424 }
425
426
427
428
429 protected final void generateCar()
430 {
431 boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability;
432 Length initialPosition = new Length(0, METER);
433 Speed initialSpeed = new Speed(100, KM_PER_HOUR);
434 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
435 try
436 {
437 initialPositions.add(new DirectedLanePosition(this.lanes.get(0), initialPosition, GTUDirectionality.DIR_PLUS));
438 Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
439 GTUFollowingModelOld gtuFollowingModel =
440 generateTruck ? this.carFollowingModelTrucks : this.carFollowingModelCars;
441 if (null == gtuFollowingModel)
442 {
443 throw new Error("gtuFollowingModel is null");
444 }
445 Parameters parameters = DefaultsFactory.getDefaultParameters();
446
447 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated), this.gtuType, vehicleLength,
448 new Length(1.8, METER), new Speed(200, KM_PER_HOUR), vehicleLength.multiplyBy(0.5), this.simulator,
449 this.network);
450 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
451 new LaneBasedGTUFollowingTacticalPlanner(gtuFollowingModel, gtu), gtu);
452 gtu.setParameters(parameters);
453 gtu.initWithAnimation(strategicalPlanner, initialPositions, initialSpeed, DefaultCarAnimation.class,
454 FundamentalDiagramsLane.this.getColorer());
455 this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
456 }
457 catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
458 {
459 exception.printStackTrace();
460 }
461 }
462
463
464
465
466 protected final void drawGraphs()
467 {
468
469 for (FundamentalDiagramLane fd : this.fundamentalDiagramsLane)
470 {
471 fd.reGraph();
472 }
473 }
474
475
476 @Override
477 public final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
478 {
479 return this.simulator;
480 }
481
482
483 @Override
484 public OTSNetwork getNetwork()
485 {
486 return this.network;
487 }
488
489
490
491
492 public final List<FundamentalDiagramLane> getFundamentalDiagrams()
493 {
494 return this.fundamentalDiagramsLane;
495 }
496
497
498
499
500
501 public Lane getLane(final int laneNr)
502 {
503 return this.lanes.get(laneNr);
504 }
505 }
506 }