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 @Override
45 public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
46 final CarFollowingModel carFollowingModel, final Desire mandatoryDesire)
47 throws ParameterException, OperationalPlanException
48 {
49 Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
50 InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
51
52
53 SortedSet<LaneChangeInfo> currentInfo = infra.getLegalLaneChangeInfo(RelativeLane.CURRENT);
54 Length currentFirst = currentInfo.isEmpty() || currentInfo.first().numberOfLaneChanges() == 0 ? Length.POSITIVE_INFINITY
55 : currentInfo.first().remainingDistance();
56 double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
57 double dLeft = 0;
58 if (perception.getLaneStructure().exists(RelativeLane.LEFT)
59 && infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).neg().lt(currentFirst))
60 {
61
62 dLeft = getDesireToLeave(parameters, infra, RelativeLane.LEFT, speed);
63
64 dLeft = dLeft < dCurr ? dCurr : dLeft > dCurr ? -dLeft : 0;
65 }
66 double dRigh = 0;
67 if (perception.getLaneStructure().exists(RelativeLane.RIGHT) && infra
68 .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).neg().lt(currentFirst))
69 {
70
71 dRigh = getDesireToLeave(parameters, infra, RelativeLane.RIGHT, speed);
72
73 dRigh = dRigh < dCurr ? dCurr : dRigh > dCurr ? -dRigh : 0;
74 }
75 return new Desire(dLeft, dRigh);
76 }
77
78
79
80
81
82
83
84
85
86
87
88 private static double getDesireToLeave(final Parameters params, final InfrastructurePerception infra,
89 final RelativeLane lane, final Speed speed) throws ParameterException, OperationalPlanException
90 {
91 double dOut = 0.0;
92 if (infra.getCrossSection().contains(lane))
93 {
94 for (LaneChangeInfo info : infra.getLegalLaneChangeInfo(lane))
95 {
96 double d = info.remainingDistance().lt0() ? info.numberOfLaneChanges()
97 : getDesireToLeave(params, info.remainingDistance(), info.numberOfLaneChanges(), speed);
98 dOut = d > dOut ? d : dOut;
99 }
100 }
101 return dOut;
102 }
103
104
105
106
107
108
109
110
111
112
113 public static double getDesireToLeave(final Parameters params, final Length x, final int n, final Speed v)
114 throws ParameterException
115 {
116 double d1 = 1 - x.si / (n * params.getParameter(LOOKAHEAD).si);
117 double d2 = 1 - (x.si / v.si) / (n * params.getParameter(T0).si);
118 d1 = d2 > d1 ? d2 : d1;
119 return d1 < 0 ? 0 : d1;
120 }
121
122 @Override
123 public final String toString()
124 {
125 return "IncentiveRoute";
126 }
127
128 }