AnticipationSpeed.java

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

  2. import java.util.function.Supplier;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.opentrafficsim.core.gtu.Gtu;
  6. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  7. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
  8. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
  9. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
  10. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;
  11. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  12. import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationSpeed.SpeedSet;

  13. /**
  14.  * Collector of leaders which derives an set of anticipation speeds from a lane. This includes all GTUs on the lane (current),
  15.  * all GTUs indicating left (left) and all GTUs indicating right (right).
  16.  * <p>
  17.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  18.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  19.  * </p>
  20.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  21.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  22.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  23.  */
  24. public class AnticipationSpeed implements PerceptionCollector<SpeedSet, LaneBasedGtu, SpeedSet>
  25. {

  26.     /** Desired speed. */
  27.     private double desiredSpeed;

  28.     /** Look-ahead distance. */
  29.     private double x0;

  30.     /** Lane. */
  31.     private RelativeLane lane;

  32.     /**
  33.      * Constructor.
  34.      * @param desiredSpeed Speed; desired speed
  35.      * @param lookAhead Length; look-ahead distance
  36.      * @param lane RelativeLane; lane
  37.      */
  38.     public AnticipationSpeed(final Speed desiredSpeed, final Length lookAhead, final RelativeLane lane)
  39.     {
  40.         this.desiredSpeed = desiredSpeed.si;
  41.         this.x0 = lookAhead.si;
  42.         this.lane = lane;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public Supplier<SpeedSet> getIdentity()
  47.     {
  48.         return new Supplier<SpeedSet>()
  49.         {
  50.             @SuppressWarnings("synthetic-access")
  51.             @Override
  52.             public SpeedSet get()
  53.             {
  54.                 SpeedSet identity = new SpeedSet();
  55.                 identity.left = AnticipationSpeed.this.desiredSpeed;
  56.                 identity.current = AnticipationSpeed.this.desiredSpeed;
  57.                 identity.right = AnticipationSpeed.this.desiredSpeed;
  58.                 return identity;
  59.             }
  60.         };
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public PerceptionAccumulator<LaneBasedGtu, SpeedSet> getAccumulator()
  65.     {
  66.         return new PerceptionAccumulator<LaneBasedGtu, SpeedSet>()
  67.         {
  68.             @SuppressWarnings("synthetic-access")
  69.             @Override
  70.             public Intermediate<SpeedSet> accumulate(final Intermediate<SpeedSet> intermediate, final LaneBasedGtu object,
  71.                     final Length distance)
  72.             {
  73.                 double v = anticipateSingle(object, distance);
  74.                 if (AnticipationSpeed.this.lane.getNumLanes() < 2)
  75.                 {
  76.                     intermediate.getObject().current =
  77.                             intermediate.getObject().current < v ? intermediate.getObject().current : v;
  78.                 }
  79.                 if (!AnticipationSpeed.this.lane.isCurrent())
  80.                 {
  81.                     if (AnticipationSpeed.this.lane.isRight())
  82.                     {
  83.                         if (object.getTurnIndicatorStatus().isLeft())
  84.                         {
  85.                             intermediate.getObject().left =
  86.                                     intermediate.getObject().left < v ? intermediate.getObject().left : v;
  87.                         }
  88.                     }
  89.                     else
  90.                     {
  91.                         if (object.getTurnIndicatorStatus().isRight())
  92.                         {
  93.                             intermediate.getObject().right =
  94.                                     intermediate.getObject().right < v ? intermediate.getObject().right : v;
  95.                         }
  96.                     }
  97.                 }
  98.                 return intermediate;
  99.             }
  100.         };

  101.     }

  102.     /**
  103.      * Anticipate a single leader by possibly lowering the anticipation speed.
  104.      * @param gtu Gtu; GTU
  105.      * @param distance Length; distance to GTU
  106.      * @return possibly lowered anticipation speed
  107.      */
  108.     final double anticipateSingle(final Gtu gtu, final Length distance)
  109.     {
  110.         Speed speed = gtu.getSpeed();
  111.         double v = speed == null ? 0.0 : speed.si;
  112.         if (v > this.desiredSpeed || distance.si > this.x0)
  113.         {
  114.             return this.desiredSpeed;
  115.         }
  116.         double f = distance.si / this.x0;
  117.         f = f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f;
  118.         return (1 - f) * v + f * this.desiredSpeed;
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public PerceptionFinalizer<SpeedSet, SpeedSet> getFinalizer()
  123.     {
  124.         return new PerceptionFinalizer<SpeedSet, SpeedSet>()
  125.         {
  126.             @Override
  127.             public SpeedSet collect(final SpeedSet intermediate)
  128.             {
  129.                 return intermediate;
  130.             }
  131.         };
  132.     }

  133.     /**
  134.      * Class to contain info from 1 lane, regarding 3 lanes.
  135.      * <p>
  136.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  137.      * <br>
  138.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  139.      * </p>
  140.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  141.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  142.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  143.      */
  144.     public static class SpeedSet
  145.     {
  146.         /** Speed regarding the left lane. */
  147.         private double left = Double.POSITIVE_INFINITY;

  148.         /** Speed regarding the current lane. */
  149.         private double current = Double.POSITIVE_INFINITY;

  150.         /** Speed regarding the right lane. */
  151.         private double right = Double.POSITIVE_INFINITY;

  152.         /**
  153.          * Returns the speed regarding the left lane.
  154.          * @return Speed; speed regarding the left lane
  155.          */
  156.         public final Speed getLeft()
  157.         {
  158.             return Speed.instantiateSI(this.left);
  159.         }

  160.         /**
  161.          * Returns the speed regarding the current lane.
  162.          * @return Speed; speed regarding the current lane
  163.          */
  164.         public final Speed getCurrent()
  165.         {
  166.             return Speed.instantiateSI(this.current);
  167.         }

  168.         /**
  169.          * Returns the speed regarding the right lane.
  170.          * @return Speed; speed regarding the right lane
  171.          */
  172.         public final Speed getRight()
  173.         {
  174.             return Speed.instantiateSI(this.right);
  175.         }
  176.     }

  177. }