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.djutils.exceptions.Try;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.Parameters;
9   import org.opentrafficsim.core.gtu.GTUType;
10  import org.opentrafficsim.core.gtu.perception.EgoPerception;
11  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.core.network.route.Route;
14  import org.opentrafficsim.road.gtu.lane.perception.InfrastructureLaneChangeInfo;
15  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
16  import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
17  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
18  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
19  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
20  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
21  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
22  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
23  
24  /**
25   * Incentive for trucks to remain on the two right-hand lanes, unless the route requires otherwise.
26   * <p>
27   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
29   * <p>
30   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 mrt. 2018 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
34   */
35  public class IncentiveStayRight implements VoluntaryIncentive
36  {
37  
38      /** {@inheritDoc} */
39      @Override
40      public Desire determineDesire(final Parameters parameters, final LanePerception perception,
41              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
42              throws ParameterException, OperationalPlanException
43      {
44          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
45          LaneStructureRecord root = perception.getLaneStructure().getRootRecord();
46          LaneStructureRecord record = root;
47          RelativeLane lane = RelativeLane.CURRENT;
48          Route route = Try.assign(() -> perception.getGtu().getStrategicalPlanner().getRoute(), "");
49          GTUType gtuType = Try.assign(() -> perception.getGtu().getGTUType(), "");
50          Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
51          // move all the way left
52          while (record.physicalLeft())
53          {
54              lane = lane.getLeft();
55              record = record.getLeft();
56          }
57          // move right until we find 'the right-hand lane',
58          double curUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane), parameters, speed);
59          while (true)
60          {
61              try
62              {
63                  if (!record.physicalRight() || !record.getRight().allowsRoute(route, gtuType))
64                  {
65                      // next lane considered not there, we've found 'the right-hand lane'
66                      break;
67                  }
68              }
69              catch (NetworkException exception)
70              {
71                  throw new RuntimeException(exception);
72              }
73              double rightUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane.getRight()), parameters, speed);
74              if (rightUrgency > curUrgency)
75              {
76                  // next lane is worse for the route, current lane is allowable for the route
77                  break;
78              }
79              lane = lane.getRight();
80              record = record.getRight();
81              curUrgency = rightUrgency;
82          }
83          if (lane.getLateralDirectionality().isRight() && lane.getNumLanes() > 1)
84          {
85              // must change right
86              return new Desire(root.legalLeft() ? -1.0 : 0.0, parameters.getParameter(LmrsParameters.DSYNC));
87          }
88          if (lane.isRight())
89          {
90              // must not change left
91              return new Desire(root.legalLeft() ? -1.0 : 0.0, 0.0);
92          }
93          return new Desire(0.0, 0.0);
94      }
95  
96      /**
97       * Returns the urgency to leave a lane.
98       * @param laneChangeInfo SortedSet&lt;InfrastructureLaneChangeInfo&gt;; lane change info on the lane
99       * @param parameters Parameters; parameters
100      * @param speed Speed; current speed
101      * @return double; urgency to leave the lane
102      * @throws ParameterException if parameter is not given
103      */
104     private double urgency(final SortedSet<InfrastructureLaneChangeInfo> laneChangeInfo, final Parameters parameters,
105             final Speed speed) throws ParameterException
106     {
107         double urgency = 0.0;
108         for (InfrastructureLaneChangeInfo info : laneChangeInfo)
109         {
110             double nextUrgency = IncentiveRoute.getDesireToLeave(parameters, info.getRemainingDistance(),
111                     info.getRequiredNumberOfLaneChanges(), speed);
112             urgency = urgency > nextUrgency ? urgency : nextUrgency;
113         }
114         return urgency;
115     }
116 
117 }