Anticipation.java

  1. package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;

  2. import org.djunits.value.vdouble.scalar.Acceleration;
  3. import org.djunits.value.vdouble.scalar.Duration;
  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.djunits.value.vdouble.scalar.Speed;

  6. /**
  7.  * Form of anticipation.
  8.  * <p>
  9.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  11.  * <p>
  12.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 feb. 2017 <br>
  13.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  15.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  16.  */
  17. public interface Anticipation
  18. {

  19.     /** Assume no anticipation. */
  20.     Anticipation NONE = new Anticipation()
  21.     {
  22.         @Override
  23.         public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
  24.                 final Length traveledDistance, final boolean downstream)
  25.         {
  26.             return neighborTriplet;
  27.         }

  28.         @Override
  29.         public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
  30.         {
  31.             return Length.ZERO;
  32.         }
  33.     };

  34.     /** Assume constant speed. */
  35.     Anticipation CONSTANT_SPEED = new Anticipation()
  36.     {
  37.         @Override
  38.         public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
  39.                 final Length traveledDistance, final boolean downstream)
  40.         {
  41.             // upstream neighbor approaches when faster
  42.             Length distance = downstream
  43.                     ? neighborTriplet.getHeadway().plus(neighborTriplet.getSpeed().times(duration)).minus(traveledDistance)
  44.                     : neighborTriplet.getHeadway().minus(neighborTriplet.getSpeed().times(duration))
  45.                             .plus(traveledDistance);
  46.             return new NeighborTriplet(distance, neighborTriplet.getSpeed(), neighborTriplet.getAcceleration());
  47.         }

  48.         @Override
  49.         public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
  50.         {
  51.             return speed.times(duration);
  52.         }
  53.     };

  54.     /** Assume constant acceleration. */
  55.     Anticipation CONSTANT_ACCELERATION = new Anticipation()
  56.     {
  57.         @Override
  58.         public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
  59.                 final Length traveledDistance, final boolean downstream)
  60.         {
  61.             if (neighborTriplet.getSpeed().si < -neighborTriplet.getAcceleration().si * duration.si)
  62.             {
  63.                 // to stand still
  64.                 double t = neighborTriplet.getSpeed().si / -neighborTriplet.getAcceleration().si;
  65.                 double dx = neighborTriplet.getSpeed().si * t + .5 * neighborTriplet.getAcceleration().si * t * t;
  66.                 dx = downstream ? dx : -dx; // upstream neighbor approaches when faster
  67.                 return new NeighborTriplet(Length.instantiateSI(neighborTriplet.getHeadway().si + dx - traveledDistance.si),
  68.                         Speed.ZERO, Acceleration.ZERO);
  69.             }
  70.             double dx = neighborTriplet.getSpeed().si * duration.si
  71.                     + .5 * neighborTriplet.getAcceleration().si * duration.si * duration.si;
  72.             double dv = neighborTriplet.getAcceleration().si * duration.si;
  73.             return new NeighborTriplet(Length.instantiateSI(neighborTriplet.getHeadway().si + dx - traveledDistance.si),
  74.                     Speed.instantiateSI(neighborTriplet.getSpeed().si + dv), neighborTriplet.getAcceleration());
  75.         }

  76.         @Override
  77.         public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
  78.         {
  79.             return speed.times(duration);
  80.         }
  81.     };

  82.     /**
  83.      * Anticipate movement.
  84.      * @param neighborTriplet NeighborTriplet; headway, speed and acceleration
  85.      * @param duration Duration; duration
  86.      * @param traveledDistance Length; distance the subject vehicle traveled during the anticipation time
  87.      * @param downstream boolean; whether the perceived GTU is downstream
  88.      * @return anticipated info
  89.      */
  90.     NeighborTriplet anticipate(NeighborTriplet neighborTriplet, Duration duration, Length traveledDistance, boolean downstream);

  91.     /**
  92.      * Anticipate own movement.
  93.      * @param speed Speed; current speed
  94.      * @param acceleration Acceleration; current acceleration
  95.      * @param duration Duration; anticipation time
  96.      * @return anticipated distance traveled
  97.      */
  98.     Length egoAnticipation(Speed speed, Acceleration acceleration, Duration duration);
  99. }