package org.opentrafficsim.demo.carFollowing;
import java.awt.Container;
import java.awt.Frame;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.naming.NamingException;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import nl.tudelft.simulation.dsol.SimRuntimeException;
import nl.tudelft.simulation.dsol.gui.swing.HTMLPanel;
import nl.tudelft.simulation.dsol.gui.swing.TablePanel;
import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
import org.opentrafficsim.core.car.LaneBasedIndividualCar;
import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
import org.opentrafficsim.core.dsol.OTSModelInterface;
import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
import org.opentrafficsim.core.geometry.OTSGeometryException;
import org.opentrafficsim.core.geometry.OTSPoint3D;
import org.opentrafficsim.core.gtu.GTUException;
import org.opentrafficsim.core.gtu.GTUType;
import org.opentrafficsim.core.gtu.animation.DefaultCarAnimation;
import org.opentrafficsim.core.gtu.animation.GTUColorer;
import org.opentrafficsim.core.gtu.following.GTUFollowingModel;
import org.opentrafficsim.core.gtu.following.IDM;
import org.opentrafficsim.core.gtu.following.IDMPlus;
import org.opentrafficsim.core.gtu.lane.changing.AbstractLaneChangeModel;
import org.opentrafficsim.core.gtu.lane.changing.Egoistic;
import org.opentrafficsim.core.network.NetworkException;
import org.opentrafficsim.core.network.OTSNode;
import org.opentrafficsim.core.network.factory.LaneFactory;
import org.opentrafficsim.core.network.lane.CrossSectionLink;
import org.opentrafficsim.core.network.lane.Lane;
import org.opentrafficsim.core.network.lane.LaneType;
import org.opentrafficsim.core.network.lane.Sensor;
import org.opentrafficsim.core.network.lane.SinkSensor;
import org.opentrafficsim.core.network.route.CompleteRoute;
import org.opentrafficsim.core.network.route.CompleteLaneBasedRouteNavigator;
import org.opentrafficsim.core.unit.AccelerationUnit;
import org.opentrafficsim.core.unit.LengthUnit;
import org.opentrafficsim.core.unit.SpeedUnit;
import org.opentrafficsim.core.unit.TimeUnit;
import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Abs;
import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Rel;
import org.opentrafficsim.graphs.AccelerationContourPlot;
import org.opentrafficsim.graphs.ContourPlot;
import org.opentrafficsim.graphs.DensityContourPlot;
import org.opentrafficsim.graphs.FlowContourPlot;
import org.opentrafficsim.graphs.LaneBasedGTUSampler;
import org.opentrafficsim.graphs.SpeedContourPlot;
import org.opentrafficsim.graphs.TrajectoryPlot;
import org.opentrafficsim.simulationengine.AbstractWrappableSimulation;
import org.opentrafficsim.simulationengine.WrappableSimulation;
import org.opentrafficsim.simulationengine.properties.AbstractProperty;
import org.opentrafficsim.simulationengine.properties.BooleanProperty;
import org.opentrafficsim.simulationengine.properties.CompoundProperty;
import org.opentrafficsim.simulationengine.properties.IDMPropertySet;
import org.opentrafficsim.simulationengine.properties.ProbabilityDistributionProperty;
import org.opentrafficsim.simulationengine.properties.PropertyException;
import org.opentrafficsim.simulationengine.properties.SelectionProperty;
/**
* Single lane road consisting of three consecutive links.
* Tests that GTUs correctly transfer themselves onto the next lane and that the graph samplers handle this situation.
*
* Copyright (c) 2013-2015 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 30 jan. 2015
* @author Alexander Verbraeck
* @author Peter Knoppers
*/
public class SequentialLanes extends AbstractWrappableSimulation implements WrappableSimulation
{
/** the model. */
private SequentialModel model;
/** Create a SequentialLanes simulation. */
public SequentialLanes()
{
ArrayList> outputProperties = new ArrayList>();
outputProperties.add(new BooleanProperty("Density", "Density contour plot", true, false, 0));
outputProperties.add(new BooleanProperty("Flow", "Flow contour plot", true, false, 1));
outputProperties.add(new BooleanProperty("Speed", "Speed contour plot", true, false, 2));
outputProperties.add(new BooleanProperty("Acceleration", "Acceleration contour plot", true, false, 3));
outputProperties.add(new BooleanProperty("Trajectories", "Trajectory (time/distance) diagram", true, false, 4));
this.properties.add(new CompoundProperty("Output graphs", "Select the graphical output", outputProperties, true,
1000));
}
/** {@inheritDoc} */
@Override
public final void stopTimersThreads()
{
super.stopTimersThreads();
this.model = null;
}
/**
* Main program.
* @param args String[]; the command line arguments (not used)
* @throws RemoteException on communications failure
* @throws SimRuntimeException when simulation cannot be created with given parameters
*/
public static void main(final String[] args) throws RemoteException, SimRuntimeException
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
SequentialLanes sequential = new SequentialLanes();
ArrayList> localProperties = sequential.getProperties();
try
{
localProperties.add(new ProbabilityDistributionProperty("Traffic composition",
"Mix of passenger cars and trucks", new String[]{"passenger car", "truck"},
new Double[]{0.8, 0.2}, false, 10));
}
catch (PropertyException exception)
{
exception.printStackTrace();
}
localProperties.add(new SelectionProperty("Car following model",
"The car following model determines "
+ "the acceleration that a vehicle will make taking into account "
+ "nearby vehicles, infrastructural restrictions (e.g. speed limit, "
+ "curvature of the road) capabilities of the vehicle and personality "
+ "of the driver.", new String[]{"IDM", "IDM+"}, 1, false, 1));
localProperties.add(IDMPropertySet.makeIDMPropertySet("Car", new DoubleScalar.Abs(1.0,
AccelerationUnit.METER_PER_SECOND_2), new DoubleScalar.Abs(1.5,
AccelerationUnit.METER_PER_SECOND_2), new DoubleScalar.Rel(2.0, LengthUnit.METER),
new DoubleScalar.Rel(1.0, TimeUnit.SECOND), 2));
localProperties.add(IDMPropertySet.makeIDMPropertySet("Truck", new DoubleScalar.Abs(
0.5, AccelerationUnit.METER_PER_SECOND_2), new DoubleScalar.Abs(1.25,
AccelerationUnit.METER_PER_SECOND_2), new DoubleScalar.Rel(2.0, LengthUnit.METER),
new DoubleScalar.Rel(1.0, TimeUnit.SECOND), 3));
sequential.buildSimulator(localProperties, null, true);
sequential.panel.getTabbedPane().addTab("info", sequential.makeInfoPane());
}
catch (RemoteException | SimRuntimeException | NamingException exception)
{
exception.printStackTrace();
}
}
});
}
/** {@inheritDoc} */
@Override
protected final Rectangle2D.Double makeAnimationRectangle()
{
return new Rectangle2D.Double(0, -100, 2010, 200);
}
/** {@inheritDoc} */
@Override
protected final OTSModelInterface makeModel(final GTUColorer colorer)
{
this.model = new SequentialModel(this.savedUserModifiedProperties, colorer);
return this.model;
}
/**
* @return an info pane to be added to the tabbed pane.
*/
protected final JComponent makeInfoPane()
{
// Make the info tab
String helpSource = "/" + StraightModel.class.getPackage().getName().replace('.', '/') + "/IDMPlus.html";
URL page = StraightModel.class.getResource(helpSource);
if (page != null)
{
try
{
HTMLPanel htmlPanel = new HTMLPanel(page);
return new JScrollPane(htmlPanel);
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
return new JPanel();
}
/** {@inheritDoc} */
@Override
protected final JPanel makeCharts()
{
// Make the tab with the plots
AbstractProperty> output =
new CompoundProperty("", "", this.properties, false, 0).findByShortName("Output graphs");
if (null == output)
{
throw new Error("Cannot find output properties");
}
ArrayList graphs = new ArrayList();
if (output instanceof CompoundProperty)
{
CompoundProperty outputProperties = (CompoundProperty) output;
for (AbstractProperty> ap : outputProperties.getValue())
{
if (ap instanceof BooleanProperty)
{
BooleanProperty bp = (BooleanProperty) ap;
if (bp.getValue())
{
graphs.add(bp);
}
}
}
}
else
{
throw new Error("output properties should be compound");
}
int graphCount = graphs.size();
int columns = (int) Math.ceil(Math.sqrt(graphCount));
int rows = 0 == columns ? 0 : (int) Math.ceil(graphCount * 1.0 / columns);
TablePanel charts = new TablePanel(columns, rows);
for (int i = 0; i < graphCount; i++)
{
String graphName = graphs.get(i).getShortName();
Container container = null;
LaneBasedGTUSampler graph;
if (graphName.contains("Trajectories"))
{
TrajectoryPlot tp =
new TrajectoryPlot("TrajectoryPlot", new DoubleScalar.Rel(0.5, TimeUnit.SECOND), this.model
.getPath());
tp.setTitle("Trajectory Graph");
tp.setExtendedState(Frame.MAXIMIZED_BOTH);
graph = tp;
container = tp.getContentPane();
}
else
{
ContourPlot cp;
if (graphName.contains("Density"))
{
cp = new DensityContourPlot("DensityPlot", this.model.getPath());
cp.setTitle("Density Contour Graph");
}
else if (graphName.contains("Speed"))
{
cp = new SpeedContourPlot("SpeedPlot", this.model.getPath());
cp.setTitle("Speed Contour Graph");
}
else if (graphName.contains("Flow"))
{
cp = new FlowContourPlot("FlowPlot", this.model.getPath());
cp.setTitle("Flow Contour Graph");
}
else if (graphName.contains("Acceleration"))
{
cp = new AccelerationContourPlot("AccelerationPlot", this.model.getPath());
cp.setTitle("Acceleration Contour Graph");
}
else
{
continue;
// throw new Error("Unhandled type of contourplot: " + graphName);
}
graph = cp;
container = cp.getContentPane();
}
// Add the container to the matrix
charts.setCell(container, i % columns, i / columns);
this.model.getPlots().add(graph);
}
return charts;
}
/** {@inheritDoc} */
@Override
public final String shortName()
{
return "Sequential Lanes";
}
/** {@inheritDoc} */
@Override
public final String description()
{
return "Simulation of a straight one-lane road consisting of three consecutive Links
"
+ "Simulation of a single lane road consisting of two 1 km stretches with a 1m stretch in between. "
+ "This will test transition of a GTU from one lane section onto the next.
"
+ "Vehicles are generated at a constant rate of 1500 veh/hour.
"
+ "Selected trajectory and contour plots are generated during the simulation.";
}
}
/**
* Build the sequential model.
*
* Copyright (c) 2013-2015 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 0 jan. 2015
* @author Peter Knoppers
*/
class SequentialModel implements OTSModelInterface
{
/** */
private static final long serialVersionUID = 20150130L;
/** the simulator. */
private OTSDEVSSimulatorInterface simulator;
/** The nodes of our network in the order that all GTUs will visit them. */
private ArrayList> nodes = new ArrayList>();
/** the car following model, e.g. IDM Plus for cars. */
private GTUFollowingModel carFollowingModelCars;
/** the car following model, e.g. IDM Plus for trucks. */
private GTUFollowingModel carFollowingModelTrucks;
/** The probability that the next generated GTU is a passenger car. */
private double carProbability;
/** The lane change model. */
private AbstractLaneChangeModel laneChangeModel = new Egoistic();
/** the headway (inter-vehicle time). */
private DoubleScalar.Rel headway;
/** number of cars created. */
private int carsCreated = 0;
/** Type of all GTUs. */
private GTUType gtuType = GTUType.makeGTUType("Car");
/** minimum distance. */
private DoubleScalar.Rel minimumDistance = new DoubleScalar.Rel(0, LengthUnit.METER);
/** The Lane where newly created Cars initially placed on. */
private Lane, ?> initialLane;
/** maximum distance. */
private DoubleScalar.Rel maximumDistance = new DoubleScalar.Rel(2001, LengthUnit.METER);
/** the contour plots. */
private ArrayList plots = new ArrayList();
/** The random number generator used to decide what kind of GTU to generate. */
private Random randomGenerator = new Random(12345);
/** User settable properties. */
private ArrayList> properties = null;
/** The sequence of Lanes that all vehicles will follow. */
private List> path = new ArrayList>();
/** The speedLimit on all Lanes. */
private DoubleScalar.Abs speedLimit;
/** The GTUColorer for the generated vehicles. */
private final GTUColorer gtuColorer;
/**
* @param properties the user settable properties
* @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer.
*/
public SequentialModel(final ArrayList> properties, final GTUColorer gtuColorer)
{
this.properties = properties;
this.gtuColorer = gtuColorer;
}
/**
* @return a newly created path (which all GTUs in this simulation will follow).
*/
public List> getPath()
{
return new ArrayList>(this.path);
}
/** {@inheritDoc} */
@Override
public final void constructModel(final SimulatorInterface, Rel, OTSSimTimeDouble> theSimulator)
throws SimRuntimeException, RemoteException
{
this.simulator = (OTSDEVSSimulatorInterface) theSimulator;
this.speedLimit = new DoubleScalar.Abs(100, SpeedUnit.KM_PER_HOUR);
this.nodes = new ArrayList>();
// TODO use: int[] linkBoundaries = {0, 1000, 1001, 2001, 2200};
int[] linkBoundaries = {0, 1000, 2001, 2200};
for (int xPos : linkBoundaries)
{
this.nodes.add(new OTSNode("Node at " + xPos, new OTSPoint3D(xPos, xPos > 1001 ? 200 : -10, 0)));
}
LaneType laneType = new LaneType("CarLane");
laneType.addCompatibility(this.gtuType);
// Now we can build a series of Links with one Lane on them
ArrayList> links = new ArrayList>();
for (int i = 1; i < this.nodes.size(); i++)
{
OTSNode fromNode = this.nodes.get(i - 1);
OTSNode toNode = this.nodes.get(i);
String linkName = fromNode.getId() + "-" + toNode.getId();
try
{
Lane[] lanes =
LaneFactory
.makeMultiLane(linkName, fromNode, toNode, null, 1, laneType, this.speedLimit, this.simulator);
if (i == this.nodes.size() - 1)
{
Sensor sensor =
new SinkSensor(lanes[0], new DoubleScalar.Rel(10.0, LengthUnit.METER), this.simulator);
lanes[0].addSensor(sensor);
}
this.path.add(lanes[0]);
links.add(lanes[0].getParentLink());
if (1 == i)
{
this.initialLane = lanes[0];
}
}
catch (NamingException | NetworkException | OTSGeometryException exception)
{
exception.printStackTrace();
}
}
// 1500 [veh / hour] == 2.4s headway
this.headway = new DoubleScalar.Rel(3600.0 / 1500.0, TimeUnit.SECOND);
// Schedule creation of the first car (it will re-schedule itself one headway later, etc.).
this.simulator.scheduleEventAbs(new DoubleScalar.Abs(0.0, TimeUnit.SECOND), this, this, "generateCar",
null);
// Schedule regular updates of the graphs
for (int t = 1; t <= 1800; t++)
{
this.simulator.scheduleEventAbs(new DoubleScalar.Abs(t - 0.001, TimeUnit.SECOND), this, this,
"drawGraphs", null);
}
try
{
String carFollowingModelName = null;
CompoundProperty propertyContainer = new CompoundProperty("", "", this.properties, false, 0);
AbstractProperty> cfmp = propertyContainer.findByShortName("Car following model");
if (null == cfmp)
{
throw new Error("Cannot find \"Car following model\" property");
}
if (cfmp instanceof SelectionProperty)
{
carFollowingModelName = ((SelectionProperty) cfmp).getValue();
}
else
{
throw new Error("\"Car following model\" property has wrong type");
}
Iterator>>> iterator =
new CompoundProperty("", "", this.properties, false, 0).iterator();
while (iterator.hasNext())
{
AbstractProperty> ap = iterator.next();
if (ap instanceof SelectionProperty)
{
SelectionProperty sp = (SelectionProperty) ap;
if ("Car following model".equals(sp.getShortName()))
{
carFollowingModelName = sp.getValue();
}
}
else if (ap instanceof ProbabilityDistributionProperty)
{
ProbabilityDistributionProperty pdp = (ProbabilityDistributionProperty) ap;
if (ap.getShortName().equals("Traffic composition"))
{
this.carProbability = pdp.getValue()[0];
}
}
else if (ap instanceof CompoundProperty)
{
CompoundProperty cp = (CompoundProperty) ap;
if (ap.getShortName().equals("Output graphs"))
{
continue; // Output settings are handled elsewhere
}
if (ap.getShortName().contains("IDM"))
{
// System.out.println("Car following model name appears to be " + ap.getShortName());
DoubleScalar.Abs a = IDMPropertySet.getA(cp);
DoubleScalar.Abs b = IDMPropertySet.getB(cp);
DoubleScalar.Rel s0 = IDMPropertySet.getS0(cp);
DoubleScalar.Rel tSafe = IDMPropertySet.getTSafe(cp);
GTUFollowingModel gtuFollowingModel = null;
if (carFollowingModelName.equals("IDM"))
{
gtuFollowingModel = new IDM(a, b, s0, tSafe, 1.0);
}
else if (carFollowingModelName.equals("IDM+"))
{
gtuFollowingModel = new IDMPlus(a, b, s0, tSafe, 1.0);
}
else
{
throw new Error("Unknown gtu following model: " + carFollowingModelName);
}
if (ap.getShortName().contains(" Car "))
{
this.carFollowingModelCars = gtuFollowingModel;
}
else if (ap.getShortName().contains(" Truck "))
{
this.carFollowingModelTrucks = gtuFollowingModel;
}
else
{
throw new Error("Cannot determine gtu type for " + ap.getShortName());
}
}
}
}
}
catch (Exception e)
{
System.out.println("Caught exception " + e);
}
}
/** {@inheritDoc} */
@Override
public SimulatorInterface, Rel, OTSSimTimeDouble> getSimulator() throws RemoteException
{
return this.simulator;
}
/**
* @return contourPlots
*/
public final ArrayList getPlots()
{
return this.plots;
}
/**
* @return minimumDistance
*/
public final DoubleScalar.Rel getMinimumDistance()
{
return this.minimumDistance;
}
/**
* @return maximumDistance
*/
public final DoubleScalar.Rel getMaximumDistance()
{
return this.maximumDistance;
}
/**
* Notify the contour plots that the underlying data has changed.
*/
protected final void drawGraphs()
{
for (LaneBasedGTUSampler plot : this.plots)
{
plot.reGraph();
}
}
/**
* Generate cars at a fixed rate (implemented by re-scheduling this method).
*/
protected final void generateCar()
{
boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability;
DoubleScalar.Rel initialPosition = new DoubleScalar.Rel(0, LengthUnit.METER);
DoubleScalar.Abs initialSpeed = new DoubleScalar.Abs(100, SpeedUnit.KM_PER_HOUR);
Map, DoubleScalar.Rel> initialPositions =
new LinkedHashMap, DoubleScalar.Rel>();
initialPositions.put(this.initialLane, initialPosition);
try
{
DoubleScalar.Rel vehicleLength =
new DoubleScalar.Rel(generateTruck ? 15 : 4, LengthUnit.METER);
GTUFollowingModel gtuFollowingModel = generateTruck ? this.carFollowingModelTrucks : this.carFollowingModelCars;
if (null == gtuFollowingModel)
{
throw new Error("gtuFollowingModel is null");
}
new LaneBasedIndividualCar<>(++this.carsCreated, this.gtuType, generateTruck ? this.carFollowingModelTrucks
: this.carFollowingModelCars, this.laneChangeModel, initialPositions, initialSpeed, vehicleLength,
new DoubleScalar.Rel(1.8, LengthUnit.METER), new DoubleScalar.Abs(200,
SpeedUnit.KM_PER_HOUR), new CompleteLaneBasedRouteNavigator(new CompleteRoute("")),
this.simulator, DefaultCarAnimation.class, this.gtuColorer);
this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
}
catch (RemoteException | SimRuntimeException | NamingException | NetworkException | GTUException exception)
{
exception.printStackTrace();
}
}
}