package org.opentrafficsim.demo.carFollowing; import java.awt.Frame; import java.awt.geom.Rectangle2D; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Random; import java.util.Set; import javax.naming.NamingException; import javax.swing.JPanel; import javax.swing.SwingUtilities; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.gui.swing.TablePanel; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; import org.djunits.unit.TimeUnit; import org.djunits.unit.UNITS; import org.djunits.value.vdouble.scalar.Acceleration; import org.djunits.value.vdouble.scalar.DoubleScalar; import org.djunits.value.vdouble.scalar.Length; import org.djunits.value.vdouble.scalar.Speed; import org.djunits.value.vdouble.scalar.Time; 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.GTUDirectionality; import org.opentrafficsim.core.gtu.GTUException; import org.opentrafficsim.core.gtu.GTUType; import org.opentrafficsim.core.gtu.animation.GTUColorer; import org.opentrafficsim.core.network.LongitudinalDirectionality; import org.opentrafficsim.core.network.NetworkException; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.core.network.OTSNode; import org.opentrafficsim.graphs.FundamentalDiagramLane; import org.opentrafficsim.road.car.LaneBasedIndividualCar; import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation; import org.opentrafficsim.road.gtu.lane.driver.LaneBasedDrivingCharacteristics; import org.opentrafficsim.road.gtu.lane.perception.LanePerceptionFull; import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner; import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModel; import org.opentrafficsim.road.gtu.lane.tactical.following.IDM; import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlus; import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.AbstractLaneChangeModel; import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic; import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner; import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner; import org.opentrafficsim.road.network.factory.LaneFactory; import org.opentrafficsim.road.network.lane.CrossSectionLink; import org.opentrafficsim.road.network.lane.DirectedLanePosition; import org.opentrafficsim.road.network.lane.Lane; import org.opentrafficsim.road.network.lane.LaneType; import org.opentrafficsim.road.network.lane.Sensor; import org.opentrafficsim.road.network.lane.SinkSensor; import org.opentrafficsim.road.network.lane.changing.OvertakingConditions; import org.opentrafficsim.simulationengine.AbstractWrappableAnimation; import org.opentrafficsim.simulationengine.OTSSimulationException; import org.opentrafficsim.simulationengine.properties.AbstractProperty; import org.opentrafficsim.simulationengine.properties.ProbabilityDistributionProperty; import org.opentrafficsim.simulationengine.properties.PropertyException; import org.opentrafficsim.simulationengine.properties.SelectionProperty; /** * Demonstrate the FundamentalDiagram plot. *
* 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: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
* initial version 17 dec. 2014
* @author Peter Knoppers
*/
public class FundamentalDiagramsLane extends AbstractWrappableAnimation implements UNITS
{
/** the model. */
private FundamentalDiagramLanePlotsModel model;
/** Create a FundamentalDiagrams simulation. */
public FundamentalDiagramsLane()
{
try
{
this.properties.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, 500));
this.properties.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();
}
}
/** {@inheritDoc} */
@Override
public final void stopTimersThreads()
{
super.stopTimersThreads();
this.model = null;
}
/**
* Main program.
* @param args String[]; the command line arguments (not used)
* @throws SimRuntimeException on ???
*/
public static void main(final String[] args) throws SimRuntimeException
{
// Create the simulation and wrap its panel in a JFrame. It does not get much easier/shorter than this...
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
FundamentalDiagramsLane fundamentalDiagramsLane = new FundamentalDiagramsLane();
fundamentalDiagramsLane.buildAnimator(new Time.Abs(0.0, SECOND), new Time.Rel(0.0, SECOND),
new Time.Rel(3600.0, SECOND), fundamentalDiagramsLane.getProperties(), null, true);
}
catch (SimRuntimeException | NamingException | OTSSimulationException exception)
{
exception.printStackTrace();
}
}
});
}
/** {@inheritDoc} */
@Override
protected final OTSModelInterface makeModel(final GTUColorer colorer)
{
this.model = new FundamentalDiagramLanePlotsModel(this.savedUserModifiedProperties, colorer);
return this.model;
}
/** {@inheritDoc} */
@Override
protected final Rectangle2D.Double makeAnimationRectangle()
{
return new Rectangle2D.Double(0, -100, 5000, 200);
}
/** {@inheritDoc} */
@Override
protected final JPanel makeCharts() throws OTSSimulationException
{
final int panelsPerRow = 3;
TablePanel charts = new TablePanel(3, panelsPerRow);
for (int plotNumber = 0; plotNumber < 9; plotNumber++)
{
FundamentalDiagramLane fd;
try
{
Lane lane = this.model.getLane(plotNumber);
int xs = (int) lane.getParentLink().getStartNode().getPoint().x;
int xe = (int) lane.getParentLink().getEndNode().getPoint().x;
fd =
new FundamentalDiagramLane("Fundamental Diagram for [" + xs + ", " + xe + "] m", new Time.Rel(1.0,
SECOND), lane, (OTSDEVSSimulatorInterface) this.model.getSimulator());
fd.setTitle("Fundamental Diagram Graph");
fd.setExtendedState(Frame.MAXIMIZED_BOTH);
this.model.getFundamentalDiagrams().add(fd);
charts.setCell(fd.getContentPane(), plotNumber / panelsPerRow, plotNumber % panelsPerRow);
}
catch (NetworkException | RemoteException | SimRuntimeException exception)
{
exception.printStackTrace();
}
}
return charts;
}
/** {@inheritDoc} */
@Override
public final String shortName()
{
return "Fundamental Diagrams";
}
/** {@inheritDoc} */
@Override
public final String description()
{
return "
* 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: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
* initial version ug 1, 2014
* @author Peter Knoppers
*/
class FundamentalDiagramLanePlotsModel implements OTSModelInterface, UNITS
{
/** */
private static final long serialVersionUID = 20140820L;
/** network. */
private OTSNetwork network = new OTSNetwork("network");
/** the simulator. */
private OTSDEVSSimulatorInterface simulator;
/** the headway (inter-vehicle time). */
private Time.Rel headway;
/** number of cars created. */
private int carsCreated = 0;
/** Type of all GTUs. */
private GTUType gtuType = GTUType.makeGTUType("Car");
/** 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 blocking car. */
private LaneBasedIndividualCar block = null;
/** starting x-position. */
private Length.Rel startX = new Length.Rel(0, METER);
/** length per lane. */
private Length.Rel laneLength = new Length.Rel(500, METER);
/** The Lanes containing the simulated Cars. */
private List