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