需要用于python GUI的Tkinter代码,用于在分支和动态编程方法中实现TSP。 下面的代码工作正常,明天我的项目演示需要一个gui平台。 我尝试在tkinter编程,但没有与代码相处。 在GUI中我需要文本框来读取no:of cities和n * n矩阵。输出屏幕显示路径成本和最佳路径。你们可以帮助我吗?提前谢谢。
import itertools
INF = 100000000
best_cost = 0
def reduce(size, w, row, col, rowred, colred):
rvalue = 0
for i in range(size):
temp = INF
for j in range(size):
temp = min(temp, w[row[i]][col[j]])
if temp > 0:
for j in range(size):
if w[row[i]][col[j]] < INF:
w[row[i]][col[j]] -= temp
rvalue += temp
rowred[i] = temp
for j in range(size):
temp = INF
for i in range(size):
temp = min(temp, w[row[i]][col[j]])
if temp > 0:
for i in range(size):
if w[row[i]][col[j]] < INF:
w[row[i]][col[j]] -= temp
rvalue += temp
colred[j] = temp
return rvalue
def bestEdge(size, w, row, col):
mosti = -INF
ri = 0
ci = 0
for i in range(size):
for j in range(size):
if not w[row[i]][col[j]]:
minrowwelt = INF
zeroes = 0
for k in range(size):
if not w[row[i]][col[k]]:
zeroes += 1
else:
minrowwelt = min(minrowwelt, w[row[i]][col[k]])
if zeroes > 1: minrowwelt = 0
mincolwelt = INF
zeroes = 0
for k in range(size):
if not w[row[k]][col[j]]:
zeroes += 1
else:
mincolwelt = min(mincolwelt, w[row[k]][col[j]])
if zeroes > 1: mincolwelt = 0
if minrowwelt + mincolwelt > mosti:
mosti = minrowwelt + mincolwelt
ri = i
ci = j
return mosti, ri, ci
def explore(n, w, edges, cost, row, col, best, fwdptr, backptr):
global best_cost
colred = [0 for _ in range(n)]
rowred = [0 for _ in range(n)]
size = n - edges
cost += reduce(size, w, row, col, rowred, colred)
if cost < best_cost:
if edges == n - 2:
for i in range(n): best[i] = fwdptr[i]
if w[row[0]][col[0]] >= INF:
avoid = 0
else:
avoid = 1
best[row[0]] = col[1 - avoid]
best[row[1]] = col[avoid]
best_cost = cost
else:
mostv, rv, cv = bestEdge(size, w, row, col)
lowerbound = cost + mostv
fwdptr[row[rv]] = col[cv]
backptr[col[cv]] = row[rv]
last = col[cv]
while fwdptr[last] != INF: last = fwdptr[last]
first = row[rv]
while backptr[first] != INF: first = backptr[first]
colrowval = w[last][first]
w[last][first] = INF
newcol = [INF for _ in range(size)]
newrow = [INF for _ in range(size)]
for i in range(rv): newrow[i] = row[i]
for i in range(rv, size - 1): newrow[i] = row[i + 1]
for i in range(cv): newcol[i] = col[i]
for i in range(cv, size - 1): newcol[i] = col[i + 1]
explore(n, w, edges + 1, cost, newrow, newcol, best, fwdptr, backptr)
w[last][first] = colrowval
backptr[col[cv]] = INF
fwdptr[row[rv]] = INF
if lowerbound < best_cost:
w[row[rv]][col[cv]] = INF
explore(n, w, edges, cost, row, col, best, fwdptr, backptr)
w[row[rv]][col[cv]] = 0
for i in range(size):
for j in range(size):
w[row[i]][col[j]] = w[row[i]][col[j]] + rowred[i] + colred[j]
# code for branch & bound
def bb(w,size):
global best_cost
for i in range(size):
a[i][i]=INF
print('The cost matrix:')
for i in range(size):
print(a[i])
col = [i for i in range(size)]
row = [i for i in range(size)]
best = [0 for _ in range(size)]
route = [0 for _ in range(size)]
fwdptr = [INF for _ in range(size)]
backptr = [INF for _ in range(size)]
best_cost = INF
explore(size, w, 0, 0, row, col, best, fwdptr, backptr)
index = 0
for i in range(size):
route[i] = index
index = best[index]
index = []
cost = 0
for i in range(size):
if i != size - 1:
src = route[i]
dst = route[i + 1]
else:
src = route[i]
dst = 0
cost += w[src][dst]
index.append(src+1)
index.append(1)
print('B&B Minimum cost:',cost)
print('B&B Optimal path:',index)
return
# code for dynamic programming
def dp(A,N):
v=[x for x in range(1,N)]
s=tuple(itertools.permutations(v,N-1))
l=len(s)
C=[]
for i in range(l):
t=s[i]
c=A[0][t[0]]
for j in range(1,len(t)):
c+=A[t[j-1]][t[j]]
c+=A[t[j]][0]
C=C+[c]
m=min(C)
print('DP Minimum cost:',m)
d={k:p for (k,p) in zip(C,s)}
r=d[m]
r=[x+1 for x in r]
r.insert(0,1)
r.append(1)
print('DPOptimal tour:',r)
return
#### getting inputs from user
n=int(input('Enter the no.of cities:'))
a=[[0 for i in range(n)]for j in range(n)]
print('Enter the cost matrix:')
for i in range(n):
for j in range(n):
a[i][j]=int(input())
# calling branch & bound
bb(a,n)
#calling dynamic programming
dp(a,n)
答案 0 :(得分:0)
这基本上是要求某人为您编写申请。
Tkinter可能很难掌握一些人,但它应该不会超过几个小时。如果您编写一个单独的应用程序,只需按下按钮就会显示“hello world”,就像您将学习here一样,这只是重复代码并使您的逻辑适合的情况。
当代码没有按照您的想法行事时,社区很乐意为您提供帮助。尝试完成教程,然后回过头来解决问题。
只是旁注:在发布时在代码中添加更多空格/注释,它将使答案更快更快,更相关。