【问题标题】:Need Tkinter code for python GUI for implementation of TSP in Branch and bound and dynamic programming methods..需要 Python GUI 的 Tkinter 代码,以便在分支和绑定以及动态编程方法中实现 TSP。
【发布时间】:2014-02-16 06:12:49
【问题描述】:

需要 Python GUI 的 Tkinter 代码,以便在分支和绑定以及动态编程方法中实现 TSP。 下面的代码工作正常,我明天需要一个 gui 平台来演示我的项目。 我尝试在 tkinter 中编程,但没有与代码相处。 在 GUI 中,我需要文本框来读取 no: of city 和一个 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)

【问题讨论】:

  • 那么您的具体问题是什么?转储 200 多行代码并仅仅说 “我需要 X” 并不是一个真正的问题。你的问题应该是“我需要X,这是我的代码,但它在做Y,我试着用Z来解决它”的形式。此外,您粘贴的代码似乎不是有效的 Python,因为在某些地方缩进被弄乱了,所以请确保您正确复制/粘贴它。
  • 你的缩进搞砸了。
  • 根据您所说的“GUI”的含义,我发现使用 Flask 将快速网站组合在一起是组合 GUI 的最简单方法之一。当然,如果你不懂网络编程,这也不会更快。
  • 我认为您不再需要此代码,因为这是 6 年前发布的,但我喜欢您实现的 B&B 算法,我建议您将其发布在某个地方供人们查看。跨度>

标签: python user-interface tkinter


【解决方案1】:

这基本上是要求某人为您编写您的应用程序。

对于某些人来说,Tkinter 可能很难掌握,但应该不会超过几个小时。如果您编写一个单独的应用程序,当按下按钮时仅显示“hello world”,就像您将学习here 一样,那么这只是重复代码并使您的逻辑适合的情况。

当代码没有按照您的想法运行时,社区很乐意为您提供帮助。尝试阅读本教程,然后返回您的问题。

附注:发布时在您的代码中添加更多空格/cmets,这将使答案更快、更相关。

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多