View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.util.ArrayList;
4   import java.util.LinkedHashSet;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.Set;
8   import java.util.function.Supplier;
9   
10  import org.djunits.unit.DurationUnit;
11  import org.djunits.unit.LengthUnit;
12  import org.djunits.unit.SpeedUnit;
13  import org.djunits.unit.util.UNITS;
14  import org.djunits.value.vdouble.scalar.Direction;
15  import org.djunits.value.vdouble.scalar.Duration;
16  import org.djunits.value.vdouble.scalar.Frequency;
17  import org.djunits.value.vdouble.scalar.Length;
18  import org.djunits.value.vdouble.scalar.Speed;
19  import org.djutils.draw.point.Point2d;
20  import org.opentrafficsim.base.parameters.ParameterException;
21  import org.opentrafficsim.base.parameters.ParameterSet;
22  import org.opentrafficsim.core.definitions.DefaultsNl;
23  import org.opentrafficsim.core.distributions.ConstantSupplier;
24  import org.opentrafficsim.core.distributions.FrequencyAndObject;
25  import org.opentrafficsim.core.distributions.ObjectDistribution;
26  import org.opentrafficsim.core.dsol.AbstractOtsModel;
27  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
28  import org.opentrafficsim.core.gtu.GtuType;
29  import org.opentrafficsim.core.idgenerator.IdSupplier;
30  import org.opentrafficsim.core.network.NetworkException;
31  import org.opentrafficsim.core.network.Node;
32  import org.opentrafficsim.core.network.route.FixedRouteGenerator;
33  import org.opentrafficsim.core.network.route.Route;
34  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
35  import org.opentrafficsim.road.definitions.DefaultsRoadNl;
36  import org.opentrafficsim.road.gtu.generator.GeneratorPositions;
37  import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator;
38  import org.opentrafficsim.road.gtu.generator.TtcRoomChecker;
39  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuTemplate;
40  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuTemplateDistribution;
41  import org.opentrafficsim.road.gtu.generator.headway.HeadwayGenerator;
42  import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractIdm;
43  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.Lmrs;
44  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
45  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory.Setting;
46  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
47  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
48  import org.opentrafficsim.road.network.RoadNetwork;
49  import org.opentrafficsim.road.network.factory.LaneFactory;
50  import org.opentrafficsim.road.network.lane.CrossSectionLink;
51  import org.opentrafficsim.road.network.lane.Lane;
52  import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
53  import org.opentrafficsim.road.network.lane.LanePosition;
54  import org.opentrafficsim.road.network.lane.LaneType;
55  import org.opentrafficsim.road.network.lane.object.detector.SinkDetector;
56  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
57  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
58  
59  import nl.tudelft.simulation.dsol.SimRuntimeException;
60  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
61  import nl.tudelft.simulation.jstats.distributions.DistUniform;
62  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
63  import nl.tudelft.simulation.jstats.streams.StreamInterface;
64  
65  /**
66   * Simulate a single lane road of 5 km length. Vehicles are generated at a constant rate of 1500 veh/hour. At time 300s a
67   * blockade is inserted at position 4 km; this blockade is removed at time 500s. The used car following algorithm is IDM+
68   * <a href="http://opentrafficsim.org/downloads/MOTUS%20reference.pdf"><i>Integrated Lane Change Model with Relaxation and
69   * Synchronization</i>, by Wouter J. Schakel, Victor L. Knoop and Bart van Arem, 2012</a>. <br>
70   * Output is a set of block charts:
71   * <ul>
72   * <li>Traffic density</li>
73   * <li>Speed</li>
74   * <li>Flow</li>
75   * <li>Acceleration</li>
76   * </ul>
77   * All these graphs display simulation time along the horizontal axis and distance along the road along the vertical axis.
78   * <p>
79   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
80   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
81   * </p>
82   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
83   */
84  public class StraightModel extends AbstractOtsModel implements UNITS
85  {
86      /** The network. */
87      private final RoadNetwork network = new RoadNetwork("network", getSimulator());
88  
89      /** The probability that the next generated GTU is a passenger car. */
90      private double carProbability;
91  
92      /** The blocking, implemented as a traffic light. */
93      private TrafficLight block = null;
94  
95      /** Maximum distance. */
96      private Length maximumDistance = new Length(5000, METER);
97  
98      /** The Lane that contains the simulated Cars. */
99      private Lane lane;
100 
101     /** The random number generator used to decide what kind of GTU to generate. */
102     private StreamInterface stream = new MersenneTwister(12345);
103 
104     /** The sequence of Lanes that all vehicles will follow. */
105     private List<Lane> path = new ArrayList<>();
106 
107     /** The speed limit on all Lanes. */
108     private Speed speedLimit = new Speed(120, KM_PER_HOUR);
109 
110     /**
111      * Constructor.
112      * @param simulator the simulator for this model
113      */
114     public StraightModel(final OtsSimulatorInterface simulator)
115     {
116         super(simulator);
117         InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
118     }
119 
120     @Override
121     public final void constructModel() throws SimRuntimeException
122     {
123         try
124         {
125             Node from = new Node(this.network, "From", new Point2d(0.0, 0), Direction.ZERO);
126             Node to = new Node(this.network, "To", new Point2d(this.maximumDistance.getSI(), 0), Direction.ZERO);
127             Node end = new Node(this.network, "End", new Point2d(this.maximumDistance.getSI() + 50.0, 0), Direction.ZERO);
128             LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
129             this.lane = LaneFactory.makeLane(this.network, "Lane", from, to, null, laneType, this.speedLimit, this.simulator,
130                     DefaultsNl.VEHICLE);
131             this.path.add(this.lane);
132             CrossSectionLink endLink = LaneFactory.makeLink(this.network, "endLink", to, end, null, this.simulator);
133             // No overtaking, single lane
134             Lane sinkLane = LaneGeometryUtil.createStraightLane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0),
135                     this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0), laneType,
136                     Map.of(DefaultsNl.VEHICLE, this.speedLimit));
137             new SinkDetector(sinkLane, new Length(10.0, METER), DefaultsNl.ROAD_USERS);
138             this.path.add(sinkLane);
139 
140             this.carProbability = (double) getInputParameter("generic.carProbability");
141 
142             // Generation of a new car / truck
143             TtcRoomChecker roomChecker = new TtcRoomChecker(new Duration(10.0, DurationUnit.SI));
144             IdSupplier idGenerator = new IdSupplier("");
145             ParameterSet params = new ParameterSet();
146             params.setDefaultParameter(AbstractIdm.DELTA);
147             GtuType car = DefaultsNl.CAR;
148             GtuType truck = DefaultsNl.TRUCK;
149             ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> speedCar =
150                     new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 90.0, 110.0), SpeedUnit.KM_PER_HOUR);
151             ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> speedTruck =
152                     new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 80, 95), SpeedUnit.KM_PER_HOUR);
153             Supplier<Route> routeGenerator = new FixedRouteGenerator(null);
154             LaneBasedStrategicalPlannerFactory<?> strategicalPlannerFactoryCars = new LaneBasedStrategicalRoutePlannerFactory(
155                     new LmrsFactory<>(Lmrs::new).setStream(this.stream).set(Setting.ACCELERATION_TRAFFIC_LIGHTS, true));
156             LaneBasedStrategicalPlannerFactory<?> strategicalPlannerFctoryTrucks = new LaneBasedStrategicalRoutePlannerFactory(
157                     new LmrsFactory<>(Lmrs::new).setStream(this.stream).set(Setting.ACCELERATION_TRAFFIC_LIGHTS, true));
158             LaneBasedGtuTemplate carTemplate = new LaneBasedGtuTemplate(car, new ConstantSupplier<>(Length.ofSI(4.0)),
159                     new ConstantSupplier<>(Length.ofSI(2.0)), speedCar, strategicalPlannerFactoryCars, routeGenerator);
160             LaneBasedGtuTemplate truckTemplate = new LaneBasedGtuTemplate(truck, new ConstantSupplier<>(Length.ofSI(15.0)),
161                     new ConstantSupplier<>(Length.ofSI(2.5)), speedTruck, strategicalPlannerFctoryTrucks, routeGenerator);
162             ObjectDistribution<LaneBasedGtuTemplate> gtuTypeDistribution = new ObjectDistribution<>(this.stream);
163             gtuTypeDistribution.add(new FrequencyAndObject<>(this.carProbability, carTemplate));
164             gtuTypeDistribution.add(new FrequencyAndObject<>(1.0 - this.carProbability, truckTemplate));
165             Supplier<Duration> headwayGenerator =
166                     new HeadwayGenerator(new Frequency(1500.0, PER_HOUR), new MersenneTwister(4L));
167             Set<LanePosition> initialLongitudinalPositions = new LinkedHashSet<>();
168             initialLongitudinalPositions.add(new LanePosition(this.lane, new Length(5.0, LengthUnit.SI)));
169             LaneBasedGtuTemplateDistribution characteristicsGenerator =
170                     new LaneBasedGtuTemplateDistribution(gtuTypeDistribution);
171             new LaneBasedGtuGenerator("Generator", headwayGenerator, characteristicsGenerator,
172                     GeneratorPositions.create(initialLongitudinalPositions, this.stream), this.network, getSimulator(),
173                     roomChecker, idGenerator);
174             // End generation
175 
176             this.block =
177                     new TrafficLight(this.lane.getId() + "_TL", this.lane, new Length(new Length(4000.0, LengthUnit.METER)));
178             this.block.setTrafficLightColor(TrafficLightColor.GREEN);
179             // Create a block at t = 5 minutes
180             this.simulator.scheduleEventAbs(Duration.ofSI(300.0), () -> createBlock());
181             // Remove the block at t = 7 minutes
182             this.simulator.scheduleEventAbs(Duration.ofSI(420.0), () -> removeBlock());
183         }
184         catch (SimRuntimeException | NetworkException | InputParameterException | ParameterException exception)
185         {
186             exception.printStackTrace();
187         }
188     }
189 
190     /**
191      * Set up the block.
192      */
193     protected final void createBlock()
194     {
195         this.block.setTrafficLightColor(TrafficLightColor.RED);
196     }
197 
198     /**
199      * Remove the block.
200      */
201     protected final void removeBlock()
202     {
203         this.block.setTrafficLightColor(TrafficLightColor.GREEN);
204     }
205 
206     @Override
207     public RoadNetwork getNetwork()
208     {
209         return this.network;
210     }
211 
212     /**
213      * Return path.
214      * @return the path for sampling the graphs
215      */
216     public final List<Lane> getPath()
217     {
218         return this.path;
219     }
220 
221 }