View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import org.djunits.value.vdouble.scalar.Acceleration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
8   import org.opentrafficsim.base.parameters.ParameterTypeDouble;
9   import org.opentrafficsim.base.parameters.ParameterTypes;
10  import org.opentrafficsim.base.parameters.Parameters;
11  import org.opentrafficsim.core.gtu.perception.EgoPerception;
12  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
13  import org.opentrafficsim.core.network.LateralDirectionality;
14  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
16  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
17  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
18  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
19  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
20  import org.opentrafficsim.road.gtu.lane.tactical.util.CarFollowingUtil;
21  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
22  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
23  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsUtil;
24  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
25  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
26  
27  /**
28   * Determines lane change desire for courtesy lane changes, which are performed to supply space for other drivers. In case
29   * drivers in adjacent lanes have desire to change to the current lane, the driver has desire to change to the other adjacent
30   * lane. The level of desire depends on lane change courtesy, as well as the distance of the leading vehicle for which desire
31   * exists. This desire exists for only a single vehicle, i.e. the one giving maximum desire. A negative desire may also result
32   * for leaders in the 2nd adjacent lane desiring to change to the 1st adjacent lane. By not changing to the 1st adjacent lane,
33   * room is reserved for the leader on the 2nd adjacent lane.
34   * <p>
35   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
37   * <p>
38   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016 <br>
39   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
40   */
41  public class IncentiveCourtesy implements VoluntaryIncentive
42  {
43  
44      /** Comfortable deceleration parameter type. */
45      protected static final ParameterTypeAcceleration B = ParameterTypes.B;
46  
47      /** Socio-speed sensitivity parameter. */
48      protected static final ParameterTypeDouble SOCIO = LmrsParameters.SOCIO;
49  
50      /** Current left lane change desire. */
51      protected static final ParameterTypeDouble DLEFT = LmrsParameters.DLEFT;
52  
53      /** Current right lane change desire. */
54      protected static final ParameterTypeDouble DRIGHT = LmrsParameters.DRIGHT;
55  
56      /** {@inheritDoc} */
57      @Override
58      public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
59              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
60              throws ParameterException, OperationalPlanException
61      {
62  
63          double dLeftYes = 0;
64          double dRightYes = 0;
65          double dLeftNo = 0;
66          double dRightNo = 0;
67          double socio = parameters.getParameter(SOCIO);
68          Acceleration b = parameters.getParameter(B);
69          NeighborsPerception neighbors = perception.getPerceptionCategory(NeighborsPerception.class);
70          Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
71          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
72          SpeedLimitInfo sli = infra.getSpeedLimitProspect(RelativeLane.CURRENT).getSpeedLimitInfo(Length.ZERO);
73          boolean leftLane = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).si > 0.0;
74          boolean rightLane = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).si > 0.0;
75          for (LateralDirectionality dir : new LateralDirectionality[] {LateralDirectionality.LEFT, LateralDirectionality.RIGHT})
76          {
77              Iterable<HeadwayGTU> leaders = neighbors.getLeaders(new RelativeLane(dir, 1));
78              if (leaders != null)
79              {
80                  for (HeadwayGTU leader : leaders)
81                  {
82                      Parameters params = leader.getParameters();
83                      double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
84                      if (desire > 0)
85                      {
86                          // TODO factor -a/b as influence factor is heavy in calculation, consider v<vEgo & 1-s/x0
87                          Acceleration a =
88                                  CarFollowingUtil.followSingleLeader(carFollowingModel, parameters, ownSpeed, sli, leader);
89                          if (a.lt0())
90                          {
91                              double d = desire * Math.min(-a.si / b.si, 1.0);
92                              if (dir.isLeft() && rightLane)
93                              {
94                                  // leader in left lane wants to change right, so we change right
95                                  dRightYes = dRightYes > d ? dRightYes : d;
96                              }
97                              else if (leftLane)
98                              {
99                                  // leader in right lane wants to change left, so we change left
100                                 dLeftYes = dLeftYes > d ? dLeftYes : d;
101                             }
102                         }
103                     }
104                 }
105             }
106             // consider close followers on 2 lanes away
107             Iterable<HeadwayGTU> followers = neighbors.getFollowers(new RelativeLane(dir, 2));
108             if (followers != null)
109             {
110                 for (HeadwayGTU follower : followers)
111                 {
112                     Parameters params = follower.getParameters();
113                     double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
114                     Acceleration a = follower.getDistance().lt0() ? b.neg()
115                             : LmrsUtil.singleAcceleration(follower.getDistance(), follower.getSpeed(), ownSpeed, desire, params,
116                                     follower.getSpeedLimitInfo(), follower.getCarFollowingModel());
117                     if (a.lt0())
118                     {
119                         if (desire > 0)
120                         {
121                             double d = desire * Math.min(-a.si / b.si, 1.0);
122                             if (dir.isLeft() && leftLane)
123                             {
124                                 // follower in second left lane wants to change right, so we do not change left
125                                 dLeftNo = dLeftNo > desire ? dLeftNo : d;
126                             }
127                             else if (rightLane)
128                             {
129                                 // follower in second right lane wants to change left, so we do not change right
130                                 dRightNo = dRightNo > desire ? dRightNo : d;
131                             }
132                         }
133                     }
134                     else
135                     {
136                         // ignore further followers
137                         break;
138                     }
139                 }
140             }
141             leaders = neighbors.getLeaders(new RelativeLane(dir, 2));
142             if (leaders != null)
143             {
144                 for (HeadwayGTU leader : leaders)
145                 {
146                     Parameters params = leader.getParameters();
147                     double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
148                     if (desire > 0)
149                     {
150                         Acceleration a = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(), desire,
151                                 params, sli, carFollowingModel);
152                         if (a.lt0())
153                         {
154                             double d = desire * Math.min(-a.si / b.si, 1.0); // (1 - leader.getDistance().si / x0.si) * desire;
155                             if (dir.isLeft() && leftLane)
156                             {
157                                 // leader in second left lane wants to change right, so we do not change left
158                                 dLeftNo = dLeftNo > d ? dLeftNo : d;
159                             }
160                             else if (rightLane)
161                             {
162                                 // leader in second right lane wants to change left, so we do not change right
163                                 dRightNo = dRightNo > d ? dRightNo : d;
164                             }
165                         }
166                     }
167                 }
168             }
169         }
170         // note: noLeft and noRight weighted with 1 always
171         dLeftYes *= socio;
172         dRightYes *= socio;
173         return new Desire(dLeftYes - dLeftNo, dRightYes - dRightNo);
174 
175     }
176 
177     /** {@inheritDoc} */
178     @Override
179     public final String toString()
180     {
181         return "IncentiveCourtesy";
182     }
183 
184 }