View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import org.djunits.unit.AccelerationUnit;
4   import org.djunits.unit.DurationUnit;
5   import org.djunits.unit.LengthUnit;
6   import org.djunits.value.vdouble.scalar.Acceleration;
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.opentrafficsim.base.parameters.ParameterException;
10  import org.opentrafficsim.base.parameters.ParameterTypes;
11  import org.opentrafficsim.base.parameters.Parameters;
12  import org.opentrafficsim.core.gtu.GTUType;
13  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterFactory;
14  
15  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterDouble;
16  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterDoubleScalar;
17  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
18  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
19  
20  /**
21   * InputParameterHelper.java. <br>
22   * <br>
23   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
24   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
25   * source code and binary code of this software is proprietary information of Delft University of Technology.
26   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
27   */
28  public final class InputParameterHelper implements ParameterFactory
29  {
30  
31      /** Input parameter map. */
32      private final InputParameterMap rootMap;
33  
34      /**
35       * Constructor.
36       * @param rootMap InputParameterMap; input parameter map
37       */
38      public InputParameterHelper(final InputParameterMap rootMap)
39      {
40          this.rootMap = rootMap;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public void setValues(final Parameters parameters, final GTUType gtuType) throws ParameterException
46      {
47          try
48          {
49              if (gtuType.isOfType(GTUType.CAR))
50              {
51                  getParametersCar(this.rootMap).setAllIn(parameters);
52              }
53              else if (gtuType.isOfType(GTUType.TRUCK))
54              {
55                  getParametersTruck(this.rootMap).setAllIn(parameters);
56              }
57              else
58              {
59                  throw new ParameterException("GTUType " + gtuType + " not supported in demo parameter factory.");
60              }
61          }
62          catch (InputParameterException exception)
63          {
64              throw new ParameterException(exception);
65          }
66      }
67  
68      /**
69       * Make a map of input parameters for a demo with a car/truck ratio and car/truck tabs with parameters.
70       * @param map InputParameterMap; the map to add the car/truck input parameters to
71       * @param probabilityDisplayPriority double; the display priority to use for the car probability in the generic map
72       */
73      public static void makeInputParameterMapCarTruck(final InputParameterMap map, final double probabilityDisplayPriority)
74      {
75          try
76          {
77              InputParameterMap genericMap;
78              if (map.getValue().containsKey("generic"))
79              {
80                  genericMap = (InputParameterMap) map.get("generic");
81              }
82              else
83              {
84                  genericMap = new InputParameterMap("generic", "Generic", "Generic parameters", 1.0);
85                  map.add(genericMap);
86              }
87              genericMap.add(new InputParameterDouble("carProbability", "Car probability",
88                      "Probability that the next generated GTU is a passenger car", 0.8, 0.0, 1.0, true, true, "%.00f",
89                      probabilityDisplayPriority));
90  
91              makeInputParameterMapCar(map, 2.0);
92  
93              makeInputParameterMapTruck(map, 3.0);
94  
95          }
96          catch (InputParameterException exception)
97          {
98              exception.printStackTrace();
99          }
100     }
101 
102     /**
103      * Make a map of input parameters for a demo with a car tabs with parameters.
104      * @param map InputParameterMap; the map to add the car input tab to
105      * @param displayPriority double; the display priority to use for the car tab in the generic map
106      */
107     public static void makeInputParameterMapCar(final InputParameterMap map, final double displayPriority)
108     {
109         try
110         {
111             InputParameterMap carMap;
112             if (map.getValue().containsKey("car"))
113             {
114                 carMap = (InputParameterMap) map.get("car");
115             }
116             else
117             {
118                 carMap = new InputParameterMap("car", "Car", "Car parameters", displayPriority);
119                 map.add(carMap);
120             }
121 
122             carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
123                     "Maximum acceleration (m/s2)", Acceleration.createSI(1.56), Acceleration.createSI(0.5),
124                     Acceleration.createSI(5.0), true, true, "%.0f", 1.0));
125             carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
126                     "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
127                     Acceleration.createSI(2.09), Acceleration.createSI(1.0), Acceleration.createSI(4.0), true, true, "%.0f",
128                     2.0));
129             carMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)", "Distance headway (m)",
130                     Length.createSI(3.0), Length.createSI(1.0), Length.createSI(10.0), true, true, "%.0f", 3.0));
131             carMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
132                     Duration.createSI(1.2), Duration.createSI(1.0), Duration.createSI(4.0), true, true, "%.0f", 4.0));
133         }
134         catch (InputParameterException exception)
135         {
136             exception.printStackTrace();
137         }
138     }
139 
140     /**
141      * Make a map of input parameters for a demo with a truck tabs with parameters.
142      * @param map InputParameterMap; the map to add the truck input tab to
143      * @param displayPriority double; the display priority to use for the truck map in the generic map
144      */
145     public static void makeInputParameterMapTruck(final InputParameterMap map, final double displayPriority)
146     {
147         try
148         {
149             InputParameterMap truckMap;
150             if (map.getValue().containsKey("truck"))
151             {
152                 truckMap = (InputParameterMap) map.get("truck");
153             }
154             else
155             {
156                 truckMap = new InputParameterMap("truck", "Truck", "Truck parameters", displayPriority);
157                 map.add(truckMap);
158             }
159 
160             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
161                     "Maximum acceleration (m/s2)", Acceleration.createSI(0.75), Acceleration.createSI(0.5),
162                     Acceleration.createSI(5.0), true, true, "%.0f", 1.0));
163             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
164                     "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
165                     Acceleration.createSI(1.25), Acceleration.createSI(1.0), Acceleration.createSI(4.0), true, true, "%.0f",
166                     2.0));
167             truckMap.add(
168                     new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)", "Distance headway (m)",
169                             Length.createSI(3.0), Length.createSI(1.0), Length.createSI(10.0), true, true, "%.0f", 3.0));
170             truckMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
171                     Duration.createSI(1.2), Duration.createSI(1.0), Duration.createSI(4.0), true, true, "%.0f", 4.0));
172         }
173         catch (InputParameterException exception)
174         {
175             exception.printStackTrace();
176         }
177     }
178 
179     /**
180      * Get the car parameters as entered.
181      * @param rootMap InputParameterMap; the root map of the model with a 'car' tab with the parameters
182      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
183      * @throws ParameterException when the parameter was given an illegal setting
184      * @throws InputParameterException when the input parameter could not be found
185      */
186     public static Parameters getParametersCar(final InputParameterMap rootMap)
187             throws ParameterException, InputParameterException
188     {
189         Parameters parametersCar = DefaultsFactory.getDefaultParameters();
190         Acceleration aCar = (Acceleration) rootMap.get("car.a").getCalculatedValue();
191         Acceleration bCar = (Acceleration) rootMap.get("car.b").getCalculatedValue();
192         Length s0Car = (Length) rootMap.get("car.s0").getCalculatedValue();
193         Duration tSafeCar = (Duration) rootMap.get("car.tSafe").getCalculatedValue();
194         parametersCar.setParameter(ParameterTypes.A, aCar);
195         parametersCar.setParameter(ParameterTypes.B, bCar);
196         parametersCar.setParameter(ParameterTypes.S0, s0Car);
197         parametersCar.setParameter(ParameterTypes.T, tSafeCar);
198         return parametersCar;
199     }
200 
201     /**
202      * Get the truck parameters as entered.
203      * @param rootMap InputParameterMap; the root map of the model with a 'truck' tab with the parameters
204      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
205      * @throws ParameterException when the parameter was given an illegal setting
206      * @throws InputParameterException when the input parameter could not be found
207      */
208     public static Parameters getParametersTruck(final InputParameterMap rootMap)
209             throws ParameterException, InputParameterException
210     {
211         Parameters parametersTruck = DefaultsFactory.getDefaultParameters();
212         Acceleration aTruck = (Acceleration) rootMap.get("truck.a").getCalculatedValue();
213         Acceleration bTruck = (Acceleration) rootMap.get("truck.b").getCalculatedValue();
214         Length s0Truck = (Length) rootMap.get("truck.s0").getCalculatedValue();
215         Duration tSafeTruck = (Duration) rootMap.get("truck.tSafe").getCalculatedValue();
216         parametersTruck.setParameter(ParameterTypes.A, aTruck);
217         parametersTruck.setParameter(ParameterTypes.B, bTruck);
218         parametersTruck.setParameter(ParameterTypes.S0, s0Truck);
219         parametersTruck.setParameter(ParameterTypes.T, tSafeTruck);
220         return parametersTruck;
221     }
222 
223 }