package ahfe; import java.awt.Color; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import javax.naming.NamingException; import javax.xml.parsers.ParserConfigurationException; import org.djunits.unit.FrequencyUnit; import org.djunits.unit.SpeedUnit; import org.djunits.unit.TimeUnit; import org.djunits.value.StorageType; import org.djunits.value.ValueException; 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.vector.FrequencyVector; import org.djunits.value.vdouble.vector.TimeVector; import org.opentrafficsim.base.parameters.ParameterException; import org.opentrafficsim.base.parameters.ParameterTypes; import org.opentrafficsim.base.parameters.Parameters; import org.opentrafficsim.core.geometry.OTSGeometryException; import org.opentrafficsim.core.gtu.AbstractGTU; import org.opentrafficsim.core.gtu.GTUException; import org.opentrafficsim.core.gtu.GTUType; import org.opentrafficsim.core.gtu.animation.AccelerationGTUColorer; import org.opentrafficsim.core.gtu.animation.SpeedGTUColorer; import org.opentrafficsim.core.gtu.animation.SwitchableGTUColorer; import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterFactoryByType; import org.opentrafficsim.core.gtu.perception.DirectEgoPerception; import org.opentrafficsim.core.network.NetworkException; import org.opentrafficsim.core.network.Node; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.core.perception.HistoryManager; import org.opentrafficsim.core.perception.HistoryManagerDEVS; import org.opentrafficsim.kpi.interfaces.LaneDataInterface; import org.opentrafficsim.kpi.sampling.KpiGtuDirectionality; import org.opentrafficsim.kpi.sampling.KpiLaneDirection; import org.opentrafficsim.kpi.sampling.SpaceTimeRegion; import org.opentrafficsim.road.animation.AbstractSimulationScript; import org.opentrafficsim.road.gtu.animation.DesiredHeadwayColorer; import org.opentrafficsim.road.gtu.animation.DesiredSpeedColorer; import org.opentrafficsim.road.gtu.animation.FixedColor; import org.opentrafficsim.road.gtu.animation.GTUTypeColorer; import org.opentrafficsim.road.gtu.animation.IncentiveColorer; import org.opentrafficsim.road.gtu.animation.SynchronizationColorer; import org.opentrafficsim.road.gtu.animation.TaskSaturationColorer; import org.opentrafficsim.road.gtu.animation.TotalDesireColorer; import org.opentrafficsim.road.gtu.generator.od.DefaultGTUCharacteristicsGeneratorOD; import org.opentrafficsim.road.gtu.generator.od.ODApplier; import org.opentrafficsim.road.gtu.generator.od.ODOptions; import org.opentrafficsim.road.gtu.generator.od.StrategicalPlannerFactorySupplierOD; import org.opentrafficsim.road.gtu.lane.LaneBasedGTU; import org.opentrafficsim.road.gtu.lane.perception.CategoricalLanePerception; import org.opentrafficsim.road.gtu.lane.perception.LanePerception; import org.opentrafficsim.road.gtu.lane.perception.PerceptionFactory; import org.opentrafficsim.road.gtu.lane.perception.categories.Anticipation; import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationTrafficPerception; import org.opentrafficsim.road.gtu.lane.perception.categories.DirectInfrastructurePerception; import org.opentrafficsim.road.gtu.lane.perception.categories.DirectNeighborsPerception; import org.opentrafficsim.road.gtu.lane.perception.categories.Estimation; import org.opentrafficsim.road.gtu.lane.perception.categories.HeadwayGtuType.PerceivedHeadwayGtuType; import org.opentrafficsim.road.gtu.lane.perception.mental.AdaptationHeadway; import org.opentrafficsim.road.gtu.lane.perception.mental.AdaptationSituationalAwareness; import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller; import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.BehavioralAdaptation; import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.Task; import org.opentrafficsim.road.gtu.lane.perception.mental.TaskCarFollowing; import org.opentrafficsim.road.gtu.lane.perception.mental.TaskLaneChanging; import org.opentrafficsim.road.gtu.lane.perception.mental.TaskLaneChanging.LateralConsideration; import org.opentrafficsim.road.gtu.lane.perception.mental.TaskRoadSideDistraction; import org.opentrafficsim.road.gtu.lane.plan.operational.LaneOperationalPlanBuilder; import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.AccelerationIncentive; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveCourtesy; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveGetInLane; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveKeep; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveRoute; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveSocioSpeed; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveSpeed; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveSpeedWithCourtesy; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.IncentiveStayRight; import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Cooperation; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.GapAcceptance; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Synchronization; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Tailgating; import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive; import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory; import org.opentrafficsim.road.gtu.strategical.od.Categorization; import org.opentrafficsim.road.gtu.strategical.od.Category; import org.opentrafficsim.road.gtu.strategical.od.Interpolation; import org.opentrafficsim.road.gtu.strategical.od.ODMatrix; import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory; import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser; import org.opentrafficsim.road.network.lane.CrossSectionLink; import org.opentrafficsim.road.network.lane.object.Distraction; import org.opentrafficsim.road.network.lane.object.Distraction.TrapezoidProfile; import org.opentrafficsim.road.network.sampling.LinkData; import org.opentrafficsim.road.network.sampling.RoadSampler; import org.opentrafficsim.road.network.sampling.data.TimeToCollision; import org.xml.sax.SAXException; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; import nl.tudelft.simulation.jstats.streams.StreamInterface; import nl.tudelft.simulation.language.io.URLResource; /** * Distraction simulation. *
* 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.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 9 apr. 2018
* Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 9 apr. 2018
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public final class DistractionScript extends AbstractSimulationScript
{
/** Distance to not consider at start of the network. */
private static Length ignoreStart = Length.createSI(2900); // Not 100m on pre-link, so 3000 total
/** Distance to not consider at end of the network. */
private static Length ignoreEnd = Length.createSI(1000);
/** Sampler. */
private RoadSampler sampler;
/**
* Main method.
* @param args String[]; command line arguments
*/
public static void main(final String[] args)
{
// Long start = System.currentTimeMillis();
DistractionScript script = new DistractionScript(args);
// script.setProperty("autorun", true);
script.start();
// Long end = System.currentTimeMillis();
// System.out.println("That took " + (end - start) / 1000 + "s.");
}
/**
* Constructor.
* @param properties String[]; properties as name-value pairs
*/
private DistractionScript(final String[] properties)
{
super("Distraction", "Distraction simulation", properties);
setGtuColorer(SwitchableGTUColorer.builder().addActiveColorer(new FixedColor(Color.BLUE, "Blue"))
.addColorer(GTUTypeColorer.DEFAULT).addColorer(new SpeedGTUColorer(new Speed(150, SpeedUnit.KM_PER_HOUR)))
.addColorer(
new DesiredSpeedColorer(new Speed(50, SpeedUnit.KM_PER_HOUR), new Speed(150, SpeedUnit.KM_PER_HOUR)))
.addColorer(new AccelerationGTUColorer(Acceleration.createSI(-6.0), Acceleration.createSI(2)))
.addColorer(new SynchronizationColorer())
.addColorer(new DesiredHeadwayColorer(Duration.createSI(0.56), Duration.createSI(2.4)))
.addColorer(new TotalDesireColorer()).addColorer(new IncentiveColorer(IncentiveRoute.class))
.addColorer(new IncentiveColorer(IncentiveSpeedWithCourtesy.class))
.addColorer(new IncentiveColorer(IncentiveSpeed.class)).addColorer(new IncentiveColorer(IncentiveKeep.class))
.addColorer(new IncentiveColorer(IncentiveGetInLane.class))
.addColorer(new IncentiveColorer(IncentiveCourtesy.class))
.addColorer(new IncentiveColorer(IncentiveSocioSpeed.class)).addColorer(new TaskSaturationColorer()).build());
}
/** {@inheritDoc} */
@Override
protected void setDefaultProperties()
{
setProperty("fTruck", "0.05");
setProperty("leftDemand", "3500");
setProperty("rightDemand", "3200");
setProperty("sampler", "true");
setProperty("warmupTime", "360");
setProperty("simulationTime", "3960");
setProperty("scenario", "test");
}
/** {@inheritDoc} */
@Override
protected void onSimulationEnd()
{
if (this.sampler != null)
{
this.sampler.writeToFile(getProperty("outputDir") + getProperty("scenario") + ".csv");
}
}
/** {@inheritDoc} */
@Override
protected OTSNetwork setupSimulation(final DEVSSimulatorInterface.TimeDoubleUnit sim) throws Exception
{
AbstractGTU.ALIGNED = false;
LaneOperationalPlanBuilder.INSTANT_LANE_CHANGES = true;
// Network
InputStream stream = URLResource.getResourceAsStream("/AHFE/Network.xml");
XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(sim);
OTSNetwork network = new OTSNetwork("Distraction");
try
{
nlp.build(stream, network, false);
}
catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException
| OTSGeometryException | ValueException | ParameterException | SimRuntimeException exception)
{
exception.printStackTrace();
}
new Distraction("distraction", ((CrossSectionLink) network.getLink("END")).getLanes().get(0), Length.createSI(1000),
sim, new TrapezoidProfile(0.2, Length.createSI(-400), Length.createSI(200), Length.createSI(400)));
// OD
List
* BSD-style license. See OpenTrafficSim License.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
private class DistractionFactorySupplier implements StrategicalPlannerFactorySupplierOD
{
/** */
DistractionFactorySupplier()
{
}
/** {@inheritDoc} */
@Override
public LaneBasedStrategicalPlannerFactory> getFactory(final Node origin, final Node destination,
final Category category, final StreamInterface randomStream) throws GTUException
{
IDMPlusFactory idm = new IDMPlusFactory(randomStream);
PerceptionFactory perc = new DefaultLMRSPerceptionFactory()
{
@Override
public LanePerception generatePerception(final LaneBasedGTU gtu)
{
Set