View Javadoc
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   * Extended version of FixedAccelerationModel. The addition is that this GtuFollowingModel stores a series of acceleration and
23   * duration values. Mostly used for testing.
24   * <p>
25   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
30   */
31  public class SequentialFixedAccelerationModel extends AbstractGtuFollowingModelMobil implements Serializable
32  {
33      /** */
34      private static final long serialVersionUID = 20150206L;
35  
36      /** The list of result values of this SequentialFixedAccelerationModel. */
37      private final List<FixedAccelerationModel> steps = new ArrayList<>();
38  
39      /** The simulator engine. */
40      private final OtsSimulatorInterface simulator;
41  
42      /** The maximum safe deceleration. */
43      private final Acceleration maximumSafeDeceleration;
44  
45      /**
46       * Construct a SequentialFixedAccelerationModel with empty list of FixedAccelerationModel steps.
47       * @param simulator OtsSimulatorInterface; the simulator (needed to obtain the current simulation time)
48       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
49       */
50      public SequentialFixedAccelerationModel(final OtsSimulatorInterface simulator, final Acceleration maximumSafeDeceleration)
51      {
52          this.simulator = simulator;
53          this.maximumSafeDeceleration = maximumSafeDeceleration;
54      }
55  
56      /**
57       * Construct a SequentialFixedAccelerationModel and load it with a list of FixedAccelerationModel steps.
58       * @param simulator OtsSimulatorInterface; the simulator (needed to obtain the current simulation time)
59       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
60       * @param steps Set&lt;FixedAccelerationModel&gt;; the list of FixedAccelerationModel steps.
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       * Add one FixedAccelerationModel step to this SequentialFixedAccelerationModel.
71       * @param step FixedAccelerationModel; the step to add
72       * @return SequentialFixedAccelerationModel; this modified SequentialFixedAccelerationModel
73       */
74      public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
75      {
76          this.steps.add(step);
77          return this;
78      }
79  
80      /**
81       * Retrieve the number of FixedAccelerationModel steps in this SequentialFixedAccelerationModel.
82       * @return int; the number of steps in this SequentialFixedAccelerationModel
83       */
84      public final int size()
85      {
86          return this.steps.size();
87      }
88  
89      /**
90       * Retrieve one FixedAccelerationModel step.
91       * @param index int; the index of the retrieved FixedAccelerationModel step
92       * @return FixedAccelerationModel
93       */
94      public final FixedAccelerationModel get(final int index)
95      {
96          return this.steps.get(index);
97      }
98  
99      /**
100      * Retrieve the simulation time at the end of the Nth step of this SequentialFixedAccelerationModel.
101      * @param index int; the step
102      * @return Time
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     /** Maximum error of the simulator clock. */
115     private static final double MAXIMUMTIMEERROR = 0.001; // 1 millisecond
116 
117     /**
118      * Find the FixedAccelerationModel that starts at the current simulator time.
119      * @return FixedAccelerationModel; the FixedAccelerationModel that starts at the current simulator time
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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         // TODO incorporate stepSize
154         return getAccelerationModel().getAcceleration();
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public final Acceleration getMaximumSafeDeceleration()
160     {
161         return this.maximumSafeDeceleration;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public final Duration getStepSize()
167     {
168         return getAccelerationModel().getStepSize();
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     public final String getName()
174     {
175         return "FSAM";
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public final String getLongName()
181     {
182         return "Fixed sequential acceleration model";
183     }
184 
185     /** {@inheritDoc} */
186     @Override
187     public final void setA(final Acceleration a)
188     {
189         //
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public final void setT(final Duration t)
195     {
196         //
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public final void setFspeed(final double fSpeed)
202     {
203         //
204     }
205 
206     // The following is inherited from CarFollowingModel
207 
208     /** {@inheritDoc} */
209     @Override
210     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
211     {
212         return null;
213     }
214 
215     /** {@inheritDoc} */
216     @Override
217     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
218     {
219         return null;
220     }
221 
222     /** {@inheritDoc} */
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     /** {@inheritDoc} */
231     @Override
232     public final String toString()
233     {
234         return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
235                 + this.maximumSafeDeceleration + "]";
236     }
237 
238 }