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