1 package org.opentrafficsim.road.gtu.lane.tactical.following;
2
3 import org.djunits.unit.AccelerationUnit;
4 import org.djunits.value.vdouble.scalar.Acceleration;
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djunits.value.vdouble.scalar.Speed;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.base.parameters.ParameterException;
9 import org.opentrafficsim.base.parameters.Parameters;
10 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11 import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
12 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
13 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14
15
16
17
18
19
20
21
22
23
24 public abstract class AbstractCarFollowingModel implements CarFollowingModel
25 {
26
27
28 private DesiredHeadwayModel desiredHeadwayModel;
29
30
31 private DesiredSpeedModel desiredSpeedModel;
32
33
34
35
36
37 public AbstractCarFollowingModel(final DesiredHeadwayModel desiredHeadwayModel, final DesiredSpeedModel desiredSpeedModel)
38 {
39 this.desiredHeadwayModel = desiredHeadwayModel;
40 this.desiredSpeedModel = desiredSpeedModel;
41 }
42
43
44 @Override
45 public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
46 {
47 Throw.whenNull(parameters, "Parameters may not be null.");
48 Throw.whenNull(speed, "Speed may not be null.");
49 return this.desiredHeadwayModel.desiredHeadway(parameters, speed);
50 }
51
52
53 @Override
54 public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
55 {
56 Throw.whenNull(parameters, "Parameters may not be null.");
57 Throw.whenNull(speedInfo, "Speed limit info may not be null.");
58 return this.desiredSpeedModel.desiredSpeed(parameters, speedInfo);
59 }
60
61
62 @Override
63 public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
64 final SpeedLimitInfo speedLimitInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
65 {
66 Throw.whenNull(parameters, "Parameters may not be null.");
67 Throw.whenNull(speed, "Speed may not be null.");
68 Throw.whenNull(speedLimitInfo, "Speed limit info may not be null.");
69 Throw.whenNull(leaders, "Leaders may not be null.");
70
71 if (!leaders.isEmpty() && leaders.first().getDistance().si <= 0)
72 {
73 return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
74 }
75
76 Acceleration acc = followingAcceleration(parameters, speed, desiredSpeed(parameters, speedLimitInfo),
77 desiredHeadway(parameters, speed), leaders);
78 return acc;
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 protected abstract Acceleration followingAcceleration(Parameters parameters, Speed speed, Speed desiredSpeed,
93 Length desiredHeadway, PerceptionIterable<? extends Headway> leaders) throws ParameterException;
94
95
96 @SuppressWarnings("checkstyle:designforextension")
97 @Override
98 public String toString()
99 {
100 return getLongName();
101 }
102
103
104 @Override
105 public final void init(final LaneBasedGTU gtu)
106 {
107 if (this.desiredHeadwayModel instanceof Initialisable)
108 {
109 ((Initialisable) this.desiredHeadwayModel).init(gtu);
110 }
111 if (this.desiredSpeedModel instanceof Initialisable)
112 {
113 ((Initialisable) this.desiredSpeedModel).init(gtu);
114 }
115 }
116
117 }