1 package org.opentrafficsim.road.gtu.lane.tactical;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import nl.tudelft.simulation.language.d3.DirectedPoint;
7
8 import org.djunits.unit.TimeUnit;
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.djunits.value.vdouble.scalar.Length;
11 import org.djunits.value.vdouble.scalar.Time;
12 import org.opentrafficsim.core.gtu.GTUException;
13 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
14 import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterTypes;
15 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
16 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan.Segment;
17 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
18 import org.opentrafficsim.core.network.NetworkException;
19 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
20 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
21 import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
22 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
23 import org.opentrafficsim.road.gtu.lane.tactical.following.AccelerationStep;
24 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class LaneBasedGTUFollowingTacticalPlanner extends AbstractLaneBasedTacticalPlanner
42 {
43
44 private static final long serialVersionUID = 20151125L;
45
46
47
48
49
50
51 public LaneBasedGTUFollowingTacticalPlanner(final GTUFollowingModelOld carFollowingModel, final LaneBasedGTU gtu)
52 {
53 super(carFollowingModel, gtu);
54 }
55
56
57 @Override
58 public OperationalPlan generateOperationalPlan(final Time startTime, final DirectedPoint locationAtStartTime)
59 throws OperationalPlanException, NetworkException, GTUException, ParameterException
60 {
61
62 LaneBasedGTU laneBasedGTU = getGtu();
63 LanePerception perception = getPerception();
64
65
66 if (laneBasedGTU.getMaximumSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
67 {
68 return new OperationalPlan(getGtu(), locationAtStartTime, startTime, new Duration(1.0, TimeUnit.SECOND));
69 }
70
71
72 perception.perceive();
73
74
75 Length maxDistance = laneBasedGTU.getBehavioralCharacteristics().getParameter(ParameterTypes.LOOKAHEAD);
76 LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);
77
78
79 Headway headway = perception.getPerceptionCategory(DefaultSimplePerception.class).getForwardHeadway();
80 AccelerationStep accelerationStep = null;
81 if (headway.getDistance().ge(maxDistance))
82 {
83
84 accelerationStep =
85 ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(laneBasedGTU,
86 lanePathInfo.getPath().getLength(), perception.getPerceptionCategory(DefaultSimplePerception.class)
87 .getSpeedLimit());
88 }
89 else
90 {
91 accelerationStep =
92 ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headway.getSpeed(),
93 headway.getDistance(), lanePathInfo.getPath().getLength(), perception.getPerceptionCategory(
94 DefaultSimplePerception.class).getSpeedLimit());
95 }
96
97
98 if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
99 {
100 return new OperationalPlan(getGtu(), locationAtStartTime, startTime, accelerationStep.getDuration());
101 }
102
103 List<Segment> operationalPlanSegmentList = new ArrayList<>();
104 if (accelerationStep.getAcceleration().si == 0.0)
105 {
106 Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
107 operationalPlanSegmentList.add(segment);
108 }
109 else
110 {
111 Segment segment =
112 new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(), accelerationStep.getAcceleration());
113 operationalPlanSegmentList.add(segment);
114 }
115 OperationalPlan op =
116 new OperationalPlan(getGtu(), lanePathInfo.getPath(), startTime, getGtu().getSpeed(), operationalPlanSegmentList);
117 return op;
118 }
119
120
121 @Override
122 public final String toString()
123 {
124 return "LaneBasedGTUFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
125 }
126 }