-
Notifications
You must be signed in to change notification settings - Fork 6
/
local_search.py
149 lines (116 loc) · 4.52 KB
/
local_search.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
def local_search(x, y, c, waitingArray, landsat, launchesfrom, ls_checktprime, eee, tau, tauprimeF, tauprimeE, sigma, sigmaprime, ls_checkt, ls_hatt, V, sL, sR):
tmpWaitingArray = {}
for i in waitingArray:
tmpWaitingArray[i] = float(waitingArray[i])
tmp_y = []
launchLoc = {}
custLoc = {}
for [v,i,j,k] in y:
tmp_y.append([v,i,j,k])
launchLoc[v,k] = i
custLoc[v,k] = j
TSPtour = []
for [i,j] in x:
TSPtour.append(i)
TSPtour.append(c+1)
availUAVs = {}
for tmpIndex in range(0, len(TSPtour)-1):
i_launch = TSPtour[tmpIndex]
availUAVs[i_launch] = list(V)
for [v,i,j,k] in y:
p = TSPtour.index(i)
q = TSPtour.index(k) - 1
if tmpIndex >= p and tmpIndex <= q:
availUAVs[i_launch].remove(v)
shift_happened = False
# For each truck customer where there is truck waiting, try to shift the UAV retrieval (that is responsible for waiting) to the next truck customer:
for truckCust in TSPtour[1:-1]:
if (tmpWaitingArray[truckCust] > 0) and (len(landsat[truckCust]) > 0):
tmp_waiting = 0
for v in landsat[truckCust]:
if tmp_waiting < ls_checktprime[v,truckCust]:
tmp_waiting = ls_checktprime[v,truckCust]
tmpv = v
tmp_waiting = 0
for v in landsat[truckCust]:
if v != tmpv:
if tmp_waiting < ls_checktprime[v,truckCust]:
tmp_waiting = ls_checktprime[v,truckCust]
tmpv_2 = v
tmpi = launchLoc[tmpv,truckCust]
tmpj = custLoc[tmpv,truckCust]
tmpk = TSPtour[TSPtour.index(truckCust)+1]
if (tauprimeF[tmpv][tmpi][tmpj] + sigmaprime[tmpj] + tauprimeE[tmpv][tmpj][tmpk] <= eee[tmpv][tmpi][tmpj][tmpk]):
if len(landsat[truckCust]) > 1:
truck_duration_from_i_to_k = (ls_checkt[tmpk] - ls_hatt[tmpi]) - (ls_checktprime[tmpv,truckCust] - ls_checktprime[tmpv_2,truckCust])
else:
truck_duration_from_i_to_k = (ls_checkt[tmpk] - ls_hatt[tmpi]) - (ls_checktprime[tmpv,truckCust] - ls_checkt[truckCust] - sigma[truckCust])
if truck_duration_from_i_to_k <= eee[tmpv][tmpi][tmpj][tmpk]:
if (tmpv not in launchesfrom[truckCust]):
tmp_y.remove([tmpv,tmpi,tmpj,truckCust])
tmp_y.append([tmpv,tmpi,tmpj,tmpk])
if tmpk != c+1:
tmpWaitingArray[tmpk] -= sR[tmpv][tmpk]
shift_happened = True
elif (tmpv in launchesfrom[truckCust]) and (len(availUAVs[truckCust]) >= 1):
tmp_y.remove([tmpv,tmpi,tmpj,truckCust])
tmp_y.append([tmpv,tmpi,tmpj,tmpk])
if tmpk != c+1:
tmpWaitingArray[tmpk] -= sR[tmpv][tmpk]
shift_happened = True
tmp_again_y = []
for [v,i,j,k] in tmp_y:
tmp_again_y.append([v,i,j,k])
tmp_again_y_1 = []
launchesfrom_temp = {}
landsat_temp = {}
for i in launchesfrom:
launchesfrom_temp[i] = []
landsat_temp[i] = []
ls_checktprime_temp = {}
new_v = availUAVs[truckCust][0]
for [v,i,j,k] in tmp_y:
if (v == tmpv) and (TSPtour.index(i) >= TSPtour.index(truckCust)):
tmp_again_y.remove([v,i,j,k])
tmp_again_y_1.append([new_v,i,j,k])
launchesfrom[i].remove(v)
launchesfrom_temp[i].append(new_v)
landsat[k].remove(v)
landsat_temp[k].append(new_v)
ls_checktprime_temp[new_v,k] = ls_checktprime[v,k]
del ls_checktprime[v,k]
if (v == new_v) and (TSPtour.index(i) >= TSPtour.index(truckCust)):
tmp_again_y.remove([v,i,j,k])
tmp_again_y_1.append([tmpv,i,j,k])
launchesfrom[i].remove(v)
launchesfrom_temp[i].append(tmpv)
landsat[k].remove(v)
landsat_temp[k].append(tmpv)
ls_checktprime_temp[tmpv,k] = ls_checktprime[v,k]
del ls_checktprime[v,k]
tmp_y = []
launchLoc = {}
custLoc = {}
for [v,i,j,k] in tmp_again_y:
tmp_y.append([v,i,j,k])
launchLoc[v,k] = i
custLoc[v,k] = j
for [v,i,j,k] in tmp_again_y_1:
tmp_y.append([v,i,j,k])
launchLoc[v,k] = i
custLoc[v,k] = j
for i in launchesfrom_temp:
launchesfrom[i] = launchesfrom[i] + launchesfrom_temp[i]
landsat[i] = landsat[i] + landsat_temp[i]
for [v,k] in ls_checktprime_temp:
ls_checktprime[v,k] = ls_checktprime_temp[v,k]
for tmpIndex in range(0, len(TSPtour)-1):
i = TSPtour[tmpIndex]
if tmpIndex > TSPtour.index(truckCust):
if (tmpv in availUAVs[i]) and (new_v not in availUAVs[i]):
availUAVs[i].append(new_v)
availUAVs[i].remove(tmpv)
elif (tmpv not in availUAVs[i]) and (new_v in availUAVs[i]):
availUAVs[i].append(tmpv)
availUAVs[i].remove(new_v)
return [shift_happened, tmp_y]