Python Forum
Travelling Salesman Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Travelling Salesman Problem
#3
This does not calculate the path distance
totalD=0
for i,j in zip(path[:4], path[1:]):
    totalD+=distance(x[i]-2,y[i]-2,x[j]+2,y[j]+2)
This almost works if the path has 5 points, but it does not work at all for a different number of points.  It also doesn't add in the distance from the last point back to the starting point.  And why are you subtracting and adding 2?  Was this copied from the code that draws the dots?  There is no need for the offset.  You should also consider using numbers instead of dots so you can tell where the route starts and ends.

Once you change the distance calculation to be correct, you'll need to do the same thing when drawing the map.  Either add in a line segment to return to start, or you could create a polygon.

You have a lot of duplicate code and some has errors.  Clean it up so each block of code only appears once.  The code that generates the map should not be in the main body and in the changeR() and in the doOverAgain(). It should appear only once, and if you need to use it multiple places it should be a function that is called from multiple places.  To understand what you are doing and where you went wrong, I edited your code and got it to run.  With lots of comments it is still only 80 lines long and could be much shorter.[/i][/i]
Reply


Messages In This Thread
Travelling Salesman Problem - by huhandrey - Oct-10-2020, 01:43 PM
RE: Travelling Salesman Problem - by deanhystad - Oct-10-2020, 10:00 PM
RE: Travelling Salesman Problem - by deanhystad - Oct-11-2020, 02:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm to solve a case of Travelling Salesman Problem Ale888 3 3,074 Dec-11-2018, 07:49 PM
Last Post: buran
  Traveller salesman backtracking synced 3 3,244 Oct-22-2017, 08:54 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020