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,
76                  LateralDirectionality.RIGHT })
77          {
78              Iterable<HeadwayGTU> leaders = neighbors.getLeaders(new RelativeLane(dir, 1));
79              if (leaders != null)
80              {
81                  for (HeadwayGTU leader : leaders)
82                  {
83                      Parameters params = leader.getParameters();
84                      double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
85                      if (desire > 0)
86                      {
87                          // TODO factor -a/b as influence factor is heavy in calculation, consider v<vEgo & 1-s/x0
88                          Acceleration a =
89                                  CarFollowingUtil.followSingleLeader(carFollowingModel, parameters, ownSpeed, sli, leader);
90                          if (a.lt0())
91                          {
92                              double d = desire * Math.min(-a.si / b.si, 1.0);
93                              if (dir.isLeft() && rightLane)
94                              {
95                                  // leader in left lane wants to change right, so we change right
96                                  dRightYes = dRightYes > d ? dRightYes : d;
97                              }
98                              else if (leftLane)
99                              {
100                                 // leader in right lane wants to change left, so we change left
101                                 dLeftYes = dLeftYes > d ? dLeftYes : d;
102                             }
103                         }
104                     }
105                 }
106             }
107             // consider close followers on 2 lanes away
108             Iterable<HeadwayGTU> followers = neighbors.getFollowers(new RelativeLane(dir, 2));
109             if (followers != null)
110             {
111                 for (HeadwayGTU follower : followers)
112                 {
113                     Parameters params = follower.getParameters();
114                     double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
115                     Acceleration a = follower.getDistance().lt0() ? b.neg()
116                             : LmrsUtil.singleAcceleration(follower.getDistance(), follower.getSpeed(), ownSpeed, desire, params,
117                                     follower.getSpeedLimitInfo(), follower.getCarFollowingModel());
118                     if (a.lt0())
119                     {
120                         if (desire > 0)
121                         {
122                             double d = desire * Math.min(-a.si / b.si, 1.0);
123                             if (dir.isLeft() && leftLane)
124                             {
125                                 // follower in second left lane wants to change right, so we do not change left
126                                 dLeftNo = dLeftNo > desire ? dLeftNo : d;
127                             }
128                             else if (rightLane)
129                             {
130                                 // follower in second right lane wants to change left, so we do not change right
131                                 dRightNo = dRightNo > desire ? dRightNo : d;
132                             }
133                         }
134                     }
135                     else
136                     {
137                         // ignore further followers
138                         break;
139                     }
140                 }
141             }
142             leaders = neighbors.getLeaders(new RelativeLane(dir, 2));
143             if (leaders != null)
144             {
145                 for (HeadwayGTU leader : leaders)
146                 {
147                     Parameters params = leader.getParameters();
148                     double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
149                     if (desire > 0)
150                     {
151                         Acceleration a = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(), desire,
152                                 params, sli, carFollowingModel);
153                         if (a.lt0())
154                         {
155                             double d = desire * Math.min(-a.si / b.si, 1.0); // (1 - leader.getDistance().si / x0.si) * desire;
156                             if (dir.isLeft() && leftLane)
157                             {
158                                 // leader in second left lane wants to change right, so we do not change left
159                                 dLeftNo = dLeftNo > d ? dLeftNo : d;
160                             }
161                             else if (rightLane)
162                             {
163                                 // leader in second right lane wants to change left, so we do not change right
164                                 dRightNo = dRightNo > d ? dRightNo : d;
165                             }
166                         }
167                     }
168                 }
169             }
170         }
171         // note: noLeft and noRight weighted with 1 always
172         dLeftYes *= socio;
173         dRightYes *= socio;
174         return new Desire(dLeftYes - dLeftNo, dRightYes - dRightNo);
175 
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public final String toString()
181     {
182         return "IncentiveCourtesy";
183     }
184 
185 }