-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdp_solution.py
73 lines (59 loc) · 1.89 KB
/
dp_solution.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
import xlrd
import copy
import time
g = {}
p = []
def optimized_path(k, a, matrix, times):
if (k, a) in g:
return g[k, a]
values = []
all_min = []
for j in a:
set_a = copy.deepcopy(list(a))
set_a.remove(j)
all_min.append([j, tuple(set_a)])
result = optimized_path(j, tuple(set_a), matrix, times)
values.append(matrix[k][j] + times[k] + result)
g[k, a] = min(values)
p.append(((k, a), all_min[values.index(g[k, a])]))
return g[k, a]
def get_matrix(n):
workbook = xlrd.open_workbook(f'P{n}.xls')
worksheet = workbook.sheet_by_index(0)
matrix = []
for i in range(n+1):
row = []
for j in range(n+1):
row.append(int(worksheet.cell(i, j).value))
matrix.append(row)
return matrix
def get_study_times(n):
workbook = xlrd.open_workbook(f'P{n}.xls')
worksheet = workbook.sheet_by_index(0)
matrix = []
for i in range(n+1):
matrix.append(int(worksheet.cell(n+1, i).value))
return matrix
for n in range(5, 76, 5):
distance_matrix = get_matrix(n)
time_list = get_study_times(n)
start = time.time()
for x in range(1, n+1):
g[x, ()] = distance_matrix[0][x] + time_list[x]
total = optimized_path(0, tuple(range(1,n+1)), distance_matrix, time_list)
print("\n\nBacktracking...")
print(f'Path for N={n}: 1 -> ', end='')
solution = p.pop()
print(solution[1][0]+1, end=' -> ')
for x in range(n):
for new_solution in p:
if tuple(solution[1]) == new_solution[0]:
solution = new_solution
print(solution[1][0]+1, end=' -> ')
break
print('1')
print(f'Total Time: {total}')
run_time = time.time() - start
print(f"Runtime: {int(run_time*1000)}ms")
g.clear()
del p[:]