IncentiveStayRight.java

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

  2. import java.util.SortedSet;

  3. import org.djunits.value.vdouble.scalar.Speed;
  4. import org.opentrafficsim.base.parameters.ParameterException;
  5. import org.opentrafficsim.base.parameters.Parameters;
  6. import org.opentrafficsim.core.gtu.perception.EgoPerception;
  7. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
  8. import org.opentrafficsim.core.network.LateralDirectionality;
  9. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  10. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  11. import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
  12. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  13. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
  14. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
  15. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
  16. import org.opentrafficsim.road.network.LaneChangeInfo;

  17. /**
  18.  * Incentive for trucks to remain on the two right-hand lanes, unless the route requires otherwise.
  19.  * <p>
  20.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  21.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * </p>
  23.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  25.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  26.  */
  27. public class IncentiveStayRight implements VoluntaryIncentive
  28. {

  29.     @Override
  30.     public Desire determineDesire(final Parameters parameters, final LanePerception perception,
  31.             final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
  32.             throws ParameterException, OperationalPlanException
  33.     {
  34.         InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
  35.         // start at left-most lane
  36.         SortedSet<RelativeLane> rootCrossSection = perception.getLaneStructure().getRootCrossSection();
  37.         RelativeLane lane = rootCrossSection.first();
  38.         // move right until we find 'the right-hand lane', which is defined by the last lane where the urgency does not increase
  39.         Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
  40.         double curUrgency = urgency(infra.getLegalLaneChangeInfo(lane), parameters, speed);
  41.         double rightUrgency;
  42.         RelativeLane right;
  43.         while (rootCrossSection.contains(right = lane.getRight())
  44.                 && (rightUrgency = urgency(infra.getLegalLaneChangeInfo(right), parameters, speed)) <= curUrgency)
  45.         {
  46.             curUrgency = rightUrgency;
  47.             lane = right;
  48.         }
  49.         boolean legalLeft = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).ge0();
  50.         if (lane.getLateralDirectionality().isRight() && lane.getNumLanes() > 1)
  51.         {
  52.             // must change right
  53.             return new Desire(legalLeft ? -1.0 : 0.0, parameters.getParameter(LmrsParameters.DSYNC));
  54.         }
  55.         if (lane.isRight())
  56.         {
  57.             // must not change left
  58.             return new Desire(legalLeft ? -1.0 : 0.0, 0.0);
  59.         }
  60.         return new Desire(0.0, 0.0);
  61.     }

  62.     /**
  63.      * Returns the urgency to leave a lane.
  64.      * @param laneChangeInfo lane change info on the lane
  65.      * @param parameters parameters
  66.      * @param speed current speed
  67.      * @return urgency to leave the lane
  68.      * @throws ParameterException if parameter is not given
  69.      */
  70.     private double urgency(final SortedSet<LaneChangeInfo> laneChangeInfo, final Parameters parameters, final Speed speed)
  71.             throws ParameterException
  72.     {
  73.         double urgency = 0.0;
  74.         for (LaneChangeInfo info : laneChangeInfo)
  75.         {
  76.             double nextUrgency =
  77.                     IncentiveRoute.getDesireToLeave(parameters, info.remainingDistance(), info.numberOfLaneChanges(), speed);
  78.             urgency = urgency > nextUrgency ? urgency : nextUrgency;
  79.         }
  80.         return urgency;
  81.     }

  82. }