View Javadoc
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   * Single lane road consisting of three consecutive links.<br>
78   * Tests that GTUs correctly transfer themselves onto the next lane and that the graph samplers handle this situation.
79   * <p>
80   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
81   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
82   * <p>
83   * $LastChangedDate: 2020-05-16 03:09:46 +0200 (Sat, 16 May 2020) $, @version $Revision: 6473 $, by $Author: wjschakel $,
84   * initial version 30 jan. 2015 <br>
85   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
86   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
87   */
88  public class SequentialLanes extends OTSSimulationApplication<SequentialModel> implements UNITS
89  {
90      /** */
91      private static final long serialVersionUID = 1L;
92  
93      /**
94       * Create a Straight Swing application.
95       * @param title String; the title of the Frame
96       * @param panel OTSAnimationPanel; the tabbed panel to display
97       * @param model SequentialModel; the model
98       * @throws OTSDrawingException on animation error
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     /** {@inheritDoc} */
109     @Override
110     protected void addTabs()
111     {
112         addStatisticsTabs(getModel().getSimulator());
113     }
114 
115     /**
116      * Main program.
117      * @param args String[]; the command line arguments (not used)
118      */
119     public static void main(final String[] args)
120     {
121         demo(true);
122     }
123 
124     /**
125      * Start the demo.
126      * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
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(), new Dimension(800,
138                     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      * Add the statistics tabs.
158      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
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(getModel().getNetwork());
173         GraphPath.initRecording(sampler, path);
174         ContourDataSource<?> dataPool = new ContourDataSource<>(sampler.getSamplerData(), path);
175         TablePanel charts = new TablePanel(3, 2);
176         SwingPlot plot = null;
177 
178         plot = new SwingTrajectoryPlot(new TrajectoryPlot("TrajectoryPlot", Duration.instantiateSI(10.0), simulator, sampler
179             .getSamplerData(), path));
180         charts.setCell(plot.getContentPane(), 0, 0);
181 
182         plot = new SwingContourPlot(new ContourPlotDensity("DensityPlot", simulator, dataPool));
183         charts.setCell(plot.getContentPane(), 1, 0);
184 
185         plot = new SwingContourPlot(new ContourPlotSpeed("SpeedPlot", simulator, dataPool));
186         charts.setCell(plot.getContentPane(), 2, 0);
187 
188         plot = new SwingContourPlot(new ContourPlotFlow("FlowPlot", simulator, dataPool));
189         charts.setCell(plot.getContentPane(), 1, 1);
190 
191         plot = new SwingContourPlot(new ContourPlotAcceleration("AccelerationPlot", simulator, dataPool));
192         charts.setCell(plot.getContentPane(), 2, 1);
193 
194         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
195     }
196 
197     /**
198      * Build the sequential model.
199      * <p>
200      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
201      * <br>
202      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
203      * <p>
204      * $LastChangedDate: 2020-05-16 03:09:46 +0200 (Sat, 16 May 2020) $, @version $Revision: 6473 $, by $Author: wjschakel $,
205      * initial version 0 jan. 2015 <br>
206      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
207      */
208     static class SequentialModel extends AbstractOTSModel implements UNITS
209     {
210         /** */
211         private static final long serialVersionUID = 20150130L;
212 
213         /** The network. */
214         private final OTSRoadNetwork network = new OTSRoadNetwork("network", true, getSimulator());
215 
216         /** The nodes of our network in the order that all GTUs will visit them. */
217         private List<OTSRoadNode> nodes = new ArrayList<>();
218 
219         /** Strategical planner generator for cars. */
220         private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
221 
222         /** Strategical planner generator for trucks. */
223         private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
224 
225         /** Car parameters. */
226         private Parameters parametersCar;
227 
228         /** Truck parameters. */
229         private Parameters parametersTruck;
230 
231         /** The probability that the next generated GTU is a passenger car. */
232         private double carProbability;
233 
234         /** The headway (inter-vehicle time). */
235         private Duration headway;
236 
237         /** Number of cars created. */
238         private int carsCreated = 0;
239 
240         /** Minimum distance. */
241         private Length minimumDistance = new Length(0, METER);
242 
243         /** The Lane where newly created Cars initially placed on. */
244         private Lane initialLane;
245 
246         /** Maximum distance. */
247         private Length maximumDistance = new Length(2001, METER);
248 
249         /** The random number generator used to decide what kind of GTU to generate. */
250         private StreamInterface stream = new MersenneTwister(12345);
251 
252         /** The sequence of Lanes that all vehicles will follow. */
253         private List<Lane> path = new ArrayList<>();
254 
255         /** The speedLimit on all Lanes. */
256         private Speed speedLimit;
257 
258         /**
259          * @param simulator OTSSimulatorInterface; the simulator for this model
260          */
261         SequentialModel(final OTSSimulatorInterface simulator)
262         {
263             super(simulator);
264             InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
265         }
266 
267         /**
268          * @return a newly created path (which all GTUs in this simulation will follow).
269          */
270         public List<Lane> getPath()
271         {
272             return new ArrayList<>(this.path);
273         }
274 
275         /** {@inheritDoc} */
276         @Override
277         public final void constructModel() throws SimRuntimeException
278         {
279             this.speedLimit = new Speed(100, KM_PER_HOUR);
280 
281             // TODO Bezier curves make 180 degree mistake when minus is true
282             boolean minus = false;
283 
284             this.nodes = new ArrayList<>();
285             try
286             {
287                 OTSPoint3D p2 = new OTSPoint3D(1020, 3);
288                 OTSPoint3D p3 = new OTSPoint3D(2000, 197);
289                 Direction dir23 = p2.horizontalDirection(p3);
290                 OTSRoadNode n0 = new OTSRoadNode(this.network, "Node-0-(0,0)", new OTSPoint3D(0, 0), Direction.ZERO);
291                 OTSRoadNode n1 = new OTSRoadNode(this.network, "Node-1-(1000,0)", new OTSPoint3D(1000, 0), Direction.ZERO);
292                 OTSRoadNode n2 = new OTSRoadNode(this.network, "Node-2-(1020,3)", p2, dir23);
293                 OTSRoadNode n3 = new OTSRoadNode(this.network, "Node-3-(2000,197)", p3, dir23);
294                 OTSRoadNode n4 = new OTSRoadNode(this.network, "Node-4-(2020,200)", new OTSPoint3D(2020, 200), Direction.ZERO);
295                 OTSRoadNode n5 = new OTSRoadNode(this.network, "Node-5-(2200,200)", new OTSPoint3D(2200, 200), Direction.ZERO);
296                 this.nodes.addAll(Arrays.asList(new OTSRoadNode[] {n0, n1, n2, n3, n4, n5}));
297 
298                 LaneType laneType = this.network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
299 
300                 // Now we can build a series of Links with one Lane on them
301                 ArrayList<CrossSectionLink> links = new ArrayList<>();
302                 OTSLine3D l01 = new OTSLine3D(n0.getPoint(), n1.getPoint());
303                 OTSLine3D l12 = LaneFactory.makeBezier(n0, n1, n2, n3);
304                 OTSLine3D l23 = minus ? new OTSLine3D(n3.getPoint(), n2.getPoint()) : new OTSLine3D(n2.getPoint(), n3
305                     .getPoint());
306                 OTSLine3D l34 = LaneFactory.makeBezier(n2, n3, n4, n5);
307                 OTSLine3D l45 = new OTSLine3D(n4.getPoint(), n5.getPoint());
308                 OTSLine3D[] lines = new OTSLine3D[] {l01, l12, l23, l34, l45};
309 
310                 for (int i = 1; i < this.nodes.size(); i++)
311                 {
312                     OTSRoadNode fromNode = this.nodes.get(i - 1);
313                     OTSRoadNode toNode = this.nodes.get(i);
314                     OTSLine3D line = lines[i - 1];
315                     String linkName = fromNode.getId() + "-" + toNode.getId();
316                     // LongitudinalDirectionality direction = line.equals(l23) && minus ? LongitudinalDirectionality.DIR_MINUS
317                     // : LongitudinalDirectionality.DIR_PLUS;
318                     Lane[] lanes = LaneFactory.makeMultiLane(this.network, linkName, fromNode, toNode, line.getPoints(), 1,
319                         laneType, this.speedLimit, this.simulator);
320                     if (i == this.nodes.size() - 1)
321                     {
322                         new SinkSensor(lanes[0], new Length(100.0, METER), Compatible.EVERYTHING, this.simulator);
323                     }
324                     this.path.add(lanes[0]);
325                     links.add(lanes[0].getParentLink());
326                     if (1 == i)
327                     {
328                         this.initialLane = lanes[0];
329                     }
330                 }
331 
332                 this.carProbability = (double) getInputParameter("generic.carProbability");
333                 this.parametersCar = InputParameterHelper.getParametersCar(getInputParameterMap());
334                 this.parametersTruck = InputParameterHelper.getParametersTruck(getInputParameterMap());
335 
336                 this.strategicalPlannerGeneratorCars = new LaneBasedStrategicalRoutePlannerFactory(new LMRSFactory(
337                     new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
338                 this.strategicalPlannerGeneratorTrucks = new LaneBasedStrategicalRoutePlannerFactory(new LMRSFactory(
339                     new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
340 
341                 // 1500 [veh / hour] == 2.4s headway
342                 this.headway = new Duration(3600.0 / 1500.0, SECOND);
343 
344                 // Schedule creation of the first car (it will re-schedule itself one headway later, etc.).
345                 this.simulator.scheduleEventAbs(Time.ZERO, this, this, "generateCar", null);
346             }
347             catch (NamingException | NetworkException | OTSGeometryException | ParameterException | InputParameterException
348                 | GTUException exception)
349             {
350                 exception.printStackTrace();
351             }
352         }
353 
354         /** {@inheritDoc} */
355         @Override
356         public OTSRoadNetwork getNetwork()
357         {
358             return this.network;
359         }
360 
361         /**
362          * @return minimumDistance
363          */
364         public final Length getMinimumDistance()
365         {
366             return this.minimumDistance;
367         }
368 
369         /**
370          * @return maximumDistance
371          */
372         public final Length getMaximumDistance()
373         {
374             return this.maximumDistance;
375         }
376 
377         /**
378          * Generate cars at a fixed rate (implemented by re-scheduling this method).
379          */
380         protected final void generateCar()
381         {
382             try
383             {
384                 boolean generateTruck = this.stream.nextDouble() > this.carProbability;
385                 Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
386                 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated), this.network.getGtuType(
387                     GTUType.DEFAULTS.CAR), vehicleLength, new Length(1.8, METER), new Speed(200, KM_PER_HOUR), vehicleLength
388                         .times(0.5), this.simulator, this.network);
389                 gtu.setParameters(generateTruck ? this.parametersTruck : this.parametersCar);
390                 gtu.setNoLaneChangeDistance(Length.ZERO);
391                 gtu.setMaximumAcceleration(Acceleration.instantiateSI(3.0));
392                 gtu.setMaximumDeceleration(Acceleration.instantiateSI(-8.0));
393 
394                 // strategical planner
395                 LaneBasedStrategicalPlanner strategicalPlanner = generateTruck ? this.strategicalPlannerGeneratorTrucks.create(
396                     gtu, null, null, null) : this.strategicalPlannerGeneratorCars.create(gtu, null, null, null);
397 
398                 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
399                 Length initialPosition = new Length(20, METER);
400                 initialPositions.add(new DirectedLanePosition(this.initialLane, initialPosition, GTUDirectionality.DIR_PLUS));
401                 Speed initialSpeed = new Speed(100.0, KM_PER_HOUR);
402                 gtu.init(strategicalPlanner, initialPositions, initialSpeed);
403                 this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
404             }
405             catch (SimRuntimeException | NetworkException | GTUException | OTSGeometryException exception)
406             {
407                 exception.printStackTrace();
408             }
409         }
410 
411         /** {@inheritDoc} */
412         @Override
413         public Serializable getSourceId()
414         {
415             return "SequentialModel";
416         }
417 
418     }
419 }