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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public class StraightModel extends AbstractOTSModel implements UNITS
73 {
74
75 private static final long serialVersionUID = 20140815L;
76
77
78 private final OTSRoadNetwork network = new OTSRoadNetwork("network", true);
79
80
81 private Duration headway;
82
83
84 private int carsCreated = 0;
85
86
87 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
88
89
90 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
91
92
93 private Parameters parametersCar;
94
95
96 private Parameters parametersTruck;
97
98
99 private double carProbability;
100
101
102 private SimpleTrafficLight block = null;
103
104
105 private Length minimumDistance = new Length(0, METER);
106
107
108 private Length maximumDistance = new Length(5000, METER);
109
110
111 private Lane lane;
112
113
114 private StreamInterface stream = new MersenneTwister(12345);
115
116
117 private List<Lane> path = new ArrayList<>();
118
119
120 private Speed speedLimit = new Speed(100, KM_PER_HOUR);
121
122
123
124
125 public StraightModel(final OTSSimulatorInterface simulator)
126 {
127 super(simulator);
128 InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
129 }
130
131
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
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
164 this.headway = new Duration(3600.0 / 1500.0, SECOND);
165
166
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
173 this.simulator.scheduleEventAbs(new Time(300, TimeUnit.BASE_SECOND), this, this, "createBlock", null);
174
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
186
187 protected final void createBlock()
188 {
189 this.block.setTrafficLightColor(TrafficLightColor.RED);
190 }
191
192
193
194
195 protected final void removeBlock()
196 {
197 this.block.setTrafficLightColor(TrafficLightColor.GREEN);
198 }
199
200
201
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
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
237
238 public List<Lane> getPath()
239 {
240 return new ArrayList<>(this.path);
241 }
242
243
244 @Override
245 public OTSRoadNetwork getNetwork()
246 {
247 return this.network;
248 }
249
250
251
252
253 public final Length getMinimumDistance()
254 {
255 return this.minimumDistance;
256 }
257
258
259
260
261 public final Length getMaximumDistance()
262 {
263 return this.maximumDistance;
264 }
265
266
267
268
269 public Lane getLane()
270 {
271 return this.lane;
272 }
273
274 }