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