View Javadoc
1   package org.opentrafficsim.demo.carFollowing;
2   
3   import java.awt.Frame;
4   import java.awt.geom.Rectangle2D;
5   import java.rmi.RemoteException;
6   import java.util.ArrayList;
7   import java.util.HashSet;
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.JPanel;
15  import javax.swing.SwingUtilities;
16  
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.core.dsol.OTSDEVSSimulatorInterface;
28  import org.opentrafficsim.core.dsol.OTSModelInterface;
29  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
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.gtu.animation.GTUColorer;
36  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
37  import org.opentrafficsim.core.network.LongitudinalDirectionality;
38  import org.opentrafficsim.core.network.NetworkException;
39  import org.opentrafficsim.core.network.OTSNetwork;
40  import org.opentrafficsim.core.network.OTSNode;
41  import org.opentrafficsim.graphs.FundamentalDiagram;
42  import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
43  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
44  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner;
45  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
46  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld;
47  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
48  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
49  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
50  import org.opentrafficsim.road.network.factory.LaneFactory;
51  import org.opentrafficsim.road.network.lane.CrossSectionLink;
52  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
53  import org.opentrafficsim.road.network.lane.Lane;
54  import org.opentrafficsim.road.network.lane.LaneType;
55  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
56  import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
57  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
58  import org.opentrafficsim.simulationengine.OTSSimulationException;
59  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
60  
61  import nl.tudelft.simulation.dsol.SimRuntimeException;
62  import nl.tudelft.simulation.dsol.gui.swing.TablePanel;
63  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
64  
65  /**
66   * Demonstrate the FundamentalDiagram plot.
67   * <p>
68   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
69   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
70   * <p>
71   * $LastChangedDate: 2016-11-10 13:37:52 +0100 (Thu, 10 Nov 2016) $, @version $Revision: 2544 $, by $Author: averbraeck $,
72   * initial version 17 dec. 2014 <br>
73   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
74   */
75  public class FundamentalDiagrams extends AbstractWrappableAnimation implements UNITS
76  {
77      /** */
78      private static final long serialVersionUID = 1L;
79  
80      /** The model. */
81      private FundamentalDiagramPlotsModel model;
82  
83      /** Create a FundamentalDiagrams simulation. */
84      public FundamentalDiagrams()
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" }, new Double[] {
96                              0.8, 0.2 }, false, 10));
97          }
98          catch (PropertyException exception)
99          {
100             exception.printStackTrace();
101         }
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public final void stopTimersThreads()
107     {
108         super.stopTimersThreads();
109         this.model = null;
110     }
111 
112     /**
113      * Main program.
114      * @param args String[]; the command line arguments (not used)
115      * @throws SimRuntimeException on ???
116      */
117     public static void main(final String[] args) throws SimRuntimeException
118     {
119         // Create the simulation and wrap its panel in a JFrame. It does not get much easier/shorter than this...
120         SwingUtilities.invokeLater(new Runnable()
121         {
122             @Override
123             public void run()
124             {
125                 try
126                 {
127                     FundamentalDiagrams fundamentalDiagrams = new FundamentalDiagrams();
128                     fundamentalDiagrams.buildAnimator(new Time(0.0, SECOND), new Duration(0.0, SECOND), new Duration(3600.0,
129                             SECOND), fundamentalDiagrams.getProperties(), null, true);
130                 }
131                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
132                 {
133                     exception.printStackTrace();
134                 }
135             }
136         });
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     protected final OTSModelInterface makeModel(final GTUColorer colorer)
142     {
143         this.model = new FundamentalDiagramPlotsModel(this.savedUserModifiedProperties, colorer);
144         return this.model;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     protected final Rectangle2D.Double makeAnimationRectangle()
150     {
151         return new Rectangle2D.Double(0, -100, 5000, 200);
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     protected final JPanel makeCharts(SimpleSimulatorInterface simulator) throws OTSSimulationException
157     {
158         final int panelsPerRow = 3;
159         TablePanel charts = new TablePanel(4, panelsPerRow);
160         for (int plotNumber = 0; plotNumber < 10; plotNumber++)
161         {
162             Length detectorLocation = new Length(400 + 500 * plotNumber, METER);
163             FundamentalDiagram fd;
164             try
165             {
166                 fd =
167                         new FundamentalDiagram("Fundamental Diagram at " + detectorLocation.getSI() + "m", new Duration(1,
168                                 MINUTE), this.model.getLane(), detectorLocation, simulator);
169                 fd.setTitle("Density Contour Graph");
170                 fd.setExtendedState(Frame.MAXIMIZED_BOTH);
171                 this.model.getFundamentalDiagrams().add(fd);
172                 charts.setCell(fd.getContentPane(), plotNumber / panelsPerRow, plotNumber % panelsPerRow);
173             }
174             catch (NetworkException exception)
175             {
176                 exception.printStackTrace();
177             }
178         }
179         return charts;
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public final String shortName()
185     {
186         return "Fundamental Diagrams";
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public final String description()
192     {
193         return "<html><h1>Fundamental Diagram Plots</H1>"
194                 + "Simulation of a single lane road of 5 km length. Vechicles are generated at a constant rate of "
195                 + "1500 veh/hour. At time 300s a blockade is inserted at position 4km; this blockade is removed at time "
196                 + "500s. This blockade simulates a bridge opening.<br>"
197                 + "The blockade causes a traffic jam that slowly dissolves after the blockade is removed.<br>"
198                 + "Output is a set of Diagrams that plot observed density, flow and speed plots against each other.</html>";
199     }
200 
201     /**
202      * Simulate a single lane road of 5 km length. Vehicles are generated at a constant rate of 1500 veh/hour. At time 300s a
203      * blockade is inserted at position 4 km; this blockade is removed at time 500s. The used car following algorithm is IDM+ <a
204      * href="http://opentrafficsim.org/downloads/MOTUS%20reference.pdf"><i>Integrated Lane Change Model with Relaxation and
205      * Synchronization</i>, by Wouter J. Schakel, Victor L. Knoop and Bart van Arem, 2012</a>. <br>
206      * Output is a set of FundamentalDiagram plots for various point along the lane.
207      * <p>
208      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
209      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
210      * <p>
211      * $LastChangedDate: 2016-11-10 13:37:52 +0100 (Thu, 10 Nov 2016) $, @version $Revision: 2544 $, by $Author: averbraeck $,
212      * initial version ug 1, 2014 <br>
213      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
214      */
215     class FundamentalDiagramPlotsModel implements OTSModelInterface, UNITS
216     {
217         /** */
218         private static final long serialVersionUID = 20140820L;
219 
220         /** The simulator. */
221         private OTSDEVSSimulatorInterface simulator;
222 
223         /** The network. */
224         private OTSNetwork network = new OTSNetwork("network");
225 
226         /** The headway (inter-vehicle time). */
227         private Duration headway;
228 
229         /** Number of cars created. */
230         private int carsCreated = 0;
231 
232         /** Type of all GTUs. */
233         private GTUType gtuType = new GTUType("Car");
234 
235         /** The car following model, e.g. IDM Plus for cars. */
236         private GTUFollowingModelOld carFollowingModelCars;
237 
238         /** The car following model, e.g. IDM Plus for trucks. */
239         private GTUFollowingModelOld carFollowingModelTrucks;
240 
241         /** The probability that the next generated GTU is a passenger car. */
242         private double carProbability;
243 
244         /** The blocking car. */
245         private LaneBasedIndividualGTU block = null;
246 
247         /** Minimum distance. */
248         private Length minimumDistance = new Length(0, METER);
249 
250         /** Maximum distance. */
251         private Length maximumDistance = new Length(5000, METER);
252 
253         /** The Lane containing the simulated Cars. */
254         private Lane lane;
255 
256         /** The speed limit. */
257         private Speed speedLimit = new Speed(100, KM_PER_HOUR);
258 
259         /** The fundamental diagram plots. */
260         private List<FundamentalDiagram> fundamentalDiagrams = new ArrayList<>();
261 
262         /** User settable properties. */
263         private List<Property<?>> fundamentalDiagramProperties = null;
264 
265         /** The random number generator used to decide what kind of GTU to generate. */
266         private Random randomGenerator = new Random(12345);
267 
268         /** The GTUColorer for the generated vehicles. */
269         private final GTUColorer gtuColorer;
270 
271         /**
272          * @param properties ArrayList&lt;AbstractProperty&lt;?&gt;&gt;; the properties
273          * @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer.
274          */
275         FundamentalDiagramPlotsModel(final List<Property<?>> properties, final GTUColorer gtuColorer)
276         {
277             this.fundamentalDiagramProperties = properties;
278             this.gtuColorer = gtuColorer;
279         }
280 
281         /** {@inheritDoc} */
282         @Override
283         public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
284                 throws SimRuntimeException, RemoteException
285         {
286             try
287             {
288                 this.simulator = (OTSDEVSSimulatorInterface) theSimulator;
289                 OTSNode from = new OTSNode(this.network, "From", new OTSPoint3D(getMinimumDistance().getSI(), 0, 0));
290                 OTSNode to = new OTSNode(this.network, "To", new OTSPoint3D(getMaximumDistance().getSI(), 0, 0));
291                 OTSNode end = new OTSNode(this.network, "End", new OTSPoint3D(getMaximumDistance().getSI() + 50.0, 0, 0));
292                 Set<GTUType> compatibility = new HashSet<>();
293                 compatibility.add(this.gtuType);
294                 LaneType laneType = new LaneType("CarLane", compatibility);
295                 this.lane =
296                         LaneFactory.makeLane(this.network, "Lane", from, to, null, laneType, this.speedLimit, this.simulator,
297                                 LongitudinalDirectionality.DIR_PLUS);
298                 CrossSectionLink endLink =
299                         LaneFactory.makeLink(this.network, "endLink", to, end, null, LongitudinalDirectionality.DIR_PLUS);
300                 // No overtaking, single lane
301                 Lane sinkLane =
302                         new Lane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0),
303                                 this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0),
304                                 laneType, LongitudinalDirectionality.DIR_PLUS, this.speedLimit, new OvertakingConditions.None());
305                 new SinkSensor(sinkLane, new Length(10.0, METER), this.simulator);
306             }
307             catch (NamingException | NetworkException | OTSGeometryException exception)
308             {
309                 exception.printStackTrace();
310             }
311 
312             // create SinkLane
313 
314             for (Property<?> p : this.fundamentalDiagramProperties)
315             {
316                 if (p instanceof SelectionProperty)
317                 {
318                     SelectionProperty sp = (SelectionProperty) p;
319                     if ("CarFollowingModel".equals(sp.getKey()))
320                     {
321                         String modelName = sp.getValue();
322                         if (modelName.equals("IDM"))
323                         {
324                             this.carFollowingModelCars =
325                                     new IDMOld(new Acceleration(1, METER_PER_SECOND_2), new Acceleration(1.5,
326                                             METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND), 1d);
327                             this.carFollowingModelTrucks =
328                                     new IDMOld(new Acceleration(0.5, METER_PER_SECOND_2), new Acceleration(1.5,
329                                             METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND), 1d);
330                         }
331                         else if (modelName.equals("IDM+"))
332                         {
333                             this.carFollowingModelCars =
334                                     new IDMPlusOld(new Acceleration(1, METER_PER_SECOND_2), new Acceleration(1.5,
335                                             METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND), 1d);
336                             this.carFollowingModelTrucks =
337                                     new IDMPlusOld(new Acceleration(0.5, METER_PER_SECOND_2), new Acceleration(1.5,
338                                             METER_PER_SECOND_2), new Length(2, METER), new Duration(1, SECOND), 1d);
339                         }
340                         else
341                         {
342                             throw new Error("Car following model " + modelName + " not implemented");
343                         }
344                     }
345                     else
346                     {
347                         throw new Error("Unhandled SelectionProperty " + p.getKey());
348                     }
349                 }
350                 else if (p instanceof ProbabilityDistributionProperty)
351                 {
352                     ProbabilityDistributionProperty pdp = (ProbabilityDistributionProperty) p;
353                     String modelName = p.getKey();
354                     if (modelName.equals("TrafficComposition"))
355                     {
356                         this.carProbability = pdp.getValue()[0];
357                     }
358                     else
359                     {
360                         throw new Error("Unhandled ProbabilityDistributionProperty " + p.getKey());
361                     }
362                 }
363                 else
364                 {
365                     throw new Error("Unhandled property: " + p);
366                 }
367             }
368 
369             // 1500 [veh / hour] == 2.4s headway
370             this.headway = new Duration(3600.0 / 1500.0, SECOND);
371 
372             try
373             {
374                 // Schedule creation of the first car (this will re-schedule itself one headway later, etc.).
375                 this.simulator.scheduleEventAbs(new Time(0.0, SECOND), this, this, "generateCar", null);
376                 // Create a block at t = 5 minutes
377                 this.simulator.scheduleEventAbs(new Time(300, SECOND), this, this, "createBlock", null);
378                 // Remove the block at t = 7 minutes
379                 this.simulator.scheduleEventAbs(new Time(420, SECOND), this, this, "removeBlock", null);
380                 // Schedule regular updates of the graph
381                 for (int t = 1; t <= 1800; t++)
382                 {
383                     this.simulator.scheduleEventAbs(new Time(t - 0.001, SECOND), this, this, "drawGraphs", null);
384                 }
385             }
386             catch (SimRuntimeException exception)
387             {
388                 exception.printStackTrace();
389             }
390         }
391 
392         /**
393          * Set up the block.
394          * @throws RemoteException on communications failure
395          */
396         protected final void createBlock() throws RemoteException
397         {
398             Length initialPosition = new Length(4000, METER);
399             Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
400             try
401             {
402                 initialPositions.add(new DirectedLanePosition(this.getLane(), initialPosition, GTUDirectionality.DIR_PLUS));
403                 BehavioralCharacteristics behavioralCharacteristics = DefaultsFactory.getDefaultBehavioralCharacteristics();
404                 // LaneBasedBehavioralCharacteristics drivingCharacteristics =
405                 // new LaneBasedBehavioralCharacteristics(this.carFollowingModelCars, this.laneChangeModel);
406 
407                 this.block =
408                         new LaneBasedIndividualGTU("999999", this.gtuType, new Length(4, METER), new Length(1.8, METER),
409                                 new Speed(0.0, KM_PER_HOUR), this.simulator, this.network);
410                 LaneBasedStrategicalPlanner strategicalPlanner =
411                         new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
412                                 new LaneBasedGTUFollowingTacticalPlanner(this.carFollowingModelCars, this.block), this.block);
413                 this.block.initWithAnimation(strategicalPlanner, initialPositions, new Speed(0.0, KM_PER_HOUR),
414                         DefaultCarAnimation.class, this.gtuColorer);
415             }
416             catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
417             {
418                 exception.printStackTrace();
419             }
420         }
421 
422         /**
423          * Remove the block.
424          */
425         protected final void removeBlock()
426         {
427             this.block.destroy();
428             this.block = null;
429         }
430 
431         /**
432          * Generate cars at a fixed rate (implemented by re-scheduling this method).
433          */
434         protected final void generateCar()
435         {
436             boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability;
437             Length initialPosition = new Length(0, METER);
438             Speed initialSpeed = new Speed(100, KM_PER_HOUR);
439             Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
440             try
441             {
442                 initialPositions.add(new DirectedLanePosition(this.getLane(), initialPosition, GTUDirectionality.DIR_PLUS));
443                 Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
444                 GTUFollowingModelOld gtuFollowingModel =
445                         generateTruck ? this.carFollowingModelTrucks : this.carFollowingModelCars;
446                 if (null == gtuFollowingModel)
447                 {
448                     throw new Error("gtuFollowingModel is null");
449                 }
450                 BehavioralCharacteristics behavioralCharacteristics = DefaultsFactory.getDefaultBehavioralCharacteristics();
451                 // LaneBasedBehavioralCharacteristics drivingCharacteristics =
452                 // new LaneBasedBehavioralCharacteristics(gtuFollowingModel, this.laneChangeModel);
453 
454                 LaneBasedIndividualGTU gtu =
455                         new LaneBasedIndividualGTU("" + (++this.carsCreated), this.gtuType, vehicleLength, new Length(1.8,
456                                 METER), new Speed(200, KM_PER_HOUR), this.simulator, this.network);
457                 LaneBasedStrategicalPlanner strategicalPlanner =
458                         new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
459                                 new LaneBasedGTUFollowingTacticalPlanner(gtuFollowingModel, gtu), gtu);
460                 gtu.initWithAnimation(strategicalPlanner, initialPositions, initialSpeed, DefaultCarAnimation.class,
461                         this.gtuColorer);
462 
463                 this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
464             }
465             catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
466             {
467                 exception.printStackTrace();
468             }
469         }
470 
471         /**
472         * 
473         */
474         protected final void drawGraphs()
475         {
476             // Notify the Fundamental Diagram plots that the underlying data has changed
477             for (FundamentalDiagram fd : this.fundamentalDiagrams)
478             {
479                 fd.reGraph();
480             }
481         }
482 
483         /** {@inheritDoc} */
484         @Override
485         public final SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
486         {
487             return null;
488         }
489 
490         /**
491          * @return fundamentalDiagramPlots
492          */
493         public final List<FundamentalDiagram> getFundamentalDiagrams()
494         {
495             return this.fundamentalDiagrams;
496         }
497 
498         /**
499          * @return minimumDistance
500          */
501         public final Length getMinimumDistance()
502         {
503             return this.minimumDistance;
504         }
505 
506         /**
507          * @return maximumDistance
508          */
509         public final Length getMaximumDistance()
510         {
511             return this.maximumDistance;
512         }
513 
514         /**
515          * @return lane.
516          */
517         public Lane getLane()
518         {
519             return this.lane;
520         }
521     }
522 
523     /** {@inheritDoc} */
524     @Override
525     public final String toString()
526     {
527         return "FundamentalDiagrams [model=" + this.model + "]";
528     }
529 }