View Javadoc
1   package org.opentrafficsim.core.gtu.behavioralcharacteristics;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.Map;
7   import java.util.Set;
8   
9   import org.djunits.unit.Unit;
10  import org.djunits.value.vdouble.scalar.AbstractDoubleScalarRel;
11  import org.djunits.value.vdouble.scalar.DoubleScalarInterface;
12  import org.opentrafficsim.core.gtu.GTUType;
13  
14  import nl.tudelft.simulation.jstats.distributions.DistNormal;
15  import nl.tudelft.simulation.jstats.streams.StreamInterface;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 18 nov. 2016 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
26   */
27  public class BehavioralCharacteristicsFactoryByType implements BehavioralCharacteristicsFactory
28  {
29  
30      /** Parameters. */
31      private final Map<GTUType, Set<ParameterEntry>> map = new HashMap<>();
32  
33      /** {@inheritDoc} */
34      @Override
35      public void setValues(final BehavioralCharacteristics defaultCharacteristics, final GTUType gtuType)
36      {
37          if (this.map.containsKey(gtuType))
38          {
39              for (ParameterEntry entry : this.map.get(gtuType))
40              {
41                  entry.setValue(defaultCharacteristics);
42              }
43          }
44      }
45  
46      /**
47       * @param gtuType the gtu type
48       * @param parameterType the parameter type
49       * @param value the value of the parameter
50       * @param <T> type
51       */
52      public <T extends DoubleScalarInterface> void addParameter(final GTUType gtuType, final ParameterType<T> parameterType,
53              final T value)
54      {
55          assureTypeInMap(gtuType);
56          this.map.get(gtuType).add(new FixedEntry<>(parameterType, value));
57      }
58  
59      /**
60       * @param gtuType the gtu type
61       * @param parameterType the parameter type
62       * @param value the value of the parameter
63       */
64      public void addParameter(final GTUType gtuType, final ParameterTypeDouble parameterType, final double value)
65      {
66          assureTypeInMap(gtuType);
67          this.map.get(gtuType).add(new FixedEntryDouble(parameterType, value));
68      }
69  
70      /**
71       * @param gtuType the gtu type
72       * @param parameterType the parameter type
73       * @param mu mean
74       * @param sig standard deviation
75       * @param stream random number stream
76       * @param <U> unit
77       * @param <T> type
78       */
79      public <U extends Unit<U>, T extends AbstractDoubleScalarRel<U, T>> void addGaussianParameter(final GTUType gtuType,
80              final ParameterType<T> parameterType, final T mu, final T sig, final StreamInterface stream)
81      {
82          assureTypeInMap(gtuType);
83          this.map.get(gtuType).add(new GaussianEntry<>(parameterType, mu, sig, stream));
84      }
85  
86      /**
87       * @param gtuType the gtu type
88       * @param parameterType the parameter type
89       * @param mu mean
90       * @param sig standard deviation
91       * @param stream random number stream
92       */
93      public void addGaussianParameter(final GTUType gtuType, final ParameterTypeDouble parameterType, final double mu,
94              final double sig, final StreamInterface stream)
95      {
96          assureTypeInMap(gtuType);
97          this.map.get(gtuType).add(new GaussianDoubleEntry(parameterType, mu, sig, stream));
98      }
99  
100     /**
101      * Assures the gtu type is in the map.
102      * @param gtuType the gtu type
103      */
104     private void assureTypeInMap(final GTUType gtuType)
105     {
106         if (!this.map.containsKey(gtuType))
107         {
108             this.map.put(gtuType, new HashSet<>());
109         }
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public String toString()
115     {
116         return "BehavioralCharacteristicsFactoryByType [map=" + this.map + "]";
117     }
118 
119     /**
120      * <p>
121      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
122      * <br>
123      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
124      * <p>
125      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 nov. 2016 <br>
126      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
127      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
128      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
129      */
130     private interface ParameterEntry
131     {
132         /**
133          * Set value for parameter.
134          * @param behavioralCharacteristics the behavioral characteristics
135          */
136         void setValue(BehavioralCharacteristics behavioralCharacteristics);
137     }
138 
139     /**
140      * <p>
141      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
142      * <br>
143      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
144      * <p>
145      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 nov. 2016 <br>
146      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
147      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
148      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
149      * @param <U> unit
150      * @param <T> value type
151      */
152     private static class GaussianEntry<U extends Unit<U>, T extends AbstractDoubleScalarRel<U, T>>
153             implements ParameterEntry, Serializable
154     {
155         /** */
156         private static final long serialVersionUID = 20170400L;
157 
158         /** Parameter type. */
159         private final ParameterType<T> parameterType;
160 
161         /** Mean value. */
162         private final T mu;
163 
164         /** Random number generator. */
165         private final DistNormal dist;
166 
167         /**
168          * @param parameterType the parameter type
169          * @param mu mean
170          * @param sig standard deviation
171          * @param stream random number stream
172          */
173         GaussianEntry(final ParameterType<T> parameterType, final T mu, final T sig, final StreamInterface stream)
174         {
175             this.parameterType = parameterType;
176             this.mu = mu;
177             this.dist = new DistNormal(stream, mu.si, sig.si);
178         }
179 
180         /** {@inheritDoc} */
181         @Override
182         public void setValue(final BehavioralCharacteristics behavioralCharacteristics)
183         {
184             T val = this.mu.instantiateRel(this.dist.draw(), this.mu.getUnit().getStandardUnit());
185             try
186             {
187                 behavioralCharacteristics.setParameter(this.parameterType, val);
188             }
189             catch (ParameterException exception)
190             {
191                 throw new RuntimeException(
192                         "Trying to set value " + val + " for parameter " + this.parameterType + ", which is out of range.",
193                         exception);
194             }
195         }
196 
197         /** {@inheritDoc} */
198         @Override
199         public String toString()
200         {
201             return "GaussianEntry [parameterType=" + this.parameterType + ", mu=" + this.mu + ", dist=" + this.dist + "]";
202         }
203 
204     }
205 
206     /**
207      * <p>
208      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
209      * <br>
210      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
211      * <p>
212      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 nov. 2016 <br>
213      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
214      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
215      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
216      */
217     private static class GaussianDoubleEntry implements ParameterEntry, Serializable
218     {
219         /** */
220         private static final long serialVersionUID = 20170400L;
221 
222         /** Parameter type. */
223         private final ParameterTypeDouble parameterType;
224 
225         /** Random number generator. */
226         private final DistNormal dist;
227 
228         /**
229          * @param parameterType the parameter type
230          * @param mu mean
231          * @param sig standard deviation
232          * @param stream random number stream
233          */
234         GaussianDoubleEntry(final ParameterTypeDouble parameterType, final double mu, final double sig,
235                 final StreamInterface stream)
236         {
237             this.parameterType = parameterType;
238             this.dist = new DistNormal(stream, mu, sig);
239         }
240 
241         /** {@inheritDoc} */
242         @Override
243         public void setValue(final BehavioralCharacteristics behavioralCharacteristics)
244         {
245             double val = this.dist.draw();
246             try
247             {
248                 behavioralCharacteristics.setParameter(this.parameterType, val);
249             }
250             catch (ParameterException exception)
251             {
252                 throw new RuntimeException(
253                         "Trying to set value " + val + " for parameter " + this.parameterType + ", which is out of range.",
254                         exception);
255             }
256         }
257 
258         /** {@inheritDoc} */
259         @Override
260         public String toString()
261         {
262             return "GaussianDoubleEntry [parameterType=" + this.parameterType + ", dist=" + this.dist + "]";
263         }
264 
265     }
266 
267     /**
268      * <p>
269      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
270      * <br>
271      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
272      * <p>
273      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 nov. 2016 <br>
274      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
275      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
276      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
277      * @param <T> value type
278      */
279     private final class FixedEntry<T extends DoubleScalarInterface> implements ParameterEntry, Serializable
280     {
281         /** */
282         private static final long serialVersionUID = 20170400L;
283 
284         /** Parameter type. */
285         private final ParameterType<T> parameterType;
286 
287         /** Value. */
288         private final T value;
289 
290         /**
291          * @param parameterType the parameter type
292          * @param value the fixed value
293          */
294         FixedEntry(final ParameterType<T> parameterType, final T value)
295         {
296             this.parameterType = parameterType;
297             this.value = value;
298         }
299 
300         /** {@inheritDoc} */
301         @Override
302         public void setValue(final BehavioralCharacteristics behavioralCharacteristics)
303         {
304             try
305             {
306                 behavioralCharacteristics.setParameter(this.parameterType, this.value);
307             }
308             catch (ParameterException exception)
309             {
310                 throw new RuntimeException("Trying to set value " + this.value + " for parameter " + this.parameterType
311                         + ", which is out of range.", exception);
312             }
313         }
314 
315         /** {@inheritDoc} */
316         @Override
317         public String toString()
318         {
319             return "FixedEntry [parameterType=" + this.parameterType + ", value=" + this.value + "]";
320         }
321 
322     }
323 
324     /**
325      * <p>
326      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
327      * <br>
328      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
329      * <p>
330      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 nov. 2016 <br>
331      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
332      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
333      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
334      */
335     private final class FixedEntryDouble implements ParameterEntry, Serializable
336     {
337         /** */
338         private static final long serialVersionUID = 20170400L;
339 
340         /** Parameter type. */
341         private final ParameterTypeDouble parameterType;
342 
343         /** Value. */
344         private final double value;
345 
346         /**
347          * @param parameterType the parameter type
348          * @param value the fixed value
349          */
350         FixedEntryDouble(final ParameterTypeDouble parameterType, final double value)
351         {
352             this.parameterType = parameterType;
353             this.value = value;
354         }
355 
356         /** {@inheritDoc} */
357         @Override
358         public void setValue(final BehavioralCharacteristics behavioralCharacteristics)
359         {
360             try
361             {
362                 behavioralCharacteristics.setParameter(this.parameterType, this.value);
363             }
364             catch (ParameterException exception)
365             {
366                 throw new RuntimeException("Trying to set value " + this.value + " for parameter " + this.parameterType
367                         + ", which is out of range.", exception);
368             }
369         }
370 
371         /** {@inheritDoc} */
372         @Override
373         public String toString()
374         {
375             return "FixedEntryDouble [parameterType=" + this.parameterType + ", value=" + this.value + "]";
376         }
377 
378     }
379 
380 }