1 package org.opentrafficsim.road.gtu.lane.tactical;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.djunits.unit.DurationUnit;
7 import org.djunits.value.vdouble.scalar.Duration;
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djunits.value.vdouble.scalar.Time;
10 import org.opentrafficsim.base.parameters.ParameterException;
11 import org.opentrafficsim.core.geometry.DirectedPoint;
12 import org.opentrafficsim.core.gtu.GTUException;
13 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
14 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan.Segment;
15 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
16 import org.opentrafficsim.core.network.NetworkException;
17 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
18 import org.opentrafficsim.road.gtu.lane.perception.CategoricalLanePerception;
19 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
20 import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
21 import org.opentrafficsim.road.gtu.lane.perception.categories.DirectDefaultSimplePerception;
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, new CategoricalLanePerception(gtu));
54 getPerception().addPerceptionCategory(new DirectDefaultSimplePerception(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, DurationUnit.SECOND));
70 }
71
72
73 Length maxDistance = laneBasedGTU.getParameters().getParameter(LOOKAHEAD);
74 LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);
75
76
77 DefaultSimplePerception simplePerception = perception.getPerceptionCategory(DefaultSimplePerception.class);
78 Headway headwayGTU = simplePerception.getForwardHeadwayGTU();
79 AccelerationStep accelerationStepGTU = null;
80 if (headwayGTU.getDistance().ge(maxDistance))
81 {
82
83 accelerationStepGTU = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
84 laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
85 }
86 else
87 {
88 accelerationStepGTU =
89 ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headwayGTU.getSpeed(),
90 headwayGTU.getDistance(), lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
91 }
92
93
94 Headway headwayObject = simplePerception.getForwardHeadwayObject();
95 AccelerationStep accelerationStepObject = null;
96 if (headwayObject.getDistance().ge(maxDistance))
97 {
98 accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
99 laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
100 }
101 else
102 {
103 accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
104 headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.getPath().getLength(),
105 simplePerception.getSpeedLimit());
106 }
107
108
109 AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
110 ? accelerationStepGTU : accelerationStepObject;
111
112
113 if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
114 {
115 return new OperationalPlan(getGtu(), locationAtStartTime, startTime, accelerationStep.getDuration());
116 }
117
118 List<Segment> operationalPlanSegmentList = new ArrayList<>();
119 if (accelerationStep.getAcceleration().si == 0.0)
120 {
121 Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
122 operationalPlanSegmentList.add(segment);
123 }
124 else
125 {
126 Segment segment =
127 new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(), accelerationStep.getAcceleration());
128 operationalPlanSegmentList.add(segment);
129 }
130 OperationalPlan op = new OperationalPlan(getGtu(), lanePathInfo.getPath(), startTime, getGtu().getSpeed(),
131 operationalPlanSegmentList);
132 return op;
133 }
134
135
136 @Override
137 public final String toString()
138 {
139 return "LaneBasedGTUFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
140 }
141 }