package org.opentrafficsim.demo.carFollowing; import static org.opentrafficsim.core.gtu.GTUType.CAR; import java.awt.Frame; 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.SwingUtilities; import org.djunits.unit.TimeUnit; import org.djunits.unit.UNITS; import org.djunits.value.vdouble.scalar.Acceleration; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Length; import org.djunits.value.vdouble.scalar.Speed; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.base.modelproperties.ProbabilityDistributionProperty; import org.opentrafficsim.base.modelproperties.Property; import org.opentrafficsim.base.modelproperties.PropertyException; import org.opentrafficsim.base.modelproperties.SelectionProperty; import org.opentrafficsim.base.parameters.Parameters; import org.opentrafficsim.core.compatibility.Compatible; import org.opentrafficsim.core.dsol.OTSModelInterface; 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.network.NetworkException; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.core.network.OTSNode; import org.opentrafficsim.graphs.FundamentalDiagram; import org.opentrafficsim.road.animation.AnimationToggles; import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation; import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU; import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner; import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld; import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld; import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld; 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.changing.OvertakingConditions; import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor; import org.opentrafficsim.simulationengine.AbstractWrappableAnimation; import org.opentrafficsim.simulationengine.OTSSimulationException; import org.opentrafficsim.simulationengine.SimpleSimulatorInterface; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.gui.swing.TablePanel; import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; /** * Demonstrate the FundamentalDiagram plot. *
* Copyright (c) 2013-2018 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 17 dec. 2014
* @author Peter Knoppers
*/
public class FundamentalDiagrams extends AbstractWrappableAnimation implements UNITS
{
/** */
private static final long serialVersionUID = 1L;
/** The model. */
private FundamentalDiagramPlotsModel model;
/** Create a FundamentalDiagrams simulation. */
public FundamentalDiagrams()
{
try
{
this.properties.add(new SelectionProperty("CarFollowingModel", "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("TrafficComposition", "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
{
FundamentalDiagrams fundamentalDiagrams = new FundamentalDiagrams();
fundamentalDiagrams.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600.0, SECOND),
fundamentalDiagrams.getProperties(), null, true);
}
catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
{
exception.printStackTrace();
}
}
});
}
/** {@inheritDoc} */
@Override
protected final OTSModelInterface makeModel()
{
this.model = new FundamentalDiagramPlotsModel(this.savedUserModifiedProperties);
return this.model;
}
/** {@inheritDoc} */
@Override
protected final void addAnimationToggles()
{
AnimationToggles.setTextAnimationTogglesStandard(this);
}
/** {@inheritDoc} */
@Override
protected final void addTabs(final SimpleSimulatorInterface simulator) throws OTSSimulationException
{
final int panelsPerRow = 3;
TablePanel charts = new TablePanel(4, panelsPerRow);
for (int plotNumber = 0; plotNumber < 10; plotNumber++)
{
Length detectorLocation = new Length(400 + 500 * plotNumber, METER);
FundamentalDiagram fd;
try
{
fd = new FundamentalDiagram("Fundamental Diagram at " + detectorLocation.getSI() + "m", new Duration(1, MINUTE),
this.model.getLane(), detectorLocation, Compatible.EVERYTHING, simulator);
fd.setTitle("Density Contour Graph");
fd.setExtendedState(Frame.MAXIMIZED_BOTH);
this.model.getFundamentalDiagrams().add(fd);
charts.setCell(fd.getContentPane(), plotNumber / panelsPerRow, plotNumber % panelsPerRow);
}
catch (NetworkException exception)
{
exception.printStackTrace();
}
}
addTab(getTabCount(), "statistics", charts);
}
/** {@inheritDoc} */
@Override
public final String shortName()
{
return "Fundamental Diagrams";
}
/** {@inheritDoc} */
@Override
public final String description()
{
return "
* Copyright (c) 2013-2018 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 ug 1, 2014
* @author Peter Knoppers
*/
class FundamentalDiagramPlotsModel implements OTSModelInterface, UNITS
{
/** */
private static final long serialVersionUID = 20140820L;
/** The simulator. */
private DEVSSimulatorInterface.TimeDoubleUnit simulator;
/** The network. */
private OTSNetwork network = new OTSNetwork("network");
/** The headway (inter-vehicle time). */
private Duration headway;
/** Number of cars created. */
private int carsCreated = 0;
/** Type of all GTUs. */
private GTUType gtuType = CAR;
/** The car following model, e.g. IDM Plus for cars. */
private GTUFollowingModelOld carFollowingModelCars;
/** The car following model, e.g. IDM Plus for trucks. */
private GTUFollowingModelOld carFollowingModelTrucks;
/** The probability that the next generated GTU is a passenger car. */
private double carProbability;
/** The blocking car. */
private LaneBasedIndividualGTU block = null;
/** Minimum distance. */
private Length minimumDistance = new Length(0, METER);
/** Maximum distance. */
private Length maximumDistance = new Length(5000, METER);
/** The Lane containing the simulated Cars. */
private Lane lane;
/** The speed limit. */
private Speed speedLimit = new Speed(100, KM_PER_HOUR);
/** The fundamental diagram plots. */
private List