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://github.com/peter-knoppers">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 the simulator (needed to obtain the current simulation time)
48       * @param maximumSafeDeceleration 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 the simulator (needed to obtain the current simulation time)
59       * @param maximumSafeDeceleration specified maximum safe deceleration
60       * @param steps 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 the step to add
72       * @return 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 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 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 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 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     @Override
141     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
142             final Speed leaderSpeed, final Length headway, final Speed speedLimit)
143     {
144         return getAccelerationModel().getAcceleration();
145     }
146 
147     @Override
148     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
149             final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
150     {
151         // TODO incorporate stepSize
152         return getAccelerationModel().getAcceleration();
153     }
154 
155     @Override
156     public final Acceleration getMaximumSafeDeceleration()
157     {
158         return this.maximumSafeDeceleration;
159     }
160 
161     @Override
162     public final Duration getStepSize()
163     {
164         return getAccelerationModel().getStepSize();
165     }
166 
167     @Override
168     public final String getName()
169     {
170         return "FSAM";
171     }
172 
173     @Override
174     public final String getLongName()
175     {
176         return "Fixed sequential acceleration model";
177     }
178 
179     @Override
180     public final void setA(final Acceleration a)
181     {
182         //
183     }
184 
185     @Override
186     public final void setT(final Duration t)
187     {
188         //
189     }
190 
191     @Override
192     public final void setFspeed(final double fSpeed)
193     {
194         //
195     }
196 
197     // The following is inherited from CarFollowingModel
198 
199     @Override
200     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
201     {
202         return null;
203     }
204 
205     @Override
206     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
207     {
208         return null;
209     }
210 
211     @Override
212     public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
213             final SpeedLimitInfo speedInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
214     {
215         return null;
216     }
217 
218     @Override
219     public final String toString()
220     {
221         return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
222                 + this.maximumSafeDeceleration + "]";
223     }
224 
225 }