1 package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2
3 import static org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Tailgating.socialPressure;
4
5 import org.djunits.value.vdouble.scalar.Duration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.djutils.exceptions.Try;
9 import org.opentrafficsim.base.parameters.ParameterException;
10 import org.opentrafficsim.base.parameters.ParameterTypeDouble;
11 import org.opentrafficsim.base.parameters.ParameterTypes;
12 import org.opentrafficsim.base.parameters.Parameters;
13 import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
14 import org.opentrafficsim.core.gtu.perception.EgoPerception;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
17 import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
18 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
19 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
20 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
21
22
23
24
25
26
27
28
29
30
31
32
33 public interface Tailgating
34 {
35
36
37 ParameterTypeDouble RHO = new ParameterTypeDouble("rho", "Social pressure", 0.0, ConstraintInterface.UNITINTERVAL);
38
39
40 Tailgating NONE = new Tailgating()
41 {
42
43 @Override
44 public void tailgate(final LanePerception perception, final Parameters parameters)
45 {
46
47 }
48 };
49
50
51 Tailgating RHO_ONLY = new Tailgating()
52 {
53
54 @Override
55 public void tailgate(final LanePerception perception, final Parameters parameters)
56 {
57 PerceptionCollectable<HeadwayGTU, LaneBasedGTU> leaders =
58 perception.getPerceptionCategoryOrNull(NeighborsPerception.class).getLeaders(RelativeLane.CURRENT);
59 if (leaders == null || leaders.isEmpty())
60 {
61 return;
62 }
63 try
64 {
65 Speed speed = perception.getPerceptionCategoryOrNull(EgoPerception.class).getSpeed();
66 Speed vCong = parameters.getParameter(ParameterTypes.VCONG);
67 Length x0 = parameters.getParameter(ParameterTypes.LOOKAHEAD);
68 Speed vGain = parameters.getParameter(LmrsParameters.VGAIN);
69 HeadwayGTU leader = leaders.first();
70 Speed desiredSpeed = Try.assign(() -> perception.getGtu().getDesiredSpeed(), "Could not obtain the GTU.");
71 double rho = socialPressure(speed, vCong, desiredSpeed, leader.getSpeed(), vGain, leader.getDistance(), x0);
72 parameters.setParameter(RHO, rho);
73 }
74 catch (ParameterException exception)
75 {
76 throw new RuntimeException("Could not obtain or set parameter value.", exception);
77 }
78 }
79 };
80
81
82 Tailgating PRESSURE = new Tailgating()
83 {
84
85 @Override
86 public void tailgate(final LanePerception perception, final Parameters parameters)
87 {
88 PerceptionCollectable<HeadwayGTU, LaneBasedGTU> leaders =
89 perception.getPerceptionCategoryOrNull(NeighborsPerception.class).getLeaders(RelativeLane.CURRENT);
90 if (leaders == null || leaders.isEmpty())
91 {
92 return;
93 }
94 try
95 {
96 Speed speed = perception.getPerceptionCategoryOrNull(EgoPerception.class).getSpeed();
97 Speed vCong = parameters.getParameter(ParameterTypes.VCONG);
98 Duration t = parameters.getParameter(ParameterTypes.T);
99 Duration tMin = parameters.getParameter(ParameterTypes.TMIN);
100 Duration tMax = parameters.getParameter(ParameterTypes.TMAX);
101 Length x0 = parameters.getParameter(ParameterTypes.LOOKAHEAD);
102 Speed vGain = parameters.getParameter(LmrsParameters.VGAIN);
103 HeadwayGTU leader = leaders.first();
104 Speed desiredSpeed = Try.assign(() -> perception.getGtu().getDesiredSpeed(), "Could not obtain the GTU.");
105 double rho = socialPressure(speed, vCong, desiredSpeed, leader.getSpeed(), vGain, leader.getDistance(), x0);
106 parameters.setParameter(RHO, rho);
107 double tNew = rho * tMin.si + (1.0 - rho) * tMax.si;
108 if (tNew < t.si)
109 {
110 parameters.setParameter(ParameterTypes.T, Duration.createSI(tNew));
111 }
112 }
113 catch (ParameterException exception)
114 {
115 throw new RuntimeException("Could not obtain or set parameter value.", exception);
116 }
117 }
118 };
119
120
121
122
123
124
125
126
127
128
129
130
131 static double socialPressure(final Speed speed, final Speed vCong, final Speed desiredSpeed, final Speed leaderSpeed,
132 final Speed vGain, final Length headway, final Length x0)
133 {
134 double dv = desiredSpeed.si - leaderSpeed.si;
135 if (dv < 0)
136 {
137 return 0.0;
138 }
139 return 1.0 - Math.exp(-(dv / vGain.si) * (1.0 - (headway.si / x0.si)));
140 }
141
142
143
144
145
146
147 void tailgate(LanePerception perception, Parameters parameters);
148
149 }