View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import java.util.SortedSet;
4   
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.Parameters;
8   import org.opentrafficsim.core.gtu.Stateless;
9   import org.opentrafficsim.core.gtu.perception.EgoPerception;
10  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
11  import org.opentrafficsim.core.network.LateralDirectionality;
12  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
13  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
14  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
15  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
16  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
17  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
18  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
19  import org.opentrafficsim.road.network.LaneChangeInfo;
20  
21  /**
22   * Incentive for trucks to remain on the two right-hand lanes, unless the route requires otherwise.
23   * <p>
24   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
29   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
30   */
31  public final class IncentiveStayRight implements VoluntaryIncentive, Stateless<IncentiveStayRight>
32  {
33  
34      /** Singleton instance. */
35      public static final IncentiveStayRight SINGLETON = new IncentiveStayRight();
36  
37      @Override
38      public IncentiveStayRight get()
39      {
40          return SINGLETON;
41      }
42  
43      /**
44       * Constructor.
45       */
46      private IncentiveStayRight()
47      {
48          //
49      }
50  
51      @Override
52      public Desire determineDesire(final Parameters parameters, final LanePerception perception,
53              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
54              throws ParameterException, OperationalPlanException
55      {
56          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
57          // start at left-most lane
58          SortedSet<RelativeLane> rootCrossSection = perception.getLaneStructure().getRootCrossSection();
59          RelativeLane lane = rootCrossSection.first();
60          // move right until we find 'the right-hand lane', which is defined by the last lane where the urgency does not increase
61          Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
62          double curUrgency = urgency(infra.getLegalLaneChangeInfo(lane), parameters, speed);
63          double rightUrgency;
64          RelativeLane right;
65          while (rootCrossSection.contains(right = lane.getRight())
66                  && (rightUrgency = urgency(infra.getLegalLaneChangeInfo(right), parameters, speed)) <= curUrgency)
67          {
68              curUrgency = rightUrgency;
69              lane = right;
70          }
71          boolean legalLeft = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).ge0();
72          if (lane.getLateralDirectionality().isRight() && lane.getNumLanes() > 1)
73          {
74              // must change right
75              return new Desire(legalLeft ? -1.0 : 0.0, parameters.getParameter(LmrsParameters.DSYNC));
76          }
77          if (lane.isRight())
78          {
79              // must not change left
80              return new Desire(legalLeft ? -1.0 : 0.0, 0.0);
81          }
82          return new Desire(0.0, 0.0);
83      }
84  
85      /**
86       * Returns the urgency to leave a lane.
87       * @param laneChangeInfo lane change info on the lane
88       * @param parameters parameters
89       * @param speed current speed
90       * @return urgency to leave the lane
91       * @throws ParameterException if parameter is not given
92       */
93      private double urgency(final SortedSet<LaneChangeInfo> laneChangeInfo, final Parameters parameters, final Speed speed)
94              throws ParameterException
95      {
96          double urgency = 0.0;
97          for (LaneChangeInfo info : laneChangeInfo)
98          {
99              double nextUrgency =
100                     IncentiveRoute.getDesireToLeave(parameters, info.remainingDistance(), info.numberOfLaneChanges(), speed);
101             urgency = urgency > nextUrgency ? urgency : nextUrgency;
102         }
103         return urgency;
104     }
105 
106     @Override
107     public String toString()
108     {
109         return "IncentiveStayRight";
110     }
111 
112 }