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