1 package org.opentrafficsim.road.gtu.lane.tactical.following;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.SortedMap;
8
9 import org.djunits.unit.TimeUnit;
10 import org.djunits.value.vdouble.scalar.Acceleration;
11 import org.djunits.value.vdouble.scalar.Duration;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.djunits.value.vdouble.scalar.Speed;
14 import org.djunits.value.vdouble.scalar.Time;
15 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
16 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
17 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
18 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class SequentialFixedAccelerationModel extends AbstractGTUFollowingModelMobil implements Serializable
33 {
34
35 private static final long serialVersionUID = 20150206L;
36
37
38 private final List<FixedAccelerationModel> steps = new ArrayList<FixedAccelerationModel>();
39
40
41 private final OTSDEVSSimulatorInterface simulator;
42
43
44 private final Acceleration maximumSafeDeceleration;
45
46
47
48
49
50
51 public SequentialFixedAccelerationModel(final OTSDEVSSimulatorInterface simulator,
52 final Acceleration maximumSafeDeceleration)
53 {
54 this.simulator = simulator;
55 this.maximumSafeDeceleration = maximumSafeDeceleration;
56 }
57
58
59
60
61
62
63
64 public SequentialFixedAccelerationModel(final OTSDEVSSimulatorInterface simulator,
65 final Acceleration maximumSafeDeceleration, final Set<FixedAccelerationModel> steps)
66 {
67 this(simulator, maximumSafeDeceleration);
68 this.steps.addAll(steps);
69 }
70
71
72
73
74
75
76 public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
77 {
78 this.steps.add(step);
79 return this;
80 }
81
82
83
84
85
86 public final int size()
87 {
88 return this.steps.size();
89 }
90
91
92
93
94
95
96 public final FixedAccelerationModel get(final int index)
97 {
98 return this.steps.get(index);
99 }
100
101
102
103
104
105
106 public final Time timeAfterCompletionOfStep(final int index)
107 {
108 Time sum = new Time(0, TimeUnit.SI);
109 for (int i = 0; i <= index; i++)
110 {
111 sum = sum.plus(this.steps.get(i).getDuration());
112 }
113 return sum;
114 }
115
116
117 private static final double MAXIMUMTIMEERROR = 0.001;
118
119
120
121
122
123 private FixedAccelerationModel getAccelerationModel()
124 {
125 Time when = this.simulator.getSimulatorTime().getTime();
126 double remainingTime = when.getSI();
127 for (FixedAccelerationModel step : this.steps)
128 {
129 if (remainingTime < -MAXIMUMTIMEERROR)
130 {
131 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
132 }
133 if (remainingTime < MAXIMUMTIMEERROR)
134 {
135 return step;
136 }
137 remainingTime -= step.getDuration().getSI();
138 }
139 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
140 }
141
142
143 @Override
144 public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
145 final Speed leaderSpeed, final Length headway, final Speed speedLimit)
146 {
147 return getAccelerationModel().getAcceleration();
148 }
149
150
151 @Override
152 public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
153 final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
154 {
155
156 return getAccelerationModel().getAcceleration();
157 }
158
159
160 @Override
161 public final Acceleration getMaximumSafeDeceleration()
162 {
163 return this.maximumSafeDeceleration;
164 }
165
166
167 @Override
168 public final Duration getStepSize()
169 {
170 return getAccelerationModel().getStepSize();
171 }
172
173
174 @Override
175 public final String getName()
176 {
177 return "FSAM";
178 }
179
180
181 @Override
182 public final String getLongName()
183 {
184 return "Fixed sequential acceleration model";
185 }
186
187
188
189
190 @Override
191 public final Speed desiredSpeed(final BehavioralCharacteristics behavioralCharacteristics, final SpeedLimitInfo speedInfo)
192 throws ParameterException
193 {
194 return null;
195 }
196
197
198 @Override
199 public final Length desiredHeadway(final BehavioralCharacteristics behavioralCharacteristics, final Speed speed)
200 throws ParameterException
201 {
202 return null;
203 }
204
205
206 @Override
207 public final Acceleration followingAcceleration(final BehavioralCharacteristics behavioralCharacteristics,
208 final Speed speed, final SpeedLimitInfo speedInfo, final SortedMap<Length, Speed> leaders) throws ParameterException
209 {
210 return null;
211 }
212
213
214 @Override
215 public final String toString()
216 {
217 return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
218 + this.maximumSafeDeceleration + "]";
219 }
220
221 }