Cooperation.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;

  2. import org.djunits.unit.AccelerationUnit;
  3. import org.djunits.value.vdouble.scalar.Acceleration;
  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.opentrafficsim.base.parameters.ParameterException;
  6. import org.opentrafficsim.base.parameters.ParameterTypes;
  7. import org.opentrafficsim.base.parameters.Parameters;
  8. import org.opentrafficsim.core.gtu.perception.EgoPerception;
  9. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
  10. import org.opentrafficsim.core.network.LateralDirectionality;
  11. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  12. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  13. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
  14. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  15. import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
  16. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
  17. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  18. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;

  19. /**
  20.  * Different forms of cooperation.
  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.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  28.  */
  29. public interface Cooperation extends LmrsParameters
  30. {

  31.     /** Simple passive cooperation. */
  32.     Cooperation PASSIVE = new Cooperation()
  33.     {
  34.         /** {@inheritDoc} */
  35.         @Override
  36.         public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
  37.                 final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
  38.                 throws ParameterException, OperationalPlanException
  39.         {
  40.             if (!perception.getLaneStructure().exists(lat.isRight() ? RelativeLane.RIGHT : RelativeLane.LEFT))
  41.             {
  42.                 return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  43.             }
  44.             Acceleration b = params.getParameter(ParameterTypes.B);
  45.             Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  46.             double dCoop = params.getParameter(DCOOP);
  47.             Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
  48.             RelativeLane relativeLane = new RelativeLane(lat, 1);
  49.             for (HeadwayGtu leader : perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane))
  50.             {
  51.                 Parameters params2 = leader.getParameters();
  52.                 double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
  53.                         : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
  54.                 if (desire >= dCoop && (leader.getSpeed().gt0() || leader.getDistance().gt0()))
  55.                 {
  56.                     Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
  57.                             desire, params, sli, cfm);
  58.                     a = Acceleration.min(a, aSingle);
  59.                 }
  60.             }
  61.             return Acceleration.max(a, b.neg());
  62.         }

  63.         /** {@inheritDoc} */
  64.         @Override
  65.         public String toString()
  66.         {
  67.             return "PASSIVE";
  68.         }
  69.     };

  70.     /** Same as passive cooperation, except that cooperation is fully ignored if the potential lane changer brakes heavily. */
  71.     Cooperation PASSIVE_MOVING = new Cooperation()
  72.     {
  73.         /** {@inheritDoc} */
  74.         @Override
  75.         public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
  76.                 final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
  77.                 throws ParameterException, OperationalPlanException
  78.         {
  79.             if (!perception.getLaneStructure().exists(lat.isRight() ? RelativeLane.RIGHT : RelativeLane.LEFT))
  80.             {
  81.                 return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  82.             }
  83.             Acceleration bCrit = params.getParameter(ParameterTypes.BCRIT);
  84.             Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  85.             double dCoop = params.getParameter(DCOOP);
  86.             Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
  87.             RelativeLane relativeLane = new RelativeLane(lat, 1);
  88.             NeighborsPerception neighbours = perception.getPerceptionCategory(NeighborsPerception.class);
  89.             PerceptionCollectable<HeadwayGtu, LaneBasedGtu> leaders = neighbours.getLeaders(RelativeLane.CURRENT);
  90.             Speed thresholdSpeed = Speed.instantiateSI(6.86); // 295m / 43s
  91.             boolean leaderInCongestion = leaders.isEmpty() ? false : leaders.first().getSpeed().lt(thresholdSpeed);
  92.             for (HeadwayGtu leader : neighbours.getLeaders(relativeLane))
  93.             {
  94.                 Parameters params2 = leader.getParameters();
  95.                 double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
  96.                         : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
  97.                 // TODO: only cooperate if merger still quite fast or there's congestion downstream anyway (which we can better
  98.                 // estimate than only considering the direct leader
  99.                 if (desire >= dCoop && (leader.getSpeed().gt0() || leader.getDistance().gt0())
  100.                         && (leader.getSpeed().ge(thresholdSpeed) || leaderInCongestion))
  101.                 {
  102.                     Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
  103.                             desire, params, sli, cfm);
  104.                     a = Acceleration.min(a, aSingle);
  105.                 }
  106.             }
  107.             return Acceleration.max(a, bCrit.neg());
  108.         }

  109.         /** {@inheritDoc} */
  110.         @Override
  111.         public String toString()
  112.         {
  113.             return "PASSIVE_MOVING";
  114.         }
  115.     };

  116.     /** Cooperation similar to the default, with nuanced differences of when to ignore. */
  117.     Cooperation ACTIVE = new Cooperation()
  118.     {
  119.         /** {@inheritDoc} */
  120.         @Override
  121.         public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
  122.                 final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
  123.                 throws ParameterException, OperationalPlanException
  124.         {
  125.             if (!perception.getLaneStructure().exists(lat.isRight() ? RelativeLane.RIGHT : RelativeLane.LEFT))
  126.             {
  127.                 return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  128.             }
  129.             Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
  130.             double dCoop = params.getParameter(DCOOP);
  131.             Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
  132.             RelativeLane relativeLane = new RelativeLane(lat, 1);
  133.             for (HeadwayGtu leader : perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane))
  134.             {
  135.                 Parameters params2 = leader.getParameters();
  136.                 double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
  137.                         : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
  138.                 if (desire >= dCoop && leader.getDistance().gt0()
  139.                         && leader.getAcceleration().gt(params.getParameter(ParameterTypes.BCRIT).neg()))
  140.                 {
  141.                     Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
  142.                             desire, params, sli, cfm);
  143.                     a = Acceleration.min(a, Synchronization.gentleUrgency(aSingle, desire, params));
  144.                 }
  145.             }
  146.             return a;
  147.         }

  148.         /** {@inheritDoc} */
  149.         @Override
  150.         public String toString()
  151.         {
  152.             return "ACTIVE";
  153.         }
  154.     };

  155.     /**
  156.      * Determine acceleration for cooperation.
  157.      * @param perception LanePerception; perception
  158.      * @param params Parameters; parameters
  159.      * @param sli SpeedLimitInfo; speed limit info
  160.      * @param cfm CarFollowingModel; car-following model
  161.      * @param lat LateralDirectionality; lateral direction for cooperation
  162.      * @param ownDesire Desire; own lane change desire
  163.      * @return acceleration for synchronization
  164.      * @throws ParameterException if a parameter is not defined
  165.      * @throws OperationalPlanException perception exception
  166.      */
  167.     Acceleration cooperate(LanePerception perception, Parameters params, SpeedLimitInfo sli, CarFollowingModel cfm,
  168.             LateralDirectionality lat, Desire ownDesire) throws ParameterException, OperationalPlanException;
  169. }