1 package org.opentrafficsim.demo;
2
3 import java.awt.Dimension;
4 import java.io.Serializable;
5 import java.rmi.RemoteException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Set;
11
12 import javax.naming.NamingException;
13
14 import org.djunits.unit.util.UNITS;
15 import org.djunits.value.vdouble.scalar.Acceleration;
16 import org.djunits.value.vdouble.scalar.Direction;
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Length;
19 import org.djunits.value.vdouble.scalar.Speed;
20 import org.djunits.value.vdouble.scalar.Time;
21 import org.opentrafficsim.base.parameters.ParameterException;
22 import org.opentrafficsim.base.parameters.Parameters;
23 import org.opentrafficsim.core.compatibility.Compatible;
24 import org.opentrafficsim.core.dsol.AbstractOTSModel;
25 import org.opentrafficsim.core.dsol.OTSAnimator;
26 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
27 import org.opentrafficsim.core.geometry.OTSGeometryException;
28 import org.opentrafficsim.core.geometry.OTSLine3D;
29 import org.opentrafficsim.core.geometry.OTSPoint3D;
30 import org.opentrafficsim.core.gtu.GTUDirectionality;
31 import org.opentrafficsim.core.gtu.GTUException;
32 import org.opentrafficsim.core.gtu.GTUType;
33 import org.opentrafficsim.core.network.NetworkException;
34 import org.opentrafficsim.demo.SequentialLanes.SequentialModel;
35 import org.opentrafficsim.draw.core.OTSDrawingException;
36 import org.opentrafficsim.draw.graphs.ContourDataSource;
37 import org.opentrafficsim.draw.graphs.ContourPlotAcceleration;
38 import org.opentrafficsim.draw.graphs.ContourPlotDensity;
39 import org.opentrafficsim.draw.graphs.ContourPlotFlow;
40 import org.opentrafficsim.draw.graphs.ContourPlotSpeed;
41 import org.opentrafficsim.draw.graphs.GraphPath;
42 import org.opentrafficsim.draw.graphs.TrajectoryPlot;
43 import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
44 import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
45 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
46 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
47 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
48 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
49 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
50 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
51 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
52 import org.opentrafficsim.road.network.OTSRoadNetwork;
53 import org.opentrafficsim.road.network.factory.LaneFactory;
54 import org.opentrafficsim.road.network.lane.CrossSectionLink;
55 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
56 import org.opentrafficsim.road.network.lane.Lane;
57 import org.opentrafficsim.road.network.lane.LaneDirection;
58 import org.opentrafficsim.road.network.lane.LaneType;
59 import org.opentrafficsim.road.network.lane.OTSRoadNode;
60 import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
61 import org.opentrafficsim.road.network.sampling.RoadSampler;
62 import org.opentrafficsim.swing.graphs.SwingContourPlot;
63 import org.opentrafficsim.swing.graphs.SwingPlot;
64 import org.opentrafficsim.swing.graphs.SwingTrajectoryPlot;
65 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
66 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
67
68 import nl.tudelft.simulation.dsol.SimRuntimeException;
69 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
70 import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
71 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
72 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
73 import nl.tudelft.simulation.jstats.streams.StreamInterface;
74 import nl.tudelft.simulation.language.DSOLException;
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public class SequentialLanes extends OTSSimulationApplication<SequentialModel> implements UNITS
89 {
90
91 private static final long serialVersionUID = 1L;
92
93
94
95
96
97
98
99
100 public SequentialLanes(final String title, final OTSAnimationPanel panel, final SequentialModel model)
101 throws OTSDrawingException
102 {
103 super(model, panel);
104 OTSRoadNetwork network = model.getNetwork();
105 System.out.println(network.getLinkMap());
106 }
107
108
109 @Override
110 protected void addTabs()
111 {
112 addStatisticsTabs(getModel().getSimulator());
113 }
114
115
116
117
118
119 public static void main(final String[] args)
120 {
121 demo(true);
122 }
123
124
125
126
127
128 public static void demo(final boolean exitOnClose)
129 {
130 try
131 {
132 OTSAnimator simulator = new OTSAnimator("SequentialLanes");
133 final SequentialModel otsModel = new SequentialModel(simulator);
134 if (TabbedParameterDialog.process(otsModel.getInputParameterMap()))
135 {
136 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
137 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
138 new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
139 SequentialLaneses.html#SequentialLanes">SequentialLanes app = new SequentialLanes("SequentialLanes", animationPanel, otsModel);
140 app.setExitOnClose(exitOnClose);
141 }
142 else
143 {
144 if (exitOnClose)
145 {
146 System.exit(0);
147 }
148 }
149 }
150 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
151 {
152 exception.printStackTrace();
153 }
154 }
155
156
157
158
159
160 protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
161 {
162 GraphPath<KpiLaneDirection> path;
163 try
164 {
165 path = GraphLaneUtil.createPath("Lane", new LaneDirection(getModel().getPath().get(0), GTUDirectionality.DIR_PLUS));
166 }
167 catch (NetworkException exception)
168 {
169 throw new RuntimeException("Could not create a path as a lane has no set speed limit.", exception);
170 }
171
172 RoadSampler sampler = new RoadSampler(simulator);
173 ContourDataSource<?> dataPool = new ContourDataSource<>(sampler, path);
174 TablePanel charts = new TablePanel(3, 2);
175 SwingPlot plot = null;
176
177 plot = new SwingTrajectoryPlot(new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler, path));
178 charts.setCell(plot.getContentPane(), 0, 0);
179
180 plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
181 charts.setCell(plot.getContentPane(), 1, 0);
182
183 plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
184 charts.setCell(plot.getContentPane(), 2, 0);
185
186 plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
187 charts.setCell(plot.getContentPane(), 1, 1);
188
189 plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
190 charts.setCell(plot.getContentPane(), 2, 1);
191
192 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 static class SequentialModel extends AbstractOTSModel implements UNITS
207 {
208
209 private static final long serialVersionUID = 20150130L;
210
211
212 private final OTSRoadNetwork network = new OTSRoadNetwork("network", true);
213
214
215 private List<OTSRoadNode> nodes = new ArrayList<>();
216
217
218 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
219
220
221 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
222
223
224 private Parameters parametersCar;
225
226
227 private Parameters parametersTruck;
228
229
230 private double carProbability;
231
232
233 private Duration headway;
234
235
236 private int carsCreated = 0;
237
238
239 private Length minimumDistance = new Length(0, METER);
240
241
242 private Lane initialLane;
243
244
245 private Length maximumDistance = new Length(2001, METER);
246
247
248 private StreamInterface stream = new MersenneTwister(12345);
249
250
251 private List<Lane> path = new ArrayList<>();
252
253
254 private Speed speedLimit;
255
256
257
258
259 SequentialModel(final OTSSimulatorInterface simulator)
260 {
261 super(simulator);
262 InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
263 }
264
265
266
267
268 public List<Lane> getPath()
269 {
270 return new ArrayList<>(this.path);
271 }
272
273
274 @Override
275 public final void constructModel() throws SimRuntimeException
276 {
277 this.speedLimit = new Speed(100, KM_PER_HOUR);
278
279
280 boolean minus = false;
281
282 this.nodes = new ArrayList<>();
283 try
284 {
285 OTSPoint3D p2 = new OTSPoint3D(1020, 3);
286 OTSPoint3D p3 = new OTSPoint3D(2000, 197);
287 Direction dir23 = p2.horizontalDirection(p3);
288 OTSRoadNode n0 = new OTSRoadNode(this.network, "Node-0-(0,0)", new OTSPoint3D(0, 0), Direction.ZERO);
289 OTSRoadNode n1 = new OTSRoadNode(this.network, "Node-1-(1000,0)", new OTSPoint3D(1000, 0), Direction.ZERO);
290 OTSRoadNode n2 = new OTSRoadNode(this.network, "Node-2-(1020,3)", p2, dir23);
291 OTSRoadNode n3 = new OTSRoadNode(this.network, "Node-3-(2000,197)", p3, dir23);
292 OTSRoadNode n4 = new OTSRoadNode(this.network, "Node-4-(2020,200)", new OTSPoint3D(2020, 200), Direction.ZERO);
293 OTSRoadNode n5 = new OTSRoadNode(this.network, "Node-5-(2200,200)", new OTSPoint3D(2200, 200), Direction.ZERO);
294 this.nodes.addAll(Arrays.asList(new OTSRoadNode[] {n0, n1, n2, n3, n4, n5}));
295
296 LaneType laneType = this.network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
297
298
299 ArrayList<CrossSectionLink> links = new ArrayList<>();
300 OTSLine3D l01 = new OTSLine3D(n0.getPoint(), n1.getPoint());
301 OTSLine3D l12 = LaneFactory.makeBezier(n0, n1, n2, n3);
302 OTSLine3D l23 =
303 minus ? new OTSLine3D(n3.getPoint(), n2.getPoint()) : new OTSLine3D(n2.getPoint(), n3.getPoint());
304 OTSLine3D l34 = LaneFactory.makeBezier(n2, n3, n4, n5);
305 OTSLine3D l45 = new OTSLine3D(n4.getPoint(), n5.getPoint());
306 OTSLine3D[] lines = new OTSLine3D[] {l01, l12, l23, l34, l45};
307
308 for (int i = 1; i < this.nodes.size(); i++)
309 {
310 OTSRoadNode fromNode = this.nodes.get(i - 1);
311 OTSRoadNode toNode = this.nodes.get(i);
312 OTSLine3D line = lines[i - 1];
313 String linkName = fromNode.getId() + "-" + toNode.getId();
314
315
316 Lane[] lanes = LaneFactory.makeMultiLane(this.network, linkName, fromNode, toNode, line.getPoints(), 1,
317 laneType, this.speedLimit, this.simulator);
318 if (i == this.nodes.size() - 1)
319 {
320 new SinkSensor(lanes[0], new Length(100.0, METER), Compatible.EVERYTHING, this.simulator);
321 }
322 this.path.add(lanes[0]);
323 links.add(lanes[0].getParentLink());
324 if (1 == i)
325 {
326 this.initialLane = lanes[0];
327 }
328 }
329
330 this.carProbability = (double) getInputParameter("generic.carProbability");
331 this.parametersCar = InputParameterHelper.getParametersCar(getInputParameterMap());
332 this.parametersTruck = InputParameterHelper.getParametersTruck(getInputParameterMap());
333
334 this.strategicalPlannerGeneratorCars = new LaneBasedStrategicalRoutePlannerFactory(
335 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
336 this.strategicalPlannerGeneratorTrucks = new LaneBasedStrategicalRoutePlannerFactory(
337 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
338
339
340 this.headway = new Duration(3600.0 / 1500.0, SECOND);
341
342
343 this.simulator.scheduleEventAbs(Time.ZERO, this, this, "generateCar", null);
344 }
345 catch (NamingException | NetworkException | OTSGeometryException | ParameterException | InputParameterException
346 | GTUException exception)
347 {
348 exception.printStackTrace();
349 }
350 }
351
352
353 @Override
354 public OTSRoadNetwork getNetwork()
355 {
356 return this.network;
357 }
358
359
360
361
362 public final Length getMinimumDistance()
363 {
364 return this.minimumDistance;
365 }
366
367
368
369
370 public final Length getMaximumDistance()
371 {
372 return this.maximumDistance;
373 }
374
375
376
377
378 protected final void generateCar()
379 {
380 try
381 {
382 boolean generateTruck = this.stream.nextDouble() > this.carProbability;
383 Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
384 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated),
385 this.network.getGtuType(GTUType.DEFAULTS.CAR), vehicleLength, new Length(1.8, METER),
386 new Speed(200, KM_PER_HOUR), vehicleLength.times(0.5), this.simulator, this.network);
387 gtu.setParameters(generateTruck ? this.parametersTruck : this.parametersCar);
388 gtu.setNoLaneChangeDistance(Length.ZERO);
389 gtu.setMaximumAcceleration(Acceleration.instantiateSI(3.0));
390 gtu.setMaximumDeceleration(Acceleration.instantiateSI(-8.0));
391
392
393 LaneBasedStrategicalPlanner strategicalPlanner =
394 generateTruck ? this.strategicalPlannerGeneratorTrucks.create(gtu, null, null, null)
395 : this.strategicalPlannerGeneratorCars.create(gtu, null, null, null);
396
397 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
398 Length initialPosition = new Length(20, METER);
399 initialPositions.add(new DirectedLanePosition(this.initialLane, initialPosition, GTUDirectionality.DIR_PLUS));
400 Speed initialSpeed = new Speed(100.0, KM_PER_HOUR);
401 gtu.init(strategicalPlanner, initialPositions, initialSpeed);
402 this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
403 }
404 catch (SimRuntimeException | NetworkException | GTUException | OTSGeometryException exception)
405 {
406 exception.printStackTrace();
407 }
408 }
409
410
411 @Override
412 public Serializable getSourceId()
413 {
414 return "SequentialModel";
415 }
416
417 }
418 }