-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallalel_tsp.py
76 lines (63 loc) · 1.88 KB
/
parallalel_tsp.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
import random
from itertools import permutations
import time
from multiprocessing import Process, cpu_count
from multiprocessing import queues
import sys
City = complex
def X(city): return city.real
def Y(city): return city.imag
def distance(a,b): return abs(b-a)
def Cities(n, w=900, h=600, seed=1):
# creo n punti a caso sul piano.
random.seed(seed)
return frozenset(City(random.randrange(w),random.randrange(h)) for _ in range(n))
Tour = list
def alltours(cities):
start = cities[0]
return [[start] + Tour(r) for r in permutations(cities[1:])]
def tour_length(t):
return sum(distance(t[i-1],t[i]) for i in range(len(t)))
def minimo_tours(x: list):
return min(x,key=tour_length)
def bruteforce(c):
l = list(c)
t0 = time.perf_counter()
m = minimo_tours(alltours(l))
print("Soluzione sequenziale:")
s = [l.index(m[i]) for i in range(len(l))]
print(s,"len = ", tour_length(m))
t1 = time.perf_counter()
print(f"Tempo di esecuzione sequenziale = {t1-t0} sec.")
return m
def worker(id,N,cities):
print(f"Started process {id}")
t0 = time.perf_counter()
c = list(cities)
citta_first = c[0]
citta_id = c[id]
c.remove(citta_first)
c.remove(citta_id)
s = [[citta_first,citta_id] + Tour(r) for r in permutations(c)]
m = minimo_tours(s)
t1 = time.perf_counter()
c = list(cities)
indici = [c.index(x) for x in m]
print(f"Process {id}: ",indici,tour_length(m),t1-t0)
if __name__ == "__main__":
N = 10
C = Cities(N)
print("------ Città: -------")
for i,c in enumerate(C):
print(i,"\t",c)
print("-"*40)
bruteforce(C)
P = N-1 # multiprocessing.cpu_count()
process_list = []
for i in range(1,P+1):
p = Process(target=worker,args=(i,N,C))
p.start()
process_list.append(p)
for p in process_list:
p.join()
print("Fine")