SequentialFixedAccelerationModel.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.following;

  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Set;

  6. import org.djunits.unit.TimeUnit;
  7. import org.djunits.value.vdouble.scalar.Acceleration;
  8. import org.djunits.value.vdouble.scalar.Duration;
  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.base.parameters.ParameterException;
  13. import org.opentrafficsim.base.parameters.Parameters;
  14. import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
  15. import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
  16. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
  17. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;

  18. /**
  19.  * Extended version of FixedAccelerationModel. The addition is that this GtuFollowingModel stores a series of acceleration and
  20.  * duration values. Mostly used for testing.
  21.  * <p>
  22.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  23.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  24.  * </p>
  25.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  26.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  27.  */
  28. public class SequentialFixedAccelerationModel extends AbstractGtuFollowingModelMobil implements Serializable
  29. {
  30.     /** */
  31.     private static final long serialVersionUID = 20150206L;

  32.     /** The list of result values of this SequentialFixedAccelerationModel. */
  33.     private final List<FixedAccelerationModel> steps = new ArrayList<>();

  34.     /** The simulator engine. */
  35.     private final OtsSimulatorInterface simulator;

  36.     /** The maximum safe deceleration. */
  37.     private final Acceleration maximumSafeDeceleration;

  38.     /**
  39.      * Construct a SequentialFixedAccelerationModel with empty list of FixedAccelerationModel steps.
  40.      * @param simulator OtsSimulatorInterface; the simulator (needed to obtain the current simulation time)
  41.      * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
  42.      */
  43.     public SequentialFixedAccelerationModel(final OtsSimulatorInterface simulator, final Acceleration maximumSafeDeceleration)
  44.     {
  45.         this.simulator = simulator;
  46.         this.maximumSafeDeceleration = maximumSafeDeceleration;
  47.     }

  48.     /**
  49.      * Construct a SequentialFixedAccelerationModel and load it with a list of FixedAccelerationModel steps.
  50.      * @param simulator OtsSimulatorInterface; the simulator (needed to obtain the current simulation time)
  51.      * @param maximumSafeDeceleration Acceleration; specified maximum safe deceleration
  52.      * @param steps Set&lt;FixedAccelerationModel&gt;; the list of FixedAccelerationModel steps.
  53.      */
  54.     public SequentialFixedAccelerationModel(final OtsSimulatorInterface simulator, final Acceleration maximumSafeDeceleration,
  55.             final Set<FixedAccelerationModel> steps)
  56.     {
  57.         this(simulator, maximumSafeDeceleration);
  58.         this.steps.addAll(steps);
  59.     }

  60.     /**
  61.      * Add one FixedAccelerationModel step to this SequentialFixedAccelerationModel.
  62.      * @param step FixedAccelerationModel; the step to add
  63.      * @return SequentialFixedAccelerationModel; this modified SequentialFixedAccelerationModel
  64.      */
  65.     public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
  66.     {
  67.         this.steps.add(step);
  68.         return this;
  69.     }

  70.     /**
  71.      * Retrieve the number of FixedAccelerationModel steps in this SequentialFixedAccelerationModel.
  72.      * @return int; the number of steps in this SequentialFixedAccelerationModel
  73.      */
  74.     public final int size()
  75.     {
  76.         return this.steps.size();
  77.     }

  78.     /**
  79.      * Retrieve one FixedAccelerationModel step.
  80.      * @param index int; the index of the retrieved FixedAccelerationModel step
  81.      * @return FixedAccelerationModel
  82.      */
  83.     public final FixedAccelerationModel get(final int index)
  84.     {
  85.         return this.steps.get(index);
  86.     }

  87.     /**
  88.      * Retrieve the simulation time at the end of the Nth step of this SequentialFixedAccelerationModel.
  89.      * @param index int; the step
  90.      * @return Time
  91.      */
  92.     public final Time timeAfterCompletionOfStep(final int index)
  93.     {
  94.         Time sum = new Time(0, TimeUnit.DEFAULT);
  95.         for (int i = 0; i <= index; i++)
  96.         {
  97.             sum = sum.plus(this.steps.get(i).getDuration());
  98.         }
  99.         return sum;
  100.     }

  101.     /** Maximum error of the simulator clock. */
  102.     private static final double MAXIMUMTIMEERROR = 0.001; // 1 millisecond

  103.     /**
  104.      * Find the FixedAccelerationModel that starts at the current simulator time.
  105.      * @return FixedAccelerationModel; the FixedAccelerationModel that starts at the current simulator time
  106.      */
  107.     private FixedAccelerationModel getAccelerationModel()
  108.     {
  109.         Time when = this.simulator.getSimulatorAbsTime();
  110.         double remainingTime = when.getSI();
  111.         for (FixedAccelerationModel step : this.steps)
  112.         {
  113.             if (remainingTime < -MAXIMUMTIMEERROR)
  114.             {
  115.                 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
  116.             }
  117.             if (remainingTime < MAXIMUMTIMEERROR)
  118.             {
  119.                 return step;
  120.             }
  121.             remainingTime -= step.getDuration().getSI();
  122.         }
  123.         throw new Error("FixedSequentialAcceleration does not have a result for " + when);
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
  128.             final Speed leaderSpeed, final Length headway, final Speed speedLimit)
  129.     {
  130.         return getAccelerationModel().getAcceleration();
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
  135.             final Speed leaderSpeed, final Length headway, final Speed speedLimit, final Duration stepSize)
  136.     {
  137.         // TODO incorporate stepSize
  138.         return getAccelerationModel().getAcceleration();
  139.     }

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     public final Acceleration getMaximumSafeDeceleration()
  143.     {
  144.         return this.maximumSafeDeceleration;
  145.     }

  146.     /** {@inheritDoc} */
  147.     @Override
  148.     public final Duration getStepSize()
  149.     {
  150.         return getAccelerationModel().getStepSize();
  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     public final String getName()
  155.     {
  156.         return "FSAM";
  157.     }

  158.     /** {@inheritDoc} */
  159.     @Override
  160.     public final String getLongName()
  161.     {
  162.         return "Fixed sequential acceleration model";
  163.     }

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     public final void setA(final Acceleration a)
  167.     {
  168.         //
  169.     }

  170.     /** {@inheritDoc} */
  171.     @Override
  172.     public final void setT(final Duration t)
  173.     {
  174.         //
  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public final void setFspeed(final double fSpeed)
  179.     {
  180.         //
  181.     }

  182.     // The following is inherited from CarFollowingModel

  183.     /** {@inheritDoc} */
  184.     @Override
  185.     public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
  186.     {
  187.         return null;
  188.     }

  189.     /** {@inheritDoc} */
  190.     @Override
  191.     public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
  192.     {
  193.         return null;
  194.     }

  195.     /** {@inheritDoc} */
  196.     @Override
  197.     public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
  198.             final SpeedLimitInfo speedInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
  199.     {
  200.         return null;
  201.     }

  202.     /** {@inheritDoc} */
  203.     @Override
  204.     public final String toString()
  205.     {
  206.         return "SequentialFixedAccelerationModel [steps=" + this.steps + ", maximumSafeDeceleration="
  207.                 + this.maximumSafeDeceleration + "]";
  208.     }

  209. }