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