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