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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * <p>
28   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
29   *          initial version 6 feb. 2015 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   */
33  public class SequentialFixedAccelerationModel extends AbstractGTUFollowingModelMobil implements Serializable
34  {
35      /** */
36      private static final long serialVersionUID = 20150206L;
37  
38      /** The list of result values of this SequentialFixedAccelerationModel. */
39      private final List<FixedAccelerationModel> steps = new ArrayList<>();
40  
41      /** The simulator engine. */
42      private final OTSSimulatorInterface simulator;
43  
44      /** The maximum safe deceleration. */
45      private final Acceleration maximumSafeDeceleration;
46  
47      /**
48       * Construct a SequentialFixedAccelerationModel with empty list of FixedAccelerationModel steps.
49       * @param simulator OTSSimulatorInterface; the simulator (needed to obtain the current simulation time)
50       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
51       */
52      public SequentialFixedAccelerationModel(final OTSSimulatorInterface simulator,
53              final Acceleration maximumSafeDeceleration)
54      {
55          this.simulator = simulator;
56          this.maximumSafeDeceleration = maximumSafeDeceleration;
57      }
58  
59      /**
60       * Construct a SequentialFixedAccelerationModel and load it with a list of FixedAccelerationModel steps.
61       * @param simulator OTSSimulatorInterface; the simulator (needed to obtain the current simulation time)
62       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
63       * @param steps Set&lt;FixedAccelerationModel&gt;; the list of FixedAccelerationModel steps.
64       */
65      public SequentialFixedAccelerationModel(final OTSSimulatorInterface simulator,
66              final Acceleration maximumSafeDeceleration, final Set<FixedAccelerationModel> steps)
67      {
68          this(simulator, maximumSafeDeceleration);
69          this.steps.addAll(steps);
70      }
71  
72      /**
73       * Add one FixedAccelerationModel step to this SequentialFixedAccelerationModel.
74       * @param step FixedAccelerationModel; the step to add
75       * @return SequentialFixedAccelerationModel; this modified SequentialFixedAccelerationModel
76       */
77      public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
78      {
79          this.steps.add(step);
80          return this;
81      }
82  
83      /**
84       * Retrieve the number of FixedAccelerationModel steps in this SequentialFixedAccelerationModel.
85       * @return int; the number of steps in this SequentialFixedAccelerationModel
86       */
87      public final int size()
88      {
89          return this.steps.size();
90      }
91  
92      /**
93       * Retrieve one FixedAccelerationModel step.
94       * @param index int; the index of the retrieved FixedAccelerationModel step
95       * @return FixedAccelerationModel
96       */
97      public final FixedAccelerationModel get(final int index)
98      {
99          return this.steps.get(index);
100     }
101 
102     /**
103      * Retrieve the simulation time at the end of the Nth step of this SequentialFixedAccelerationModel.
104      * @param index int; the step
105      * @return Time
106      */
107     public final Time timeAfterCompletionOfStep(final int index)
108     {
109         Time sum = new Time(0, TimeUnit.DEFAULT);
110         for (int i = 0; i <= index; i++)
111         {
112             sum = sum.plus(this.steps.get(i).getDuration());
113         }
114         return sum;
115     }
116 
117     /** Maximum error of the simulator clock. */
118     private static final double MAXIMUMTIMEERROR = 0.001; // 1 millisecond
119 
120     /**
121      * Find the FixedAccelerationModel that starts at the current simulator time.
122      * @return FixedAccelerationModel; the FixedAccelerationModel that starts at the current simulator time
123      */
124     private FixedAccelerationModel getAccelerationModel()
125     {
126         Time when = this.simulator.getSimulatorAbsTime();
127         double remainingTime = when.getSI();
128         for (FixedAccelerationModel step : this.steps)
129         {
130             if (remainingTime < -MAXIMUMTIMEERROR)
131             {
132                 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
133             }
134             if (remainingTime < MAXIMUMTIMEERROR)
135             {
136                 return step;
137             }
138             remainingTime -= step.getDuration().getSI();
139         }
140         throw new Error("FixedSequentialAcceleration does not have a result for " + when);
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
146             final Speed leaderSpeed, final Length headway, final Speed speedLimit)
147     {
148         return getAccelerationModel().getAcceleration();
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
154             final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
155     {
156         // TODO incorporate stepSize
157         return getAccelerationModel().getAcceleration();
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public final Acceleration getMaximumSafeDeceleration()
163     {
164         return this.maximumSafeDeceleration;
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public final Duration getStepSize()
170     {
171         return getAccelerationModel().getStepSize();
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public final String getName()
177     {
178         return "FSAM";
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public final String getLongName()
184     {
185         return "Fixed sequential acceleration model";
186     }
187 
188     /** {@inheritDoc} */
189     @Override
190     public final void setA(final Acceleration a)
191     {
192         //
193     }
194 
195     /** {@inheritDoc} */
196     @Override
197     public final void setT(final Duration t)
198     {
199         //
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     public final void setFspeed(final double fSpeed)
205     {
206         //
207     }
208 
209     // The following is inherited from CarFollowingModel
210 
211     /** {@inheritDoc} */
212     @Override
213     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
214     {
215         return null;
216     }
217 
218     /** {@inheritDoc} */
219     @Override
220     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
221     {
222         return null;
223     }
224 
225     /** {@inheritDoc} */
226     @Override
227     public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
228             final SpeedLimitInfo speedInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
229     {
230         return null;
231     }
232 
233     /** {@inheritDoc} */
234     @Override
235     public final String toString()
236     {
237         return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
238                 + this.maximumSafeDeceleration + "]";
239     }
240 
241 }