-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKNN sim.py
249 lines (220 loc) · 11.1 KB
/
KNN sim.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import traci
import pandas as pd
import numpy as np
from classes import car, Environment
from random import randint
import matplotlib.pyplot as plt
import seaborn as sns
from random import random
from time import sleep
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import preprocessing, metrics
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.decomposition import PCA
columns_list = ['egoY', 'egoV','egoA',
'leftFollowerDistanceToEgo', 'leftFollowerY', 'leftFollowerV','leftFollowerA',
'leftLeaderDistanceToEgo', 'leftLeaderY', 'leftLeaderV','leftLeaderA',
'rightFollowerDistanceToEgo', 'rightFollowerY', 'rightFollowerV', 'rightFollowerA',
'rightLeaderDistanceToEgo', 'rightLeaderY', 'rightLeaderV', 'rightLeaderA',
'LeaderDistanceToEgo', 'LeaderY', 'LeaderV','LeaderA',
'FollowerDistanceToEgo', 'FollowerY', 'FollowerV', 'FollowerA']
# default lane change mode 0b011001010101
# cars lane change mode 0b011001010001 # prevent cooperative lane change
def show_PCA_plot(cumsum):
plt.ylabel('Variance cumulative sum')
plt.xlabel('Number of features')
plt.title('PCA Analysis')
plt.ylim(0,1.05)
plt.plot(cumsum, 'o-')
plt.show()
performance = []
# Read data
df = pd.read_csv("data.csv", index_col=False)
# apply PCA to training data
X = df[columns_list]
sc = StandardScaler()
X = sc.fit_transform(X)
pca = PCA(n_components=11)
X = pca.fit_transform(X)
# Convert string labels into numbers.
le = LabelEncoder()
Y = le.fit_transform(df['action'])
knn = KNeighborsClassifier(n_neighbors=25)
knn.fit(X, Y) # Train the model using the training sets
# if __name__ == '__main__':
for episode in range(100):
all_data = []
time = 0
pred_actions = []
ego = car(car_id='ego')
env = Environment(render=True if episode%10 == 0 else True, ego_id=ego.carid)
env.start('Highway.sumocfg')
# prevent ego vehicle from doing any lane change by itself
traci.vehicle.setLaneChangeMode(ego.carid, 0x00)
last_distance = 0
rows_list = []
risk_detected = False
risk_start = -1
risk_distance = 0
collided = False
for step in range(1400):
traci.simulationStep()
if env.collision_happened():
collided = True
if step > 210: #ego vehicle should be now on the road
if not ego.car_left():
neighbour_ids = ego.get_neighbour_IDs()
left_leader = car(neighbour_ids[0])
left_follower = car(neighbour_ids[1])
right_leader = car(neighbour_ids[2])
right_follower = car(neighbour_ids[3])
leader = car(neighbour_ids[4])
follower = car(neighbour_ids[5])
ego.get_values(0)
left_leader.get_values(ego.position_x)
left_follower.get_values(ego.position_x)
right_leader.get_values(ego.position_x)
right_follower.get_values(ego.position_x)
leader.get_values(ego.position_x)
follower.get_values(ego.position_x)
# correct NA values
if ego.lane_id == '1to2_2': # right most lane
left_follower.position_x = ego.position_x - 50
left_follower.position_y = -1.88 * 4 # 4th lane
left_follower.velocity = ego.velocity
left_follower.acceleration = ego.acceleration
left_follower.distance_to_ego = left_follower.position_x - ego.position_x
left_leader.position_x = ego.position_x + 50
left_leader.position_y = -1.88 * 4 # 4th lane
left_leader.velocity = ego.velocity
left_leader.acceleration = ego.acceleration
left_leader.distance_to_ego = left_leader.position_x - ego.position_x
elif ego.lane_id == '1to2_0':
right_follower.position_x = ego.position_x - 50
right_follower.position_y = 1.88 # -1th lane
right_follower.velocity = ego.velocity
right_follower.acceleration = ego.acceleration
right_follower.distance_to_ego = right_follower.position_x - ego.position_x
right_leader.position_x = ego.position_x + 50
right_leader.position_y = 1.88 # -1th lane
right_leader.velocity = ego.velocity
right_leader.acceleration = ego.acceleration
right_leader.distance_to_ego = right_leader.position_x - ego.position_x
#compute risk
if not risk_detected or ego.position_x - risk_start > risk_distance:
if random() < 0.01:
risk_detected = True
start = ego.position_x
risky_lane = randint(0, 2)
risk_distance = randint(30, 100)
else:
risk_detected = False
data_dict = {
'timestep' : traci.simulation.getTime(),
'egoX' : ego.position_x,
'egoY' : ego.position_y,
'egoV' : ego.velocity,
'egoA' : ego.acceleration,
'egolaneID' : ego.lane_id,
'leftFollowerID' : left_follower.carid,
'leftFollowerX' : left_follower.position_x ,
'leftFollowerY' : left_follower.position_y,
'leftFollowerV' : left_follower.velocity,
'leftFollowerA' : left_follower.acceleration,
'leftFollowerLaneID' : left_follower.lane_id,
'leftFollowerDistanceToEgo' : left_follower.distance_to_ego,
'leftLeaderID' : left_leader.carid,
'leftLeaderX' : left_leader.position_x,
'leftLeaderY' : left_leader.position_y,
'leftLeaderV' : left_leader.velocity,
'leftLeaderA' : left_leader.acceleration,
'leftLeaderLaneID' : left_leader.lane_id,
'leftLeaderDistanceToEgo' : left_leader.distance_to_ego,
'rightFollowerID' : right_leader.carid,
'rightFollowerX' : left_follower.position_x,
'rightFollowerY' : left_follower.position_y,
'rightFollowerV' : left_follower.velocity,
'rightFollowerA' : left_follower.acceleration,
'rightFollowerLaneID' : left_follower.lane_id,
'rightFollowerDistanceToEgo' : right_follower.distance_to_ego,
'rightLeaderID' : right_leader.carid,
'rightLeaderX' : right_leader.position_x,
'rightLeaderY' : right_leader.position_y,
'rightLeaderV' : right_leader.velocity,
'rightLeaderA' : right_leader.acceleration,
'rightLeaderLaneID' : right_leader.lane_id,
'rightLeaderDistanceToEgo' : right_leader.distance_to_ego,
'LeaderID' : leader.carid,
'LeaderX' : leader.position_x,
'LeaderY' : leader.position_y,
'LeaderV' : leader.velocity,
'LeaderA' : leader.acceleration,
'LeaderLaneID' : leader.lane_id,
'LeaderDistanceToEgo' : leader.distance_to_ego,
'FollowerID' : follower.carid,
'FollowerX' : follower.position_x,
'FollowerY' : follower.position_y,
'FollowerV' : follower.velocity,
'FollowerA' : follower.acceleration,
'FollowerLaneID' : follower.lane_id,
'FollowerDistanceToEgo' : follower.distance_to_ego,
'action' : -1,
# 'risk' : -1 if not risk_detected else risky_lane
}
row = []
row.append(data_dict)
all_data.append(data_dict)
state = pd.DataFrame(row)
state = state[columns_list]
state = pca.transform(sc.transform(state))
state = pd.DataFrame(state)
# state = state.assign(risk=risk)
last_distance = ego.position_x
rows_list.append(data_dict)
pred_action = le.inverse_transform(knn.predict(state)) # Predict the response for test dataset
pred_actions.append(pred_action)
if pred_action[0] == 'go_right' and ego.lane_id == '1to2_0':
pred_action[0] = 'stay'
elif pred_action[0] == 'go_left' and ego.lane_id == '1to2_1':
pred_action[0] == 'stay'
if risk_detected:
lane = int(ego.lane_id[5])
print("Risk deteted in lane ", risky_lane)
if (risky_lane == lane + 1) and pred_action[0] == 'go_left':
action = 'stay'
print("Lane change request denied because of risk in lane ", risky_lane)
elif (risky_lane == lane - 1) and pred_action[0] == 'go_right':
action = 'stay'
print("Lane change request denied because of risk in lane ", risky_lane)
elif lane == risky_lane:
time += 1
else:
print("no risk")
ego.perform_action(pred_action[0])
env.close()
all_data = pd.DataFrame(all_data)
count = 0
for x in pred_actions:
if x == 'go_right' or x == 'go_left':
count += 1
performance_dict = {'time_to_end' : data_dict['timestep'] if not collided else -1,
'collision' : collided,
'nb_lane_change_requests' : count,
'nb_emergency_brakes': (all_data.egoA.values < -7).sum() + (all_data.FollowerA.values < -7).sum() +
(all_data.rightFollowerA.values < -7).sum() + (all_data.leftFollowerA.values < -7).sum(),
'time_driven_in_a_risky_lane' : time
}
performance = []
performance.append(performance_dict)
performance_df = pd.DataFrame(performance)
print(time)
try:
old_perf_df = pd.read_csv("performance.csv")
performance_df = performance_df.append(old_perf_df, ignore_index=True, sort=False)
except:
pass
performance_df.to_csv('performance.csv', index=False)
print('\n\n\nepisode : {}\n\n\n'.format(episode))
print('done')