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.InfrastructureLaneChangeInfo;
16  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
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.MandatoryIncentive;
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<InfrastructureLaneChangeInfo> currentInfo = infra.getInfrastructureLaneChangeInfo(RelativeLane.CURRENT);
55          Length currentFirst = currentInfo.isEmpty() || currentInfo.first().getRequiredNumberOfLaneChanges() == 0
56                  ? Length.POSITIVE_INFINITY : currentInfo.first().getRemainingDistance();
57          double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
58          double dLeft = 0;
59          if (perception.getLaneStructure().getExtendedCrossSection().contains(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().getExtendedCrossSection().contains(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 (InfrastructureLaneChangeInfo info : infra.getInfrastructureLaneChangeInfo(lane))
96              {
97                  double d = getDesireToLeave(params, info.getRemainingDistance(), info.getRequiredNumberOfLaneChanges(), 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; parameters
107      * @param x Length; remaining distance for lane changes
108      * @param n int; number of required lane changes
109      * @param v Speed; 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     /** {@inheritDoc} */
123     @Override
124     public final String toString()
125     {
126         return "IncentiveRoute";
127     }
128 
129 }