-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedibus_v2_b&b.py
516 lines (372 loc) · 11.4 KB
/
pedibus_v2_b&b.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import numpy as np
import time
import math
import copy
import pprint as pp
import operator
import threading
from itertools import chain
from collections import defaultdict
start = time.time()
############## VARIABLES ##############
# file dei dati:
file = 'pedibus_100.dat'
# contiene per ogni nodo i nodi raggiungibili
zero_paths = {}
zero_sorted_paths = []
reachables = {}
is_reachable_by = {}
nodi_disponibili = [];
validated_paths = {}
basic_solution = []
#initialize dictionary for bus stop coordinates
coord_x = {} #per coordinate x quando parso il dat
coord_y = {} #per coordinate y quando parso il dat
danger = []
tree = defaultdict(list) #lista soluzioni
############## FUNCTION DECLARATION ##############
#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(dat_file, delimiter='\n', dtype=None)
cast = 0
if "20.dat" in dat_file:
cast = 1
n = int(file_dat[1-cast][11:]) #parse param n: dimension of array
ALPHA = float(file_dat[3-cast][15:]) #parse param alpha
value = 5-cast
file_dat = file_dat[value:]
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)
def is_reachable(center_node, other_node):
d1 = costs[center_node][0]
d2 = costs[other_node][0]
if costs[center_node][other_node]+d2<=d1*ALPHA:
return True
else:
return False
def concat(path):
key = "";
for i in range (0,len(path)):
key=key+"-"+str(path[i])
return key
#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+1]][my_path[i]]
return path_danger
### METODI NUOVI ###
def init_reachables(center_node):
node_list = {};
#init reachability
for i in range (1,n+1):
if i!=center_node and is_reachable(center_node, i):
node_list[str(i)] = node_dist(center_node,i)
#validated_paths[concat([center_node,i])] = [center_node,i]
return node_list
def init_reachable_by(node):
reachable_by = {};
#init reachability
for i in range (1,n+1):
if i!=node and str(node) in reachables[i]:
reachable_by[i] = node_dist(node,i)
#validated_paths[concat([center_node,i])] = [center_node,i]
return reachable_by
def check_path(old_path,new_node):
path_temp = copy.copy(old_path)
path_temp.append(new_node)
#controlla se old_path + new node validato
if(concat(path_temp) in validated_paths):
return True, path_temp
#TODO migliora
if(concat(old_path) in validated_paths):
dist = validated_paths[concat(old_path)]
dist = dist + costs[old_path[-1]][new_node]
if(dist<costs[new_node][0]*ALPHA):
validated_paths[concat(path_temp)] = dist
return True, path_temp
return False, old_path
def explore_thread(prec_path,my_node,index, threadSolution, nodeDisp, zeroSort):
if(not is_reachable_by[my_node]):
threadSolution.append(prec_path)
return prec_path
check_node = is_reachable_by[my_node][index][0]
if check_node in nodeDisp:
prec_node = check_node
else:
index+=1
if(index<len(is_reachable_by[my_node])):
return explore_thread(prec_path,my_node,index, threadSolution, nodeDisp, zeroSort)
else:
threadSolution.append(prec_path)
return prec_path
bool_path, prec_path = check_path(prec_path, prec_node)
if(bool_path):
nodi_disponibili.remove(prec_node)
zeroSort.remove((prec_node,costs[prec_node][0]))
#esplora piu profondo
return explore_thread(prec_path,prec_node,0, threadSolution, nodeDisp, zeroSort)
else:
#esplora altro ramo
index+=1
if(index<len(is_reachable_by[my_node])):
return explore_path(prec_path,my_node,index, threadSolution, nodeDisp, zeroSort)
else:
threadSolution.append(prec_path)
return prec_path
def explore_path(prec_path,my_node,index):
if(not is_reachable_by[my_node]):
basic_solution.append(prec_path)
return prec_path
check_node = is_reachable_by[my_node][index][0]
if check_node in nodi_disponibili:
prec_node = check_node
else:
index+=1
if(index<len(is_reachable_by[my_node])):
return explore_path(prec_path,my_node,index)
else:
basic_solution.append(prec_path)
return prec_path
bool_path, prec_path = check_path(prec_path, prec_node)
if(bool_path):
nodi_disponibili.remove(prec_node)
zero_sorted_paths.remove((prec_node,costs[prec_node][0]))
#esplora piu profondo
return explore_path(prec_path,prec_node,0)
else:
#esplora altro ramo
index+=1
if(index<len(is_reachable_by[my_node])):
return explore_path(prec_path,my_node,index)
else:
basic_solution.append(prec_path)
return prec_path
def reverse_solution(solution):
for pat in solution:
pat.reverse()
def print_solution_vertical(solution):
sol = {};
for i in range (1,(n+1)):
sol[i] = 0
for path in solution:
for j in range(0,(len(path)-1)):
sol[path[j]]=path[j+1]
for k in range (1,n+1):
print k," ",sol[k]
def print_solution_to_file(solution):
sol = {};
output_name = "pedibus_" + str(n) + ".sol"
file = open(output_name, "w")
for i in range (1,(n+1)):
sol[i] = 0
for path in solution:
for j in range(0,(len(path)-1)):
sol[path[j]]=path[j+1]
for k in range (1,n+1):
print >>file, k,sol[k]
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
def compute_challenge_value(leaves,danger):
beta = 0.1
if(n>10 and n <= 100):
beta = 0.01
if(n>100 and n <= 1000):
beta = 0.001
if (n > 1000):
beta = 0.0001
return round(leaves+(danger*beta),4)
############## VARIABLES ##############
# contiene per ogni nodo i nodi raggiungibili
zero_paths = {}
zero_sorted_paths = []
reachables = {}
is_reachable_by = {}
nodi_disponibili = [];
validated_paths = {}
basic_solution = []
#initialize dictionary for bus stop coordinates
coord_x = {} #per coordinate x quando parso il dat
coord_y = {} #per coordinate y quando parso il dat
danger = []
tree = defaultdict(list) #lista soluzioni
############## BODY ##############
n, ALPHA, node, danger, costs = parse_dat_file(file)
BEST_LEAVES = n
BEST_RISK = 9999
BEST_SOL = []
MAX_THREADS = 300
threadLock = threading.Lock()
threads = []
#print parameters for check
print "n: ", n, "\n" "ALPHA: ", ALPHA, "\n\n"
#pp.pprint(danger)
#INIZIALIZZA REACHABLES // ZERO PATHS // NODI DISP
for i in range (1,n+1):
reachables[i]=init_reachables(i)
zero_paths[i] = costs[i][0]
nodi_disponibili.append(i)
#INIZIALIZZA ZERO PATHS
zero_sorted_paths = sorted(zero_paths.items(), key=operator.itemgetter(1))
#INIZIALIZZA IS_REACHABLE_BY
for i in range (1,n+1):
x = init_reachable_by(i)
is_reachable_by[i] = sorted(x.items(), key=operator.itemgetter(1))
#local_solution = []
#nodi_disp = [1...n]
while (len(zero_sorted_paths) > 0 and len(basic_solution)<=BEST_LEAVES):
current_path = [0]
#prendi il piu vicino V a zero
current_node = zero_sorted_paths[0][0]
#creo current_path = [0,V]
current_path.append(current_node)
validated_paths[concat(current_path)] = costs[current_node][0]
#rimuovo V dai nodi_disponibili
nodi_disponibili.remove(current_node)
zero_sorted_paths.remove((current_node,costs[current_node][0]))
explore_path(current_path,current_node,0)
BEST_SOL = copy.deepcopy(basic_solution)
BEST_LEAVES = len(basic_solution)
BEST_RISK = compute_danger_sol(basic_solution)
####################
# if (len(basic_solution)<BEST_LEAVES):
# BEST_SOL = copy.deepcopy(basic_solution)
# BEST_LEAVES = len(BEST_SOL)
# BEST_RISK = compute_danger_sol(basic_solution)
# print "SOL:", BEST_SOL, "LEAVES ", BEST_LEAVES, " Risk: ", BEST_RISK
# ESPLORA SOLUZIONI ALTERNATIVE DA 0 E CONFRONTA
for i in range (1,n):
selected_node = i
#reset nodi disp
nodi_disponibili = []
zero_sorted_paths = []
for j in range (1,n+1):
nodi_disponibili.append(j)
#reset basic solution
basic_solution = []
#reset zero sorted
zero_sorted_paths = sorted(zero_paths.items(), key=operator.itemgetter(1))
while (len(zero_sorted_paths) > 0 and len(basic_solution)<=BEST_LEAVES):
current_path = [0]
#prendi l'i-esimo nodo piu vicino a zero
current_node = zero_sorted_paths[selected_node][0]
selected_node = 0
#creo current_path = [0,V]
current_path.append(current_node)
validated_paths[concat(current_path)] = costs[current_node][0]
#rimuovo V dai nodi_disponibili
nodi_disponibili.remove(current_node)
zero_sorted_paths.remove((current_node,costs[current_node][0]))
explore_path(current_path,current_node,0)
# UPDATE BEST IF NEEDED
new_leaves = len(basic_solution)
new_risk = compute_danger_sol(basic_solution)
if(new_leaves<BEST_LEAVES or (new_leaves==BEST_LEAVES and new_risk<BEST_RISK)):
BEST_SOL = basic_solution
BEST_LEAVES = new_leaves
BEST_RISK = new_risk
print "\n\n----------------------------------------------------\n"
print "BEST SOLUTION:"
print BEST_SOL
print "\nLEAVES:",BEST_LEAVES
print "DANGER:",BEST_RISK
print "CHALLENGE VALUE:",compute_challenge_value(BEST_LEAVES,BEST_RISK),"\n"
#per ogni nodo
reverse_solution(BEST_SOL)
print_solution_vertical(BEST_SOL)
print "----------------------------------------------------"
#time
time_final = time.time()-start
print 'TOTAL time:', round(time_final,3), 'seconds.\n\n'
print 'SOLUTION SAVED IN FILE: pedibus_' + str(n) + ".sol\n"
print_solution_to_file(BEST_SOL)
############# COME FUNZIA #############
#per ogni nodo che contiene V si prende il piu vicino U
#controllo U-V-0
#se path ok:
#aggiorno current_path
#rimuovo U dai nodi_disponibili
#se path non ok:
#per ogni nodo che contiene U prendo il piu vicino K
#controllo K-U-V-0
#se si
#rimuovo U dai nodi_disponibili