2014-02-16 20 views
-1

需要Tkinter代碼以實現分支和綁定以及動態編程方法中的TSP的python GUI .. 以下代碼工作正常我明天需要一個gui平臺來進行我的項目演示。 我嘗試在tkinter編程,但沒有相處的代碼。 在GUI中,我需要文本框來讀取no:城市和n * n矩陣。輸出屏幕顯示路徑和最佳路徑的成本。你們可以幫我嗎?提前致謝。需要Tkinter代碼的python圖形用戶接口來實現分支和綁定以及動態編程方法中的TSP ..

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) 
+1

那麼你的具體問題是什麼?傾銷200多行代碼,只是說*「我需要X」*並不是一個真正的問題。你的問題應該是*「我需要X,這是我的代碼,但是它在做Y,我試着用Z來解決它」*。另外,你粘貼的代碼看起來並不是有效的Python,因爲在一些地方縮進會搞砸,所以確保你正確地複製/粘貼它。 – Carpetsmoker

+0

你的縮進被搞砸了。 –

+0

根據「GUI」的含義,我發現使用Flask將一個快速網站放在一起是將GUI集合在一起的最簡單方法之一。當然,如果你不瞭解網絡編程,這不會更快。 – amccormack

回答

0

這基本上是要求有人爲你寫你的應用程序。

Tkinter對於某些人來說很難掌握,但應該不會超過幾個小時。如果你編寫一個單獨的應用程序,當按下按鈕時你會學到here,那麼它只是顯示「hello world」,那麼它只是一個重複代碼並使你的邏輯適合的情況。

如果代碼不符合您的想法,社區將很樂意爲您提供幫助。嘗試閱讀教程,然後回到問題中。

只是一個方面:在發佈時在代碼中放置更多的空白/註釋,它會使答案更快更相關。

相關問題