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.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.ParameterTypeDuration;
9   import org.opentrafficsim.base.parameters.ParameterTypeLength;
10  import org.opentrafficsim.base.parameters.ParameterTypes;
11  import org.opentrafficsim.base.parameters.Parameters;
12  import org.opentrafficsim.core.gtu.perception.EgoPerception;
13  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
14  import org.opentrafficsim.core.network.LateralDirectionality;
15  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
16  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
17  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
18  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
19  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
20  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive;
21  import org.opentrafficsim.road.network.LaneChangeInfo;
22  
23  /**
24   * Determines desire by assessing the number of required lane change to be performed and the distance within which these have to
25   * be performed. Desire starts to increase from 0 linearly over a distance of x0 per required lane change, or per v*t0 per
26   * required lane change. For v>x0/t0 this gives that remaining time is critical, while for v<x0/t0 remaining space is
27   * critical. The desire is set towards the adjacent lane with a better situation. Negative desire towards the other lane, the
28   * extent of which pertains to the other adjacent lane, is also set.
29   * <p>
30   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32   * </p>
33   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
34   */
35  public class IncentiveRoute implements MandatoryIncentive
36  {
37  
38      /** Look ahead parameter type. */
39      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
40  
41      /** Look-ahead time for mandatory lane changes parameter type. */
42      public static final ParameterTypeDuration T0 = ParameterTypes.T0;
43  
44      /** {@inheritDoc} */
45      @Override
46      public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
47              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire)
48              throws ParameterException, OperationalPlanException
49      {
50          Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
51          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
52  
53          // desire to leave current lane
54          SortedSet<LaneChangeInfo> currentInfo = infra.getLegalLaneChangeInfo(RelativeLane.CURRENT);
55          Length currentFirst = currentInfo.isEmpty() || currentInfo.first().numberOfLaneChanges() == 0 ? Length.POSITIVE_INFINITY
56                  : currentInfo.first().remainingDistance();
57          double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
58          double dLeft = 0;
59          if (perception.getLaneStructure().exists(RelativeLane.LEFT)
60                  && infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).neg().lt(currentFirst))
61          {
62              // desire to leave left lane
63              dLeft = getDesireToLeave(parameters, infra, RelativeLane.LEFT, speed);
64              // desire to leave from current to left lane
65              dLeft = dLeft < dCurr ? dCurr : dLeft > dCurr ? -dLeft : 0;
66          }
67          double dRigh = 0;
68          if (perception.getLaneStructure().exists(RelativeLane.RIGHT) && infra
69                  .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).neg().lt(currentFirst))
70          {
71              // desire to leave right lane
72              dRigh = getDesireToLeave(parameters, infra, RelativeLane.RIGHT, speed);
73              // desire to leave from current to right lane
74              dRigh = dRigh < dCurr ? dCurr : dRigh > dCurr ? -dRigh : 0;
75          }
76          return new Desire(dLeft, dRigh);
77      }
78  
79      /**
80       * Calculates desire to leave a lane.
81       * @param params Parameters; parameters
82       * @param infra InfrastructurePerception; infrastructure perception
83       * @param lane RelativeLane; relative lane to evaluate
84       * @param speed Speed; speed
85       * @return desire to leave a lane
86       * @throws ParameterException in case of a parameter exception
87       * @throws OperationalPlanException in case of perception exceptions
88       */
89      private static double getDesireToLeave(final Parameters params, final InfrastructurePerception infra,
90              final RelativeLane lane, final Speed speed) throws ParameterException, OperationalPlanException
91      {
92          double dOut = 0.0;
93          if (infra.getCrossSection().contains(lane))
94          {
95              for (LaneChangeInfo info : infra.getLegalLaneChangeInfo(lane))
96              {
97                  double d = info.remainingDistance().lt0() ? info.numberOfLaneChanges()
98                          : getDesireToLeave(params, info.remainingDistance(), info.numberOfLaneChanges(), speed);
99                  dOut = d > dOut ? d : dOut;
100             }
101         }
102         return dOut;
103     }
104 
105     /**
106      * Calculates desire to leave a lane for a single infrastructure info.
107      * @param params Parameters; parameters
108      * @param x Length; remaining distance for lane changes
109      * @param n int; number of required lane changes
110      * @param v Speed; current speed
111      * @return desire to leave a lane for a single infrastructure info
112      * @throws ParameterException in case of a parameter exception
113      */
114     public static double getDesireToLeave(final Parameters params, final Length x, final int n, final Speed v)
115             throws ParameterException
116     {
117         double d1 = 1 - x.si / (n * params.getParameter(LOOKAHEAD).si);
118         double d2 = 1 - (x.si / v.si) / (n * params.getParameter(T0).si);
119         d1 = d2 > d1 ? d2 : d1;
120         return d1 < 0 ? 0 : d1;
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final String toString()
126     {
127         return "IncentiveRoute";
128     }
129 
130 }