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.TrajectoryPlot; 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 Trajectories 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$, @version $Revision$, by $Author$, * initial version 17 dec. 2014
* @author Peter Knoppers */ public class Trajectories extends AbstractWrappableAnimation implements UNITS { /** the model. */ private TrajectoriesModel model; /** Create a Trajectories simulation. */ public Trajectories() { 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, 10)); 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, 9)); } 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 { Trajectories trajectories = new Trajectories(); trajectories.buildAnimator(new Time.Abs(0.0, SECOND), new Time.Rel(0.0, SECOND), new Time.Rel( 3600.0, SECOND), trajectories.getProperties(), null, true); } catch (SimRuntimeException | NamingException | OTSSimulationException exception) { exception.printStackTrace(); } } }); } /** {@inheritDoc} */ @Override protected final Rectangle2D.Double makeAnimationRectangle() { return new Rectangle2D.Double(0, -100, 5000, 200); } /** {@inheritDoc} */ @Override protected final OTSModelInterface makeModel(final GTUColorer colorer) { this.model = new TrajectoriesModel(this.savedUserModifiedProperties, colorer); return this.model; } /** {@inheritDoc} */ @Override protected final JPanel makeCharts() throws OTSSimulationException { TablePanel charts = new TablePanel(1, 1); Time.Rel sampleInterval = new Time.Rel(0.5, SECOND); List path = new ArrayList(); path.add(this.model.getLane()); TrajectoryPlot tp = new TrajectoryPlot("Trajectory Plot", sampleInterval, path); tp.setTitle("Density Contour Graph"); tp.setExtendedState(Frame.MAXIMIZED_BOTH); this.model.setTrajectoryPlot(tp); charts.setCell(tp.getContentPane(), 0, 0); return charts; } /** {@inheritDoc} */ @Override public final String shortName() { return "Trajectory plot"; } /** {@inheritDoc} */ @Override public final String description() { return "

Trajectories

" + "Simulation of a single lane road of 5 km length. Vechicles are generated at a constant rate of " + "1500 veh/hour. At time 300s a blockade is inserted at position 4km; this blockade is removed at time " + "420s. This blockade simulates a bridge opening.
" + "The blockade causes a traffic jam that slowly dissolves after the blockade is removed.
" + "Output is a Trajectory plots."; } } /** * Simulate a single lane road of 5 km length. Vehicles are generated at a constant rate of 1500 veh/hour. At time 300s a * blockade is inserted at position 4 km; this blockade is removed at time 500s. The used car following algorithm is IDM+ Integrated Lane Change Model with Relaxation and * Synchronization, by Wouter J. Schakel, Victor L. Knoop and Bart van Arem, 2012.
* Output is a trajectory plot with simulation time along the horizontal axis and distance along the road along the vertical * axis. *

* 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 ug 1, 2014
* @author Peter Knoppers */ class TrajectoriesModel implements OTSModelInterface, UNITS { /** */ private static final long serialVersionUID = 20140815L; /** the simulator. */ private OTSDEVSSimulatorInterface simulator; /** network. */ private OTSNetwork network = new OTSNetwork("network"); /** 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; /** minimum distance. */ private Length.Rel minimumDistance = new Length.Rel(0, METER); /** maximum distance. */ private Length.Rel maximumDistance = new Length.Rel(5000, METER); /** The Lane containing the simulated Cars. */ private Lane lane; /** the speed limit. */ private Speed speedLimit = new Speed(100, KM_PER_HOUR); /** the trajectory plot. */ private TrajectoryPlot trajectoryPlot; /** User settable properties. */ private ArrayList> properties = null; /** The random number generator used to decide what kind of GTU to generate. */ private Random randomGenerator = new Random(12345); /** The GTUColorer for the generated vehicles. */ private final GTUColorer gtuColorer; /** * @param properties ArrayList<AbstractProperty<?>>; the properties * @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer. */ public TrajectoriesModel(final ArrayList> properties, final GTUColorer gtuColorer) { this.properties = properties; this.gtuColorer = gtuColorer; } /** {@inheritDoc} */ @Override public final void constructModel( final SimulatorInterface, DoubleScalar.Rel, OTSSimTimeDouble> theSimulator) throws SimRuntimeException, RemoteException { this.simulator = (OTSDEVSSimulatorInterface) theSimulator; OTSNode from = new OTSNode("From", new OTSPoint3D(getMinimumDistance().getSI(), 0, 0)); OTSNode to = new OTSNode("To", new OTSPoint3D(getMaximumDistance().getSI(), 0, 0)); OTSNode end = new OTSNode("End", new OTSPoint3D(getMaximumDistance().getSI() + 50.0, 0, 0)); LaneType laneType = new LaneType("CarLane"); laneType.addCompatibility(this.gtuType); try { this.lane = LaneFactory.makeLane("Lane", from, to, null, laneType, this.speedLimit, this.simulator, LongitudinalDirectionality.DIR_PLUS); CrossSectionLink endLink = LaneFactory.makeLink("endLink", to, end, null, LongitudinalDirectionality.DIR_PLUS); // No overtaking, single (sink) lane Lane sinkLane = new Lane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0), this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0), laneType, LongitudinalDirectionality.DIR_PLUS, this.speedLimit, new OvertakingConditions.None()); Sensor sensor = new SinkSensor(sinkLane, new Length.Rel(10.0, METER), this.simulator); sinkLane.addSensor(sensor, GTUType.ALL); } catch (NamingException | NetworkException | OTSGeometryException exception1) { exception1.printStackTrace(); } for (AbstractProperty p : this.properties) { if (p instanceof SelectionProperty) { SelectionProperty sp = (SelectionProperty) p; if ("Car following model".equals(sp.getShortName())) { String modelName = sp.getValue(); if (modelName.equals("IDM")) { this.carFollowingModelCars = new IDM(new Acceleration(1, METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length.Rel(2, METER), new Time.Rel(1, SECOND), 1d); this.carFollowingModelTrucks = new IDM(new Acceleration(0.5, METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length.Rel(2, METER), new Time.Rel(1, SECOND), 1d); } else if (modelName.equals("IDM+")) { this.carFollowingModelCars = new IDMPlus(new Acceleration(1, METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length.Rel(2, METER), new Time.Rel(1, SECOND), 1d); this.carFollowingModelTrucks = new IDMPlus(new Acceleration(0.5, METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length.Rel(2, METER), new Time.Rel(1, SECOND), 1d); } else { throw new Error("Car following model " + modelName + " not implemented"); } } else { throw new Error("Unhandled SelectionProperty " + p.getShortName()); } } else if (p instanceof ProbabilityDistributionProperty) { ProbabilityDistributionProperty pdp = (ProbabilityDistributionProperty) p; String modelName = p.getShortName(); if (modelName.equals("Traffic composition")) { this.carProbability = pdp.getValue()[0]; } else { throw new Error("Unhandled ProbabilityDistributionProperty " + p.getShortName()); } } else { throw new Error("Unhandled property: " + p); } } // 1500 [vehicles / hour] == 2.4s headway this.headway = new Time.Rel(3600.0 / 1500.0, SECOND); try { // Schedule creation of the first car (this will re-schedule itself one headway later, etc.). this.simulator.scheduleEventAbs(new DoubleScalar.Abs(0.0, SECOND), this, this, "generateCar", null); // Create a block at t = 5 minutes this.simulator.scheduleEventAbs(new DoubleScalar.Abs(300, SECOND), this, this, "createBlock", null); // Remove the block at t = 7 minutes this.simulator.scheduleEventAbs(new DoubleScalar.Abs(420, SECOND), this, this, "removeBlock", null); // Schedule regular updates of the graph for (int t = 1; t <= 1800; t++) { this.simulator.scheduleEventAbs(new DoubleScalar.Abs(t - 0.001, SECOND), this, this, "drawGraph", null); } } catch (SimRuntimeException exception) { exception.printStackTrace(); } } /** * Set up the block. * @throws NamingException on error during adding of animation handler * @throws SimRuntimeException on ??? * @throws NetworkException on network inconsistency * @throws GTUException if creation of the GTU fails * @throws OTSGeometryException when the initial position is not on the cecnter line of the lane */ protected final void createBlock() throws NamingException, SimRuntimeException, NetworkException, GTUException, OTSGeometryException { Length.Rel initialPosition = new Length.Rel(4000, METER); Set initialPositions = new LinkedHashSet<>(1); initialPositions.add(new DirectedLanePosition(this.getLane(), initialPosition, GTUDirectionality.DIR_PLUS)); LaneBasedDrivingCharacteristics drivingCharacteristics = new LaneBasedDrivingCharacteristics(this.carFollowingModelCars, this.laneChangeModel); LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(drivingCharacteristics, new LaneBasedGTUFollowingTacticalPlanner()); this.block = new LaneBasedIndividualCar("999999", this.gtuType, initialPositions, new Speed(0.0, KM_PER_HOUR), new Length.Rel(4, METER), new Length.Rel(1.8, METER), new Speed(0.0, KM_PER_HOUR), this.simulator, strategicalPlanner, new LanePerceptionFull(), DefaultCarAnimation.class, this.gtuColorer, this.network); } /** * Remove the block. */ protected final void removeBlock() { this.block.destroy(); this.block = null; } /** * Generate cars at a fixed rate (implemented by re-scheduling this method). */ protected final void generateCar() { boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability; Length.Rel initialPosition = new Length.Rel(0, METER); Set initialPositions = new LinkedHashSet<>(1); initialPositions.add(new DirectedLanePosition(this.getLane(), initialPosition, GTUDirectionality.DIR_PLUS)); Speed initialSpeed = new Speed(100, KM_PER_HOUR); try { Length.Rel vehicleLength = new Length.Rel(generateTruck ? 15 : 4, METER); GTUFollowingModel gtuFollowingModel = generateTruck ? this.carFollowingModelTrucks : this.carFollowingModelCars; if (null == gtuFollowingModel) { throw new Error("gtuFollowingModel is null"); } LaneBasedDrivingCharacteristics drivingCharacteristics = new LaneBasedDrivingCharacteristics(gtuFollowingModel, this.laneChangeModel); LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(drivingCharacteristics, new LaneBasedGTUFollowingTacticalPlanner()); new LaneBasedIndividualCar("" + (++this.carsCreated), this.gtuType, initialPositions, initialSpeed, vehicleLength, new Length.Rel(1.8, METER), new Speed(200, KM_PER_HOUR), this.simulator, strategicalPlanner, new LanePerceptionFull(), DefaultCarAnimation.class, this.gtuColorer, this.network); // Re-schedule this method after headway seconds this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null); } catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception) { exception.printStackTrace(); } } /** {@inheritDoc} */ @Override public final SimulatorInterface, DoubleScalar.Rel, OTSSimTimeDouble> getSimulator() throws RemoteException { return this.simulator; } /** * Notify the contour plots that the underlying data has changed. */ protected final void drawGraph() { this.trajectoryPlot.reGraph(); } /** * @return minimum distance of the simulation */ public final Length.Rel getMinimumDistance() { return this.minimumDistance; } /** * @return maximum distance of the simulation */ public final Length.Rel getMaximumDistance() { return this.maximumDistance; } /** * @param trajectoryPlot TrajectoryPlot */ public final void setTrajectoryPlot(final TrajectoryPlot trajectoryPlot) { this.trajectoryPlot = trajectoryPlot; } /** * @return lane. */ public Lane getLane() { return this.lane; } }