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 getPerception().addPerceptionCategory(new DefaultSimplePerception(getPerception()));
55 }
56
57
58 @Override
59 public final OperationalPlan generateOperationalPlan(final Time startTime, final DirectedPoint locationAtStartTime)
60 throws OperationalPlanException, NetworkException, GTUException, ParameterException
61 {
62
63 LaneBasedGTU laneBasedGTU = getGtu();
64 LanePerception perception = getPerception();
65
66
67 if (laneBasedGTU.getMaximumSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
68 {
69 return new OperationalPlan(getGtu(), locationAtStartTime, startTime, new Duration(1.0, TimeUnit.SECOND));
70 }
71
72
73 perception.perceive();
74
75
76 Length maxDistance = laneBasedGTU.getBehavioralCharacteristics().getParameter(ParameterTypes.LOOKAHEAD);
77 LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);
78
79
80 Headway headwayGTU = perception.getPerceptionCategory(DefaultSimplePerception.class).getForwardHeadwayGTU();
81 AccelerationStep accelerationStepGTU = null;
82 if (headwayGTU.getDistance().ge(maxDistance))
83 {
84
85 accelerationStepGTU = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
86 laneBasedGTU, lanePathInfo.getPath().getLength(),
87 perception.getPerceptionCategory(DefaultSimplePerception.class).getSpeedLimit());
88 }
89 else
90 {
91 accelerationStepGTU = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
92 headwayGTU.getSpeed(), headwayGTU.getDistance(), lanePathInfo.getPath().getLength(),
93 perception.getPerceptionCategory(DefaultSimplePerception.class).getSpeedLimit());
94 }
95
96
97 Headway headwayObject = perception.getPerceptionCategory(DefaultSimplePerception.class).getForwardHeadwayObject();
98 AccelerationStep accelerationStepObject = null;
99 if (headwayObject.getDistance().ge(maxDistance))
100 {
101 accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
102 laneBasedGTU, lanePathInfo.getPath().getLength(),
103 perception.getPerceptionCategory(DefaultSimplePerception.class).getSpeedLimit());
104 }
105 else
106 {
107 accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
108 headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.getPath().getLength(),
109 perception.getPerceptionCategory(DefaultSimplePerception.class).getSpeedLimit());
110 }
111
112
113 AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
114 ? accelerationStepGTU : accelerationStepObject;
115
116
117 if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
118 {
119 return new OperationalPlan(getGtu(), locationAtStartTime, startTime, accelerationStep.getDuration());
120 }
121
122 List<Segment> operationalPlanSegmentList = new ArrayList<>();
123 if (accelerationStep.getAcceleration().si == 0.0)
124 {
125 Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
126 operationalPlanSegmentList.add(segment);
127 }
128 else
129 {
130 Segment segment =
131 new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(), accelerationStep.getAcceleration());
132 operationalPlanSegmentList.add(segment);
133 }
134 OperationalPlan op = new OperationalPlan(getGtu(), lanePathInfo.getPath(), startTime, getGtu().getSpeed(),
135 operationalPlanSegmentList);
136 return op;
137 }
138
139
140 @Override
141 public final String toString()
142 {
143 return "LaneBasedGTUFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
144 }
145 }