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.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   * Extended version of FixedAccelerationModel. The addition is that this GTUFollowingModel stores a series of acceleration and
24   * duration values. Mostly used for testing.
25   * <p>
26   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
28   * <p>
29   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
30   *          initial version 6 feb. 2015 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   */
34  public class SequentialFixedAccelerationModel extends AbstractGTUFollowingModelMobil implements Serializable
35  {
36      /** */
37      private static final long serialVersionUID = 20150206L;
38  
39      /** The list of result values of this SequentialFixedAccelerationModel. */
40      private final List<FixedAccelerationModel> steps = new ArrayList<>();
41  
42      /** The simulator engine. */
43      private final DEVSSimulatorInterface.TimeDoubleUnit simulator;
44  
45      /** The maximum safe deceleration. */
46      private final Acceleration maximumSafeDeceleration;
47  
48      /**
49       * Construct a SequentialFixedAccelerationModel with empty list of FixedAccelerationModel steps.
50       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator (needed to obtain the current simulation time)
51       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
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       * Construct a SequentialFixedAccelerationModel and load it with a list of FixedAccelerationModel steps.
62       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator (needed to obtain the current simulation time)
63       * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
64       * @param steps Set&lt;FixedAccelerationModel&gt;; the list of FixedAccelerationModel steps.
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       * Add one FixedAccelerationModel step to this SequentialFixedAccelerationModel.
75       * @param step FixedAccelerationModel; the step to add
76       * @return SequentialFixedAccelerationModel; this modified SequentialFixedAccelerationModel
77       */
78      public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
79      {
80          this.steps.add(step);
81          return this;
82      }
83  
84      /**
85       * Retrieve the number of FixedAccelerationModel steps in this SequentialFixedAccelerationModel.
86       * @return int; the number of steps in this SequentialFixedAccelerationModel
87       */
88      public final int size()
89      {
90          return this.steps.size();
91      }
92  
93      /**
94       * Retrieve one FixedAccelerationModel step.
95       * @param index int; the index of the retrieved FixedAccelerationModel step
96       * @return FixedAccelerationModel
97       */
98      public final FixedAccelerationModel get(final int index)
99      {
100         return this.steps.get(index);
101     }
102 
103     /**
104      * Retrieve the simulation time at the end of the Nth step of this SequentialFixedAccelerationModel.
105      * @param index int; the step
106      * @return Time
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     /** Maximum error of the simulator clock. */
119     private static final double MAXIMUMTIMEERROR = 0.001; // 1 millisecond
120 
121     /**
122      * Find the FixedAccelerationModel that starts at the current simulator time.
123      * @return FixedAccelerationModel; the FixedAccelerationModel that starts at the current simulator time
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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         // TODO incorporate stepSize
158         return getAccelerationModel().getAcceleration();
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final Acceleration getMaximumSafeDeceleration()
164     {
165         return this.maximumSafeDeceleration;
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     public final Duration getStepSize()
171     {
172         return getAccelerationModel().getStepSize();
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public final String getName()
178     {
179         return "FSAM";
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public final String getLongName()
185     {
186         return "Fixed sequential acceleration model";
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public final void setA(final Acceleration a)
192     {
193         //
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public final void setT(final Duration t)
199     {
200         //
201     }
202 
203     /** {@inheritDoc} */
204     @Override
205     public final void setFspeed(final double fSpeed)
206     {
207         //
208     }
209 
210     // The following is inherited from CarFollowingModel
211 
212     /** {@inheritDoc} */
213     @Override
214     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
215     {
216         return null;
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
222     {
223         return null;
224     }
225 
226     /** {@inheritDoc} */
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     /** {@inheritDoc} */
235     @Override
236     public final String toString()
237     {
238         return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
239                 + this.maximumSafeDeceleration + "]";
240     }
241 
242 }