1 package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2
3 import java.util.SortedSet;
4
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djunits.value.vdouble.scalar.Speed;
7 import org.opentrafficsim.base.parameters.ParameterException;
8 import org.opentrafficsim.base.parameters.ParameterTypeDuration;
9 import org.opentrafficsim.base.parameters.ParameterTypeLength;
10 import org.opentrafficsim.base.parameters.ParameterTypes;
11 import org.opentrafficsim.base.parameters.Parameters;
12 import org.opentrafficsim.core.gtu.perception.EgoPerception;
13 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
14 import org.opentrafficsim.core.network.LateralDirectionality;
15 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
16 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
17 import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
18 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
19 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
20 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive;
21 import org.opentrafficsim.road.network.LaneChangeInfo;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class IncentiveRoute implements MandatoryIncentive
36 {
37
38
39 protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
40
41
42 public static final ParameterTypeDuration T0 = ParameterTypes.T0;
43
44
45 @Override
46 public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
47 final CarFollowingModel carFollowingModel, final Desire mandatoryDesire)
48 throws ParameterException, OperationalPlanException
49 {
50 Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
51 InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
52
53
54 SortedSet<LaneChangeInfo> currentInfo = infra.getLegalLaneChangeInfo(RelativeLane.CURRENT);
55 Length currentFirst = currentInfo.isEmpty() || currentInfo.first().numberOfLaneChanges() == 0 ? Length.POSITIVE_INFINITY
56 : currentInfo.first().remainingDistance();
57 double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
58 double dLeft = 0;
59 if (perception.getLaneStructure().exists(RelativeLane.LEFT)
60 && infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).neg().lt(currentFirst))
61 {
62
63 dLeft = getDesireToLeave(parameters, infra, RelativeLane.LEFT, speed);
64
65 dLeft = dLeft < dCurr ? dCurr : dLeft > dCurr ? -dLeft : 0;
66 }
67 double dRigh = 0;
68 if (perception.getLaneStructure().exists(RelativeLane.RIGHT) && infra
69 .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).neg().lt(currentFirst))
70 {
71
72 dRigh = getDesireToLeave(parameters, infra, RelativeLane.RIGHT, speed);
73
74 dRigh = dRigh < dCurr ? dCurr : dRigh > dCurr ? -dRigh : 0;
75 }
76 return new Desire(dLeft, dRigh);
77 }
78
79
80
81
82
83
84
85
86
87
88
89 private static double getDesireToLeave(final Parameters params, final InfrastructurePerception infra,
90 final RelativeLane lane, final Speed speed) throws ParameterException, OperationalPlanException
91 {
92 double dOut = 0.0;
93 if (infra.getCrossSection().contains(lane))
94 {
95 for (LaneChangeInfo info : infra.getLegalLaneChangeInfo(lane))
96 {
97 double d = info.remainingDistance().lt0() ? info.numberOfLaneChanges()
98 : getDesireToLeave(params, info.remainingDistance(), info.numberOfLaneChanges(), speed);
99 dOut = d > dOut ? d : dOut;
100 }
101 }
102 return dOut;
103 }
104
105
106
107
108
109
110
111
112
113
114 public static double getDesireToLeave(final Parameters params, final Length x, final int n, final Speed v)
115 throws ParameterException
116 {
117 double d1 = 1 - x.si / (n * params.getParameter(LOOKAHEAD).si);
118 double d2 = 1 - (x.si / v.si) / (n * params.getParameter(T0).si);
119 d1 = d2 > d1 ? d2 : d1;
120 return d1 < 0 ? 0 : d1;
121 }
122
123
124 @Override
125 public final String toString()
126 {
127 return "IncentiveRoute";
128 }
129
130 }