-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdanger_methods.py
129 lines (100 loc) · 2.76 KB
/
danger_methods.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
#Parsa il file, occhio che ritorna 5 valori, costs e' una matrice con tutti i costi
def parse_dat_file(dat_file):
file_dat = np.genfromtxt(file, delimiter='\n', dtype=None)
n = int(file_dat[1][11:]) #parse param n: dimension of array
ALPHA = float(file_dat[3][15:]) #parse param alpha
file_dat = file_dat[5:]
raw_x = []
raw_y = []
raw_d = []
costs = []
#start split coord x in vector raw_x and idem for y
for row in file_dat:
if "coordX" in row:
isX = True
isY = False
isD = False
if "coordY" in row:
isX = False
isY = True
isD = False
if "d [*,*]" in row:
isY = False
isD = True
if isX:
raw_x.append(" ".join(row.split()))
if isY:
raw_y.append(" ".join(row.split()))
if isD:
raw_d.append(" ".join(row.split()))
#delete initial words and final semicolumn
raw_x.pop(0)
raw_x.pop(len(raw_x)-1)
raw_y.pop(0)
raw_y.pop(len(raw_y)-1)
raw_d.pop(0)
raw_d.pop(0)
raw_d.pop(len(raw_d)-1)
raw_d =' '.join(raw_d)
raw_d = raw_d.split(' ')
raw_x =' '.join(raw_x)
raw_x = raw_x.split(' ')
raw_y =' '.join(raw_y)
raw_y = raw_y.split(' ')
#transfer vector raw_x in a dictionary. key=index, value=coordX
i=0
for column in raw_x:
if i%2 == 0:
even = int(column)
if i%2 != 0:
coord_x[even] = int(column)
i = i+1
#transfer vector raw_y in a dictionary. key=index, value=coordY
i=0
for column in raw_y:
if i%2 == 0:
even = int(column)
if i%2 != 0:
coord_y[even] = int(column)
i = i+1
#transfer raw_d in a matrix
row = []
danger = []
for i in range (0, len(raw_d)+1):
if (i%(n+2)) != 0:
row.append(float(raw_d[i]))
else:
if i != 0:
danger.append(row)
row = []
costs = [costs[:] for costs in [[0] * (n + 1)] * (n + 1)]
for i in range(0, (n+1)):
for j in range(0, (n+1)):
costs[i][j] = float("{0:.4f}".format(math.sqrt((coord_x[i]-coord_x[j])**2 + (coord_y[i]-coord_y[j])**2)))
#possibile ottimizzare le fusione in un unico dizionario, anche piu sopra
#merge the two dictionaries
coord = defaultdict(list)
for k, v in chain(coord_x.items(), coord_y.items()):
coord[k].append(v)
return n, ALPHA, coord, danger, costs
#calcola il pericolo di un path
def compute_danger(my_path):
path_danger = 0
for i in range(0, len(my_path)-1):
path_danger = path_danger + danger[my_path[i]][my_path[i+1]]
return path_danger
#tra un vettori di path ritorna quello con meno dangerous
def min_dangerous(paths):
min_danger = 9999
min_danger_path = []
for pat in paths:
if compute_danger(pat) < min_danger:
min_danger = compute_danger(pat)
min_danger_path = pat
return min_danger_path
#calcola il danger da una soluzione completa
def compute_danger_sol(my_sol):
total_danger = 0
for s_path in my_sol:
total_danger = total_danger + compute_danger(s_path)
return total_danger