1 package org.opentrafficsim.demo.timing;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.naming.NamingException;
7 import javax.swing.SwingUtilities;
8
9 import org.djunits.unit.UNITS;
10 import org.djunits.value.vdouble.scalar.Acceleration;
11 import org.djunits.value.vdouble.scalar.Duration;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.djunits.value.vdouble.scalar.Time;
14 import org.opentrafficsim.base.modelproperties.CompoundProperty;
15 import org.opentrafficsim.base.modelproperties.ProbabilityDistributionProperty;
16 import org.opentrafficsim.base.modelproperties.Property;
17 import org.opentrafficsim.base.modelproperties.PropertyException;
18 import org.opentrafficsim.base.modelproperties.SelectionProperty;
19 import org.opentrafficsim.core.gtu.AbstractGTU;
20 import org.opentrafficsim.demo.carFollowing.CircularRoad;
21 import org.opentrafficsim.road.gtu.lane.AbstractLaneBasedGTU;
22 import org.opentrafficsim.road.modelproperties.IDMPropertySet;
23 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
24 import org.opentrafficsim.simulationengine.OTSSimulationException;
25 import org.opentrafficsim.simulationengine.SimpleAnimator;
26
27 import nl.tudelft.simulation.dsol.SimRuntimeException;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class TimeCircularRoadSimulation implements UNITS
41 {
42
43 private static final boolean GRAPHS = true;
44
45
46 public TimeCircularRoadSimulation()
47 {
48 try
49 {
50 AbstractWrappableAnimation simulation = new CircularRoad();
51 List<Property<?>> activeProperties = new ArrayList<>();
52 activeProperties.addAll(simulation.getProperties());
53 for (Property<?> ap : activeProperties)
54 {
55 if (ap instanceof SelectionProperty)
56 {
57 SelectionProperty sp = (SelectionProperty) ap;
58 if ("TacticalPlanner".equals(sp.getKey()))
59 {
60 sp.setValue("DIRECTED/IDM");
61 }
62 }
63 }
64 if (!GRAPHS)
65 {
66 for (Property<?> ap : activeProperties)
67 {
68 if (ap instanceof CompoundProperty)
69 {
70 CompoundProperty cp = (CompoundProperty) ap;
71 cp.setReadOnly(false);
72 if ("OutputGraphs".equals(cp.getKey()))
73 {
74 while (cp.size() > 0)
75 {
76 cp.remove(0);
77 }
78 }
79 }
80 }
81 }
82 activeProperties.add(new ProbabilityDistributionProperty("TrafficComposition", "Traffic composition",
83 "<html>Mix of passenger cars and trucks</html>", new String[] { "passenger car", "truck" },
84 new Double[] { 0.8, 0.2 }, false, 5));
85 CompoundProperty modelSelection =
86 new CompoundProperty("ModelSelection", "Model selection", "Modeling specific settings", null, false, 300);
87 modelSelection.add(new SelectionProperty("SimulationScale", "Simulation scale", "Level of detail of the simulation",
88 new String[] { "Micro", "Macro", "Meta" }, 0, true, 0));
89 modelSelection.add(new SelectionProperty("CarFollowingModel", "Car following model", "",
90 new String[] { "IDM", "IDM+" }, 1, false, 1));
91 modelSelection.add(IDMPropertySet.makeIDMPropertySet("IDMCar", "Car", new Acceleration(1.56, METER_PER_SECOND_2),
92 new Acceleration(2.09, METER_PER_SECOND_2), new Length(3.0, METER), new Duration(1.2, SECOND), 2));
93 modelSelection
94 .add(IDMPropertySet.makeIDMPropertySet("IDMTruck", "Truck", new Acceleration(0.75, METER_PER_SECOND_2),
95 new Acceleration(1.25, METER_PER_SECOND_2), new Length(3.0, METER), new Duration(1.2, SECOND), 3));
96 activeProperties.add(modelSelection);
97 SimpleAnimator sim = simulation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600.0, SECOND),
98 activeProperties, null, false);
99
100 SwingUtilities.invokeLater(new Runnable()
101 {
102 @Override
103 public void run()
104 {
105 simulation.getPanel().getTabbedPane().remove(0);
106 simulation.getPanel().getTabbedPane().remove(2);
107 }
108 });
109 sim.setSpeedFactor(1.0E9);
110
111 Thread.sleep(5000);
112
113 sim.scheduleEventRel(new Duration(59.999, MINUTE), this, this, "stop", new Object[] { System.currentTimeMillis() });
114 sim.start();
115 }
116 catch (SimRuntimeException | PropertyException | NamingException | OTSSimulationException
117 | InterruptedException exception)
118 {
119 exception.printStackTrace();
120 }
121 }
122
123
124
125
126
127 protected void stop(final long startTime)
128 {
129 System.out.println("Time of simulation was : " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
130 System.out.println("Aligned GTU moves = " + AbstractGTU.ALIGN_COUNT);
131 System.out.println("Cached positions = " + AbstractLaneBasedGTU.CACHED_POSITION);
132 System.out.println("Non-cached positions = " + AbstractLaneBasedGTU.NON_CACHED_POSITION);
133 System.exit(0);
134 }
135
136
137
138
139 public static void main(final String[] args)
140 {
141 AbstractGTU.ALIGNED = true;
142 AbstractLaneBasedGTU.CACHING = true;
143
144 new TimeCircularRoadSimulation();
145 }
146
147 }