-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocal_search.py
More file actions
473 lines (403 loc) · 15.1 KB
/
Local_search.py
File metadata and controls
473 lines (403 loc) · 15.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import matplotlib.pyplot as plt
import numpy as np
import math
import random
import copy
from scipy.spatial.distance import cdist
from ultis import *
from Ant import Ant
from Load_data import *
import time
max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = load_data()
cus_num=len(demand)-1
print(cus_num)
neighboor=[[] for _ in range(cus_num+1)]
neighboor[0]=0
for i in range(1,cus_num+1):
min_dis = 10**10
for j in range(1,cus_num+1):
if j!=i:
if ((xcoord[i]-xcoord[j])**2+(ycoord[i]-ycoord[j])**2)**(1/2)<min_dis:
min_dis = ((xcoord[i]-xcoord[j])**2+(ycoord[i]-ycoord[j])**2)**(1/2)
vt = j
neighboor[i] = vt
def check(route):
# max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = data_zip
# max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = load_data()
route2 = copy.deepcopy(route)
# check cap
cap = 0
for x in route2:
cap += demand[x]
if cap>max_cap:
return False
#check time
cur_time=0
for i in range(len(route2)-1):
cur_time=cur_time+s_time[route2[i]]+distance(route2[i],route2[i+1])
if cur_time<e_time[route[i+1]]:
cur_time=e_time[route2[i+1]]
if cur_time>l_time[route2[i+1]]:
return False
return True
def distance(i,j): #tính khoảng cách 2 điểm
# max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = load_data()
# max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = data_zip
return ((xcoord[i]-xcoord[j])**2+(ycoord[i]-ycoord[j])**2)**(1/2)
def cost2(route): # tính tổng đường đi của 1 cá thể
if route[0]!=-1:
sum=0
for i in route:
for j in range(0,len(i)-1):
sum+=distance(int(i[j]),int(i[j+1]))
return sum
else:
return float('inf')
# (-1)
def route_1(routes):
route=copy.deepcopy(routes)
for i in range(len(route)):
for j in range(len(route[i])):
route[i][j]-=1
return route
def route_1_ver2(routes):
route=copy.deepcopy(routes)
for i in range(len(route)):
route[i]-=1
return route
# (+1)
def route__1(routes):
route=copy.deepcopy(routes)
for i in range(len(route)):
for j in range(len(route[i])):
route[i][j]+=1
return route
def search(route):
route=route_1(route)
a = len(route)
lst = []
route_id = np.random.choice(np.arange(a), size = a, replace=False)
for i in route_id:
lst.append(route[i])
route = lst
for i in range(len(route)-1):
for j in range(i+1,len(route)):
for k in range(1,len(route[i])-1):
for t in range(1,len(route[j])-1):
new_route=copy.deepcopy(route)
z=new_route[i][k]
new_route[i][k]=new_route[j][t]
new_route[j][t]=z
if check(new_route[i]) and check(new_route[j]) and cost2(new_route)< cost2(route):
z=route[i][k]
route[i][k]=route[j][t]
route[j][t]=z
while [0,0] in route:
route.remove([0,0])
return route__1(route)
def search2(routes,colony,q):
# routes=route_1(routes)
routes=route_1(routes)
a = len(routes)
lst = []
route_id = np.random.choice(np.arange(a), size = a, replace=False)
for i in route_id:
lst.append(routes[i])
routes = lst
m = len(routes)
matrix = central_3(routes, colony)
p = int(q * m)
if p == 0:
matr = matrix[:, :1]
else:
matr = matrix[:, :p]
while routes[-1]==[0,0]:
routes.pop()
r = np.random.random()
if r <0.1:
matr = np.array([i[::-1] for i in matr])
for i in range(len(routes)):
cnt = 0
if routes[i] == [0,0]:
continue
for j in matr[i]:
cnt += 1
if i!=j and routes[j] != [0,0]:
k=1
while (k<len(routes[i])-1):
for t in range(1,len(routes[j])-1):
if k<len(routes[i])-1:
new_route=copy.deepcopy(routes)
z=new_route[i][k]
new_route[i].pop(k)
new_route[j].insert(t,z)
if cost2(new_route)< cost2(routes) and check(new_route[j]):
routes[i].pop(k)
routes[j].insert(t,z)
k+=1
while [0,0] in routes:
routes.remove([0,0])
return route__1(routes)
def search4(route, colony, n_customer):
lst = []
cus = []
for key, value in enumerate(route):
distance = 0
for i in range(len(value)-1):
distance += colony.distance_matrix[value[i], value[i+1]]
lst.append(distance)
route = route_1(route)
r = np.random.randint(1,6)
if r < 4:
# print(1)
if np.random.random() < 0.5:
select_1, select_2, select_3 = np.argsort(np.array(lst))[-3:]
selected_route = [copy.deepcopy(route[select_1]) ,
copy.deepcopy(route[select_2]),
copy.deepcopy(route[select_3])]
a = [select_1, select_2, select_3]
else:
select_1, select_2, select_3 = np.random.choice(np.arange(0, len(route)), size=3, replace=False)
selected_route = [copy.deepcopy(route[select_1]) ,
copy.deepcopy(route[select_2]),
copy.deepcopy(route[select_3])]
a = [select_1, select_2, select_3]
else:
# print(3)
select_1, select_2 = central(route, colony)
selected_route = [copy.deepcopy(route[select_1]) ,
copy.deepcopy(route[select_2])
]
a = [select_1, select_2]
if r<4 and len(route[select_1]) + len(route[select_2]) + len(route[select_3]) > n_customer/2:
if np.random.random() < 0.5:
select_1, select_2 = central(route, colony)
selected_route = [copy.deepcopy(route[select_1]) ,
copy.deepcopy(route[select_2])
]
a = [select_1, select_2]
else:
return route__1(route)
for i in range(len(selected_route)-1): # Bắt đầu từ route_1
for j in range(i+1,len(selected_route)): # Lặp qua các route tiếp theo
for k1 in range(1,len(selected_route[i])-2): # Lặp qua các tp ở route 1
for k2 in range(k1+1,len(selected_route[i])-1): #
for t1 in range(1,len(selected_route[j])-2):
for t2 in range(t1+1,len(selected_route[j])-1):
new_route=copy.deepcopy(selected_route)
zk=copy.deepcopy(new_route[i][k1:k2+1]) # Ok
zt=copy.deepcopy(new_route[j][t1:t2+1]) # Ok
del new_route[i][k1:k2+1] # Xoá
del new_route[j][t1:t2+1] # Xoá
new_route[i]=new_route[i][:k1]+zt+new_route[i][k1:]
new_route[j]=new_route[j][:t1]+zk+new_route[j][t1:]
if cost2(new_route)< cost2(selected_route) and check(new_route[i]) and check(new_route[j]):
zk=copy.deepcopy(selected_route[i][k1:k2+1])
zt=copy.deepcopy(selected_route[j][t1:t2+1])
del selected_route[i][k1:k2+1]
del selected_route[j][t1:t2+1]
selected_route[i]=selected_route[i][:k1]+zt+selected_route[i][k1:]
selected_route[j]=selected_route[j][:t1]+zk+selected_route[j][t1:]
for key, value in enumerate(selected_route):
route[a[key]] = value
return route__1(route)
while [0,0] in route:
route.remove([0,0])
return route__1(route)
def local_search(t, colony, n_customer, q):
t1=copy.deepcopy(t)
routes=[]
# routes = t1
for i in range(len(t1)):
routes.append(t1[i])
routes=search4(search2(search(routes), colony, q), colony, n_customer)
index=0
result={}
for x in (routes):
if x!=[1,1]:
result[index]=x
index+=1
return cost2((route_1(routes))), result
# Destroy Chinh
def cost_route(route): # tính tổng đường đi của 1 cá thể
sum=0
for i in range(len(route)-1):
sum+=distance(int(route[i]),int(route[i+1]))
return sum
def ls1(t,num_slices=10):
t1=copy.deepcopy(t)
routes=[]
# routes = t1
for i in range(len(t1)):
routes.append(t1[i])
routes=route_1(routes)
while routes[-1]==[0,0]:
routes.pop()
index = random.sample(range(0, len(routes)), 3)
# print(index)
new_route=[[],[],[]]
change=[[0,1],[1,2],[2,0]]
for i in range(0,3):
cut=int((len(routes[index[i]])-2)/num_slices)
# print(cut)
cut1=len(routes[index[i]])-2-num_slices*cut
# print(cut1)
t=1
for j in range(cut1):
new_route[i].append(routes[index[i]][t:t+cut+1])
t+=cut+1
for j in range(num_slices-cut1):
new_route[i].append(routes[index[i]][t:t+cut])
t+=cut
for i in range(0,num_slices):
best=100000000
best_choice=100
best_a=0
best_b=0
for j in range(0,3):
a=copy.deepcopy(routes[index[change[j][0]]])
b=copy.deepcopy(routes[index[change[j][1]]])
if len(new_route[change[j][0]][i])>0:
t1=a.index(new_route[change[j][0]][i][0])
for k in range(len(new_route[change[j][0]][i])):
a.pop(t1)
a=a[:t1]+new_route[change[j][1]][i]+a[t1:]
else:
a=a[:-1]+new_route[change[j][1]][i]+a[-1:]
if len(new_route[change[j][1]][i])>0:
t2=b.index(new_route[change[j][1]][i][0])
for k in range(len(new_route[change[j][1]][i])):
b.pop(t2)
b=b[:t2]+new_route[change[j][0]][i]+b[t2:]
else:
b=b[:-1]+new_route[change[j][0]][i]+b[-1:]
if check(a) and check(b) and cost_route(a)+cost_route(b)+cost_route(routes[index[3-change[j][0]-change[j][1]]])<best:
best=cost_route(a)+cost_route(b)+cost_route(routes[index[3-change[j][0]-change[j][1]]])
best_choice=j
best_a=a
best_b=b
if best != 100000000:
routes[index[change[best_choice][0]]]=best_a
routes[index[change[best_choice][1]]]=best_b
index=0
result={}
a = route__1(routes)
for x in (a):
if x!=[1,1]:
result[index]=x
index+=1
return cost2((route_1(a))), result
def dif(i,j):
# max_cap, xcoord, ycoord, demand, e_time, l_time, s_time, data = load_data()
return abs(int(e_time[i])-int(e_time[j]))+abs(int(l_time[i])-int(l_time[j]))
def ls2(t,epochs=10):
t1=copy.deepcopy(t)
routes=[]
# routes = t1
for i in range(len(t1)):
routes.append(t1[i])
routes=route_1(routes)
while routes[-1]==[0,0]:
routes.pop()
for i in range(epochs):
for i in range(epochs):
choose_custom=random.sample(range(1, cus_num+1 ), 1)[0]
for i in range(len(routes)):
if choose_custom in routes[i]:
choose_route=i
best=1000
for i in range(len(routes)):
if i != choose_route:
for j in range(len(routes[i])):
if dif(routes[i][j],choose_custom)<best:
best=dif(routes[i][j],choose_custom)
best_custom=routes[i][j]
best_route=i
new_route1=copy.deepcopy(routes[best_route])
new_route2=copy.deepcopy(routes[choose_route])
new_route2[routes[choose_route].index(choose_custom)]=best_custom
new_route1[routes[best_route].index(best_custom)]= choose_custom
if check(new_route1) and check(new_route2):
routes[best_route]=new_route1
routes[choose_route]=new_route2
index=0
result={}
a = route__1(routes)
for x in (a):
if x!=[1,1]:
result[index]=x
index+=1
return cost2((route_1(a))), result
# Local search Cuong
# #tìm hàm neighboor[i] = j <=> khách hàng thứ i gần j nhất
#chèn 1 điểm vào 1 đường
def insert_cus(cus1,route):
min_insert = 10**10
best_index1=0
route_in = copy.deepcopy(route)
for i in range(1,len(route)):
route_in.insert(i,cus1)
if check(route_in):
if cost_route(route_in)<min_insert:
min_insert = cost_route(route_in)
best_index1 = i
del route_in[i]
return best_index1
def min1_dist(ch_cus,routes):
neigh_routes = -1
ch_routes = -1 #ch_cus : là khách hàng được chọn để tráo vị trí với neighboor của nó
routes1=route_1(routes)
while routes1[-1]==[0,0]:
routes1.pop()
neigh_cus = neighboor[ch_cus] #neighboor của nó
# print(neigh_cus)
for i1 in range (len(routes1)):
if ch_cus in routes1[i1]:
# print('Done_1') #vị trí route chứa ch_cus
ch_routes = i1
if neigh_cus in routes1[i1]:
# print('Done_2') #vị trí route chứa neighboor
neigh_routes = i1
# print(neigh_routes, ch_routes)
if neigh_routes == -1 and ch_routes == -1:
return False, 0.0
if neigh_routes == ch_routes:
return False,0.0
else:
new_ch_routes = copy.deepcopy(routes1[ch_routes])
# print(neigh_routes)
new_neigh_routes = copy.deepcopy(routes1[neigh_routes])
sum1 = cost_route(new_ch_routes)+ cost_route(new_neigh_routes)
find_vt = insert_cus(ch_cus,new_neigh_routes)
if find_vt>0:
new_neigh_routes.insert(find_vt,ch_cus)
# if ch_cus in new_ch_routes: # chèn ch_cus vào vị trí best của neigh
new_ch_routes.remove(ch_cus)
# else:
# return False, 0.0 #xóa ch_cus khỏi route ban đầu của nó
if cost_route(new_ch_routes)+cost_route(new_neigh_routes) < sum1: #nếu mà quãng đường tối ưu thì OK
routes1[ch_routes]=new_ch_routes
routes1[neigh_routes]=new_neigh_routes
return True,route__1(routes1)
else:
return False,0.0
else:
return False,0.0
def ls3(t):
t1=copy.deepcopy(t)
routes=[]
# routes = t1
for i in range(len(t1)):
routes.append(t1[i])
for choose_cus in range(1,cus_num+1):
xx1 = min1_dist(choose_cus,routes)
if xx1[0]==True:
routes = xx1[1]
index=0
result={}
a = routes
for x in (a):
if x!=[1,1]:
result[index]=x
index+=1
return cost2((route_1(a))), result