1 package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2
3 import java.util.LinkedHashSet;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.djunits.value.vdouble.scalar.Time;
9 import org.opentrafficsim.core.gtu.GTUException;
10 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
11 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
12 import org.opentrafficsim.core.gtu.perception.EgoPerception;
13 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
14 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
15 import org.opentrafficsim.core.network.NetworkException;
16 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
17 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
18 import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
19 import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
20 import org.opentrafficsim.road.gtu.lane.perception.categories.IntersectionPerception;
21 import org.opentrafficsim.road.gtu.lane.perception.categories.NeighborsPerception;
22 import org.opentrafficsim.road.gtu.lane.plan.operational.LaneOperationalPlanBuilder.LaneChange;
23 import org.opentrafficsim.road.gtu.lane.plan.operational.SimpleOperationalPlan;
24 import org.opentrafficsim.road.gtu.lane.tactical.AbstractLaneBasedTacticalPlanner;
25 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
26 import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil;
27 import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil.ConflictPlans;
28 import org.opentrafficsim.road.gtu.lane.tactical.util.SpeedLimitUtil;
29 import org.opentrafficsim.road.gtu.lane.tactical.util.TrafficLightUtil;
30 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsUtil;
31 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsUtil.LmrsData;
32 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive;
33 import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
34 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
35 import org.opentrafficsim.road.network.speed.SpeedLimitProspect;
36
37 import nl.tudelft.simulation.language.d3.DirectedPoint;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class LMRS extends AbstractLaneBasedTacticalPlanner
54 {
55
56
57 private static final long serialVersionUID = 20160300L;
58
59
60 private final ConflictPlans yieldPlans = new ConflictPlans();
61
62
63 private final LaneChange laneChange = new LaneChange();
64
65
66 private final LmrsData lmrsData = new LmrsData();
67
68
69 private final LinkedHashSet<MandatoryIncentive> mandatoryIncentives = new LinkedHashSet<>();
70
71
72 private final LinkedHashSet<VoluntaryIncentive> voluntaryIncentives = new LinkedHashSet<>();
73
74
75
76
77
78
79 public LMRS(final CarFollowingModel carFollowingModel, final LaneBasedGTU gtu)
80 {
81 super(carFollowingModel, gtu);
82 getPerception().addPerceptionCategory(new DefaultSimplePerception(getPerception()));
83 getPerception().addPerceptionCategory(new InfrastructurePerception(getPerception()));
84 getPerception().addPerceptionCategory(new NeighborsPerception(getPerception()));
85 getPerception().addPerceptionCategory(new IntersectionPerception(getPerception()));
86 getPerception().addPerceptionCategory(new EgoPerception(getPerception()));
87 }
88
89
90
91
92
93 public final void addMandatoryIncentive(final MandatoryIncentive incentive)
94 {
95 if (incentive != null)
96 {
97 this.mandatoryIncentives.add(incentive);
98 }
99 }
100
101
102
103
104
105 public final void addVoluntaryIncentive(final VoluntaryIncentive incentive)
106 {
107 if (incentive != null)
108 {
109 this.voluntaryIncentives.add(incentive);
110 }
111 }
112
113
114
115
116 public final void setDefaultIncentives()
117 {
118 this.mandatoryIncentives.clear();
119 this.voluntaryIncentives.clear();
120 this.mandatoryIncentives.add(new IncentiveRoute());
121 this.voluntaryIncentives.add(new IncentiveSpeedWithCourtesy());
122 this.voluntaryIncentives.add(new IncentiveKeep());
123 }
124
125
126 @Override
127 public final OperationalPlan generateOperationalPlan(final Time startTime, final DirectedPoint locationAtStartTime)
128 throws OperationalPlanException, GTUException, NetworkException, ParameterException
129 {
130
131 if (getGtu().getId().equals("WWP.LANE:2") && startTime.si > 0)
132 {
133 double q = 8;
134 }
135
136
137 getPerception().perceive();
138 SpeedLimitProspect slp = getPerception().getPerceptionCategory(InfrastructurePerception.class)
139 .getSpeedLimitProspect(RelativeLane.CURRENT);
140 SpeedLimitInfo sli = slp.getSpeedLimitInfo(Length.ZERO);
141 BehavioralCharacteristics bc = getGtu().getBehavioralCharacteristics();
142
143
144 SimpleOperationalPlan simplePlan = LmrsUtil.determinePlan(getGtu(), startTime, getCarFollowingModel(), this.laneChange,
145 this.lmrsData, getPerception(), this.mandatoryIncentives, this.voluntaryIncentives);
146
147
148 Speed speed = getPerception().getPerceptionCategory(EgoPerception.class).getSpeed();
149 simplePlan.minimumAcceleration(SpeedLimitUtil.considerSpeedLimitTransitions(bc, speed, slp, getCarFollowingModel()));
150
151
152
153 simplePlan.minimumAcceleration(TrafficLightUtil.respondToTrafficLights(bc,
154 getPerception().getPerceptionCategory(IntersectionPerception.class).getTrafficLights(RelativeLane.CURRENT),
155 getCarFollowingModel(), speed, sli));
156
157
158 Acceleration acceleration = getPerception().getPerceptionCategory(EgoPerception.class).getAcceleration();
159 simplePlan.minimumAcceleration(ConflictUtil.approachConflicts(bc,
160 getPerception().getPerceptionCategory(IntersectionPerception.class).getConflicts(RelativeLane.CURRENT),
161 getPerception().getPerceptionCategory(NeighborsPerception.class).getLeaders(RelativeLane.CURRENT),
162 getCarFollowingModel(), getGtu().getLength(), speed, acceleration, sli, this.yieldPlans));
163
164
165 return buildPlanFromSimplePlan(getGtu(), startTime, bc, simplePlan, this.laneChange);
166
167 }
168
169
170 @Override
171 public final String toString()
172 {
173 String mandatory;
174 mandatory = "mandatoryIncentives=" + this.mandatoryIncentives + ", ";
175 String voluntary;
176 if (!this.voluntaryIncentives.isEmpty())
177 {
178 voluntary = "voluntaryIncentives=" + this.voluntaryIncentives;
179 }
180 else
181 {
182 voluntary = "voluntaryIncentives=[]";
183 }
184 return "LMRS [" + mandatory + voluntary + "]";
185 }
186
187 }