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.InfrastructureLaneChangeInfo;
16 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
17 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
18 import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
19 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
20 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
21 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class IncentiveRoute implements MandatoryIncentive
37 {
38
39
40 protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
41
42
43 public static final ParameterTypeDuration T0 = ParameterTypes.T0;
44
45
46 @Override
47 public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
48 final CarFollowingModel carFollowingModel, final Desire mandatoryDesire)
49 throws ParameterException, OperationalPlanException
50 {
51 Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
52 InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
53
54
55 SortedSet<InfrastructureLaneChangeInfo> currentInfo = infra.getInfrastructureLaneChangeInfo(RelativeLane.CURRENT);
56 Length currentFirst = currentInfo.isEmpty() || currentInfo.first().getRequiredNumberOfLaneChanges() == 0
57 ? Length.POSITIVE_INFINITY : currentInfo.first().getRemainingDistance();
58 double dCurr = getDesireToLeave(parameters, infra, RelativeLane.CURRENT, speed);
59 double dLeft = 0;
60 if (perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT)
61 && infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).neg().lt(currentFirst))
62 {
63
64 dLeft = getDesireToLeave(parameters, infra, RelativeLane.LEFT, speed);
65
66 dLeft = dLeft < dCurr ? dCurr : dLeft > dCurr ? -dLeft : 0;
67 }
68 double dRigh = 0;
69 if (perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT) && infra
70 .getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).neg().lt(currentFirst))
71 {
72
73 dRigh = getDesireToLeave(parameters, infra, RelativeLane.RIGHT, speed);
74
75 dRigh = dRigh < dCurr ? dCurr : dRigh > dCurr ? -dRigh : 0;
76 }
77 return new Desire(dLeft, dRigh);
78 }
79
80
81
82
83
84
85
86
87
88
89
90 private static double getDesireToLeave(final Parameters params, final InfrastructurePerception infra,
91 final RelativeLane lane, final Speed speed) throws ParameterException, OperationalPlanException
92 {
93 double dOut = 0.0;
94 if (infra.getCrossSection().contains(lane))
95 {
96 for (InfrastructureLaneChangeInfo info : infra.getInfrastructureLaneChangeInfo(lane))
97 {
98 double d = getDesireToLeave(params, info.getRemainingDistance(), info.getRequiredNumberOfLaneChanges(), 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 }