* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate$, @version $Revision$, by $Author$,
* initial version 15 apr. 2015
* @author Peter Knoppers
*/
public class SuitabilityGraph implements OTSModelInterface, UNITS
{
/** */
private static final long serialVersionUID = 20150415L;
/** The JPanel that contains all the graphs. */
private JPanel graphPanel;
/** Number of lanes on the main roadway (do not set higher than size of colorTable). */
private static final int LANECOUNT = 4;
/** Speed limit values in km/h. */
private static final double[] SPEEDLIMITS = { 30, 50, 80, 120 };
/** Arrangements of lanes to aim for. Negative numbers indicate lanes on right side of the roadway. */
private static final int[] TARGETLANES = { 1, 2, -2, -1 };
/** Time horizon for lane changes. */
private Duration timeHorizon = new Duration(100, SECOND);
/** Time range for graphs (also adjusts distance range). */
private Duration timeRange = new Duration(110, SECOND);
/** Colors that correspond to the lanes; taken from electrical resistor color codes. */
private static final Color[] COLORTABLE = { new Color(160, 82, 45) /* brown */, Color.RED, Color.ORANGE, Color.YELLOW,
Color.GREEN, Color.BLUE, new Color(199, 21, 133) /* violet */, Color.GRAY, Color.WHITE };
/** The graphs. */
private JFreeChart[][] charts;
/**
* Start the program.
* @param args String[]; command line arguments (not used)
* @throws SimRuntimeException should never happen
*/
public static void main(final String[] args) throws SimRuntimeException
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
SuitabilityGraph suitabilityGraph = new SuitabilityGraph();
new SimulatorFrame("Suitability graph", suitabilityGraph.getPanel());
try
{
suitabilityGraph.drawPlots();
}
catch (NamingException | NetworkException | SimRuntimeException | OTSGeometryException | GTUException exception)
{
exception.printStackTrace();
}
}
});
}
/**
* Draw the plots.
* @throws NetworkException on network inconsistency
* @throws NamingException on ???
* @throws SimRuntimeException on ???
* @throws OTSGeometryException x
* @throws GTUException x
*/
protected final void drawPlots()
throws NamingException, NetworkException, SimRuntimeException, OTSGeometryException, GTUException
{
SimpleSimulator simulator = new SimpleSimulator(new Time(0, TimeUnit.SI), new Duration(0, TimeUnit.SI),
new Duration(99999, TimeUnit.SI), this);
final int rows = SPEEDLIMITS.length;
final int columns = TARGETLANES.length;
for (int row = 0; row < rows; row++)
{
int targetLaneConfiguration = TARGETLANES[row];
for (int column = 0; column < columns; column++)
{
Network network = new OTSNetwork("suitability graph network");
Speed speedLimit = new Speed(SPEEDLIMITS[column], KM_PER_HOUR);
double mainLength = speedLimit.getSI() * this.timeRange.getSI();
OTSNode from = new OTSNode(network, "From", new OTSPoint3D(-mainLength, 0, 0));
OTSNode branchPoint = new OTSNode(network, "Branch point", new OTSPoint3D(0, 0, 0));
GTUType gtuType = CAR;
Set compatibility = new HashSet();
compatibility.add(gtuType);
LaneType laneType = new LaneType("CarLane", compatibility);
Lane[] lanes = LaneFactory.makeMultiLane(network, "Test road", from, branchPoint, null, LANECOUNT, laneType,
speedLimit, simulator, LongitudinalDirectionality.DIR_PLUS);
OTSNode destination =
new OTSNode(network, "Destination", new OTSPoint3D(1000, targetLaneConfiguration > 0 ? 100 : -100, 0));
LaneFactory.makeMultiLane(network, "DestinationLink", branchPoint, destination, null,
Math.abs(targetLaneConfiguration),
targetLaneConfiguration > 0 ? 0 : LANECOUNT + targetLaneConfiguration, 0, laneType, speedLimit,
simulator, LongitudinalDirectionality.DIR_PLUS);
OTSNode nonDestination = new OTSNode(network, "Non-Destination",
new OTSPoint3D(1000, targetLaneConfiguration > 0 ? -100 : 100, 0));
LaneFactory.makeMultiLane(network, "Non-DestinationLink", branchPoint, nonDestination, null,
LANECOUNT - Math.abs(targetLaneConfiguration),
targetLaneConfiguration > 0 ? LANECOUNT - targetLaneConfiguration : 0, 0, laneType, speedLimit,
simulator, LongitudinalDirectionality.DIR_PLUS);
CompleteRoute route = new CompleteRoute("route", gtuType);
route.addNode(from);
route.addNode(branchPoint);
route.addNode(destination);
SuitabilityData dataset = (SuitabilityData) ((XYPlot) (this.charts[row][column].getPlot())).getDataset();
for (int laneIndex = 0; laneIndex < LANECOUNT; laneIndex++)
{
int key = dataset.addSeries("Lane " + (laneIndex + 1));
Lane lane = lanes[laneIndex];
for (int position = 0; position <= mainLength; position += 10)
{
Length longitudinalPosition = new Length(position, METER);
// TODO Length suitability =
// navigator.suitability(lane, longitudinalPosition, null, this.timeHorizon);
// if (suitability.getSI() <= mainLength)
// {
// dataset.addXYPair(key, mainLength - position, suitability.getSI());
// }
}
dataset.reGraph();
}
}
}
}
/**
* Instantiate the class.
*/
public SuitabilityGraph()
{
this.graphPanel = new JPanel(new BorderLayout());
final int rows = SPEEDLIMITS.length;
final int columns = TARGETLANES.length;
TablePanel chartsPanel = new TablePanel(rows, rows);
this.graphPanel.add(chartsPanel, BorderLayout.CENTER);
this.charts = new JFreeChart[rows][columns];
for (int row = 0; row < rows; row++)
{
int targetLaneConfiguration = TARGETLANES[row];
String targetLaneDescription =
String.format("%s lane %s exit", Math.abs(targetLaneConfiguration) == 1 ? "single" : "double",
targetLaneConfiguration > 0 ? "left" : "right");
for (int column = 0; column < columns; column++)
{
Speed speedLimit = new Speed(SPEEDLIMITS[column], KM_PER_HOUR);
JFreeChart chart = createChart(String.format("Speed limit %.0f%s, %s", speedLimit.getInUnit(),
speedLimit.getUnit(), targetLaneDescription), speedLimit);
chartsPanel.setCell(new ChartPanel(chart), column, row);
this.charts[row][column] = chart;
}
}
}
/**
* @param caption String; the caption for the chart
* @param speedLimit Speed; the speed limit
* @return JFreeChart; the newly created graph
*/
private JFreeChart createChart(final String caption, final Speed speedLimit)
{
ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", false));
XYDataset chartData = new SuitabilityData();
JFreeChart chartPanel =
ChartFactory.createXYLineChart(caption, "", "", chartData, PlotOrientation.VERTICAL, true, false, false);
chartPanel.setBorderVisible(true);
chartPanel.setBorderPaint(new Color(192, 192, 192));
NumberAxis timeAxis = new NumberAxis("\u2192 " + "Remaining time to junction [s]");
double distanceRange = this.timeRange.getSI() * speedLimit.getSI();
NumberAxis distanceAxis = new NumberAxis("\u2192 " + "Remaining distance to junction [m]");
distanceAxis.setRange(0, distanceRange);
distanceAxis.setInverted(true);
distanceAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
timeAxis.setAutoRangeIncludesZero(true);
timeAxis.setRange(0, this.timeRange.getSI());
timeAxis.setInverted(true);
timeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// time axis gets messed up on auto range (probably due to all data being relative to distance axis)
// ((XYPlot) chartPanel.getPlot()).setDomainAxis(1, timeAxis);
NumberAxis yAxis = new NumberAxis("\u2192 " + "Distance to vacate lane [m]");
yAxis.setAutoRangeIncludesZero(true);
yAxis.setRange(-0.1, distanceRange);
chartPanel.getXYPlot().setDomainAxis(distanceAxis);
chartPanel.getXYPlot().setRangeAxis(yAxis);
final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chartPanel.getXYPlot().getRenderer();
renderer.setBaseLinesVisible(true);
renderer.setBaseShapesVisible(false);
// Set paint color and stroke for each series
for (int index = 0; index < LANECOUNT; index++)
{
renderer.setSeriesPaint(index, COLORTABLE[index]);
renderer.setSeriesStroke(index, new BasicStroke(4.0f));
}
return chartPanel;
}
/**
* Return the JPanel that contains all the graphs.
* @return JPanel
*/
public final JPanel getPanel()
{
return this.graphPanel;
}
/** {@inheritDoc} */
@Override
public final void constructModel(final SimulatorInterface