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.DoubleScalar;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.opentrafficsim.core.gtu.GTUException;
12 import org.opentrafficsim.core.gtu.RelativePosition;
13 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
14 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterTypes;
15 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
16 import org.opentrafficsim.core.network.LateralDirectionality;
17 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
18 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
19 import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
20 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
21 import org.opentrafficsim.road.gtu.lane.tactical.AbstractLaneBasedTacticalPlanner;
22 import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractGTUFollowingModelMobil;
23 import org.opentrafficsim.road.gtu.lane.tactical.following.DualAccelerationStep;
24 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
25 import org.opentrafficsim.road.network.lane.Lane;
26
27
28
29
30
31
32
33
34
35
36
37
38 public abstract class AbstractLaneChangeModel implements LaneChangeModel
39 {
40
41 private static Acceleration extraThreshold = new Acceleration(0.000001, AccelerationUnit.SI);
42
43
44 @SuppressWarnings("checkstyle:parameternumber")
45 @Override
46 public final LaneMovementStep computeLaneChangeAndAcceleration(final LaneBasedGTU gtu,
47 final Collection<Headway> sameLaneGTUs, final Collection<Headway> preferredLaneGTUs,
48 final Collection<Headway> nonPreferredLaneGTUs, final Speed speedLimit,
49 final Acceleration preferredLaneRouteIncentive, final Acceleration laneChangeThreshold,
50 final Acceleration nonPreferredLaneRouteIncentive) throws ParameterException, OperationalPlanException
51 {
52 try
53 {
54 LanePerception perception = gtu.getTacticalPlanner().getPerception();
55 Length headway = gtu.getBehavioralCharacteristics().getParameter(ParameterTypes.LOOKAHEAD);
56 Map<Lane, Length> positions = gtu.positions(RelativePosition.REFERENCE_POSITION);
57 Lane lane = positions.keySet().iterator().next();
58 Length longitudinalPosition = positions.get(lane);
59
60
61 final LateralDirectionality preferred = LateralDirectionality.RIGHT;
62 final LateralDirectionality nonPreferred = LateralDirectionality.LEFT;
63 Lane nonPreferredLane =
64 perception.getPerceptionCategory(DefaultSimplePerception.class).bestAccessibleAdjacentLane(lane, nonPreferred,
65 longitudinalPosition);
66 Lane preferredLane =
67 perception.getPerceptionCategory(DefaultSimplePerception.class).bestAccessibleAdjacentLane(lane, preferred,
68 longitudinalPosition);
69 AbstractLaneBasedTacticalPlanner albtp = (AbstractLaneBasedTacticalPlanner) gtu.getTacticalPlanner();
70 if (null == albtp)
71 {
72 throw new Error(gtu + " returns null for its tactical planner");
73 }
74 GTUFollowingModelOld gtuFollowingModel = (GTUFollowingModelOld) albtp.getCarFollowingModel();
75 if (null == gtuFollowingModel)
76 {
77 throw new Error(gtu + " has null GTUFollowingModel");
78 }
79 DualAccelerationStep straightAccelerationSteps =
80 gtuFollowingModel.computeDualAccelerationStep(gtu, sameLaneGTUs, headway, speedLimit);
81 if (straightAccelerationSteps.getLeaderAcceleration().getSI() < -9999)
82 {
83 System.out.println("Problem");
84 gtuFollowingModel.computeDualAccelerationStep(gtu, sameLaneGTUs, headway, speedLimit);
85 }
86 Acceleration straightA = applyDriverPersonality(straightAccelerationSteps).plus(laneChangeThreshold);
87 DualAccelerationStep nonPreferredAccelerationSteps =
88 null == nonPreferredLane ? null : gtuFollowingModel.computeDualAccelerationStep(gtu, nonPreferredLaneGTUs,
89 headway, speedLimit);
90 if (null != nonPreferredAccelerationSteps
91 && nonPreferredAccelerationSteps.getFollowerAcceleration().getSI() < -gtu.getStrategicalPlanner()
92 .getBehavioralCharacteristics().getParameter(ParameterTypes.B).getSI())
93 {
94 nonPreferredAccelerationSteps = AbstractGTUFollowingModelMobil.TOODANGEROUS;
95 }
96 Acceleration nonPreferredA =
97 null == nonPreferredLane ? null : applyDriverPersonality(nonPreferredAccelerationSteps);
98 DualAccelerationStep preferredAccelerationSteps =
99 null == preferredLane ? null : gtuFollowingModel.computeDualAccelerationStep(gtu, preferredLaneGTUs,
100 headway, speedLimit);
101 if (null != preferredAccelerationSteps
102 && preferredAccelerationSteps.getFollowerAcceleration().getSI() < -gtu.getStrategicalPlanner()
103 .getBehavioralCharacteristics().getParameter(ParameterTypes.B).getSI())
104 {
105 preferredAccelerationSteps = AbstractGTUFollowingModelMobil.TOODANGEROUS;
106 }
107 Acceleration preferredA = null == preferredLane ? null : applyDriverPersonality(preferredAccelerationSteps);
108 if (null == preferredA)
109 {
110
111 if (null == nonPreferredA)
112 {
113
114 return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
115 }
116 else
117 {
118
119 if (DoubleScalar.plus(nonPreferredA, nonPreferredLaneRouteIncentive).gt(straightA.plus(extraThreshold)))
120 {
121
122 return new LaneMovementStep(nonPreferredAccelerationSteps.getLeaderAccelerationStep(), nonPreferred);
123 }
124 else
125 {
126
127 return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
128 }
129 }
130 }
131
132 if (null == nonPreferredA)
133 {
134
135 if (DoubleScalar.plus(preferredA, preferredLaneRouteIncentive).plus(extraThreshold).ge(straightA))
136 {
137
138 return new LaneMovementStep(preferredAccelerationSteps.getLeaderAccelerationStep(), preferred);
139 }
140 else
141 {
142
143 return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
144 }
145 }
146
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
154 return new LaneMovementStep(straightAccelerationSteps.getLeaderAccelerationStep(), null);
155
156 }
157 if (preferredAttractiveness.getSI() >= 0 && preferredAttractiveness.gt(nonPreferredAttractiveness))
158 {
159
160 return new LaneMovementStep(preferredAccelerationSteps.getLeaderAccelerationStep(), preferred);
161 }
162
163 return new LaneMovementStep(nonPreferredAccelerationSteps.getLeaderAccelerationStep(), nonPreferred);
164 }
165 catch (GTUException exception)
166 {
167 throw new RuntimeException(exception);
168 }
169 }
170
171
172
173
174
175
176
177
178
179 public abstract Acceleration applyDriverPersonality(DualAccelerationStep accelerationSteps);
180
181 }