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.djutils.exceptions.Try;
  5. import org.opentrafficsim.base.parameters.ParameterException;
  6. import org.opentrafficsim.base.parameters.Parameters;
  7. import org.opentrafficsim.core.gtu.GTUType;
  8. import org.opentrafficsim.core.gtu.perception.EgoPerception;
  9. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
  10. import org.opentrafficsim.core.network.NetworkException;
  11. import org.opentrafficsim.core.network.route.Route;
  12. import org.opentrafficsim.road.gtu.lane.perception.InfrastructureLaneChangeInfo;
  13. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  14. import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
  15. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  16. import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
  17. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  18. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
  19. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
  20. import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;

  21. /**
  22.  * Incentive for trucks to remain on the two right-hand lanes, unless the route requires otherwise.
  23.  * <p>
  24.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  25.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  26.  * <p>
  27.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 mrt. 2018 <br>
  28.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  29.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  30.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  31.  */
  32. public class IncentiveStayRight implements VoluntaryIncentive
  33. {

  34.     /** {@inheritDoc} */
  35.     @Override
  36.     public Desire determineDesire(final Parameters parameters, final LanePerception perception,
  37.             final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
  38.             throws ParameterException, OperationalPlanException
  39.     {
  40.         InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
  41.         LaneStructureRecord root = perception.getLaneStructure().getRootRecord();
  42.         LaneStructureRecord record = root;
  43.         RelativeLane lane = RelativeLane.CURRENT;
  44.         Route route = Try.assign(() -> perception.getGtu().getStrategicalPlanner().getRoute(), "");
  45.         GTUType gtuType = Try.assign(() -> perception.getGtu().getGTUType(), "");
  46.         Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
  47.         // move all the way left
  48.         while (record.physicalLeft())
  49.         {
  50.             lane = lane.getLeft();
  51.             record = record.getLeft();
  52.         }
  53.         // move right until we find 'the right-hand lane',
  54.         double curUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane), parameters, speed);
  55.         while (true)
  56.         {
  57.             try
  58.             {
  59.                 if (!record.physicalRight() || !record.getRight().allowsRoute(route, gtuType))
  60.                 {
  61.                     // next lane considered not there, we've found 'the right-hand lane'
  62.                     break;
  63.                 }
  64.             }
  65.             catch (NetworkException exception)
  66.             {
  67.                 throw new RuntimeException(exception);
  68.             }
  69.             double rightUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane.getRight()), parameters, speed);
  70.             if (rightUrgency > curUrgency)
  71.             {
  72.                 // next lane is worse for the route, current lane is allowable for the route
  73.                 break;
  74.             }
  75.             lane = lane.getRight();
  76.             record = record.getRight();
  77.             curUrgency = rightUrgency;
  78.         }
  79.         if (lane.getLateralDirectionality().isRight() && lane.getNumLanes() > 1)
  80.         {
  81.             // must change right
  82.             return new Desire(root.legalLeft() ? -1.0 : 0.0, parameters.getParameter(LmrsParameters.DSYNC));
  83.         }
  84.         if (lane.isRight())
  85.         {
  86.             // must not change left
  87.             return new Desire(root.legalLeft() ? -1.0 : 0.0, 0.0);
  88.         }
  89.         return new Desire(0.0, 0.0);
  90.     }

  91.     /**
  92.      * Returns the urgency to leave a lane.
  93.      * @param laneChangeInfo SortedSet&lt;InfrastructureLaneChangeInfo&gt;; lane change info on the lane
  94.      * @param parameters Parameters; parameters
  95.      * @param speed Speed; current speed
  96.      * @return double; urgency to leave the lane
  97.      * @throws ParameterException if parameter is not given
  98.      */
  99.     private double urgency(final SortedSet<InfrastructureLaneChangeInfo> laneChangeInfo, final Parameters parameters,
  100.             final Speed speed) throws ParameterException
  101.     {
  102.         double urgency = 0.0;
  103.         for (InfrastructureLaneChangeInfo info : laneChangeInfo)
  104.         {
  105.             double nextUrgency = IncentiveRoute.getDesireToLeave(parameters, info.getRemainingDistance(),
  106.                     info.getRequiredNumberOfLaneChanges(), speed);
  107.             urgency = urgency > nextUrgency ? urgency : nextUrgency;
  108.         }
  109.         return urgency;
  110.     }

  111. }