View Javadoc
1   package org.opentrafficsim.demo.lanechange;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.CAR;
4   
5   import java.awt.BasicStroke;
6   import java.awt.BorderLayout;
7   import java.awt.Color;
8   import java.rmi.RemoteException;
9   import java.util.ArrayList;
10  
11  import javax.naming.NamingException;
12  import javax.swing.JPanel;
13  import javax.swing.SwingUtilities;
14  import javax.swing.event.EventListenerList;
15  
16  import org.djunits.unit.DurationUnit;
17  import org.djunits.unit.TimeUnit;
18  import org.djunits.unit.UNITS;
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.jfree.chart.ChartFactory;
24  import org.jfree.chart.ChartPanel;
25  import org.jfree.chart.JFreeChart;
26  import org.jfree.chart.StandardChartTheme;
27  import org.jfree.chart.axis.NumberAxis;
28  import org.jfree.chart.plot.PlotOrientation;
29  import org.jfree.chart.plot.XYPlot;
30  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
31  import org.jfree.data.DomainOrder;
32  import org.jfree.data.general.DatasetChangeEvent;
33  import org.jfree.data.general.DatasetChangeListener;
34  import org.jfree.data.general.DatasetGroup;
35  import org.jfree.data.xy.XYDataset;
36  import org.opentrafficsim.core.dsol.OTSModelInterface;
37  import org.opentrafficsim.core.geometry.OTSGeometryException;
38  import org.opentrafficsim.core.geometry.OTSPoint3D;
39  import org.opentrafficsim.core.gtu.GTUException;
40  import org.opentrafficsim.core.gtu.GTUType;
41  import org.opentrafficsim.core.network.Network;
42  import org.opentrafficsim.core.network.NetworkException;
43  import org.opentrafficsim.core.network.OTSNetwork;
44  import org.opentrafficsim.core.network.OTSNode;
45  import org.opentrafficsim.core.network.route.CompleteRoute;
46  import org.opentrafficsim.gui.SimulatorFrame;
47  import org.opentrafficsim.road.network.factory.LaneFactory;
48  import org.opentrafficsim.road.network.lane.Lane;
49  import org.opentrafficsim.road.network.lane.LaneType;
50  import org.opentrafficsim.simulationengine.SimpleSimulator;
51  
52  import nl.tudelft.simulation.dsol.SimRuntimeException;
53  import nl.tudelft.simulation.dsol.gui.swing.TablePanel;
54  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
55  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
56  
57  /**
58   * <p>
59   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
60   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
61   * <p>
62   * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
63   * initial version 15 apr. 2015 <br>
64   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
65   */
66  public class SuitabilityGraph implements OTSModelInterface, UNITS
67  {
68      /** */
69      private static final long serialVersionUID = 20150415L;
70  
71      /** The JPanel that contains all the graphs. */
72      private JPanel graphPanel;
73  
74      /** Number of lanes on the main roadway (do not set higher than size of colorTable). */
75      private static final int LANECOUNT = 4;
76  
77      /** Speed limit values in km/h. */
78      private static final double[] SPEEDLIMITS = { 30, 50, 80, 120 };
79  
80      /** Arrangements of lanes to aim for. Negative numbers indicate lanes on right side of the roadway. */
81      private static final int[] TARGETLANES = { 1, 2, -2, -1 };
82  
83      /** Time horizon for lane changes. */
84      private Duration timeHorizon = new Duration(100, SECOND);
85  
86      /** Time range for graphs (also adjusts distance range). */
87      private Duration timeRange = new Duration(110, SECOND);
88  
89      /** Colors that correspond to the lanes; taken from electrical resistor color codes. */
90      private static final Color[] COLORTABLE = { new Color(160, 82, 45) /* brown */, Color.RED, Color.ORANGE, Color.YELLOW,
91              Color.GREEN, Color.BLUE, new Color(199, 21, 133) /* violet */, Color.GRAY, Color.WHITE };
92  
93      /** The graphs. */
94      private JFreeChart[][] charts;
95  
96      /**
97       * Start the program.
98       * @param args String[]; command line arguments (not used)
99       * @throws SimRuntimeException should never happen
100      */
101     public static void main(final String[] args) throws SimRuntimeException
102     {
103         SwingUtilities.invokeLater(new Runnable()
104         {
105             @Override
106             public void run()
107             {
108                 SuitabilityGraph suitabilityGraph = new SuitabilityGraph();
109                 new SimulatorFrame("Suitability graph", suitabilityGraph.getPanel());
110                 try
111                 {
112                     suitabilityGraph.drawPlots();
113                 }
114                 catch (NamingException | NetworkException | SimRuntimeException | OTSGeometryException | GTUException exception)
115                 {
116                     exception.printStackTrace();
117                 }
118             }
119         });
120     }
121 
122     /**
123      * Draw the plots.
124      * @throws NetworkException on network inconsistency
125      * @throws NamingException on ???
126      * @throws SimRuntimeException on ???
127      * @throws OTSGeometryException x
128      * @throws GTUException x
129      */
130     protected final void drawPlots()
131             throws NamingException, NetworkException, SimRuntimeException, OTSGeometryException, GTUException
132     {
133         SimpleSimulator simulator = new SimpleSimulator(new Time(0, TimeUnit.BASE_SECOND), new Duration(0, DurationUnit.SI),
134                 new Duration(99999, DurationUnit.SI), this);
135         final int rows = SPEEDLIMITS.length;
136         final int columns = TARGETLANES.length;
137         for (int row = 0; row < rows; row++)
138         {
139             int targetLaneConfiguration = TARGETLANES[row];
140             for (int column = 0; column < columns; column++)
141             {
142                 Network network = new OTSNetwork("suitability graph network");
143                 Speed speedLimit = new Speed(SPEEDLIMITS[column], KM_PER_HOUR);
144                 double mainLength = speedLimit.getSI() * this.timeRange.getSI();
145                 OTSNode from = new OTSNode(network, "From", new OTSPoint3D(-mainLength, 0, 0));
146                 OTSNode branchPoint = new OTSNode(network, "Branch point", new OTSPoint3D(0, 0, 0));
147                 GTUType gtuType = CAR;
148                 LaneType laneType = LaneType.TWO_WAY_LANE;
149                 Lane[] lanes = LaneFactory.makeMultiLane(network, "Test road", from, branchPoint, null, LANECOUNT, laneType,
150                         speedLimit, simulator);
151                 OTSNode destination =
152                         new OTSNode(network, "Destination", new OTSPoint3D(1000, targetLaneConfiguration > 0 ? 100 : -100, 0));
153                 LaneFactory.makeMultiLane(network, "DestinationLink", branchPoint, destination, null,
154                         Math.abs(targetLaneConfiguration),
155                         targetLaneConfiguration > 0 ? 0 : LANECOUNT + targetLaneConfiguration, 0, laneType, speedLimit,
156                         simulator);
157                 OTSNode nonDestination = new OTSNode(network, "Non-Destination",
158                         new OTSPoint3D(1000, targetLaneConfiguration > 0 ? -100 : 100, 0));
159                 LaneFactory.makeMultiLane(network, "Non-DestinationLink", branchPoint, nonDestination, null,
160                         LANECOUNT - Math.abs(targetLaneConfiguration),
161                         targetLaneConfiguration > 0 ? LANECOUNT - targetLaneConfiguration : 0, 0, laneType, speedLimit,
162                         simulator);
163                 CompleteRoute route = new CompleteRoute("route", gtuType);
164                 route.addNode(from);
165                 route.addNode(branchPoint);
166                 route.addNode(destination);
167                 SuitabilityData dataset = (SuitabilityData) ((XYPlot) (this.charts[row][column].getPlot())).getDataset();
168                 for (int laneIndex = 0; laneIndex < LANECOUNT; laneIndex++)
169                 {
170                     int key = dataset.addSeries("Lane " + (laneIndex + 1));
171                     Lane lane = lanes[laneIndex];
172                     for (int position = 0; position <= mainLength; position += 10)
173                     {
174                         Length longitudinalPosition = new Length(position, METER);
175                         // TODO Length suitability =
176                         // navigator.suitability(lane, longitudinalPosition, null, this.timeHorizon);
177                         // if (suitability.getSI() <= mainLength)
178                         // {
179                         // dataset.addXYPair(key, mainLength - position, suitability.getSI());
180                         // }
181                     }
182                     dataset.reGraph();
183                 }
184             }
185         }
186     }
187 
188     /**
189      * Instantiate the class.
190      */
191     public SuitabilityGraph()
192     {
193         this.graphPanel = new JPanel(new BorderLayout());
194         final int rows = SPEEDLIMITS.length;
195         final int columns = TARGETLANES.length;
196         TablePanel chartsPanel = new TablePanel(rows, rows);
197         this.graphPanel.add(chartsPanel, BorderLayout.CENTER);
198         this.charts = new JFreeChart[rows][columns];
199         for (int row = 0; row < rows; row++)
200         {
201             int targetLaneConfiguration = TARGETLANES[row];
202             String targetLaneDescription =
203                     String.format("%s lane %s exit", Math.abs(targetLaneConfiguration) == 1 ? "single" : "double",
204                             targetLaneConfiguration > 0 ? "left" : "right");
205             for (int column = 0; column < columns; column++)
206             {
207                 Speed speedLimit = new Speed(SPEEDLIMITS[column], KM_PER_HOUR);
208                 JFreeChart chart = createChart(String.format("Speed limit %.0f%s, %s", speedLimit.getInUnit(),
209                         speedLimit.getUnit(), targetLaneDescription), speedLimit);
210                 chartsPanel.setCell(new ChartPanel(chart), column, row);
211                 this.charts[row][column] = chart;
212             }
213         }
214     }
215 
216     /**
217      * @param caption String; the caption for the chart
218      * @param speedLimit Speed; the speed limit
219      * @return JFreeChart; the newly created graph
220      */
221     private JFreeChart createChart(final String caption, final Speed speedLimit)
222     {
223         ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", false));
224         XYDataset chartData = new SuitabilityData();
225         JFreeChart chartPanel =
226                 ChartFactory.createXYLineChart(caption, "", "", chartData, PlotOrientation.VERTICAL, true, false, false);
227         chartPanel.setBorderVisible(true);
228         chartPanel.setBorderPaint(new Color(192, 192, 192));
229         NumberAxis timeAxis = new NumberAxis("\u2192 " + "Remaining time to junction [s]");
230         double distanceRange = this.timeRange.getSI() * speedLimit.getSI();
231         NumberAxis distanceAxis = new NumberAxis("\u2192 " + "Remaining distance to junction [m]");
232         distanceAxis.setRange(0, distanceRange);
233         distanceAxis.setInverted(true);
234         distanceAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
235         timeAxis.setAutoRangeIncludesZero(true);
236         timeAxis.setRange(0, this.timeRange.getSI());
237         timeAxis.setInverted(true);
238         timeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
239         // time axis gets messed up on auto range (probably due to all data being relative to distance axis)
240         // ((XYPlot) chartPanel.getPlot()).setDomainAxis(1, timeAxis);
241         NumberAxis yAxis = new NumberAxis("\u2192 " + "Distance to vacate lane [m]");
242         yAxis.setAutoRangeIncludesZero(true);
243         yAxis.setRange(-0.1, distanceRange);
244         chartPanel.getXYPlot().setDomainAxis(distanceAxis);
245         chartPanel.getXYPlot().setRangeAxis(yAxis);
246         final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chartPanel.getXYPlot().getRenderer();
247         renderer.setDefaultLinesVisible(true);
248         renderer.setDefaultShapesVisible(false);
249         // Set paint color and stroke for each series
250         for (int index = 0; index < LANECOUNT; index++)
251         {
252             renderer.setSeriesPaint(index, COLORTABLE[index]);
253             renderer.setSeriesStroke(index, new BasicStroke(4.0f));
254         }
255 
256         return chartPanel;
257     }
258 
259     /**
260      * Return the JPanel that contains all the graphs.
261      * @return JPanel
262      */
263     public final JPanel getPanel()
264     {
265         return this.graphPanel;
266     }
267 
268     /** {@inheritDoc} */
269     @Override
270     public final void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator)
271             throws SimRuntimeException
272     {
273         // Do nothing
274     }
275 
276     /** {@inheritDoc} */
277     @Override
278     public final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
279     {
280         return null;
281     }
282 
283     /** {@inheritDoc} */
284     @Override
285     public final OTSNetwork getNetwork()
286     {
287         return null; // multiple networks defined...
288     }
289     
290 }
291 
292 /** */
293 class SuitabilityData implements XYDataset
294 {
295 
296     /** The X values. */
297     private ArrayList<ArrayList<Double>> xValues = new ArrayList<ArrayList<Double>>();
298 
299     /** The Y values. */
300     private ArrayList<ArrayList<Double>> yValues = new ArrayList<ArrayList<Double>>();
301 
302     /** The names of the series. */
303     private ArrayList<String> seriesKeys = new ArrayList<String>();
304 
305     /** List of parties interested in changes of this ContourPlot. */
306     private transient EventListenerList listenerList = new EventListenerList();
307 
308     /** Not used internally. */
309     private DatasetGroup datasetGroup = null;
310 
311     /**
312      * Redraw this graph (after the underlying data has been changed).
313      */
314     public final void reGraph()
315     {
316         notifyListeners(new DatasetChangeEvent(this, null)); // This guess work actually works!
317     }
318 
319     /**
320      * Notify interested parties of an event affecting this TrajectoryPlot.
321      * @param event DatasetChangedEvent
322      */
323     private void notifyListeners(final DatasetChangeEvent event)
324     {
325         for (DatasetChangeListener dcl : this.listenerList.getListeners(DatasetChangeListener.class))
326         {
327             dcl.datasetChanged(event);
328         }
329     }
330 
331     /**
332      * Add storage for another series of XY values.
333      * @param seriesName String; the name of the new series
334      * @return int; the index to use to address the new series
335      */
336     public final int addSeries(final String seriesName)
337     {
338         this.xValues.add(new ArrayList<Double>());
339         this.yValues.add(new ArrayList<Double>());
340         this.seriesKeys.add(seriesName);
341         return this.xValues.size() - 1;
342     }
343 
344     /**
345      * Add an XY pair to the data.
346      * @param seriesKey int; key to the data series
347      * @param x double; x value of the pair
348      * @param y double; y value of the pair
349      */
350     public final void addXYPair(final int seriesKey, final double x, final double y)
351     {
352         this.xValues.get(seriesKey).add(x);
353         this.yValues.get(seriesKey).add(y);
354     }
355 
356     /** {@inheritDoc} */
357     @Override
358     public final int getSeriesCount()
359     {
360         return this.seriesKeys.size();
361     }
362 
363     /** {@inheritDoc} */
364     @Override
365     public final Comparable<?> getSeriesKey(final int series)
366     {
367         return this.seriesKeys.get(series);
368     }
369 
370     /** {@inheritDoc} */
371     @Override
372     public final int indexOf(@SuppressWarnings("rawtypes") final Comparable seriesKey)
373     {
374         return this.seriesKeys.indexOf(seriesKey);
375     }
376 
377     /** {@inheritDoc} */
378     @Override
379     public final void addChangeListener(final DatasetChangeListener listener)
380     {
381         this.listenerList.add(DatasetChangeListener.class, listener);
382     }
383 
384     /** {@inheritDoc} */
385     @Override
386     public final void removeChangeListener(final DatasetChangeListener listener)
387     {
388         this.listenerList.remove(DatasetChangeListener.class, listener);
389     }
390 
391     /** {@inheritDoc} */
392     @Override
393     public final DatasetGroup getGroup()
394     {
395         return this.datasetGroup;
396     }
397 
398     /** {@inheritDoc} */
399     @Override
400     public final void setGroup(final DatasetGroup group)
401     {
402         this.datasetGroup = group;
403     }
404 
405     /** {@inheritDoc} */
406     @Override
407     public DomainOrder getDomainOrder()
408     {
409         return DomainOrder.ASCENDING;
410     }
411 
412     /** {@inheritDoc} */
413     @Override
414     public final int getItemCount(final int series)
415     {
416         return this.xValues.get(series).size();
417     }
418 
419     /** {@inheritDoc} */
420     @Override
421     public final Number getX(final int series, final int item)
422     {
423         return this.xValues.get(series).get(item);
424     }
425 
426     /** {@inheritDoc} */
427     @Override
428     public final double getXValue(final int series, final int item)
429     {
430         return this.xValues.get(series).get(item);
431     }
432 
433     /** {@inheritDoc} */
434     @Override
435     public final Number getY(final int series, final int item)
436     {
437         return this.yValues.get(series).get(item);
438     }
439 
440     /** {@inheritDoc} */
441     @Override
442     public final double getYValue(final int series, final int item)
443     {
444         return this.yValues.get(series).get(item);
445     }
446 
447 }