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.GTUType;
  7. import org.opentrafficsim.core.gtu.Try;
  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.VoluntaryIncentive;

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

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

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

  110. }