-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
175 lines (129 loc) · 3.84 KB
/
new.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
import time
start = time.time() #faccio partire il tempo
############## IMPORT LIBRARIES ##############
from itertools import chain
from collections import defaultdict
import copy
import numpy as np
import pprint as pp
import math
import itertools
import operator
############## FUNCTION DECLARATION ##############
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 distanza euclidea tra due nodi
def node_dist(index_1, index_2):
sub_x = math.pow((node[index_1][0] - node[index_2][0]), 2)
sub_y = math.pow((node[index_1][1] - node[index_2][1]), 2)
return math.sqrt(sub_x + sub_y)
#crea dizionario con distanza di un nodo ad ogni altro nodo
def node_distance():
for key1, value1 in node.items():
distance.clear()
for key2, value2 in node.items():
if key1 != key2:
distance[key2] = node_dist(key1, key2)
neighbor[key1] = distance.copy()
return neighbor
############## VARIABLES ##############
#initialize dictionary for bus stop coordinates
coord_x = {} #per coordinate x quando parso il dat
coord_y = {} #per coordinate y quando parso il dat
neighbor = {} #ogni nodo con gli altri per distanza
distance = {} #distanza da un nodo ad un altro, per poi metterla in neighbor
cluster = {} #cluster di ogni nodo (i nodi all'interno del raggio alpha*distanza da root)
queue = [] #nodi non ancora assegnati
solution = []
with_out_root = {}
tree = defaultdict(list) #lista soluzioni
file = 'res/pedibus_10.dat'
############## BODY ##############
n, ALPHA, node, danger, costs = parse_dat_file(file)
#print parameters for check
print "n: ",n, "\n" "ALPHA: ", ALPHA, "\n\n"
with_out_root = copy.copy(node)
with_out_root.pop(0)
for nod in with_out_root:
queue.append(nod)
neighbor = node_distance()
node_sorted = sorted(neighbor[0].items(), key=operator.itemgetter(1))
############## END BODY ##############
#time
print '\nIt took', time.time()-start, 'seconds.'