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