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-01-24 02:44:57 +0100 (Fri, 24 Jan 2020) $, @version $Revision: 6119 $, by $Author: averbraeck $,
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(),
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      * 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(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      * Build the sequential model.
197      * <p>
198      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
199      * <br>
200      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
201      * <p>
202      * $LastChangedDate: 2020-01-24 02:44:57 +0100 (Fri, 24 Jan 2020) $, @version $Revision: 6119 $, by $Author: averbraeck $,
203      * initial version 0 jan. 2015 <br>
204      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
205      */
206     static class SequentialModel extends AbstractOTSModel implements UNITS
207     {
208         /** */
209         private static final long serialVersionUID = 20150130L;
210 
211         /** The network. */
212         private final OTSRoadNetwork network = new OTSRoadNetwork("network", true);
213 
214         /** The nodes of our network in the order that all GTUs will visit them. */
215         private List<OTSRoadNode> nodes = new ArrayList<>();
216 
217         /** Strategical planner generator for cars. */
218         private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
219 
220         /** Strategical planner generator for trucks. */
221         private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
222 
223         /** Car parameters. */
224         private Parameters parametersCar;
225 
226         /** Truck parameters. */
227         private Parameters parametersTruck;
228 
229         /** The probability that the next generated GTU is a passenger car. */
230         private double carProbability;
231 
232         /** The headway (inter-vehicle time). */
233         private Duration headway;
234 
235         /** Number of cars created. */
236         private int carsCreated = 0;
237 
238         /** Minimum distance. */
239         private Length minimumDistance = new Length(0, METER);
240 
241         /** The Lane where newly created Cars initially placed on. */
242         private Lane initialLane;
243 
244         /** Maximum distance. */
245         private Length maximumDistance = new Length(2001, METER);
246 
247         /** The random number generator used to decide what kind of GTU to generate. */
248         private StreamInterface stream = new MersenneTwister(12345);
249 
250         /** The sequence of Lanes that all vehicles will follow. */
251         private List<Lane> path = new ArrayList<>();
252 
253         /** The speedLimit on all Lanes. */
254         private Speed speedLimit;
255 
256         /**
257          * @param simulator OTSSimulatorInterface; the simulator for this model
258          */
259         SequentialModel(final OTSSimulatorInterface simulator)
260         {
261             super(simulator);
262             InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
263         }
264 
265         /**
266          * @return a newly created path (which all GTUs in this simulation will follow).
267          */
268         public List<Lane> getPath()
269         {
270             return new ArrayList<>(this.path);
271         }
272 
273         /** {@inheritDoc} */
274         @Override
275         public final void constructModel() throws SimRuntimeException
276         {
277             this.speedLimit = new Speed(100, KM_PER_HOUR);
278 
279             // TODO Bezier curves make 180 degree mistake when minus is true
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                 // Now we can build a series of Links with one Lane on them
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                     // LongitudinalDirectionality direction = line.equals(l23) && minus ? LongitudinalDirectionality.DIR_MINUS
315                     // : LongitudinalDirectionality.DIR_PLUS;
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                 // 1500 [veh / hour] == 2.4s headway
340                 this.headway = new Duration(3600.0 / 1500.0, SECOND);
341 
342                 // Schedule creation of the first car (it will re-schedule itself one headway later, etc.).
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         /** {@inheritDoc} */
353         @Override
354         public OTSRoadNetwork getNetwork()
355         {
356             return this.network;
357         }
358 
359         /**
360          * @return minimumDistance
361          */
362         public final Length getMinimumDistance()
363         {
364             return this.minimumDistance;
365         }
366 
367         /**
368          * @return maximumDistance
369          */
370         public final Length getMaximumDistance()
371         {
372             return this.maximumDistance;
373         }
374 
375         /**
376          * Generate cars at a fixed rate (implemented by re-scheduling this method).
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                 // strategical planner
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         /** {@inheritDoc} */
411         @Override
412         public Serializable getSourceId()
413         {
414             return "SequentialModel";
415         }
416 
417     }
418 }