1 package org.opentrafficsim.road.gtu.generator;
2
3 import java.io.Serializable;
4 import java.util.LinkedList;
5 import java.util.Queue;
6 import java.util.Set;
7
8 import javax.naming.NamingException;
9
10 import nl.tudelft.simulation.dsol.SimRuntimeException;
11
12 import org.djunits.unit.LengthUnit;
13 import org.djunits.unit.TimeUnit;
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.core.distributions.Generator;
19 import org.opentrafficsim.core.distributions.ProbabilityException;
20 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
21 import org.opentrafficsim.core.geometry.OTSGeometryException;
22 import org.opentrafficsim.core.gtu.GTUException;
23 import org.opentrafficsim.core.gtu.RelativePosition;
24 import org.opentrafficsim.core.gtu.animation.GTUColorer;
25 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
26 import org.opentrafficsim.core.network.NetworkException;
27 import org.opentrafficsim.core.network.OTSNetwork;
28 import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
29 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
30 import org.opentrafficsim.road.gtu.lane.LaneBasedGTUCharacteristics;
31 import org.opentrafficsim.road.gtu.lane.LaneBasedGTUCharacteristicsGenerator;
32 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
33 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
34 import org.opentrafficsim.road.network.lane.Lane;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class LaneBasedGTUGenerator implements Serializable
50 {
51
52 private static final long serialVersionUID = 20160000L;
53
54
55 private final Queue<LaneBasedGTUCharacteristics> unplacedTemplates = new LinkedList<>();
56
57
58 private final String id;
59
60
61 private final Generator<Duration> interarrivelTimeGenerator;
62
63
64 private final LaneBasedGTUCharacteristicsGenerator laneBasedGTUCharacteristicsGenerator;
65
66
67 private final Time endTime;
68
69
70 private final long maxGTUs;
71
72
73 private long generatedGTUs = 0;
74
75
76 Duration reTryInterval = new Duration(0.1, TimeUnit.SI);
77
78
79 final Set<DirectedLanePosition> initialLongitudinalPositions;
80
81
82 final RoomChecker roomChecker;
83
84
85 final GTUColorer gtuColorer;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public LaneBasedGTUGenerator(String id, final Generator<Duration> interarrivelTimeGenerator, final long maxGTUs,
106 final Time startTime, final Time endTime, final GTUColorer gtuColorer,
107 final LaneBasedGTUCharacteristicsGenerator laneBasedGTUCharacteristicsGenerator,
108 final Set<DirectedLanePosition> initialLongitudinalPositions, final OTSNetwork network, RoomChecker roomChecker)
109 throws SimRuntimeException, ProbabilityException
110 {
111 this.id = id;
112 this.interarrivelTimeGenerator = interarrivelTimeGenerator;
113 this.laneBasedGTUCharacteristicsGenerator = laneBasedGTUCharacteristicsGenerator;
114 this.endTime = endTime;
115 this.maxGTUs = maxGTUs;
116 this.initialLongitudinalPositions = initialLongitudinalPositions;
117 this.roomChecker = roomChecker;
118 this.gtuColorer = gtuColorer;
119 laneBasedGTUCharacteristicsGenerator.getSimulator().scheduleEventAbs(startTime, this, this,
120 "generateCharacteristics", new Object[] {});
121 }
122
123
124
125
126
127
128
129
130
131 @SuppressWarnings("unused")
132 private void generateCharacteristics() throws ProbabilityException, SimRuntimeException, ParameterException,
133 GTUException
134 {
135 OTSDEVSSimulatorInterface simulator = this.laneBasedGTUCharacteristicsGenerator.getSimulator();
136 if (this.generatedGTUs >= this.maxGTUs
137 || this.laneBasedGTUCharacteristicsGenerator.getSimulator().getSimulatorTime().get().ge(this.endTime))
138 {
139 return;
140 }
141 synchronized (this.unplacedTemplates)
142 {
143 this.generatedGTUs++;
144 this.unplacedTemplates.add(this.laneBasedGTUCharacteristicsGenerator.draw());
145 if (this.unplacedTemplates.size() == 1)
146 {
147 simulator.scheduleEventNow(this, this, "tryToPlaceGTU", new Object[] {});
148 }
149 }
150 if (this.generatedGTUs < this.maxGTUs)
151 {
152 simulator.scheduleEventRel(this.interarrivelTimeGenerator.draw(), this, this, "generateCharacteristics",
153 new Object[] {});
154 }
155 }
156
157
158 private int lastReportedQueueLength = 0;
159
160
161
162
163
164
165
166
167
168
169 @SuppressWarnings("unused")
170 private void tryToPlaceGTU() throws SimRuntimeException, GTUException, NamingException, NetworkException,
171 OTSGeometryException, ProbabilityException
172 {
173
174 LaneBasedGTUCharacteristics characteristics;
175 OTSDEVSSimulatorInterface simulator = this.laneBasedGTUCharacteristicsGenerator.getSimulator();
176 synchronized (this.unplacedTemplates)
177 {
178 characteristics = this.unplacedTemplates.peek();
179 }
180 if (null == characteristics)
181 {
182 return;
183 }
184 Length shortestHeadway = new Length(Double.MAX_VALUE, LengthUnit.SI);
185 Speed leaderSpeed = null;
186 for (DirectedLanePosition dlp : this.initialLongitudinalPositions)
187 {
188 Lane lane = dlp.getLane();
189
190 LaneBasedGTU leader =
191 lane.getGtuAhead(dlp.getPosition(), dlp.getGtuDirection(), RelativePosition.FRONT, simulator
192 .getSimulatorTime().getTime());
193 if (null != leader)
194 {
195 Length headway = leader.position(lane, leader.getRear()).minus(dlp.getPosition());
196 if (headway.si < 0)
197 {
198 headway = new Length(Math.abs(headway.si), headway.getUnit());
199 }
200 headway = new Length(headway.si - characteristics.getLength().si / 2, LengthUnit.SI);
201 if (shortestHeadway.gt(headway))
202 {
203 shortestHeadway = headway;
204 leaderSpeed = leader.getSpeed();
205 }
206 }
207 }
208 if (shortestHeadway.si > 0)
209 {
210 Speed safeSpeed = characteristics.getSpeed();
211 if (null != leaderSpeed)
212 {
213 safeSpeed = this.roomChecker.canPlace(leaderSpeed, shortestHeadway, characteristics);
214 }
215 if (null != safeSpeed)
216 {
217
218 synchronized (this.unplacedTemplates)
219 {
220 this.unplacedTemplates.remove();
221 }
222 if (safeSpeed.gt(characteristics.getMaximumSpeed()))
223 {
224 safeSpeed = characteristics.getMaximumSpeed();
225 }
226 String gtuId = null == characteristics.getIdGenerator() ? null : characteristics.getIdGenerator().nextId();
227 LaneBasedIndividualGTU gtu =
228 new LaneBasedIndividualGTU(gtuId, characteristics.getGTUType(), characteristics.getLength(),
229 characteristics.getWidth(), characteristics.getMaximumSpeed(), simulator, characteristics
230 .getNetwork());
231 gtu.initWithAnimation(characteristics.getStrategicalPlannerFactory().create(gtu),
232 this.initialLongitudinalPositions, safeSpeed, DefaultCarAnimation.class, this.gtuColorer);
233
234
235 }
236 }
237 int queueLength = this.unplacedTemplates.size();
238 if (queueLength != this.lastReportedQueueLength)
239 {
240 System.out.println("Generator " + this.id + ": queue length is " + queueLength + " at time "
241 + simulator.getSimulatorTime().get());
242 this.lastReportedQueueLength = queueLength;
243 }
244 if (queueLength > 0)
245 {
246
247 this.laneBasedGTUCharacteristicsGenerator.getSimulator().scheduleEventRel(this.reTryInterval, this, this,
248 "tryToPlaceGTU", new Object[] {});
249 }
250 }
251
252
253 public String toString()
254 {
255 return "LaneBasedGTUGenerator " + this.id + " on " + this.initialLongitudinalPositions;
256 }
257
258
259
260
261 public long getGeneratedGTUs()
262 {
263 return this.generatedGTUs;
264 }
265
266
267
268
269 public void setGeneratedGTUs(long generatedGTUs)
270 {
271 this.generatedGTUs = generatedGTUs;
272 }
273
274
275
276
277
278 public String getId()
279 {
280 return this.id;
281 }
282
283
284
285
286
287 public Time getEndTime()
288 {
289 return this.endTime;
290 }
291
292
293
294
295
296 public long getMaxGTUs()
297 {
298 return this.maxGTUs;
299 }
300
301
302
303
304
305 public GTUColorer getGtuColorer()
306 {
307 return this.gtuColorer;
308 }
309
310
311
312
313
314 public interface RoomChecker
315 {
316
317
318
319
320
321
322
323
324
325
326
327 public Speed canPlace(final Speed leaderSpeed, final Length headway,
328 final LaneBasedGTUCharacteristics laneBasedGTUCharacteristics) throws NetworkException;
329 }
330
331 }