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