(0.5, TimeUnit.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
{
/** */
private static final long serialVersionUID = 20141121L;
/** the simulator. */
private OTSDEVSSimulatorInterface simulator;
/** 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 DoubleScalar.Rel minimumDistance = new DoubleScalar.Rel(0, LengthUnit.METER);
/** The speed limit. */
private DoubleScalar.Abs speedLimit = new DoubleScalar.Abs(100, SpeedUnit.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());
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());
}
}
}
}
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);
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);
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;
generateCar(new DoubleScalar.Rel(laneRelativePos, LengthUnit.METER), lane, gtuType);
/*
* if (pos > trackLength / 4 && pos < 3 * trackLength / 4) { generateCar(new
* DoubleScalar.Rel(pos + headway / 2, LengthUnit.METER), laneIndex, gtuType); }
*/
pos += actualHeadway;
}
}
// Schedule regular updates of the graph
this.simulator.scheduleEventAbs(new DoubleScalar.Abs(9.999, TimeUnit.SECOND), this, this,
"drawGraphs", null);
}
catch (RemoteException | 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 DoubleScalar.Abs(
this.simulator.getSimulatorTime().get().getSI() + 10, TimeUnit.SECOND), this, this, "drawGraphs", null);
}
catch (RemoteException | SimRuntimeException exception)
{
exception.printStackTrace();
}
}
/**
* Generate cars at a fixed rate (implemented by re-scheduling this method).
* @param initialPosition DoubleScalar.Rel<LengthUnit>; 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 RemoteException on communications failure
* @throws GTUException when something goes wrong during construction of the car
*/
protected final void generateCar(final DoubleScalar.Rel initialPosition, final Lane lane,
final GTUType gtuType) throws NamingException, NetworkException, SimRuntimeException, RemoteException,
GTUException
{
boolean generateTruck = this.randomGenerator.nextDouble() > this.carProbability;
DoubleScalar.Abs initialSpeed = new DoubleScalar.Abs(0, SpeedUnit.KM_PER_HOUR);
Map, DoubleScalar.Rel> initialPositions =
new LinkedHashMap, DoubleScalar.Rel>();
initialPositions.put(lane, initialPosition);
DoubleScalar.Rel vehicleLength =
new DoubleScalar.Rel(generateTruck ? 15 : 4, LengthUnit.METER);
new LaneBasedIndividualCar<>(++this.carsCreated, 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);
}
/** {@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 DoubleScalar.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 (RemoteException | SimRuntimeException exception)
{
exception.printStackTrace();
}
throw new Error(errorMessage);
}
}