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