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
19
20
21
22
23
24
25
26
27 public class BehavioralCharacteristicsFactoryByType implements BehavioralCharacteristicsFactory
28 {
29
30
31 private final Map<GTUType, Set<ParameterEntry>> map = new HashMap<>();
32
33
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
48
49
50
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
61
62
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
72
73
74
75
76
77
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
88
89
90
91
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
102
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
113 @Override
114 public String toString()
115 {
116 return "BehavioralCharacteristicsFactoryByType [map=" + this.map + "]";
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 private interface ParameterEntry
131 {
132
133
134
135
136 void setValue(BehavioralCharacteristics behavioralCharacteristics);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
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
159 private final ParameterType<T> parameterType;
160
161
162 private final T mu;
163
164
165 private final DistNormal dist;
166
167
168
169
170
171
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
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
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
208
209
210
211
212
213
214
215
216
217 private static class GaussianDoubleEntry implements ParameterEntry, Serializable
218 {
219
220 private static final long serialVersionUID = 20170400L;
221
222
223 private final ParameterTypeDouble parameterType;
224
225
226 private final DistNormal dist;
227
228
229
230
231
232
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
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
259 @Override
260 public String toString()
261 {
262 return "GaussianDoubleEntry [parameterType=" + this.parameterType + ", dist=" + this.dist + "]";
263 }
264
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279 private final class FixedEntry<T extends DoubleScalarInterface> implements ParameterEntry, Serializable
280 {
281
282 private static final long serialVersionUID = 20170400L;
283
284
285 private final ParameterType<T> parameterType;
286
287
288 private final T value;
289
290
291
292
293
294 FixedEntry(final ParameterType<T> parameterType, final T value)
295 {
296 this.parameterType = parameterType;
297 this.value = value;
298 }
299
300
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
316 @Override
317 public String toString()
318 {
319 return "FixedEntry [parameterType=" + this.parameterType + ", value=" + this.value + "]";
320 }
321
322 }
323
324
325
326
327
328
329
330
331
332
333
334
335 private final class FixedEntryDouble implements ParameterEntry, Serializable
336 {
337
338 private static final long serialVersionUID = 20170400L;
339
340
341 private final ParameterTypeDouble parameterType;
342
343
344 private final double value;
345
346
347
348
349
350 FixedEntryDouble(final ParameterTypeDouble parameterType, final double value)
351 {
352 this.parameterType = parameterType;
353 this.value = value;
354 }
355
356
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
372 @Override
373 public String toString()
374 {
375 return "FixedEntryDouble [parameterType=" + this.parameterType + ", value=" + this.value + "]";
376 }
377
378 }
379
380 }