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