1 package org.opentrafficsim.demo;
2
3 import static org.opentrafficsim.core.gtu.GTUType.CAR;
4
5 import java.rmi.RemoteException;
6 import java.util.ArrayList;
7 import java.util.HashSet;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Set;
11 import java.util.SortedMap;
12 import java.util.TreeMap;
13
14 import javax.naming.NamingException;
15
16 import org.djunits.unit.DurationUnit;
17 import org.djunits.unit.LengthUnit;
18 import org.djunits.unit.SpeedUnit;
19 import org.djunits.unit.UNITS;
20 import org.djunits.value.vdouble.scalar.DoubleScalar;
21 import org.djunits.value.vdouble.scalar.Duration;
22 import org.djunits.value.vdouble.scalar.Length;
23 import org.djunits.value.vdouble.scalar.Speed;
24 import org.opentrafficsim.base.parameters.ParameterException;
25 import org.opentrafficsim.core.distributions.Distribution;
26 import org.opentrafficsim.core.distributions.Distribution.FrequencyAndObject;
27 import org.opentrafficsim.core.distributions.Generator;
28 import org.opentrafficsim.core.distributions.ProbabilityException;
29 import org.opentrafficsim.core.dsol.AbstractOTSModel;
30 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
31 import org.opentrafficsim.core.geometry.OTSGeometryException;
32 import org.opentrafficsim.core.geometry.OTSPoint3D;
33 import org.opentrafficsim.core.gtu.GTU;
34 import org.opentrafficsim.core.gtu.GTUDirectionality;
35 import org.opentrafficsim.core.gtu.GTUException;
36 import org.opentrafficsim.core.gtu.GTUType;
37 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterFactory;
38 import org.opentrafficsim.core.idgenerator.IdGenerator;
39 import org.opentrafficsim.core.network.Network;
40 import org.opentrafficsim.core.network.NetworkException;
41 import org.opentrafficsim.core.network.Node;
42 import org.opentrafficsim.core.network.OTSNetwork;
43 import org.opentrafficsim.core.network.OTSNode;
44 import org.opentrafficsim.core.network.route.FixedRouteGenerator;
45 import org.opentrafficsim.core.network.route.ProbabilisticRouteGenerator;
46 import org.opentrafficsim.core.network.route.Route;
47 import org.opentrafficsim.core.network.route.RouteGenerator;
48 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
49 import org.opentrafficsim.road.gtu.generator.CFRoomChecker;
50 import org.opentrafficsim.road.gtu.generator.GeneratorPositions;
51 import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator;
52 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedTemplateGTUType;
53 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedTemplateGTUTypeDistribution;
54 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
55 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
56 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
57 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
58 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
59 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
60 import org.opentrafficsim.road.network.factory.LaneFactory;
61 import org.opentrafficsim.road.network.lane.CrossSectionLink;
62 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
63 import org.opentrafficsim.road.network.lane.Lane;
64 import org.opentrafficsim.road.network.lane.LaneType;
65 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
66 import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
67
68 import nl.tudelft.simulation.dsol.SimRuntimeException;
69 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterDouble;
70 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
71 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
72 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterSelectionMap;
73 import nl.tudelft.simulation.event.EventInterface;
74 import nl.tudelft.simulation.event.EventListenerInterface;
75 import nl.tudelft.simulation.event.EventType;
76 import nl.tudelft.simulation.jstats.distributions.DistContinuous;
77 import nl.tudelft.simulation.jstats.distributions.DistErlang;
78 import nl.tudelft.simulation.jstats.distributions.DistUniform;
79 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
80 import nl.tudelft.simulation.jstats.streams.StreamInterface;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public class NetworksModel extends AbstractOTSModel implements EventListenerInterface, UNITS
104 {
105
106 private static final long serialVersionUID = 20140815L;
107
108
109 private final OTSNetwork network = new OTSNetwork("network");
110
111
112 private GTUType gtuType = CAR;
113
114
115 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorCars = null;
116
117
118 private LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerGeneratorTrucks = null;
119
120
121 private double carProbability;
122
123
124 private Length minimumDistance = new Length(0, METER);
125
126
127 private Length maximumDistance = new Length(5000, METER);
128
129
130 private StreamInterface stream = new MersenneTwister(12345);
131
132
133 private RouteGenerator routeGeneratorMain;
134
135
136 private RouteGenerator routeGeneratorRamp;
137
138
139 private Speed speedLimit = new Speed(60, KM_PER_HOUR);
140
141
142 private List<List<Lane>> paths = new ArrayList<>();
143
144
145 private IdGenerator idGenerator = new IdGenerator("");
146
147
148 private DistContinuous headwayGenerator;
149
150
151
152
153 public NetworksModel(final OTSSimulatorInterface simulator)
154 {
155 super(simulator);
156 createInputParameters();
157 }
158
159
160
161
162 private void createInputParameters()
163 {
164 InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
165 try
166 {
167 InputParameterMap genericMap = (InputParameterMap) this.inputParameterMap.get("generic");
168
169 genericMap.add(new InputParameterDouble("flow", "Flow per input lane", "Traffic flow per input lane", 500d, 0d,
170 3000d, true, true, "%.0f veh/h", 1.5));
171
172 SortedMap<String, String> networks = new TreeMap<>();
173 networks.put("Merge 1 plus 1 into 1", "M111");
174 networks.put("Merge 2 plus 1 into 2", "M212");
175 networks.put("Merge 2 plus 2 into 4", "M224");
176 networks.put("Split 1 into 1 plus 1", "S111");
177 networks.put("Split 2 into 1 plus 2", "S212");
178 networks.put("Split 4 into 2 plus 2", "S422");
179 InputParameterSelectionMap<String, String> paramSelect = new InputParameterSelectionMap<String, String>("network",
180 "Network to run simulation for", "Network to run simulaton for", networks, "M111", 2.0);
181 genericMap.add(paramSelect);
182 }
183 catch (InputParameterException exception)
184 {
185 exception.printStackTrace();
186 }
187
188 }
189
190
191 @Override
192 @SuppressWarnings("checkstyle:methodlength")
193 public final void constructModel() throws SimRuntimeException
194 {
195 this.network.addListener(this, Network.GTU_ADD_EVENT);
196 this.network.addListener(this, Network.GTU_REMOVE_EVENT);
197 try
198 {
199 this.carProbability = (double) getInputParameter("generic.carProbability");
200
201 ParameterFactory params = new InputParameterHelper(getInputParameterMap());
202 this.strategicalPlannerGeneratorCars = new LaneBasedStrategicalRoutePlannerFactory(
203 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()), params);
204 this.strategicalPlannerGeneratorTrucks = new LaneBasedStrategicalRoutePlannerFactory(
205 new LMRSFactory(new IDMPlusFactory(this.stream), new DefaultLMRSPerceptionFactory()), params);
206
207 OTSNode from = new OTSNode(this.network, "From", new OTSPoint3D(0, 0, 0));
208 OTSNode end = new OTSNode(this.network, "End", new OTSPoint3D(2000, 0, 0));
209 OTSNode from2a = new OTSNode(this.network, "From2a", new OTSPoint3D(0, -50, 0));
210 OTSNode from2b = new OTSNode(this.network, "From2b", new OTSPoint3D(490, -2, 0));
211 OTSNode firstVia = new OTSNode(this.network, "Via1", new OTSPoint3D(500, 0, 0));
212 OTSNode end2a = new OTSNode(this.network, "End2a", new OTSPoint3D(1020, -2, 0));
213 OTSNode end2b = new OTSNode(this.network, "End2b", new OTSPoint3D(2000, -50, 0));
214 OTSNode secondVia = new OTSNode(this.network, "Via2", new OTSPoint3D(1000, 0, 0));
215
216 String networkType = getInputParameter("generic.network").toString();
217 boolean merge = networkType.startsWith("M");
218 int lanesOnMain = Integer.parseInt("" + networkType.charAt(merge ? 1 : 3));
219 int lanesOnBranch = Integer.parseInt("" + networkType.charAt(2));
220 int lanesOnCommon = lanesOnMain + lanesOnBranch;
221 int lanesOnCommonCompressed = Integer.parseInt("" + networkType.charAt(merge ? 3 : 1));
222
223 double contP = (double) getInputParameter("generic.flow");
224 Duration averageHeadway = new Duration(3600.0 / contP, SECOND);
225 Duration minimumHeadway = new Duration(3, SECOND);
226 this.headwayGenerator =
227 new DistErlang(new MersenneTwister(1234), 4, DoubleScalar.minus(averageHeadway, minimumHeadway).getSI());
228
229 LaneType laneType = LaneType.TWO_WAY_LANE;
230 if (merge)
231 {
232
233 ArrayList<Node> mainRouteNodes = new ArrayList<>();
234 mainRouteNodes.add(from);
235 mainRouteNodes.add(firstVia);
236 mainRouteNodes.add(secondVia);
237 mainRouteNodes.add(end);
238 Route mainRoute = new Route("main", mainRouteNodes);
239 this.routeGeneratorMain = new FixedRouteGenerator(mainRoute);
240
241 ArrayList<Node> rampRouteNodes = new ArrayList<>();
242 rampRouteNodes.add(from2a);
243 rampRouteNodes.add(from2b);
244 rampRouteNodes.add(firstVia);
245 rampRouteNodes.add(secondVia);
246 rampRouteNodes.add(end);
247 Route rampRoute = new Route("ramp", rampRouteNodes);
248 this.routeGeneratorRamp = new FixedRouteGenerator(rampRoute);
249 }
250 else
251 {
252
253 List<FrequencyAndObject<Route>> routeProbabilities = new ArrayList<>();
254
255 ArrayList<Node> mainRouteNodes = new ArrayList<>();
256 mainRouteNodes.add(from);
257 mainRouteNodes.add(firstVia);
258 mainRouteNodes.add(secondVia);
259 mainRouteNodes.add(end);
260 Route mainRoute = new Route("main", mainRouteNodes);
261 routeProbabilities.add(new FrequencyAndObject<>(lanesOnMain, mainRoute));
262
263 ArrayList<Node> sideRouteNodes = new ArrayList<>();
264 sideRouteNodes.add(from);
265 sideRouteNodes.add(firstVia);
266 sideRouteNodes.add(secondVia);
267 sideRouteNodes.add(end2a);
268 sideRouteNodes.add(end2b);
269 Route sideRoute = new Route("side", sideRouteNodes);
270 routeProbabilities.add(new FrequencyAndObject<>(lanesOnBranch, sideRoute));
271 try
272 {
273 this.routeGeneratorMain = new ProbabilisticRouteGenerator(routeProbabilities, new MersenneTwister(1234));
274 }
275 catch (ProbabilityException exception)
276 {
277 exception.printStackTrace();
278 }
279 }
280
281 if (merge)
282 {
283 setupGenerator(LaneFactory.makeMultiLane(this.network, "From2a to From2b", from2a, from2b, null, lanesOnBranch,
284 0, lanesOnCommon - lanesOnBranch, laneType, this.speedLimit, this.simulator));
285 LaneFactory.makeMultiLaneBezier(this.network, "From2b to FirstVia", from2a, from2b, firstVia, secondVia,
286 lanesOnBranch, lanesOnCommon - lanesOnBranch, lanesOnCommon - lanesOnBranch, laneType, this.speedLimit,
287 this.simulator);
288 }
289 else
290 {
291 LaneFactory.makeMultiLaneBezier(this.network, "SecondVia to end2a", firstVia, secondVia, end2a, end2b,
292 lanesOnBranch, lanesOnCommon - lanesOnBranch, lanesOnCommon - lanesOnBranch, laneType, this.speedLimit,
293 this.simulator);
294 setupSink(LaneFactory.makeMultiLane(this.network, "end2a to end2b", end2a, end2b, null, lanesOnBranch,
295 lanesOnCommon - lanesOnBranch, 0, laneType, this.speedLimit, this.simulator), laneType);
296 }
297
298 Lane[] startLanes = LaneFactory.makeMultiLane(this.network, "From to FirstVia", from, firstVia, null,
299 merge ? lanesOnMain : lanesOnCommonCompressed, laneType, this.speedLimit, this.simulator);
300 setupGenerator(startLanes);
301 Lane[] common = LaneFactory.makeMultiLane(this.network, "FirstVia to SecondVia", firstVia, secondVia, null,
302 lanesOnCommon, laneType, this.speedLimit, this.simulator);
303 setupSink(
304 LaneFactory.makeMultiLane(this.network, "SecondVia to end", secondVia, end, null,
305 merge ? lanesOnCommonCompressed : lanesOnMain, laneType, this.speedLimit, this.simulator),
306 laneType);
307
308 for (int index = 0; index < lanesOnCommon; index++)
309 {
310 this.paths.add(new ArrayList<Lane>());
311 Lane lane = common[index];
312
313 while (lane.prevLanes(this.gtuType).size() > 0)
314 {
315 if (lane.prevLanes(this.gtuType).size() > 1)
316 {
317 throw new NetworkException("This network should not have lane merge points");
318 }
319 lane = lane.prevLanes(this.gtuType).keySet().iterator().next();
320 }
321
322 while (true)
323 {
324 this.paths.get(index).add(lane);
325 int branching = lane.nextLanes(this.gtuType).size();
326 if (branching == 0)
327 {
328 break;
329 }
330 if (branching > 1)
331 {
332 throw new NetworkException("This network should not have lane split points");
333 }
334 lane = lane.nextLanes(this.gtuType).keySet().iterator().next();
335 }
336 }
337 }
338 catch (SimRuntimeException | NetworkException | OTSGeometryException | InputParameterException | GTUException
339 | ParameterException | NamingException | ProbabilityException exception)
340 {
341 exception.printStackTrace();
342 }
343 }
344
345
346
347
348
349
350
351
352
353
354 private Lane[] setupGenerator(final Lane[] lanes)
355 throws SimRuntimeException, GTUException, ProbabilityException, ParameterException
356 {
357 for (Lane lane : lanes)
358 {
359 makeGenerator(lane);
360 }
361 return lanes;
362 }
363
364
365
366
367
368
369
370
371
372
373 private LaneBasedGTUGenerator makeGenerator(final Lane lane)
374 throws GTUException, SimRuntimeException, ProbabilityException, ParameterException
375 {
376 Distribution<LaneBasedTemplateGTUType> distribution = new Distribution<>(this.stream);
377 Length initialPosition = new Length(16, METER);
378 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
379 initialPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
380
381 LaneBasedTemplateGTUType template = makeTemplate(this.stream, lane,
382 new ContinuousDistDoubleScalar.Rel<Length, LengthUnit>(new DistUniform(this.stream, 3, 6), METER),
383 new ContinuousDistDoubleScalar.Rel<Length, LengthUnit>(new DistUniform(this.stream, 1.6, 2.0), METER),
384 new ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit>(new DistUniform(this.stream, 140, 180), KM_PER_HOUR),
385 initialPositions, this.strategicalPlannerGeneratorCars);
386
387 distribution.add(new FrequencyAndObject<>(this.carProbability, template));
388 template = makeTemplate(this.stream, lane,
389 new ContinuousDistDoubleScalar.Rel<Length, LengthUnit>(new DistUniform(this.stream, 8, 14), METER),
390 new ContinuousDistDoubleScalar.Rel<Length, LengthUnit>(new DistUniform(this.stream, 2.0, 2.5), METER),
391 new ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit>(new DistUniform(this.stream, 100, 140), KM_PER_HOUR),
392 initialPositions, this.strategicalPlannerGeneratorTrucks);
393
394 distribution.add(new FrequencyAndObject<>(1.0 - this.carProbability, template));
395 LaneBasedTemplateGTUTypeDistribution templateDistribution = new LaneBasedTemplateGTUTypeDistribution(distribution);
396 LaneBasedGTUGenerator.RoomChecker roomChecker = new CFRoomChecker();
397 return new LaneBasedGTUGenerator(lane.getId(), new Generator<Duration>()
398 {
399 @SuppressWarnings("synthetic-access")
400 @Override
401 public Duration draw()
402 {
403 return new Duration(NetworksModel.this.headwayGenerator.draw(), DurationUnit.SI);
404 }
405 }, templateDistribution, GeneratorPositions.create(initialPositions, this.stream), this.network, this.simulator,
406 roomChecker, this.idGenerator);
407 }
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 LaneBasedTemplateGTUType makeTemplate(final StreamInterface randStream, final Lane lane,
423 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> lengthDistribution,
424 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> widthDistribution,
425 final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maximumSpeedDistribution,
426 final Set<DirectedLanePosition> initialPositions,
427 final LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerFactory) throws GTUException
428 {
429 return new LaneBasedTemplateGTUType(this.gtuType, new Generator<Length>()
430 {
431 @Override
432 public Length draw()
433 {
434 return lengthDistribution.draw();
435 }
436 }, new Generator<Length>()
437 {
438 @Override
439 public Length draw()
440 {
441 return widthDistribution.draw();
442 }
443 }, new Generator<Speed>()
444 {
445 @Override
446 public Speed draw()
447 {
448 return maximumSpeedDistribution.draw();
449 }
450 }, strategicalPlannerFactory,
451 lane.getParentLink().getStartNode().getId().equals("From") ? this.routeGeneratorMain : this.routeGeneratorRamp);
452
453 }
454
455
456
457
458
459
460
461
462
463 private Lane[] setupSink(final Lane[] lanes, final LaneType laneType) throws NetworkException, OTSGeometryException
464 {
465 CrossSectionLink link = lanes[0].getParentLink();
466 Node to = link.getEndNode();
467 Node from = link.getStartNode();
468 double endLinkLength = 50;
469 double endX = to.getPoint().x + (endLinkLength / link.getLength().getSI()) * (to.getPoint().x - from.getPoint().x);
470 double endY = to.getPoint().y + (endLinkLength / link.getLength().getSI()) * (to.getPoint().y - from.getPoint().y);
471 Node end = new OTSNode(this.network, link.getId() + "END", new OTSPoint3D(endX, endY, to.getPoint().z));
472 CrossSectionLink endLink = LaneFactory.makeLink(this.network, link.getId() + "endLink", to, end, null, this.simulator);
473 for (Lane lane : lanes)
474 {
475
476 Lane sinkLane = new Lane(endLink, lane.getId() + "." + "sinkLane", lane.getLateralCenterPosition(1.0),
477 lane.getLateralCenterPosition(1.0), lane.getWidth(1.0), lane.getWidth(1.0), laneType, this.speedLimit,
478 new OvertakingConditions.LeftAndRight());
479 new SinkSensor(sinkLane, new Length(10.0, METER), this.simulator);
480 }
481 return lanes;
482 }
483
484
485 private Set<GTU> knownGTUs = new HashSet<>();
486
487
488 @Override
489 public void notify(final EventInterface event) throws RemoteException
490 {
491 EventType eventType = event.getType();
492 if (Network.GTU_ADD_EVENT.equals(eventType))
493 {
494 System.out.println("A GTU just got created. It's Id is " + (String) event.getContent());
495 this.knownGTUs.add(this.network.getGTU((String) event.getContent()));
496 }
497 else if (Network.GTU_REMOVE_EVENT.equals(eventType))
498 {
499 System.out.println("A GTU with id " + ((String) event.getContent()) + " was removed from the network");
500 this.knownGTUs.remove(this.network.getGTU((String) event.getContent()));
501 }
502 }
503
504
505 @Override
506 public OTSNetwork getNetwork()
507 {
508 return this.network;
509 }
510
511
512
513
514
515 public final List<Lane> getPath(final int index)
516 {
517 return this.paths.get(index);
518 }
519
520
521
522
523
524 public final int pathCount()
525 {
526 return this.paths.size();
527 }
528
529
530
531
532 public final Length getMinimumDistance()
533 {
534 return this.minimumDistance;
535 }
536
537
538
539
540 public final Length getMaximumDistance()
541 {
542 return this.maximumDistance;
543 }
544
545 }