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