1 package org.opentrafficsim.demo;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.LinkedHashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.unit.TimeUnit;
11 import org.djunits.unit.util.UNITS;
12 import org.djunits.value.vdouble.scalar.Acceleration;
13 import org.djunits.value.vdouble.scalar.Direction;
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Speed;
17 import org.djunits.value.vdouble.scalar.Time;
18 import org.opentrafficsim.base.parameters.ParameterException;
19 import org.opentrafficsim.base.parameters.Parameters;
20 import org.opentrafficsim.core.compatibility.Compatible;
21 import org.opentrafficsim.core.dsol.AbstractOTSModel;
22 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
23 import org.opentrafficsim.core.geometry.OTSGeometryException;
24 import org.opentrafficsim.core.geometry.OTSPoint3D;
25 import org.opentrafficsim.core.gtu.GTUDirectionality;
26 import org.opentrafficsim.core.gtu.GTUException;
27 import org.opentrafficsim.core.gtu.GTUType;
28 import org.opentrafficsim.core.network.NetworkException;
29 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
30 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
31 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
32 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
33 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
34 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
35 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
36 import org.opentrafficsim.road.network.OTSRoadNetwork;
37 import org.opentrafficsim.road.network.factory.LaneFactory;
38 import org.opentrafficsim.road.network.lane.CrossSectionLink;
39 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
40 import org.opentrafficsim.road.network.lane.Lane;
41 import org.opentrafficsim.road.network.lane.LaneType;
42 import org.opentrafficsim.road.network.lane.OTSRoadNode;
43 import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
44 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
45 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
46
47 import nl.tudelft.simulation.dsol.SimRuntimeException;
48 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
49 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
50 import nl.tudelft.simulation.jstats.streams.StreamInterface;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class StraightModel extends AbstractOTSModel implements UNITS
74 {
75
76 private static final long serialVersionUID = 20140815L;
77
78
79 private final OTSRoadNetwork network = new OTSRoadNetwork("network", true);
80
81
82 private Duration headway;
83
84
85 private int carsCreated = 0;
86
87
88 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
89
90
91 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
92
93
94 private Parameters parametersCar;
95
96
97 private Parameters parametersTruck;
98
99
100 private double carProbability;
101
102
103 private SimpleTrafficLight block = null;
104
105
106 private Length minimumDistance = new Length(0, METER);
107
108
109 private Length maximumDistance = new Length(5000, METER);
110
111
112 private Lane lane;
113
114
115 private StreamInterface stream = new MersenneTwister(12345);
116
117
118 private List<Lane> path = new ArrayList<>();
119
120
121 private Speed speedLimit = new Speed(100, KM_PER_HOUR);
122
123
124
125
126 public StraightModel(final OTSSimulatorInterface simulator)
127 {
128 super(simulator);
129 InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
130 }
131
132
133 @Override
134 public final void constructModel() throws SimRuntimeException
135 {
136 try
137 {
138 OTSRoadNode from =
139 new OTSRoadNode(this.network, "From", new OTSPoint3D(getMinimumDistance().getSI(), 0, 0), Direction.ZERO);
140 OTSRoadNode to =
141 new OTSRoadNode(this.network, "To", new OTSPoint3D(getMaximumDistance().getSI(), 0, 0), Direction.ZERO);
142 OTSRoadNode end = new OTSRoadNode(this.network, "End", new OTSPoint3D(getMaximumDistance().getSI() + 50.0, 0, 0),
143 Direction.ZERO);
144 LaneType laneType = this.network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
145 this.lane = LaneFactory.makeLane(this.network, "Lane", from, to, null, laneType, this.speedLimit, this.simulator);
146 this.path.add(this.lane);
147 CrossSectionLink endLink = LaneFactory.makeLink(this.network, "endLink", to, end, null, this.simulator);
148
149 Lane sinkLane = new Lane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0),
150 this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0), laneType,
151 this.speedLimit);
152 new SinkSensor(sinkLane, new Length(10.0, METER), Compatible.EVERYTHING, this.simulator);
153 this.path.add(sinkLane);
154
155 this.carProbability = (double) getInputParameter("generic.carProbability");
156 this.parametersCar = InputParameterHelper.getParametersCar(getInputParameterMap());
157 this.parametersTruck = InputParameterHelper.getParametersTruck(getInputParameterMap());
158
159 this.strategicalPlannerGeneratorCars = new LaneBasedStrategicalRoutePlannerFactory(
160 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
161 this.strategicalPlannerGeneratorTrucks = new LaneBasedStrategicalRoutePlannerFactory(
162 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()));
163
164
165 this.headway = new Duration(3600.0 / 1500.0, SECOND);
166
167
168 this.simulator.scheduleEventAbs(Time.ZERO, this, this, "generateCar", null);
169
170 this.block = new SimpleTrafficLight(this.lane.getId() + "_TL", this.lane,
171 new Length(new Length(4000.0, LengthUnit.METER)), this.simulator);
172 this.block.setTrafficLightColor(TrafficLightColor.GREEN);
173
174 this.simulator.scheduleEventAbs(new Time(300, TimeUnit.BASE_SECOND), this, this, "createBlock", null);
175
176 this.simulator.scheduleEventAbs(new Time(420, TimeUnit.BASE_SECOND), this, this, "removeBlock", null);
177 }
178 catch (SimRuntimeException | NetworkException | OTSGeometryException | InputParameterException | GTUException
179 | ParameterException exception)
180 {
181 exception.printStackTrace();
182 }
183 }
184
185
186
187
188 protected final void createBlock()
189 {
190 this.block.setTrafficLightColor(TrafficLightColor.RED);
191 }
192
193
194
195
196 protected final void removeBlock()
197 {
198 this.block.setTrafficLightColor(TrafficLightColor.GREEN);
199 }
200
201
202
203
204 protected final void generateCar()
205 {
206 try
207 {
208 boolean generateTruck = this.stream.nextDouble() > this.carProbability;
209 Length vehicleLength = new Length(generateTruck ? 15 : 4, METER);
210 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated),
211 this.network.getGtuType(GTUType.DEFAULTS.CAR), vehicleLength, new Length(1.8, METER),
212 new Speed(200, KM_PER_HOUR), vehicleLength.times(0.5), this.simulator, this.network);
213 gtu.setParameters(generateTruck ? this.parametersTruck : this.parametersCar);
214 gtu.setNoLaneChangeDistance(Length.ZERO);
215 gtu.setMaximumAcceleration(Acceleration.instantiateSI(3.0));
216 gtu.setMaximumDeceleration(Acceleration.instantiateSI(-8.0));
217
218
219 LaneBasedStrategicalPlanner strategicalPlanner =
220 generateTruck ? this.strategicalPlannerGeneratorTrucks.create(gtu, null, null, null)
221 : this.strategicalPlannerGeneratorCars.create(gtu, null, null, null);
222
223 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
224 Length initialPosition = new Length(20, METER);
225 initialPositions.add(new DirectedLanePosition(this.lane, initialPosition, GTUDirectionality.DIR_PLUS));
226 Speed initialSpeed = new Speed(100.0, KM_PER_HOUR);
227 gtu.init(strategicalPlanner, initialPositions, initialSpeed);
228 this.simulator.scheduleEventRel(this.headway, this, this, "generateCar", null);
229 }
230 catch (SimRuntimeException | NetworkException | GTUException | OTSGeometryException exception)
231 {
232 exception.printStackTrace();
233 }
234 }
235
236
237
238
239 public List<Lane> getPath()
240 {
241 return new ArrayList<>(this.path);
242 }
243
244
245 @Override
246 public OTSRoadNetwork getNetwork()
247 {
248 return this.network;
249 }
250
251
252
253
254 public final Length getMinimumDistance()
255 {
256 return this.minimumDistance;
257 }
258
259
260
261
262 public final Length getMaximumDistance()
263 {
264 return this.maximumDistance;
265 }
266
267
268
269
270 public Lane getLane()
271 {
272 return this.lane;
273 }
274
275
276 @Override
277 public Serializable getSourceId()
278 {
279 return "StraightModel";
280 }
281
282 }