View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import java.util.SortedSet;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.value.vdouble.scalar.Acceleration;
7   import org.djunits.value.vdouble.scalar.Dimensionless;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.opentrafficsim.core.gtu.GTUException;
11  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
12  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
13  import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterTypes;
14  import org.opentrafficsim.core.gtu.perception.EgoPerception;
15  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
16  import org.opentrafficsim.core.network.LateralDirectionality;
17  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
18  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
19  import org.opentrafficsim.road.gtu.lane.perception.categories.DirectIntersectionPerception;
20  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
21  import org.opentrafficsim.road.gtu.lane.perception.categories.IntersectionPerception;
22  import org.opentrafficsim.road.gtu.lane.perception.categories.NeighborsPerception;
23  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
24  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayConflict;
25  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
26  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUSimple;
27  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
28  import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil;
29  import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil.ConflictPlans;
30  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
31  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
32  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
33  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
34  
35  /**
36   * Determines lane change desire for speed. The anticipation speed in the current and adjacent lanes are compared. The larger
37   * the difference, the larger the lane change desire. For negative differences, negative desire results. Anticipation speed
38   * involves the the most critical vehicle considered to be in a lane. Vehicles are more critical if their speed is lower, and if
39   * they are closer. The set of vehicles considered to be on a lane includes drivers on adjacent lanes of the considered lane,
40   * with a lane change desire towards the considered lane above a certain certain threshold. If such vehicles have low speeds
41   * (i.e. vehicle accelerating to merge), this may result in a courtesy lane change, or in not changing lane out of courtesy from
42   * the 2nd lane of the mainline. Vehicle on the current lane of the driver, are not considered on adjacent lanes. This would
43   * maintain a large speed difference between the lanes where all drivers do not change lane as they consider leading vehicles to
44   * be on the adjacent lane, lowering the anticipation speed on the adjacent lane. The desire for speed is reduced as
45   * acceleration is larger, preventing over-assertive lane changes as acceleration out of congestion in the adjacent lane has
46   * progressed more.<br>
47   * <br>
48   * <b>Note:</b> This incentive includes speed, and a form of courtesy. It should therefore not be combined with incentives
49   * solely for speed, or solely for courtesy.
50   * <p>
51   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
52   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
53   * <p>
54   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016 <br>
55   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
56   */
57  public class IncentiveSpeedWithCourtesy implements VoluntaryIncentive
58  {
59  
60      /** {@inheritDoc} */
61      @Override
62      public final Desire determineDesire(final BehavioralCharacteristics behavioralCharacteristics,
63              final LanePerception perception, final CarFollowingModel carFollowingModel, final Desire mandatoryDesire,
64              final Desire voluntaryDesire) throws ParameterException, OperationalPlanException
65      {
66  
67          // zero if no lane change is possible
68          double leftDist = perception.getPerceptionCategory(InfrastructurePerception.class)
69                  .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).si;
70          double rightDist = perception.getPerceptionCategory(InfrastructurePerception.class)
71                  .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).si;
72  
73          // gather some info
74          Speed vCur = anticipationSpeed(RelativeLane.CURRENT, behavioralCharacteristics, perception, carFollowingModel);
75          Speed vGain = behavioralCharacteristics.getParameter(LmrsParameters.VGAIN);
76  
77          // calculate aGain (default 1; lower as acceleration is higher than 0)
78          Dimensionless aGain;
79          /*
80           * Instead of instantaneous car-following acceleration, use current acceleration; only then is the acceleration factor
81           * consistent with possible lane change incentives pertaining to speed (which used to be only vehicles in the original
82           * LMRS, but can be any number of reason here. E.g. traffic lights, conflicts, etc.)
83           */
84          Acceleration aCur = perception.getPerceptionCategory(EgoPerception.class).getAcceleration();
85          if (aCur.si > 0)
86          {
87              Acceleration a = behavioralCharacteristics.getParameter(ParameterTypes.A);
88              aGain = a.minus(aCur).divideBy(a);
89          }
90          else
91          {
92              aGain = new Dimensionless(1, DimensionlessUnit.SI);
93          }
94  
95          // left desire
96          Dimensionless dLeft;
97          if (leftDist > 0.0 && perception.getPerceptionCategory(InfrastructurePerception.class).getCrossSection()
98                  .contains(RelativeLane.LEFT))
99          {
100             Speed vLeft = anticipationSpeed(RelativeLane.LEFT, behavioralCharacteristics, perception, carFollowingModel);
101             dLeft = aGain.multiplyBy(vLeft.minus(vCur)).divideBy(vGain);
102         }
103         else
104         {
105             dLeft = Dimensionless.ZERO;
106         }
107 
108         // right desire
109         Dimensionless dRight;
110         if (rightDist > 0.0 && perception.getPerceptionCategory(InfrastructurePerception.class).getCrossSection()
111                 .contains(RelativeLane.RIGHT))
112         {
113             Speed vRight = anticipationSpeed(RelativeLane.RIGHT, behavioralCharacteristics, perception, carFollowingModel);
114             dRight = aGain.multiplyBy(vRight.minus(vCur)).divideBy(vGain);
115         }
116         else
117         {
118             dRight = Dimensionless.ZERO;
119         }
120 
121         // return desire
122         return new Desire(dLeft, dRight);
123     }
124 
125     /**
126      * Determine the anticipation speed on the given lane. This depends on leading vehicles, leading vehicles in adjacent lanes
127      * with their indicator to this lane, and conflicts.
128      * @param lane lane to anticipate the speed on
129      * @param bc behavioral characteristics
130      * @param perception perception
131      * @param cfm car-following model, used for the desired speed
132      * @return anticipation speed on lane
133      * @throws ParameterException if a parameter is not defined
134      * @throws OperationalPlanException perception exception
135      */
136     private Speed anticipationSpeed(final RelativeLane lane, final BehavioralCharacteristics bc,
137             final LanePerception perception, final CarFollowingModel cfm) throws ParameterException, OperationalPlanException
138     {
139 
140         SpeedLimitInfo sli = perception.getPerceptionCategory(InfrastructurePerception.class).getSpeedLimitProspect(lane)
141                 .getSpeedLimitInfo(Length.ZERO);
142         Speed anticipationSpeed = cfm.desiredSpeed(bc, sli);
143         Speed desiredSpeed = new Speed(anticipationSpeed);
144         Length x0 = bc.getParameter(ParameterTypes.LOOKAHEAD);
145 
146         // leaders with right indicators on left lane of considered lane
147         if (perception.getPerceptionCategory(InfrastructurePerception.class).getCrossSection().contains(lane.getLeft()))
148         {
149             for (HeadwayGTU headwayGTU : perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(lane.getLeft()))
150             {
151                 // leaders on the current lane with indicator to an adjacent lane are not considered
152                 if (headwayGTU.isRightTurnIndicatorOn() && !lane.getLeft().equals(RelativeLane.CURRENT))
153                 {
154                     anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayGTU);
155                 }
156             }
157         }
158 
159         // leaders with left indicators on right lane of considered lane
160         if (perception.getPerceptionCategory(InfrastructurePerception.class).getCrossSection().contains(lane.getRight()))
161         {
162             for (HeadwayGTU headwayGTU : perception.getPerceptionCategory(NeighborsPerception.class)
163                     .getLeaders(lane.getRight()))
164             {
165                 // leaders on the current lane with indicator to an adjacent lane are not considered
166                 if (headwayGTU.isLeftTurnIndicatorOn() && !lane.getRight().equals(RelativeLane.CURRENT))
167                 {
168                     anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayGTU);
169                 }
170             }
171         }
172 
173         // leaders in the considered lane
174         for (HeadwayGTU headwayGTU : perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(lane))
175         {
176             anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayGTU);
177         }
178 
179         // conflicts in the considered lane
180         if (perception.contains(DirectIntersectionPerception.class))
181         {
182             for (HeadwayConflict headwayConflict : perception.getPerceptionCategory(IntersectionPerception.class)
183                     .getConflicts(lane))
184             {
185                 Length vehicleLength = perception.getPerceptionCategory(EgoPerception.class).getLength();
186                 Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
187                 SortedSet<HeadwayGTU> leaders = perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(lane);
188                 if (!headwayConflict.getConflictType().isCrossing())
189                 {
190                     // consider first downstream vehicle on split or merge (ignore others)
191                     SortedSet<HeadwayGTU> conflictVehicles = headwayConflict.getDownstreamConflictingGTUs();
192                     if (!conflictVehicles.isEmpty() && conflictVehicles.first().isParallel())
193                     {
194                         HeadwayGTU conflictingGtu = conflictVehicles.first();
195                         Length distance = headwayConflict.getDistance().plus(headwayConflict.getLength())
196                                 .plus(conflictingGtu.getOverlapRear());
197                         HeadwayGTU leadingGtu;
198                         try
199                         {
200                             leadingGtu = new HeadwayGTUSimple(conflictingGtu.getId(), conflictingGtu.getGtuType(), distance,
201                                     conflictingGtu.getLength());
202                         }
203                         catch (GTUException exception)
204                         {
205                             throw new OperationalPlanException("Could not create HeadwayGTUSimple for GTU on split.",
206                                     exception);
207                         }
208                         anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, leadingGtu);
209                     }
210                 }
211                 switch (headwayConflict.getConflictPriority())
212                 {
213                     case SPLIT:
214                     {
215                         // nothing
216                         break;
217                     }
218                     case PRIORITY:
219                     {
220                         if (ConflictUtil.stopForPriorityConflict(headwayConflict, leaders, speed, vehicleLength, bc,
221                                 new ConflictPlans()))
222                         {
223                             anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayConflict);
224                         }
225                         break;
226                     }
227                     case GIVE_WAY:
228                     {
229                         Acceleration acceleration = perception.getPerceptionCategory(EgoPerception.class).getAcceleration();
230                         if (ConflictUtil.stopForGiveWayConflict(headwayConflict, leaders, speed, acceleration, vehicleLength,
231                                 bc, sli, cfm))
232                         {
233                             anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayConflict);
234                         }
235                         break;
236                     }
237                     case STOP:
238                     {
239                         Acceleration acceleration = perception.getPerceptionCategory(EgoPerception.class).getAcceleration();
240                         if (ConflictUtil.stopForStopConflict(headwayConflict, leaders, speed, acceleration, vehicleLength, bc,
241                                 sli, cfm))
242                         {
243                             anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayConflict);
244                         }
245                         break;
246                     }
247                     case ALL_STOP:
248                     {
249                         if (ConflictUtil.stopForAllStopConflict(headwayConflict, new ConflictPlans()))
250                         {
251                             anticipationSpeed = anticipateSingle(anticipationSpeed, desiredSpeed, x0, headwayConflict);
252                         }
253                         break;
254                     }
255                     default:
256                         throw new RuntimeException(
257                                 "Conflict priority " + headwayConflict.getConflictPriority() + " is unknown.");
258                 }
259             }
260         }
261 
262         return anticipationSpeed;
263     }
264 
265     /**
266      * Anticipate a single leader by possibly lowering the anticipation speed.
267      * @param anticipationSpeed anticipation speed
268      * @param desiredSpeed desired speed on anticipated lane
269      * @param x0 look-ahead distance
270      * @param headway leader/object to anticipate
271      * @return possibly lowered anticipation speed
272      */
273     private Speed anticipateSingle(final Speed anticipationSpeed, final Speed desiredSpeed, final Length x0,
274             final Headway headway)
275     {
276         Speed speed = headway.getSpeed() == null ? Speed.ZERO : headway.getSpeed();
277         if (speed.gt(anticipationSpeed) || headway.getDistance().gt(x0))
278         {
279             return anticipationSpeed;
280         }
281         Speed vSingle = Speed.interpolate(speed, desiredSpeed, headway.getDistance().si / x0.si);
282         return anticipationSpeed.lt(vSingle) ? anticipationSpeed : vSingle;
283     }
284 
285     /** {@inheritDoc} */
286     @Override
287     public final String toString()
288     {
289         return "IncentiveSpeedWithCourtesy";
290     }
291 
292 }