View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil;
2   
3   import java.util.Collection;
4   import java.util.Map;
5   
6   import org.djunits.unit.AccelerationUnit;
7   import org.djunits.value.vdouble.scalar.Acceleration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
11  import org.opentrafficsim.base.parameters.ParameterException;
12  import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
13  import org.opentrafficsim.base.parameters.ParameterTypeLength;
14  import org.opentrafficsim.base.parameters.ParameterTypes;
15  import org.opentrafficsim.core.gtu.GtuException;
16  import org.opentrafficsim.core.gtu.RelativePosition;
17  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
18  import org.opentrafficsim.core.network.LateralDirectionality;
19  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
20  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
21  import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
22  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
23  import org.opentrafficsim.road.gtu.lane.tactical.AbstractLaneBasedTacticalPlanner;
24  import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractGtuFollowingModelMobil;
25  import org.opentrafficsim.road.gtu.lane.tactical.following.DualAccelerationStep;
26  import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
27  import org.opentrafficsim.road.network.lane.Lane;
28  
29  /**
30   * Common code for a family of lane change models like in M. Treiber and A. Kesting <i>Traffic Flow Dynamics</i>,
31   * Springer-Verlag Berlin Heidelberg 2013, pp 239-244.
32   * <p>
33   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
34   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
35   * </p>
36   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
37   */
38  public abstract class AbstractLaneChangeModel implements LaneChangeModel
39  {
40  
41      /** Look ahead parameter type. */
42      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
43  
44      /** Comfortable deceleration parameter type. */
45      protected static final ParameterTypeAcceleration B = ParameterTypes.B;
46  
47      /** Attempt to overcome rounding errors. */
48      private static Acceleration extraThreshold = new Acceleration(0.000001, AccelerationUnit.SI);
49  
50      @SuppressWarnings("checkstyle:parameternumber")
51      @Override
52      public final LaneMovementStep computeLaneChangeAndAcceleration(final LaneBasedGtu gtu,
53              final Collection<Headway> sameLaneGTUs, final Collection<Headway> preferredLaneGTUs,
54              final Collection<Headway> nonPreferredLaneGTUs, final Speed speedLimit,
55              final Acceleration preferredLaneRouteIncentive, final Acceleration laneChangeThreshold,
56              final Acceleration nonPreferredLaneRouteIncentive) throws ParameterException, OperationalPlanException
57      {
58          try
59          {
60              LanePerception perception = gtu.getTacticalPlanner().getPerception();
61              Length headway = gtu.getParameters().getParameter(LOOKAHEAD);
62              Map<Lane, Length> positions = gtu.positions(RelativePosition.REFERENCE_POSITION);
63              Lane lane = positions.keySet().iterator().next();
64              Length longitudinalPosition = positions.get(lane);
65              // TODO make this driving side dependent; i.e. implement a general way to figure out on which side of the
66              // road cars are supposed to drive
67              final LateralDirectionality preferred = LateralDirectionality.RIGHT;
68              final LateralDirectionality nonPreferred = LateralDirectionality.LEFT;
69              DefaultSimplePerception simplePerception = perception.getPerceptionCategory(DefaultSimplePerception.class);
70              simplePerception.updateAccessibleAdjacentLanes();
71              Lane nonPreferredLane = simplePerception.bestAccessibleAdjacentLane(lane, nonPreferred, longitudinalPosition);
72              Lane preferredLane = simplePerception.bestAccessibleAdjacentLane(lane, preferred, longitudinalPosition);
73              AbstractLaneBasedTacticalPlanner albtp = (AbstractLaneBasedTacticalPlanner) gtu.getTacticalPlanner();
74              if (null == albtp)
75              {
76                  throw new NullPointerException(gtu + " returns null for its tactical planner");
77              }
78              GtuFollowingModelOld gtuFollowingModel = (GtuFollowingModelOld) albtp.getCarFollowingModel();
79              if (null == gtuFollowingModel)
80              {
81                  throw new NullPointerException(gtu + " has null GtuFollowingModel");
82              }
83              DualAccelerationStep straightAccelerationSteps =
84                      gtuFollowingModel.computeDualAccelerationStep(gtu, sameLaneGTUs, headway, speedLimit);
85              if (straightAccelerationSteps.getLeaderAcceleration().getSI() < -9999)
86              {
87                  System.out.println("Problem");
88                  gtuFollowingModel.computeDualAccelerationStep(gtu, sameLaneGTUs, headway, speedLimit);
89              }
90              Acceleration straightA = applyDriverPersonality(straightAccelerationSteps).plus(laneChangeThreshold);
91              DualAccelerationStep nonPreferredAccelerationSteps = null == nonPreferredLane ? null
92                      : gtuFollowingModel.computeDualAccelerationStep(gtu, nonPreferredLaneGTUs, headway, speedLimit);
93              if (null != nonPreferredAccelerationSteps && nonPreferredAccelerationSteps.getFollowerAcceleration()
94                      .getSI() < -gtu.getParameters().getParameter(B).getSI())
95              {
96                  nonPreferredAccelerationSteps = AbstractGtuFollowingModelMobil.TOODANGEROUS;
97              }
98              Acceleration nonPreferredA =
99                      null == nonPreferredLane ? null : applyDriverPersonality(nonPreferredAccelerationSteps);
100             DualAccelerationStep preferredAccelerationSteps = null == preferredLane ? null
101                     : gtuFollowingModel.computeDualAccelerationStep(gtu, preferredLaneGTUs, headway, speedLimit);
102             if (null != preferredAccelerationSteps && preferredAccelerationSteps.getFollowerAcceleration()
103                     .getSI() < -gtu.getParameters().getParameter(B).getSI())
104             {
105                 preferredAccelerationSteps = AbstractGtuFollowingModelMobil.TOODANGEROUS;
106             }
107             Acceleration preferredA = null == preferredLane ? null : applyDriverPersonality(preferredAccelerationSteps);
108             if (null == preferredA)
109             {
110                 // Lane change to the preferred lane is not possible
111                 if (null == nonPreferredA)
112                 {
113                     // No lane change possible; this is definitely the easy case
114                     return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
115                 }
116                 else
117                 {
118                     // Merge to nonPreferredLane is possible; merge to preferredLane is NOT possible
119                     if (DoubleScalar.plus(nonPreferredA, nonPreferredLaneRouteIncentive).gt(straightA.plus(extraThreshold)))
120                     {
121                         // Merge to the nonPreferred lane; i.e. start an overtaking procedure
122                         return new LaneMovementStep(nonPreferredAccelerationSteps.getLeaderAccelerationStep(), nonPreferred);
123                     }
124                     else
125                     {
126                         // Stay in the current lane
127                         return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
128                     }
129                 }
130             }
131             // A merge to the preferredLane is possible
132             if (null == nonPreferredA)
133             {
134                 // Merge to preferredLane is possible; merge to nonPreferred lane is NOT possible
135                 if (DoubleScalar.plus(preferredA, preferredLaneRouteIncentive).plus(extraThreshold).ge(straightA))
136                 {
137                     // Merge to the preferred lane; i.e. finish (or cancel) an overtaking procedure
138                     return new LaneMovementStep(preferredAccelerationSteps.getLeaderAccelerationStep(), preferred);
139                 }
140                 else
141                 {
142                     // Stay in current lane
143                     return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
144                 }
145             }
146             // All merges are possible
147             Acceleration preferredAttractiveness =
148                     preferredA.plus(preferredLaneRouteIncentive).minus(straightA).plus(extraThreshold);
149             Acceleration nonPreferredAttractiveness =
150                     nonPreferredA.plus(nonPreferredLaneRouteIncentive).minus(straightA).minus(extraThreshold);
151             if (preferredAttractiveness.getSI() < 0 && nonPreferredAttractiveness.getSI() <= 0)
152             {
153                 // Stay in current lane
154                 return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
155 
156             }
157             if (preferredAttractiveness.getSI() >= 0 && preferredAttractiveness.gt(nonPreferredAttractiveness))
158             {
159                 // Merge to the preferred lane; i.e. finish (or cancel) an overtaking procedure
160                 return new LaneMovementStep(preferredAccelerationSteps.getLeaderAccelerationStep(), preferred);
161             }
162             // Merge to the adjacent nonPreferred lane; i.e. start an overtaking procedure
163             return new LaneMovementStep(nonPreferredAccelerationSteps.getLeaderAccelerationStep(), nonPreferred);
164         }
165         catch (GtuException exception)
166         {
167             throw new RuntimeException(exception);
168         }
169     }
170 
171     /**
172      * Return the weighted acceleration as described by the personality. This incorporates the personality of the driver to the
173      * lane change decisions.
174      * @param accelerationSteps the DualAccelerationStep that contains the AccelerationStep that the reference GTU will make and
175      *            the AccelerationStep that the (new) follower GTU will make
176      * @return the acceleration that the personality of the driver uses (in a comparison to a similarly computed acceleration in
177      *         the non-, or different-lane-changed state) to decide if a lane change should be performed
178      */
179     public abstract Acceleration applyDriverPersonality(DualAccelerationStep accelerationSteps);
180 
181 }