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      @Override
45      public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
46              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire)
47              throws ParameterException, OperationalPlanException
48      {
49          Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
50          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
51  
52          // desire to leave current lane
53          SortedSet<LaneChangeInfo> currentInfo = infra.getLegalLaneChangeInfo(RelativeLane.CURRENT);
54          Length currentFirst = currentInfo.isEmpty() || currentInfo.first().numberOfLaneChanges() == 0 ? Length.POSITIVE_INFINITY
55                  : currentInfo.first().remainingDistance();
56          double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
57          double dLeft = 0;
58          if (perception.getLaneStructure().exists(RelativeLane.LEFT)
59                  && infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).neg().lt(currentFirst))
60          {
61              // desire to leave left lane
62              dLeft = getDesireToLeave(parameters, infra, RelativeLane.LEFT, speed);
63              // desire to leave from current to left lane
64              dLeft = dLeft < dCurr ? dCurr : dLeft > dCurr ? -dLeft : 0;
65          }
66          double dRigh = 0;
67          if (perception.getLaneStructure().exists(RelativeLane.RIGHT) && infra
68                  .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).neg().lt(currentFirst))
69          {
70              // desire to leave right lane
71              dRigh = getDesireToLeave(parameters, infra, RelativeLane.RIGHT, speed);
72              // desire to leave from current to right lane
73              dRigh = dRigh < dCurr ? dCurr : dRigh > dCurr ? -dRigh : 0;
74          }
75          return new Desire(dLeft, dRigh);
76      }
77  
78      /**
79       * Calculates desire to leave a lane.
80       * @param params parameters
81       * @param infra infrastructure perception
82       * @param lane relative lane to evaluate
83       * @param speed speed
84       * @return desire to leave a lane
85       * @throws ParameterException in case of a parameter exception
86       * @throws OperationalPlanException in case of perception exceptions
87       */
88      private static double getDesireToLeave(final Parameters params, final InfrastructurePerception infra,
89              final RelativeLane lane, final Speed speed) throws ParameterException, OperationalPlanException
90      {
91          double dOut = 0.0;
92          if (infra.getCrossSection().contains(lane))
93          {
94              for (LaneChangeInfo info : infra.getLegalLaneChangeInfo(lane))
95              {
96                  double d = info.remainingDistance().lt0() ? info.numberOfLaneChanges()
97                          : getDesireToLeave(params, info.remainingDistance(), info.numberOfLaneChanges(), speed);
98                  dOut = d > dOut ? d : dOut;
99              }
100         }
101         return dOut;
102     }
103 
104     /**
105      * Calculates desire to leave a lane for a single infrastructure info.
106      * @param params parameters
107      * @param x remaining distance for lane changes
108      * @param n number of required lane changes
109      * @param v current speed
110      * @return desire to leave a lane for a single infrastructure info
111      * @throws ParameterException in case of a parameter exception
112      */
113     public static double getDesireToLeave(final Parameters params, final Length x, final int n, final Speed v)
114             throws ParameterException
115     {
116         double d1 = 1 - x.si / (n * params.getParameter(LOOKAHEAD).si);
117         double d2 = 1 - (x.si / v.si) / (n * params.getParameter(T0).si);
118         d1 = d2 > d1 ? d2 : d1;
119         return d1 < 0 ? 0 : d1;
120     }
121 
122     @Override
123     public final String toString()
124     {
125         return "IncentiveRoute";
126     }
127 
128 }