1 package org.opentrafficsim.demo.conflictAndControl;
2
3 import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4 import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR;
5
6 import java.awt.BorderLayout;
7 import java.awt.geom.Rectangle2D;
8 import java.awt.geom.Rectangle2D.Double;
9 import java.net.URL;
10 import java.rmi.RemoteException;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.LinkedHashSet;
14 import java.util.Set;
15
16 import javax.naming.NamingException;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.SwingUtilities;
20
21 import org.djunits.unit.AccelerationUnit;
22 import org.djunits.unit.DurationUnit;
23 import org.djunits.unit.LengthUnit;
24 import org.djunits.value.vdouble.scalar.Acceleration;
25 import org.djunits.value.vdouble.scalar.Duration;
26 import org.djunits.value.vdouble.scalar.Length;
27 import org.djunits.value.vdouble.scalar.Speed;
28 import org.djunits.value.vdouble.scalar.Time;
29 import org.opentrafficsim.base.modelproperties.Property;
30 import org.opentrafficsim.base.modelproperties.PropertyException;
31 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
32 import org.opentrafficsim.core.dsol.OTSModelInterface;
33 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
34 import org.opentrafficsim.core.geometry.OTSGeometryException;
35 import org.opentrafficsim.core.gtu.GTUDirectionality;
36 import org.opentrafficsim.core.gtu.GTUException;
37 import org.opentrafficsim.core.gtu.GTUType;
38 import org.opentrafficsim.core.gtu.RelativePosition;
39 import org.opentrafficsim.core.gtu.animation.GTUColorer;
40 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
41 import org.opentrafficsim.core.network.NetworkException;
42 import org.opentrafficsim.core.network.OTSNetwork;
43 import org.opentrafficsim.demo.carFollowing.DefaultsFactory;
44 import org.opentrafficsim.road.animation.AnimationToggles;
45 import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
46 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
47 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner;
48 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
49 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
50 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
51 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
52 import org.opentrafficsim.road.network.lane.CrossSectionLink;
53 import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
54 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
55 import org.opentrafficsim.road.network.lane.Lane;
56 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
57 import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
58 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
59 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
60 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
61 import org.opentrafficsim.simulationengine.OTSSimulationException;
62 import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
63 import org.opentrafficsim.trafficcontrol.TrafficController;
64 import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
65
66 import nl.tudelft.simulation.dsol.SimRuntimeException;
67 import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
68 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
69 import nl.tudelft.simulation.event.EventInterface;
70 import nl.tudelft.simulation.event.EventListenerInterface;
71 import nl.tudelft.simulation.event.EventProducer;
72 import nl.tudelft.simulation.event.EventType;
73 import nl.tudelft.simulation.language.io.URLResource;
74
75
76
77
78
79
80
81
82
83
84
85
86 public class DemoTrafcodAndTurbo extends AbstractWrappableAnimation
87 {
88
89
90 private static final long serialVersionUID = 20161118L;
91
92
93
94
95
96
97 public static void main(final String[] args) throws SimRuntimeException
98 {
99 SwingUtilities.invokeLater(new Runnable()
100 {
101 @Override
102 public void run()
103 {
104 try
105 {
106 DemoTrafcodAndTurbo model = new DemoTrafcodAndTurbo();
107
108 model.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
109 new ArrayList<Property<?>>(), null, true);
110 }
111 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
112 {
113 exception.printStackTrace();
114 }
115 }
116 });
117 }
118
119
120 private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
121
122
123 TrafCOD trafCOD;
124
125
126 @Override
127 public final String shortName()
128 {
129 return "TrafCOD demonstration 2";
130 }
131
132
133 @Override
134 public final String description()
135 {
136 return "TrafCOD demonstration";
137 }
138
139
140 @Override
141 protected final void addTabs(final SimpleSimulatorInterface simulator) throws OTSSimulationException, PropertyException
142 {
143 JScrollPane scrollPane = new JScrollPane(DemoTrafcodAndTurbo.this.controllerDisplayPanel);
144 JPanel wrapper = new JPanel(new BorderLayout());
145 wrapper.add(scrollPane);
146 addTab(getTabCount() - 1, this.trafCOD.getId(), wrapper);
147 }
148
149
150 @Override
151 protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
152 {
153 return new TrafCODModel();
154 }
155
156
157 @Override
158 protected final void addAnimationToggles()
159 {
160 AnimationToggles.setTextAnimationTogglesStandard(this);
161 }
162
163
164 @Override
165 protected final Double makeAnimationRectangle()
166 {
167 return new Rectangle2D.Double(-200, -200, 400, 400);
168 }
169
170
171
172
173 class TrafCODModel extends EventProducer implements OTSModelInterface, EventListenerInterface
174 {
175
176 private static final long serialVersionUID = 20161020L;
177
178
179 private OTSNetwork network;
180
181 @SuppressWarnings("synthetic-access")
182 @Override
183 public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
184 throws SimRuntimeException, RemoteException
185 {
186 try
187 {
188 URL url = URLResource.getResource("/conflictAndControl/TurboRoundaboutAndSignal.xml");
189 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((OTSDEVSSimulatorInterface) theSimulator);
190 this.network = nlp.build(url);
191
192 ((CrossSectionLink) this.network.getLink("EBNA")).setPriority(Priority.PRIORITY);
193 ((CrossSectionLink) this.network.getLink("NBWA")).setPriority(Priority.PRIORITY);
194 ((CrossSectionLink) this.network.getLink("WBSA")).setPriority(Priority.PRIORITY);
195 ((CrossSectionLink) this.network.getLink("SBEA")).setPriority(Priority.PRIORITY);
196 ConflictBuilder.buildConflicts(this.network, VEHICLE, (OTSDEVSSimulatorInterface) theSimulator,
197 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
198
199
200
201
202
203
204
205
206 String[] directions = { "E", "S", "W", "N" };
207
208 Set<TrafficLight> trafficLights = new HashSet<>();
209 Set<TrafficLightSensor> sensors = new HashSet<>();
210 Length stopLineMargin = new Length(0.1, LengthUnit.METER);
211 Length headDetectorLength = new Length(1, LengthUnit.METER);
212 Length headDetectorMargin = stopLineMargin.plus(headDetectorLength).plus(new Length(3, LengthUnit.METER));
213 Length longDetectorLength = new Length(30, LengthUnit.METER);
214 Length longDetectorMargin = stopLineMargin.plus(longDetectorLength).plus(new Length(10, LengthUnit.METER));
215 int stream = 1;
216 for (String direction : directions)
217 {
218 for (int laneNumber = 3; laneNumber >= 1; laneNumber--)
219 {
220 Lane lane = (Lane) ((CrossSectionLink) this.network.getLink(direction + "S", direction + "C"))
221 .getCrossSectionElement("FORWARD" + laneNumber);
222 if (lane != null)
223 {
224 if (stream != 7)
225 {
226 trafficLights.add(new SimpleTrafficLight(String.format("TL%02d", stream), lane,
227 lane.getLength().minus(stopLineMargin), (OTSDEVSSimulatorInterface) theSimulator));
228 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
229 lane.getLength().minus(headDetectorMargin), lane,
230 lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
231 RelativePosition.FRONT, RelativePosition.REAR,
232 (OTSDEVSSimulatorInterface) theSimulator));
233 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
234 lane.getLength().minus(longDetectorMargin), lane,
235 lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
236 RelativePosition.FRONT, RelativePosition.REAR,
237 (OTSDEVSSimulatorInterface) theSimulator));
238 }
239 else
240 {
241 lane = (Lane) ((CrossSectionLink) this.network.getLink("ESS1", "ESS"))
242 .getCrossSectionElement("FORWARD");
243 trafficLights.add(new SimpleTrafficLight(String.format("TL%02d", stream), lane,
244 lane.getLength().minus(stopLineMargin), (OTSDEVSSimulatorInterface) theSimulator));
245 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
246 lane.getLength().minus(headDetectorMargin), lane,
247 lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
248 RelativePosition.FRONT, RelativePosition.REAR,
249 (OTSDEVSSimulatorInterface) theSimulator));
250 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
251 lane.getLength().minus(longDetectorMargin), lane,
252 lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
253 RelativePosition.FRONT, RelativePosition.REAR,
254 (OTSDEVSSimulatorInterface) theSimulator));
255
256 }
257
258 }
259 stream++;
260 }
261 }
262 String controllerName = "Not so simple TrafCOD controller";
263 DemoTrafcodAndTurbo.this.trafCOD =
264 new TrafCOD(controllerName, URLResource.getResource("/conflictAndControl/Intersection12Dir.tfc"),
265 trafficLights, sensors, (DEVSSimulator<Time, Duration, OTSSimTimeDouble>) theSimulator,
266 DemoTrafcodAndTurbo.this.controllerDisplayPanel);
267 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
268 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
269 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
270 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
271 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
272 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
273
274
275 addListener(DemoTrafcodAndTurbo.this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 }
296 catch (Exception exception)
297 {
298 exception.printStackTrace();
299 }
300 }
301
302 @SuppressWarnings("synthetic-access")
303 @Override
304 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
305 {
306 return DemoTrafcodAndTurbo.this.trafCOD.getSimulator();
307 }
308
309
310 @Override
311 public final OTSNetwork getNetwork()
312 {
313 return this.network;
314 }
315
316
317 @Override
318 public void notify(final EventInterface event) throws RemoteException
319 {
320 EventType type = event.getType();
321 Object[] payload = (Object[]) event.getContent();
322 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
323 {
324
325
326 return;
327 }
328 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
329 {
330 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
331 }
332 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
333 {
334 System.out.println(String.format("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]));
335 }
336 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
337 {
338 System.out.println("Warning " + payload[1]);
339 }
340 else
341 {
342 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
343 String separator = "";
344 for (Object o : payload)
345 {
346 System.out.print(separator + o);
347 separator = ",";
348 }
349 System.out.println("]");
350 }
351 }
352
353
354
355
356
357
358
359
360
361
362
363
364
365 private Lane setupBlock(final Lane lane, OTSDEVSSimulatorInterface theSimulator, GTUColorer gtuColorer)
366 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
367 {
368 Length initialPosition = lane.getLength();
369 Duration tSafe = new Duration(0, DurationUnit.SECOND);
370 Acceleration ac1 = new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2);
371 Length l = new Length(1.0, LengthUnit.METER);
372 IDMPlusOld carFollowingModelCars = new IDMPlusOld(ac1, ac1, l, tSafe, 1.0);
373 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
374 initialPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
375 GTUType gtuType = CAR;
376 BehavioralCharacteristics behavioralCharacteristics = DefaultsFactory.getDefaultBehavioralCharacteristics();
377 LaneBasedIndividualGTU block = new LaneBasedIndividualGTU("999999", gtuType, new Length(1, LengthUnit.METER),
378 lane.getWidth(1), Speed.ZERO, theSimulator, (OTSNetwork) this.network);
379 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
380 new LaneBasedGTUFollowingTacticalPlanner(carFollowingModelCars, block), block);
381 block.initWithAnimation(strategicalPlanner, initialPositions, Speed.ZERO, DefaultCarAnimation.class, gtuColorer);
382 return lane;
383 }
384
385 }
386
387 }