-
Notifications
You must be signed in to change notification settings - Fork 4
/
MOBIL.java
211 lines (191 loc) · 9.33 KB
/
MOBIL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package simtools.gaml.extensions.traffic.carfollowing;
import static simtools.gaml.extensions.traffic.DrivingSkill.getCurrentRoad;
import static simtools.gaml.extensions.traffic.DrivingSkill.getAccBias;
import static simtools.gaml.extensions.traffic.DrivingSkill.setFollower;
import static simtools.gaml.extensions.traffic.DrivingSkill.getAccGainThreshold;
import static simtools.gaml.extensions.traffic.DrivingSkill.getAllowedLanes;
import static simtools.gaml.extensions.traffic.DrivingSkill.getLCCooldown;
import static simtools.gaml.extensions.traffic.DrivingSkill.getLaneChangeLimit;
import static simtools.gaml.extensions.traffic.DrivingSkill.getLeadingDistance;
import static simtools.gaml.extensions.traffic.DrivingSkill.getLowestLane;
import static simtools.gaml.extensions.traffic.DrivingSkill.getMaxSafeDeceleration;
import static simtools.gaml.extensions.traffic.DrivingSkill.getNumLanesOccupied;
import static simtools.gaml.extensions.traffic.DrivingSkill.getPolitenessFactor;
import static simtools.gaml.extensions.traffic.DrivingSkill.getProbaUseLinkedRoad;
import static simtools.gaml.extensions.traffic.DrivingSkill.getRightSideDriving;
import static simtools.gaml.extensions.traffic.DrivingSkill.getSpeed;
import static simtools.gaml.extensions.traffic.DrivingSkill.getTimeSinceLC;
import static simtools.gaml.extensions.traffic.DrivingSkill.getVehicleLength;
import static simtools.gaml.extensions.traffic.DrivingSkill.setLeadingDistance;
import static simtools.gaml.extensions.traffic.DrivingSkill.setLeadingSpeed;
import static simtools.gaml.extensions.traffic.DrivingSkill.setLeadingVehicle;
import static simtools.gaml.extensions.traffic.DrivingSkill.setTimeSinceLC;
import static simtools.gaml.extensions.traffic.carfollowing.Utils.findFollower;
import static simtools.gaml.extensions.traffic.carfollowing.Utils.findLeader;
import static simtools.gaml.extensions.traffic.carfollowing.Utils.rescaleProba;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.runtime.IScope;
import simtools.gaml.extensions.traffic.DrivingSkill;
import simtools.gaml.extensions.traffic.RoadNodeSkill;
import simtools.gaml.extensions.traffic.RoadSkill;
public class MOBIL {
/**
* Choose a new lane according to the lane change model MOBIL
* (https://traffic-simulation.de/info/info_MOBIL.html).
*
* @param scope
* @param road the road which the vehicle is moving on
* @param segment the index of the current road segment
* @param distToSegmentEnd the distance to the endpoint of the segment
* @return a pair composed of the optimal lowest lane index and
* the resulting acceleration of the vehicle
*/
public static ImmutablePair<Integer, Double> chooseLane(final IScope scope,
final IAgent vehicle,
final IAgent road,
int currentLowestLane) {
double VL = getVehicleLength(vehicle);
int numLanesOccupied = getNumLanesOccupied(vehicle);
// Rescale probabilities based on step duration
double timeStep = scope.getSimulation().getClock().getStepInSeconds();
Double probaUseLinkedRoad = rescaleProba(getProbaUseLinkedRoad(vehicle), timeStep);
int numCurrentLanes = RoadSkill.getNumLanes(road);
int linkedLaneLimit = Utils.computeLinkedLaneLimit(vehicle, road);
List<Integer> allowedLanes = getAllowedLanes(vehicle);
// Determine the lanes which is considered for switching
int laneChangeLimit = getLaneChangeLimit(vehicle);
int lower = 0;
int upper = numCurrentLanes + linkedLaneLimit - numLanesOccupied;
if (laneChangeLimit != -1) {
lower = Math.max(lower, currentLowestLane - laneChangeLimit);
upper = Math.min(upper, currentLowestLane + laneChangeLimit);
}
List<Integer> limitedLaneRange = IntStream.rangeClosed(lower, upper).
boxed().collect(Collectors.toList());
// Compute acceleration if the vehicle stays on the same lane
Triple<IAgent, Double, Boolean> leaderTriple = findLeader(
scope, vehicle, currentLowestLane);
Triple<IAgent, Double, Boolean> followerTriple = findFollower(
scope, vehicle, currentLowestLane);
IAgent currentBackVehicle = followerTriple != null ? followerTriple.getLeft() : null;
double stayAccelM;
if (leaderTriple.getMiddle() < 0) { // || followerTriple == null) {
stayAccelM = -Double.MAX_VALUE;
} else {
// Find the leading vehicle on current lanes
IAgent leadingVehicle = leaderTriple.getLeft();
double leadingDist = leaderTriple.getMiddle();
boolean leadingSameDirection = leaderTriple.getRight();
double leadingSpeed = getSpeed(leadingVehicle);
leadingSpeed = leadingSameDirection ? leadingSpeed : -leadingSpeed;
setLeadingVehicle(vehicle, leadingVehicle);
setLeadingDistance(vehicle, leadingDist);
setLeadingSpeed(vehicle, leadingSpeed);
setFollower(vehicle, currentBackVehicle);
// Calculate acc(M) - Acceleration of current vehicle M if no lane change occurs
stayAccelM = IDM.computeAcceleration(scope, vehicle, road, leadingDist, leadingSpeed);
// Do not allow changing lane when approaching intersections
// Reason: in some cases the vehicle is forced to slow down (e.g. approaching final target in path),
// but it can gain acceleration by switching lanes to follow a fast vehicle.
// TODO: do this when leader is not a intersection
if ((leadingVehicle != null &&
leadingVehicle.getSpecies().implementsSkill(RoadNodeSkill.SKILL_ROAD_NODE)) ||
getTimeSinceLC(vehicle) < getLCCooldown(vehicle)) {
double t = getTimeSinceLC(vehicle);
setTimeSinceLC(vehicle, t + timeStep);
return ImmutablePair.of(currentLowestLane, stayAccelM);
}
}
int bestLowestLane = currentLowestLane;
double bestAccel = stayAccelM;
double bestIncentive = 0;
// Examine all lanes within range
for (int tmpLowestLane : limitedLaneRange) {
if (tmpLowestLane == currentLowestLane ||
(!allowedLanes.isEmpty() && !allowedLanes.contains(tmpLowestLane))) {
continue;
}
if (currentLowestLane <= numCurrentLanes - numLanesOccupied &&
tmpLowestLane > numCurrentLanes - numLanesOccupied) {
if (scope.getRandom().next() > probaUseLinkedRoad) {
continue;
}
}
Triple<IAgent, Double, Boolean> newLeaderTriple =
findLeader(scope, vehicle, tmpLowestLane);
Triple<IAgent, Double, Boolean> newFollowerTriple =
findFollower(scope, vehicle, tmpLowestLane);
if (newLeaderTriple.getMiddle() < 0 ||
newFollowerTriple.getMiddle() < 0) {
// Will crash into another vehicle if switch to this lane
continue;
}
// Find the leading vehicle of M on this new lane
IAgent leadingVehicle = newLeaderTriple.getLeft();
double leadingDist = newLeaderTriple.getMiddle();
boolean leadingSameDirection = newLeaderTriple.getRight();
double leadingSpeed = getSpeed(leadingVehicle);
leadingSpeed = leadingSameDirection ? leadingSpeed : -leadingSpeed;
// Calculate acc'(M) - acceleration of M on new lane
double changeAccelM = IDM.computeAcceleration(scope, vehicle, road, leadingDist, leadingSpeed);
// Find back vehicle B' on new lane
double stayAccelB;
double changeAccelB;
// Ignore follower in incentive criterion if:
// 1. No follower was found
// 2. New follower if switch lanes is still the old one
// 3. The follower is actually following another vehicle
if (newFollowerTriple.getLeft() == null ||
newFollowerTriple.getLeft() == currentBackVehicle ||
getLeadingDistance(newFollowerTriple.getLeft()) < newFollowerTriple.getMiddle()) {
stayAccelB = 0;
changeAccelB = 0;
} else {
IAgent backVehicle = newFollowerTriple.getLeft();
double backDist = newFollowerTriple.getMiddle();
// Calculate acc(B') - acceleration of B' if M does not change to this lane
// NOTE: in this case, the leading vehicle is the one we have found above for M
stayAccelB = IDM.computeAcceleration(scope, backVehicle, road, backDist + VL + leadingDist, leadingSpeed);
// Calculate acc'(B') - acceleration of B' if M changes to this lane
// NOTE: in this case, M is the new leading vehicle of B'
changeAccelB = IDM.computeAcceleration(scope, backVehicle, road, backDist, getSpeed(vehicle));
}
// MOBIL params
double p = getPolitenessFactor(vehicle);
double bSave = getMaxSafeDeceleration(vehicle);
double aThr = getAccGainThreshold(vehicle);
double aBias = getAccBias(vehicle);
// Safety criterion
if (changeAccelB <= -bSave) {
continue;
}
// Incentive criterion
boolean biasCond = getRightSideDriving(vehicle) ?
tmpLowestLane < currentLowestLane : tmpLowestLane > currentLowestLane;
int biasSign = biasCond ? 1 : -1;
double incentive = changeAccelM - stayAccelM + p * (changeAccelB - stayAccelB) + aBias * biasSign;
if (incentive > aThr && incentive > bestIncentive) {
bestIncentive = incentive;
bestLowestLane = tmpLowestLane;
bestAccel = changeAccelM;
setLeadingVehicle(vehicle, leadingVehicle);
setLeadingDistance(vehicle, leadingDist);
setLeadingSpeed(vehicle, leadingSpeed);
setFollower(vehicle, newFollowerTriple.getLeft());
}
}
if (bestLowestLane != currentLowestLane) {
setTimeSinceLC(vehicle, 0.0);
} else {
double t = getTimeSinceLC(vehicle);
setTimeSinceLC(vehicle, t + timeStep);
}
return ImmutablePair.of(bestLowestLane, bestAccel);
}
}