1 package org.opentrafficsim.road.gtu.lane.perception.mental;
2
3 import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVE;
4 import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVEZERO;
5
6 import java.util.Set;
7
8 import org.djutils.exceptions.Throw;
9 import org.djutils.exceptions.Try;
10 import org.djutils.immutablecollections.Immutable;
11 import org.djutils.immutablecollections.ImmutableLinkedHashSet;
12 import org.djutils.immutablecollections.ImmutableSet;
13 import org.opentrafficsim.base.Identifiable;
14 import org.opentrafficsim.base.parameters.ParameterException;
15 import org.opentrafficsim.base.parameters.ParameterTypeDouble;
16 import org.opentrafficsim.base.parameters.Parameters;
17 import org.opentrafficsim.core.gtu.GTUException;
18 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
19 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
20
21 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class Fuller implements Mental
46 {
47
48
49
50
51 public static final ParameterTypeDouble TC = new ParameterTypeDouble("TC", "Task capability", 1.0, POSITIVE);
52
53
54 public static final ParameterTypeDouble TS_CRIT =
55 new ParameterTypeDouble("TScrit", "Critical task saturation", 0.8, POSITIVEZERO)
56 {
57
58 private static final long serialVersionUID = 20180403L;
59
60
61 @Override
62 public void check(final Double value, final Parameters params) throws ParameterException
63 {
64 Double tsMax = params.getParameterOrNull(TS_MAX);
65 Throw.when(tsMax != null && value > tsMax, ParameterException.class,
66 "Value for TS_CRIT should not be larger than TS_MAX.");
67 }
68 };
69
70
71 public static final ParameterTypeDouble TS_MAX =
72 new ParameterTypeDouble("TSmax", "Maximum task saturation", 2.0, POSITIVEZERO)
73 {
74
75 private static final long serialVersionUID = 20180403L;
76
77
78 @Override
79 public void check(final Double value, final Parameters params) throws ParameterException
80 {
81 Double tsCrit = params.getParameterOrNull(TS_CRIT);
82 Throw.when(tsCrit != null && value < tsCrit, ParameterException.class,
83 "Value for TS_MAX should not be smaller than TS_CRIT.");
84 }
85 };
86
87
88 public static final ParameterTypeDouble TS = new ParameterTypeDouble("TS", "Task saturation", 0.0, POSITIVEZERO);
89
90
91
92
93 private final Set<Task> tasks;
94
95
96 private final Set<BehavioralAdaptation> behavioralAdapatations;
97
98
99
100
101
102
103 public Fuller(final Set<Task> tasks, final Set<BehavioralAdaptation> behavioralAdapatations)
104 {
105 Throw.whenNull(tasks, "Tasks may not be null.");
106 Throw.whenNull(behavioralAdapatations, "Behavioral adaptations may not be null.");
107 this.tasks = tasks;
108 this.behavioralAdapatations = behavioralAdapatations;
109 }
110
111
112
113
114
115 public void addTask(final Task task)
116 {
117 this.tasks.add(task);
118 }
119
120
121
122
123
124 public void removeTask(final Task task)
125 {
126 this.tasks.remove(task);
127 }
128
129
130
131
132
133 public ImmutableSet<Task> getTasks()
134 {
135 return new ImmutableLinkedHashSet<>(this.tasks, Immutable.WRAP);
136 }
137
138
139 @Override
140 public void apply(final LanePerception perception) throws ParameterException, GTUException
141 {
142 LaneBasedGTU gtu = Try.assign(() -> perception.getGtu(), "Could not obtain GTU.");
143 Parameters parameters = gtu.getParameters();
144 double taskDemand = 0.0;
145
146
147 for (Task task : this.tasks)
148 {
149 taskDemand += task.demand(perception, gtu, parameters);
150 }
151 double taskSaturation = taskDemand / parameters.getParameter(TC);
152 parameters.setParameter(TS, taskSaturation);
153
154 for (BehavioralAdaptation behavioralAdapatation : this.behavioralAdapatations)
155 {
156 behavioralAdapatation.adapt(parameters, taskSaturation);
157 }
158
159
160
161 }
162
163
164 @Override
165 public String toString()
166 {
167 return "Fuller [tasks=" + this.tasks + ", behavioralAdapatations=" + this.behavioralAdapatations + "]";
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 @FunctionalInterface
183 public interface Task
184 {
185
186
187
188
189
190
191
192
193
194 double demand(LanePerception perception, LaneBasedGTU gtu, Parameters parameters)
195 throws ParameterException, GTUException;
196
197
198
199
200 class Constant extends IdentifiableTask
201 {
202
203 private final double taskDemand;
204
205
206
207
208
209
210 public Constant(final String id, final double taskDemand)
211 {
212 super(id);
213 this.taskDemand = taskDemand;
214 }
215
216
217 @Override
218 public double demand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
219 throws ParameterException, GTUException
220 {
221 return this.taskDemand;
222 }
223
224 }
225
226
227
228
229 class Exponential extends IdentifiableTask
230 {
231
232 private final double initialTaskDemand;
233
234
235 private final double additionalTaskDemand;
236
237
238 private final double tau;
239
240
241 private final double start;
242
243
244
245
246
247
248
249
250
251 public Exponential(final String id, final double initialTaskDemand, final double finalTaskDemand, final double tau,
252 final SimulatorInterface.TimeDoubleUnit simulator)
253 {
254 super(id);
255 this.initialTaskDemand = initialTaskDemand;
256 this.additionalTaskDemand = finalTaskDemand - initialTaskDemand;
257 this.tau = tau;
258 this.start = simulator.getSimulatorTime().si;
259 }
260
261
262 @Override
263 public double demand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
264 throws ParameterException, GTUException
265 {
266 double t = gtu.getSimulator().getSimulatorTime().si - this.start;
267 return this.initialTaskDemand + this.additionalTaskDemand * (1.0 - Math.exp(-t / this.tau));
268 }
269
270 }
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public abstract static class IdentifiableTask implements Task, Identifiable
286 {
287
288 private final String id;
289
290
291
292
293
294 public IdentifiableTask(final String id)
295 {
296 this.id = id;
297 }
298
299
300 @Override
301 public String getId()
302 {
303 return this.id;
304 }
305
306
307 @Override
308 public String toString()
309 {
310 return "IdentifiableTask [id=" + getId() + "]";
311 }
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325
326 @FunctionalInterface
327 public interface BehavioralAdaptation
328 {
329
330
331
332
333
334
335 void adapt(Parameters parameters, double taskSaturation) throws ParameterException;
336 }
337
338 }