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.Set;
7   
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.unit.TimeUnit;
10  import org.djunits.unit.util.UNITS;
11  import org.djunits.value.vdouble.scalar.Acceleration;
12  import org.djunits.value.vdouble.scalar.Direction;
13  import org.djunits.value.vdouble.scalar.Duration;
14  import org.djunits.value.vdouble.scalar.Length;
15  import org.djunits.value.vdouble.scalar.Speed;
16  import org.djunits.value.vdouble.scalar.Time;
17  import org.opentrafficsim.base.parameters.ParameterException;
18  import org.opentrafficsim.base.parameters.Parameters;
19  import org.opentrafficsim.core.compatibility.Compatible;
20  import org.opentrafficsim.core.dsol.AbstractOTSModel;
21  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
22  import org.opentrafficsim.core.geometry.OTSGeometryException;
23  import org.opentrafficsim.core.geometry.OTSPoint3D;
24  import org.opentrafficsim.core.gtu.GTUDirectionality;
25  import org.opentrafficsim.core.gtu.GTUException;
26  import org.opentrafficsim.core.gtu.GTUType;
27  import org.opentrafficsim.core.network.NetworkException;
28  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
29  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
30  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
31  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
32  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
33  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
34  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
35  import org.opentrafficsim.road.network.OTSRoadNetwork;
36  import org.opentrafficsim.road.network.factory.LaneFactory;
37  import org.opentrafficsim.road.network.lane.CrossSectionLink;
38  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
39  import org.opentrafficsim.road.network.lane.Lane;
40  import org.opentrafficsim.road.network.lane.LaneType;
41  import org.opentrafficsim.road.network.lane.OTSRoadNode;
42  import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
43  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
44  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
45  
46  import nl.tudelft.simulation.dsol.SimRuntimeException;
47  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
48  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
49  import nl.tudelft.simulation.jstats.streams.StreamInterface;
50  
51  /**
52   * Simulate a single lane road of 5 km length. Vehicles are generated at a constant rate of 1500 veh/hour. At time 300s a
53   * blockade is inserted at position 4 km; this blockade is removed at time 500s. The used car following algorithm is IDM+
54   * <a href="http://opentrafficsim.org/downloads/MOTUS%20reference.pdf"><i>Integrated Lane Change Model with Relaxation and
55   * Synchronization</i>, by Wouter J. Schakel, Victor L. Knoop and Bart van Arem, 2012</a>. <br>
56   * Output is a set of block charts:
57   * <ul>
58   * <li>Traffic density</li>
59   * <li>Speed</li>
60   * <li>Flow</li>
61   * <li>Acceleration</li>
62   * </ul>
63   * All these graphs display simulation time along the horizontal axis and distance along the road along the vertical axis.
64   * <p>
65   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
66   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
67   * <p>
68   * $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $,
69   * initial version ug 1, 2014 <br>
70   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
71   */
72  public class StraightModel extends AbstractOTSModel implements UNITS
73  {
74      /** */
75      private static final long serialVersionUID = 20140815L;
76  
77      /** The network. */
78      private final OTSRoadNetwork network = new OTSRoadNetwork("network", true);
79  
80      /** The headway (inter-vehicle time). */
81      private Duration headway;
82  
83      /** Number of cars created. */
84      private int carsCreated = 0;
85  
86      /** Strategical planner generator for cars. */
87      private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
88  
89      /** Strategical planner generator for trucks. */
90      private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
91  
92      /** Car parameters. */
93      private Parameters parametersCar;
94  
95      /** Truck parameters. */
96      private Parameters parametersTruck;
97  
98      /** The probability that the next generated GTU is a passenger car. */
99      private double carProbability;
100 
101     /** The blocking, implemented as a traffic light. */
102     private SimpleTrafficLight block = null;
103 
104     /** Minimum distance. */
105     private Length minimumDistance = new Length(0, METER);
106 
107     /** Maximum distance. */
108     private Length maximumDistance = new Length(5000, METER);
109 
110     /** The Lane that contains the simulated Cars. */
111     private Lane lane;
112 
113     /** The random number generator used to decide what kind of GTU to generate. */
114     private StreamInterface stream = new MersenneTwister(12345);
115 
116     /** The sequence of Lanes that all vehicles will follow. */
117     private List<Lane> path = new ArrayList<>();
118 
119     /** The speed limit on all Lanes. */
120     private Speed speedLimit = new Speed(100, KM_PER_HOUR);
121 
122     /**
123      * @param simulator OTSSimulatorInterface; the simulator for this model
124      */
125     public StraightModel(final OTSSimulatorInterface simulator)
126     {
127         super(simulator);
128         InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final void constructModel() throws SimRuntimeException
134     {
135         try
136         {
137             OTSRoadNode from =
138                     new OTSRoadNode(this.network, "From", new OTSPoint3D(getMinimumDistance().getSI(), 0, 0), Direction.ZERO);
139             OTSRoadNode to =
140                     new OTSRoadNode(this.network, "To", new OTSPoint3D(getMaximumDistance().getSI(), 0, 0), Direction.ZERO);
141             OTSRoadNode end = new OTSRoadNode(this.network, "End", new OTSPoint3D(getMaximumDistance().getSI() + 50.0, 0, 0),
142                     Direction.ZERO);
143             LaneType laneType = this.network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
144             this.lane = LaneFactory.makeLane(this.network, "Lane", from, to, null, laneType, this.speedLimit, this.simulator);
145             this.path.add(this.lane);
146             CrossSectionLink endLink = LaneFactory.makeLink(this.network, "endLink", to, end, null, this.simulator);
147             // No overtaking, single lane
148             Lane sinkLane = new Lane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0),
149                     this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0), laneType,
150                     this.speedLimit);
151             new SinkSensor(sinkLane, new Length(10.0, METER), Compatible.EVERYTHING, this.simulator);
152             this.path.add(sinkLane);
153 
154             this.carProbability = (double) getInputParameter("generic.carProbability");
155             this.parametersCar = InputParameterHelper.getParametersCar(getInputParameterMap());
156             this.parametersTruck = InputParameterHelper.getParametersTruck(getInputParameterMap());
157 
158             this.strategicalPlannerGeneratorCars = new LaneBasedStrategicalRoutePlannerFactory(
159                     new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
160             this.strategicalPlannerGeneratorTrucks = new LaneBasedStrategicalRoutePlannerFactory(
161                     new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
162 
163             // 1500 [veh / hour] == 2.4s headway
164             this.headway = new Duration(3600.0 / 1500.0, SECOND);
165 
166             // Schedule creation of the first car (it will re-schedule itself one headway later, etc.).
167             this.simulator.scheduleEventAbs(Time.ZERO, this, this, "generateCar", null);
168 
169             this.block = new SimpleTrafficLight(this.lane.getId() + "_TL", this.lane,
170                     new Length(new Length(4000.0, LengthUnit.METER)), this.simulator);
171             this.block.setTrafficLightColor(TrafficLightColor.GREEN);
172             // Create a block at t = 5 minutes
173             this.simulator.scheduleEventAbs(new Time(300, TimeUnit.BASE_SECOND), this, this, "createBlock", null);
174             // Remove the block at t = 7 minutes
175             this.simulator.scheduleEventAbs(new Time(420, TimeUnit.BASE_SECOND), this, this, "removeBlock", null);
176         }
177         catch (SimRuntimeException | NetworkException | OTSGeometryException | InputParameterException | GTUException
178                 | ParameterException exception)
179         {
180             exception.printStackTrace();
181         }
182     }
183 
184     /**
185      * Set up the block.
186      */
187     protected final void createBlock()
188     {
189         this.block.setTrafficLightColor(TrafficLightColor.RED);
190     }
191 
192     /**
193      * Remove the block.
194      */
195     protected final void removeBlock()
196     {
197         this.block.setTrafficLightColor(TrafficLightColor.GREEN);
198     }
199 
200     /**
201      * Generate cars at a fixed rate (implemented by re-scheduling this method).
202      */
203     protected final void generateCar()
204     {
205         try
206         {
207             boolean generateTruck = this.stream.nextDouble() > this.carProbability;
208             Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
209             LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated),
210                     this.network.getGtuType(GTUType.DEFAULTS.CAR), vehicleLength, new Length(1.8, METER),
211                     new Speed(200, KM_PER_HOUR), vehicleLength.times(0.5), this.simulator, this.network);
212             gtu.setParameters(generateTruck ? this.parametersTruck : this.parametersCar);
213             gtu.setNoLaneChangeDistance(Length.ZERO);
214             gtu.setMaximumAcceleration(Acceleration.instantiateSI(3.0));
215             gtu.setMaximumDeceleration(Acceleration.instantiateSI(-8.0));
216 
217             // strategical planner
218             LaneBasedStrategicalPlanner strategicalPlanner =
219                     generateTruck ? this.strategicalPlannerGeneratorTrucks.create(gtu, null, null, null)
220                             : this.strategicalPlannerGeneratorCars.create(gtu, null, null, null);
221 
222             Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
223             Length initialPosition = new Length(20, METER);
224             initialPositions.add(new DirectedLanePosition(this.lane, initialPosition, GTUDirectionality.DIR_PLUS));
225             Speed initialSpeed = new Speed(100.0, KM_PER_HOUR);
226             gtu.init(strategicalPlanner, initialPositions, initialSpeed);
227             this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
228         }
229         catch (SimRuntimeException | NetworkException | GTUException | OTSGeometryException exception)
230         {
231             exception.printStackTrace();
232         }
233     }
234 
235     /**
236      * @return List&lt;Lane&gt;; the set of lanes for the specified index
237      */
238     public List<Lane> getPath()
239     {
240         return new ArrayList<>(this.path);
241     }
242 
243     /** {@inheritDoc} */
244     @Override
245     public OTSRoadNetwork getNetwork()
246     {
247         return this.network;
248     }
249 
250     /**
251      * @return minimumDistance
252      */
253     public final Length getMinimumDistance()
254     {
255         return this.minimumDistance;
256     }
257 
258     /**
259      * @return maximumDistance
260      */
261     public final Length getMaximumDistance()
262     {
263         return this.maximumDistance;
264     }
265 
266     /**
267      * @return lane.
268      */
269     public Lane getLane()
270     {
271         return this.lane;
272     }
273 
274 }