1 package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2
3 import java.util.SortedSet;
4
5 import org.djunits.value.vdouble.scalar.Speed;
6 import org.djutils.exceptions.Try;
7 import org.opentrafficsim.base.parameters.ParameterException;
8 import org.opentrafficsim.base.parameters.Parameters;
9 import org.opentrafficsim.core.gtu.GTUType;
10 import org.opentrafficsim.core.gtu.perception.EgoPerception;
11 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
12 import org.opentrafficsim.core.network.NetworkException;
13 import org.opentrafficsim.core.network.route.Route;
14 import org.opentrafficsim.road.gtu.lane.perception.InfrastructureLaneChangeInfo;
15 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
16 import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
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.LmrsParameters;
22 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class IncentiveStayRight implements VoluntaryIncentive
36 {
37
38
39 @Override
40 public Desire determineDesire(final Parameters parameters, final LanePerception perception,
41 final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
42 throws ParameterException, OperationalPlanException
43 {
44 InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
45 LaneStructureRecord root = perception.getLaneStructure().getRootRecord();
46 LaneStructureRecord record = root;
47 RelativeLane lane = RelativeLane.CURRENT;
48 Route route = Try.assign(() -> perception.getGtu().getStrategicalPlanner().getRoute(), "");
49 GTUType gtuType = Try.assign(() -> perception.getGtu().getGTUType(), "");
50 Speed speed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
51
52 while (record.physicalLeft())
53 {
54 lane = lane.getLeft();
55 record = record.getLeft();
56 }
57
58 double curUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane), parameters, speed);
59 while (true)
60 {
61 try
62 {
63 if (!record.physicalRight() || !record.getRight().allowsRoute(route, gtuType))
64 {
65
66 break;
67 }
68 }
69 catch (NetworkException exception)
70 {
71 throw new RuntimeException(exception);
72 }
73 double rightUrgency = urgency(infra.getInfrastructureLaneChangeInfo(lane.getRight()), parameters, speed);
74 if (rightUrgency > curUrgency)
75 {
76
77 break;
78 }
79 lane = lane.getRight();
80 record = record.getRight();
81 curUrgency = rightUrgency;
82 }
83 if (lane.getLateralDirectionality().isRight() && lane.getNumLanes() > 1)
84 {
85
86 return new Desire(root.legalLeft() ? -1.0 : 0.0, parameters.getParameter(LmrsParameters.DSYNC));
87 }
88 if (lane.isRight())
89 {
90
91 return new Desire(root.legalLeft() ? -1.0 : 0.0, 0.0);
92 }
93 return new Desire(0.0, 0.0);
94 }
95
96
97
98
99
100
101
102
103
104 private double urgency(final SortedSet<InfrastructureLaneChangeInfo> laneChangeInfo, final Parameters parameters,
105 final Speed speed) throws ParameterException
106 {
107 double urgency = 0.0;
108 for (InfrastructureLaneChangeInfo info : laneChangeInfo)
109 {
110 double nextUrgency = IncentiveRoute.getDesireToLeave(parameters, info.getRemainingDistance(),
111 info.getRequiredNumberOfLaneChanges(), speed);
112 urgency = urgency > nextUrgency ? urgency : nextUrgency;
113 }
114 return urgency;
115 }
116
117 }