-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathбустинг имитации отжига 4.py
407 lines (331 loc) · 14.8 KB
/
бустинг имитации отжига 4.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
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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 23 12:19:37 2019
@author: Belov
"""
#LICENSE ON christofides_tsp
#=======
#Copyright (c) 2016 D. S. Rahul
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#Имитация отжига (С) 2019,2016 Белов С.В.,D. S. Rahul
#mail@belovsergey.ru
import pandas as pd
import os
import numpy as np
from itertools import cycle, dropwhile, islice
import random
from multiprocessing import Process, Queue
import tqdm
import sys
import re
#import io
#import json
from pytsp.christofides_tsp import christofides_tsp
import time
#----------------------------------------------
import time
start_time = time.time()
from math import sin, cos, sqrt, atan2, radians
from scipy.spatial.distance import pdist, squareform
class ReadData():
def __init__(self, filename):
self.name = filename[:-4]
self.size = self.getSize()
self.EdgeWeightType = self.getEdgeWeightType()
self.format_ = self.getFormat() # for EXPLICIT data only
self.time_to_read = 0
def getFormat(self):
format_ = "None"
try:
with open(f'{self.name}.tsp') as data:#TSP_Data/
datalist = data.read().split()
for ind, elem in enumerate(datalist):
if elem == "EDGE_WEIGHT_FORMAT:":
format_ = datalist[ind + 1]
break
elif elem == "EDGE_WEIGHT_FORMAT":
format_ = datalist[ind + 2]
break
return format_
except IOError:
print("Input file "+self.name+" not found")
sys.exit(1)
def getEdgeWeightType(self):
EdgeType = "None"
try:
with open(f'{self.name}.tsp') as data:#TSP_Data/
datalist = data.read().split()
for ind, elem in enumerate(datalist):
if elem == "EDGE_WEIGHT_TYPE:":
EdgeType = datalist[ind + 1]
#print(EdgeType)
break
elif elem == "EDGE_WEIGHT_TYPE":
EdgeType = datalist[ind + 2]
#print(EdgeType)
break
return EdgeType
except IOError:
print("Input file "+self.name+" not found")
sys.exit(1)
def getSize(self):
"""
Return size of instances (i.e. Number of
cities)
"""
size = 0
try:
with open(f'{self.name}.tsp') as data:#TSP_Data/
datalist = data.read().split()
for ind, elem in enumerate(datalist):
if elem == "DIMENSION:":
size = datalist[ind + 1]
#print(size)
break
elif elem == "DIMENSION":
size = datalist[ind + 2]
#print(size)
break
return int(size)
except IOError:
print("Input file "+self.name+" not found")
sys.exit(1)
def read_Data(self):
with open(f'{self.name}.tsp') as data:#TSP_Data/
cities = []
Isdata = True
while (Isdata):
line = data.readline().split()
if len(line) <= 0:
break
tempcity = []
for i, elem in enumerate(line):
try:
temp = float(elem)
tempcity.append(temp)
except ValueError:
break
if len(tempcity) > 0:
cities.append(np.array(tempcity))
return np.array(cities)
def GetDistanceMat(self):
if self.EdgeWeightType == "EXPLICIT":
DistanceMat = self.getMat()
self.time_to_read = time.time() - start_time
return DistanceMat
elif self.EdgeWeightType == "EUC_2D" or "CEIL_2D":
DistanceMat = self.EuclidDist()
self.time_to_read = time.time() - start_time
return DistanceMat
elif self.EdgeWeightType == "GEO":
DistanceMat = self.GeographicDist()
self.time_to_read = time.time() - start_time
return DistanceMat
else:
return None
def EuclidDist(self):
cities = self.read_Data()
#DistanceDict = {}
A = cities[:, 1:3]
DistanceMat = np.round(squareform(pdist(A)))
return DistanceMat
def GeographicDist(self):
a = time.time()
R = 6373.0
cities = self.read_Data()
DistanceMat = np.zeros((self.size, self.size))
for i in range(self.size):
for j in range(0, i + 1):
node1 = cities[i]
node2 = cities[j]
lat1 = radians(node1[1])
lat1 = radians(node1[1])
lon1 = radians(node1[2])
lat2 = radians(node2[1])
lon2 = radians(node2[2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
DistanceMat[i, j] = distance
DistanceMat[j, i] = distance
return DistanceMat
def getMat(self):
DataFormat = self.getFormat()
if DataFormat == "FULL_MATRIX":
cities = self.read_Data()
DistanceMat = cities[:self.size]
return DistanceMat
elif DataFormat == "LOWER_DIAG_ROW":
with open(f'{self.name}.tsp') as file:#TSP_Data/
indicator = False
data = file.read().split()
templist = []
cities = []
for elem in data:
if elem == "EDGE_WEIGHT_SECTION":
indicator = True
continue
if indicator:
try:
it = float(elem)
templist.append(it)
except:
break
if it == 0:
cities.append(templist)
templist = []
DistanceMat = np.zeros((self.size, self.size))
for i in range(self.size):
temp = []
l = len(cities[i])
for j in range(self.size):
if j <= (l - 1):
temp.append(cities[i][j])
else:
temp.append(cities[j][i])
DistanceMat[i] = temp
return DistanceMat
elif DataFormat == "UPPER_DIAG_ROW":
with open(f'{self.name}.tsp') as file:#TSP_Data/
indicator = False
data = file.read().split()
templist = []
cities = []
for elem in data:
if elem == "EDGE_WEIGHT_SECTION":
indicator = True
continue
if indicator:
try:
it = float(elem)
templist.append(it)
except ValueError:
break
if it == 0:
cities.append(templist)
templist = []
print("Need to complete it")
else:
sys.exit("No Format Match for EXPLICIT data")
def read_file(file):
file_data=pd.read_csv(file , sep=';', header=0,encoding='utf-8')#читаем файл
#Трансформируем в матрицу
#Сначала получаем список уникальных ячеек
cols=np.concatenate((file_data['Ячейка1'].unique(),file_data['Ячейка2'].unique()), axis=0)
cols=np.unique(cols, axis=0)
#теперь формируем матрицу значений с пустыми значениями
data=pd.DataFrame(columns=cols,index=cols)
#теперь заполняем матрицу, через итерацию по строкам
for _, row in file_data.iterrows():
weight=row.Расстояние
rw=row.Ячейка1
cl=row.Ячейка2
data[rw][cl]=weight
data[cl][rw]=weight
return data.fillna(0).astype(float).values,cols
def route_cost(graph,result):
first_id=result[0]
cost=graph[result[-1]][first_id]
for next_id in result[1:-1]:
cost+=graph[first_id][next_id]
first_id=next_id
return cost
def simulated_annealing(graph, min_cost, path, alpha,queue):
random.seed()
current_route_cost=min_cost
lp=len(path)
for temperature in np.logspace(0,10,num=lp**2,base=1e-1):#температура от 1 до 10-15 степени
new_solution = path.copy()
left_index = random.randint(2, lp- 1)
right_index = random.randint(0, lp - left_index)
last=right_index + left_index
new_solution[right_index: last] = reversed(new_solution[right_index: last])
new_route_cost=route_cost(graph, new_solution)
if new_route_cost < current_route_cost or np.exp((current_route_cost - new_route_cost) / temperature) > random.random():
cycled = cycle(new_solution)
skipped = dropwhile(lambda x: x != 0, cycled)
sliced = islice(skipped, None, lp)
path = list(sliced)
current_route_cost =route_cost(graph,path)
#temperature*=alpha
queue.put([path,current_route_cost])
def _start_new_proc(processes,iter_limit,graph, cost, best_path, alpha, queue):
while(len(processes)<iter_limit):
p=Process(target=simulated_annealing, args=(graph, cost, best_path, alpha, queue,))
p.start()
processes.append([p,cost])
return processes
#----------------------------
if __name__ == "__main__":
if len (sys.argv) != 4:
raise ValueError("Отсутствуют ключи <адрес файла для расчета> <адрес файла для результата> -iter_limit<Количество одновременных процессов (больше - лучше, но дольше и идет нагрузка на процессор)>")
rootDir = os.path.abspath(os.curdir)
file=rootDir+'\\'+sys.argv[1]
file_result=rootDir+'\\'+sys.argv[2]
print('in: '+file)
print('out: '+file_result)
match = re.search(r'-iter_limit(\d+)', sys.argv[3])
if not match:
raise ValueError("Нулевой iter_limit")
iter_limit=int(match[1])
r = ReadData(file)
graph=r.GetDistanceMat()
#
# =============================================================================
#начальный путь
initial_path = christofides_tsp(graph)
initial_min_cost=route_cost(graph, initial_path)
total_lim=len(initial_path)
print('initial cost: '+str(initial_min_cost))
queue = Queue()
size=len(initial_path)#размер пути
alpha=1-1e-3#параметр уменьшения температуры
limit=iter_limit#минимальное количество не улучшений, чтобы выйти из основного цикла
if limit<5: limit=5
print('iter_limit: '+str(iter_limit))
pbar = tqdm.tqdm(desc='calculate',mininterval=2, maxinterval=5)
best_path = initial_path.copy()
min_cost=initial_min_cost
#создаем процессы
match_eqv=limit
processes=_start_new_proc([],iter_limit,graph, initial_min_cost, initial_path, alpha, queue)
while(processes):
#проверяем какие процессы закончились
for ind,el in enumerate(processes):
p=el[0]
if p.is_alive(): continue
recent_cost=el[1]
p.close()
processes.pop(ind)
total_lim-=1
#выбераем лучший путь
if not queue.empty():
path,cost = queue.get()
if cost<min_cost:#глобальный результат
min_cost,best_path=cost,path
if cost<recent_cost:#локальный результат
match_eqv=limit
else:
cost,path=min_cost,best_path
match_eqv-=1
pbar.set_description(desc='iteration left: '+str(match_eqv)+', min_cost: '+str(min_cost)+', recent_cost: '+str(recent_cost)+', curr_cost: '+str(cost), refresh=False)
pbar.update(1)
if match_eqv>=0 and total_lim>=0: #новые процессы не создаем - вышли за лимиты итераций улучшения
#запускаем новый процесс
processes=_start_new_proc(processes,len(processes)+1,graph, cost, path, alpha, queue)
break
else:
time.sleep(0.03)
pbar.set_description(desc='Done.', refresh=True)
pbar.close()
print('final cost: '+str(min_cost))
#эмпирическая оценка улучшения маршрута
upgrade_value=float(min_cost)/initial_min_cost
print('upgrade_value (the less the better): '+str(upgrade_value))
queue.close()
print('best cost: '+str(min_cost))
print('path: '+str(best_path))