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