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