View Javadoc
1   package org.opentrafficsim.demo.timing;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.naming.NamingException;
7   
8   import org.djunits.unit.UNITS;
9   import org.djunits.value.vdouble.scalar.Acceleration;
10  import org.djunits.value.vdouble.scalar.Duration;
11  import org.djunits.value.vdouble.scalar.Length;
12  import org.djunits.value.vdouble.scalar.Time;
13  import org.opentrafficsim.base.modelproperties.CompoundProperty;
14  import org.opentrafficsim.base.modelproperties.ProbabilityDistributionProperty;
15  import org.opentrafficsim.base.modelproperties.Property;
16  import org.opentrafficsim.base.modelproperties.PropertyException;
17  import org.opentrafficsim.base.modelproperties.SelectionProperty;
18  import org.opentrafficsim.core.gtu.AbstractGTU;
19  import org.opentrafficsim.core.network.NetworkException;
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.OTSSimulationException;
24  import org.opentrafficsim.simulationengine.SimpleAnimator;
25  import org.opentrafficsim.simulationengine.WrappableAnimation;
26  
27  import nl.tudelft.simulation.dsol.SimRuntimeException;
28  
29  /**
30   * <p>
31   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
33   * </p>
34   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
35   * initial version Apr 14, 2017 <br>
36   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
37   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
38   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
39   */
40  public class TimeCircularRoadAnimation implements UNITS
41  {
42      /** */
43      public TimeCircularRoadAnimation()
44      {
45          try
46          {
47              WrappableAnimation simulation = new CircularRoad();
48              List<Property<?>> activeProperties = new ArrayList<>();
49              activeProperties.addAll(simulation.getProperties());
50              for (Property<?> ap : activeProperties)
51              {
52                  if (ap instanceof SelectionProperty)
53                  {
54                      SelectionProperty sp = (SelectionProperty) ap;
55                      if ("TacticalPlanner".equals(sp.getKey()))
56                      {
57                          sp.setValue("DIRECTED/IDM");
58                      }
59                  }
60              }
61              activeProperties.add(new ProbabilityDistributionProperty("TrafficComposition", "Traffic composition",
62                      "<html>Mix of passenger cars and trucks</html>", new String[] { "passenger car", "truck" },
63                      new Double[] { 0.8, 0.2 }, false, 5));
64              CompoundProperty modelSelection =
65                      new CompoundProperty("ModelSelection", "Model selection", "Modeling specific settings", null, false, 300);
66              modelSelection.add(new SelectionProperty("SimulationScale", "Simulation scale", "Level of detail of the simulation",
67                      new String[] { "Micro", "Macro", "Meta" }, 0, true, 0));
68              modelSelection.add(new SelectionProperty("CarFollowingModel", "Car following model", "",
69                      new String[] { "IDM", "IDM+" }, 1, false, 1));
70              modelSelection.add(IDMPropertySet.makeIDMPropertySet("IDMCar", "Car", new Acceleration(1.56, METER_PER_SECOND_2),
71                      new Acceleration(2.09, METER_PER_SECOND_2), new Length(3.0, METER), new Duration(1.2, SECOND), 2));
72              modelSelection
73                      .add(IDMPropertySet.makeIDMPropertySet("IDMTruck", "Truck", new Acceleration(0.75, METER_PER_SECOND_2),
74                              new Acceleration(1.25, METER_PER_SECOND_2), new Length(3.0, METER), new Duration(1.2, SECOND), 3));
75              activeProperties.add(modelSelection);
76              SimpleAnimator sim = (SimpleAnimator) simulation.buildAnimator(Time.ZERO, Duration.ZERO,
77                      new Duration(3600.0, SECOND), activeProperties, null, false);
78              
79              sim.setSpeedFactor(1.0E9);
80    
81              Thread.sleep(5000);
82              
83              sim.scheduleEventRel(new Duration(59.999, MINUTE), this, this, "stop", new Object[] { System.currentTimeMillis() });
84              sim.start();
85          }
86          catch (SimRuntimeException | PropertyException | NetworkException | NamingException | OTSSimulationException
87                  | InterruptedException exception)
88          {
89              exception.printStackTrace();
90          }
91      }
92  
93      /**
94       * Timer.
95       * @param startTime start time of simulation
96       */
97      protected void stop(final long startTime)
98      {
99          System.out.println("Time of animation was : " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
100         System.out.println("Aligned GTU moves     = " + AbstractGTU.ALIGN_COUNT);
101         System.out.println("Cached positions      = " + AbstractLaneBasedGTU.CACHED_POSITION);
102         System.out.println("Non-cached positions  = " + AbstractLaneBasedGTU.NON_CACHED_POSITION);
103         System.exit(0);
104     }
105 
106     /**
107      * @param args args should be empty
108      */
109     public static void main(final String[] args)
110     {
111         AbstractGTU.ALIGNED = true;
112         AbstractLaneBasedGTU.CACHING = true;
113 
114         new TimeCircularRoadAnimation();
115     }
116 
117 }