Уведомления

Группа в Telegram: @pythonsu

#1 Окт. 10, 2020 16:51:08

huhandrey
Зарегистрирован: 2020-10-10
Сообщения: 2
Репутация: +  0  -
Профиль   Отправить e-mail  

Нужна помощь с Travelling Salesman Problem. Срочно

Всем, привет! Нужна помощь с заданием. Код, который прикреплен снизу, находит кратчайший маршрут через все точки. мне нужно сделать так, чтобы в конце линия возвращалась к исзодной точке. Заранее благодарю
import random, math
import itertools
import tkinter as tk
from tkinter import simpledialog

def distance(x1,y1,x2,y2):
#distance between 2 points
#math.sqrt((x1-x2)**2+(y1-y2)**2)
return math.sqrt((x1-x2)**2+(y1-y2)**2)

def changeR (event=None):
global N
x=
y=
for i in range(N):
x.append(random.choice(range(650)))
y.append(random.choice(range(650)))
bestpath,minDistance=findBestpath(x,y)

hC.delete('all')
#display points
for i in range(N):
hC.create_oval(x-2,y-2,x+2,y+2, fill='black')

#display the best path
xy=[x[bestpath],y[bestpath]]
for i in bestpath:
xy.extend([x,y])
hC.create_line(xy,fill='red')



def changeN():
global N
n=simpledialog.askinteger('Get N', ‘Enter the number of points:’)
if not n:return
N=n
doAlloverAgain()

def startNewJob():
doAlloverAgain()

def findBestpath(x,y):
n=len(x)
paths=list(itertools.permutations(range(n)))
bestpath=0
minDistance=9999999999999999999999999
for path in paths:
totalD=0
for i,j in zip(path, path):
totalD+=distance(x-2,y-2,x+2,y+2)
if totalD<minDistance:
minDistance=totalD
bestpath=path
return bestpath, minDistance


def doAlloverAgain():
x=
y=
for i in range(N):
x.append(random.choice(range(boundary)))
y.append(random.choice(range(boundary)))
result,minDistance=findBestpath(x,y)

hC.delete('all')
#display points
for i in range(N):
hC.create_oval(x-2,y-2,x+2,y+2, fill='black')

#display the best path
xy=[x[path],y[path]]
for i in path:
xy.extend([x,y])
hC.create_line(xy,fill='red')



N=5
boundary=200
x=
y=
for i in range(N):
x.append(random.choice(range(200)))
y.append(random.choice(range(200)))

paths=list(itertools.permutations(range(N)))
bestpath=0
minDistance=9999999999999999999
for path in paths:
#we are going to compute the total distance for each path
#total distance=sum of distances between points
#for example:path2,3,4,1,0)
#distance(x,y,x,y)
#distance(x,y,x,y)
#distance(x,y,x,y)
totalD=0
for i,j in zip(path, path):
totalD+=distance(x,y,x,y)
if totalD<minDistance:
minDistance=totalD
bestpath=path
result=list(path)

hw=tk.Tk()
hw.title('G1005a Travelling Salesman problem, Khegay Andrey, 12184775')

hF=tk.Frame(hw)
hF.pack(side='left')

hB=tk.Button(hF, text='Start New Job', command=startNewJob)
hB.pack()
hB2=tk.Button(hF, text='Change N', command=changeN)
hB2.pack()
hB3=tk.Button(hF, text='Change range', command= changeR)
hB3.pack()

hC=tk.Canvas(hw,width=650, height=300)
hC.pack(side='left')

#display points
for i in range(N):
hC.create_oval(x-2,y-2,x+2,y+2, fill='black')

#display the best path
xy=[x[bestpath],y[bestpath]]
for i in bestpath:
xy.extend([x,y])
hC.create_line(xy,fill='red')

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version