();
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;
int pos = graphName.indexOf(' ') + 1;
String laneNumberText = graphName.substring(pos, pos + 1);
int lane = Integer.parseInt(laneNumberText) - 1;
if (graphName.contains("Trajectories"))
{
TrajectoryPlot tp = new TrajectoryPlot(graphName, new Time.Rel(0.5, SECOND), this.model.getPath(lane));
tp.setTitle("Trajectory Graph");
tp.setExtendedState(Frame.MAXIMIZED_BOTH);
graph = tp;
container = tp.getContentPane();
}
else
{
ContourPlot cp;
if (graphName.contains("Density"))
{
cp = new DensityContourPlot(graphName, this.model.getPath(lane));
cp.setTitle("Density Contour Graph");
}
else if (graphName.contains("Speed"))
{
cp = new SpeedContourPlot(graphName, this.model.getPath(lane));
cp.setTitle("Speed Contour Graph");
}
else if (graphName.contains("Flow"))
{
cp = new FlowContourPlot(graphName, this.model.getPath(lane));
cp.setTitle("Flow Contour Graph");
}
else if (graphName.contains("Acceleration"))
{
cp = new AccelerationContourPlot(graphName, this.model.getPath(lane));
cp.setTitle("Acceleration Contour Graph");
}
else
{
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 "Circular Road simulation";
}
/** {@inheritDoc} */
@Override
public final String description()
{
return "Circular Road simulation
"
+ "Vehicles are unequally distributed over a two lane ring road.
"
+ "When simulation starts, all vehicles begin driving, some lane changes will occurr and some "
+ "shockwaves should develop.
"
+ "Trajectories and contourplots are generated during the simulation for both lanes.";
}
}
/**
* Simulate traffic on a circular, two-lane road.
*
* 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 1 nov. 2014
* @author Peter Knoppers
*/
class RoadSimulationModel implements OTSModelInterface, UNITS
{
/** */
private static final long serialVersionUID = 20141121L;
/** the simulator. */
private OTSDEVSSimulatorInterface simulator;
/** network. */
private OTSNetwork network = new OTSNetwork("network");
/** Number of cars created. */
private int carsCreated = 0;
/** 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;
/** Minimum distance. */
private Length.Rel minimumDistance = new Length.Rel(0, METER);
/** The speed limit. */
private Speed speedLimit = new Speed(100, KM_PER_HOUR);
/** The plots. */
private ArrayList plots = new ArrayList();
/** User settable properties. */
private ArrayList> properties = null;
/** The sequence of Lanes that all vehicles will follow. */
private ArrayList> paths = new ArrayList>();
/** 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 RoadSimulationModel(final ArrayList> properties, final GTUColorer gtuColorer)
{
this.properties = properties;
this.gtuColorer = gtuColorer;
}
/**
* @param index int; the rank number of the path
* @return List<Lane>; the set of lanes for the specified index
*/
public List getPath(final int index)
{
return this.paths.get(index);
}
/** {@inheritDoc} */
@Override
public void constructModel(final SimulatorInterface, Rel, OTSSimTimeDouble> theSimulator)
throws SimRuntimeException, RemoteException
{
final int laneCount = 2;
for (int laneIndex = 0; laneIndex < laneCount; laneIndex++)
{
this.paths.add(new ArrayList());
}
this.simulator = (OTSDEVSSimulatorInterface) theSimulator;
double radius = 6000 / 2 / Math.PI;
double headway = 40;
double headwayVariability = 0;
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 ("Lane changing".equals(sp.getShortName()))
{
String strategyName = sp.getValue();
if ("Egoistic".equals(strategyName))
{
this.laneChangeModel = new Egoistic();
}
else if ("Altruistic".equals(strategyName))
{
this.laneChangeModel = new Altruistic();
}
else
{
throw new Error("Lane changing " + strategyName + " not implemented");
}
}
}
else if (ap instanceof ProbabilityDistributionProperty)
{
ProbabilityDistributionProperty pdp = (ProbabilityDistributionProperty) ap;
if (ap.getShortName().equals("Traffic composition"))
{
this.carProbability = pdp.getValue()[0];
}
}
else if (ap instanceof IntegerProperty)
{
IntegerProperty ip = (IntegerProperty) ap;
if ("Track length".equals(ip.getShortName()))
{
radius = ip.getValue() / 2 / Math.PI;
}
}
else if (ap instanceof ContinuousProperty)
{
ContinuousProperty cp = (ContinuousProperty) ap;
if (cp.getShortName().equals("Mean density"))
{
headway = 1000 / cp.getValue();
}
if (cp.getShortName().equals("Density variability"))
{
headwayVariability = cp.getValue();
}
}
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());
Acceleration a = IDMPropertySet.getA(cp);
Acceleration b = IDMPropertySet.getB(cp);
Length.Rel s0 = IDMPropertySet.getS0(cp);
Time.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());
}
}
}
}
GTUType gtuType = GTUType.makeGTUType("car");
LaneType laneType = new LaneType("CarLane");
laneType.addCompatibility(gtuType);
OTSNode start = new OTSNode("Start", new OTSPoint3D(radius, 0, 0));
OTSNode halfway = new OTSNode("Halfway", new OTSPoint3D(-radius, 0, 0));
OTSPoint3D[] coordsHalf1 = new OTSPoint3D[127];
for (int i = 0; i < coordsHalf1.length; i++)
{
double angle = Math.PI * (1 + i) / (1 + coordsHalf1.length);
coordsHalf1[i] = new OTSPoint3D(radius * Math.cos(angle), radius * Math.sin(angle), 0);
}
Lane[] lanes1 =
LaneFactory.makeMultiLane("FirstHalf", start, halfway, coordsHalf1, laneCount, laneType,
this.speedLimit, this.simulator, LongitudinalDirectionality.DIR_PLUS);
OTSPoint3D[] coordsHalf2 = new OTSPoint3D[127];
for (int i = 0; i < coordsHalf2.length; i++)
{
double angle = Math.PI + Math.PI * (1 + i) / (1 + coordsHalf2.length);
coordsHalf2[i] = new OTSPoint3D(radius * Math.cos(angle), radius * Math.sin(angle), 0);
}
Lane[] lanes2 =
LaneFactory.makeMultiLane("SecondHalf", halfway, start, coordsHalf2, laneCount, laneType,
this.speedLimit, this.simulator, LongitudinalDirectionality.DIR_PLUS);
for (int laneIndex = 0; laneIndex < laneCount; laneIndex++)
{
this.paths.get(laneIndex).add(lanes1[laneIndex]);
this.paths.get(laneIndex).add(lanes2[laneIndex]);
}
// Put the (not very evenly spaced) cars on the track
double variability = (headway - 20) * headwayVariability;
System.out.println("headway is " + headway + " variability limit is " + variability);
Random random = new Random(12345);
for (int laneIndex = 0; laneIndex < laneCount; laneIndex++)
{
double lane1Length = lanes1[laneIndex].getLength().getSI();
double trackLength = lane1Length + lanes2[laneIndex].getLength().getSI();
for (double pos = 0; pos <= trackLength - headway - variability;)
{
Lane lane = pos >= lane1Length ? lanes2[laneIndex] : lanes1[laneIndex];
// Actual headway is uniformly distributed around headway
double laneRelativePos = pos > lane1Length ? pos - lane1Length : pos;
double actualHeadway = headway + (random.nextDouble() * 2 - 1) * variability;
System.out.println(lane + ", len=" + lane.getLength() + ", pos=" + laneRelativePos);
generateCar(new Length.Rel(laneRelativePos, METER), lane, gtuType);
pos += actualHeadway;
}
}
// Schedule regular updates of the graph
this.simulator.scheduleEventAbs(new DoubleScalar.Abs(9.999, SECOND), this, this, "drawGraphs",
null);
}
catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
{
exception.printStackTrace();
}
}
/**
* Notify the contour plots that the underlying data has changed.
*/
protected final void drawGraphs()
{
for (LaneBasedGTUSampler plot : this.plots)
{
plot.reGraph();
}
// Re schedule this method
try
{
this.simulator.scheduleEventAbs(new Time.Abs(this.simulator.getSimulatorTime().get().getSI() + 10, SECOND),
this, this, "drawGraphs", null);
}
catch (SimRuntimeException exception)
{
exception.printStackTrace();
}
}
/**
* Generate cars at a fixed rate (implemented by re-scheduling this method).
* @param initialPosition Length.Rel; the initial position of the new cars
* @param lane Lane; the lane on which the new cars are placed
* @param gtuType GTUType<String>; the type of the new cars
* @throws NamingException on ???
* @throws SimRuntimeException cannot happen
* @throws NetworkException on network inconsistency
* @throws GTUException when something goes wrong during construction of the car
* @throws OTSGeometryException when the initial position is outside the center line of the lane
*/
protected final void generateCar(final Length.Rel initialPosition, final Lane lane, final GTUType gtuType)
throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
{
boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability;
Speed initialSpeed = new Speed(0, KM_PER_HOUR);
Set initialPositions = new LinkedHashSet<>(1);
initialPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
Length.Rel vehicleLength = new Length.Rel(generateTruck ? 15 : 4, METER);
LaneBasedDrivingCharacteristics drivingCharacteristics =
new LaneBasedDrivingCharacteristics(generateTruck ? this.carFollowingModelTrucks
: this.carFollowingModelCars, this.laneChangeModel);
LaneBasedStrategicalPlanner strategicalPlanner =
new LaneBasedStrategicalRoutePlanner(drivingCharacteristics, new LaneBasedCFLCTacticalPlanner());
new LaneBasedIndividualCar("" + (++this.carsCreated), 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);
}
/** {@inheritDoc} */
@Override
public SimulatorInterface, Rel, OTSSimTimeDouble> getSimulator() throws RemoteException
{
return this.simulator;
}
/**
* @return plots
*/
public final ArrayList getPlots()
{
return this.plots;
}
/**
* @return minimumDistance
*/
public final Length.Rel getMinimumDistance()
{
return this.minimumDistance;
}
/**
* Stop simulation and throw an Error.
* @param theSimulator OTSDEVSSimulatorInterface; the simulator
* @param errorMessage String; the error message
*/
public void stopSimulator(final OTSDEVSSimulatorInterface theSimulator, final String errorMessage)
{
System.out.println("Error: " + errorMessage);
try
{
if (theSimulator.isRunning())
{
theSimulator.stop();
}
}
catch (SimRuntimeException exception)
{
exception.printStackTrace();
}
throw new Error(errorMessage);
}
}