1 package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2
3 import org.djunits.value.vdouble.scalar.Acceleration;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Speed;
6 import org.opentrafficsim.base.parameters.ParameterException;
7 import org.opentrafficsim.base.parameters.ParameterTypeAcceleration;
8 import org.opentrafficsim.base.parameters.ParameterTypeDouble;
9 import org.opentrafficsim.base.parameters.ParameterTypes;
10 import org.opentrafficsim.base.parameters.Parameters;
11 import org.opentrafficsim.core.gtu.perception.EgoPerception;
12 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
13 import org.opentrafficsim.core.network.LateralDirectionality;
14 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
16 import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
17 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
18 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
19 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
20 import org.opentrafficsim.road.gtu.lane.tactical.util.CarFollowingUtil;
21 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
22 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
23 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsUtil;
24 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
25 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class IncentiveCourtesy implements VoluntaryIncentive
42 {
43
44
45 protected static final ParameterTypeAcceleration B = ParameterTypes.B;
46
47
48 protected static final ParameterTypeDouble SOCIO = LmrsParameters.SOCIO;
49
50
51 protected static final ParameterTypeDouble DLEFT = LmrsParameters.DLEFT;
52
53
54 protected static final ParameterTypeDouble DRIGHT = LmrsParameters.DRIGHT;
55
56
57 @Override
58 public final Desire determineDesire(final Parameters parameters, final LanePerception perception,
59 final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
60 throws ParameterException, OperationalPlanException
61 {
62
63 double dLeftYes = 0;
64 double dRightYes = 0;
65 double dLeftNo = 0;
66 double dRightNo = 0;
67 double socio = parameters.getParameter(SOCIO);
68 Acceleration b = parameters.getParameter(B);
69 NeighborsPerception neighbors = perception.getPerceptionCategory(NeighborsPerception.class);
70 Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
71 InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
72 SpeedLimitInfo sli = infra.getSpeedLimitProspect(RelativeLane.CURRENT).getSpeedLimitInfo(Length.ZERO);
73 boolean leftLane = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.LEFT).si > 0.0;
74 boolean rightLane = infra.getLegalLaneChangePossibility(RelativeLane.CURRENT, LateralDirectionality.RIGHT).si > 0.0;
75 for (LateralDirectionality dir : new LateralDirectionality[] { LateralDirectionality.LEFT,
76 LateralDirectionality.RIGHT })
77 {
78 Iterable<HeadwayGTU> leaders = neighbors.getLeaders(new RelativeLane(dir, 1));
79 if (leaders != null)
80 {
81 for (HeadwayGTU leader : leaders)
82 {
83 Parameters params = leader.getParameters();
84 double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
85 if (desire > 0)
86 {
87
88 Acceleration a =
89 CarFollowingUtil.followSingleLeader(carFollowingModel, parameters, ownSpeed, sli, leader);
90 if (a.lt0())
91 {
92 double d = desire * Math.min(-a.si / b.si, 1.0);
93 if (dir.isLeft() && rightLane)
94 {
95
96 dRightYes = dRightYes > d ? dRightYes : d;
97 }
98 else if (leftLane)
99 {
100
101 dLeftYes = dLeftYes > d ? dLeftYes : d;
102 }
103 }
104 }
105 }
106 }
107
108 Iterable<HeadwayGTU> followers = neighbors.getFollowers(new RelativeLane(dir, 2));
109 if (followers != null)
110 {
111 for (HeadwayGTU follower : followers)
112 {
113 Parameters params = follower.getParameters();
114 double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
115 Acceleration a = follower.getDistance().lt0() ? b.neg()
116 : LmrsUtil.singleAcceleration(follower.getDistance(), follower.getSpeed(), ownSpeed, desire, params,
117 follower.getSpeedLimitInfo(), follower.getCarFollowingModel());
118 if (a.lt0())
119 {
120 if (desire > 0)
121 {
122 double d = desire * Math.min(-a.si / b.si, 1.0);
123 if (dir.isLeft() && leftLane)
124 {
125
126 dLeftNo = dLeftNo > desire ? dLeftNo : d;
127 }
128 else if (rightLane)
129 {
130
131 dRightNo = dRightNo > desire ? dRightNo : d;
132 }
133 }
134 }
135 else
136 {
137
138 break;
139 }
140 }
141 }
142 leaders = neighbors.getLeaders(new RelativeLane(dir, 2));
143 if (leaders != null)
144 {
145 for (HeadwayGTU leader : leaders)
146 {
147 Parameters params = leader.getParameters();
148 double desire = dir.isLeft() ? params.getParameter(DRIGHT) : params.getParameter(DLEFT);
149 if (desire > 0)
150 {
151 Acceleration a = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(), desire,
152 params, sli, carFollowingModel);
153 if (a.lt0())
154 {
155 double d = desire * Math.min(-a.si / b.si, 1.0);
156 if (dir.isLeft() && leftLane)
157 {
158
159 dLeftNo = dLeftNo > d ? dLeftNo : d;
160 }
161 else if (rightLane)
162 {
163
164 dRightNo = dRightNo > d ? dRightNo : d;
165 }
166 }
167 }
168 }
169 }
170 }
171
172 dLeftYes *= socio;
173 dRightYes *= socio;
174 return new Desire(dLeftYes - dLeftNo, dRightYes - dRightNo);
175
176 }
177
178
179 @Override
180 public final String toString()
181 {
182 return "IncentiveCourtesy";
183 }
184
185 }