8

I am working in a delivery company. We currently solve 50+ locations routes by "hand".

I have been thinking about using Google Maps API to solve this problem, but I have read that there is a 24 points limit.

Currently we are using rails in our server so I am thinking about using a ruby script that would get the coordinates of the 50+ locations and output a reasonable solution.

What algorithm would you use to approach this problem?

Is Ruby a good programming language to solve this type of problem?

Do you know of any existing ruby script?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
jfanals
  • 1,006
  • 1
  • 13
  • 28
  • 1
    …you do, of course, realize that this is one of the hardest problems there is, right? With no really great answer? There are still reasonable solutions, but just making sure you know that they really won't be *great*. – Matchu Nov 23 '10 at 22:03
  • Yeap... I am not looking for the "optimal" solution... a reasonable one would do :) – jfanals Nov 23 '10 at 22:09
  • 1
    I've added the tag "traveling-salesman". Have you tried looking at other questions in that tag? – Andrew Grimm Nov 23 '10 at 22:17
  • Thanks for adding the tag... I did search for "traveling salesman" in stackoverflow with not much success... I am looking for a practical solution for the 50+ locations that can be executed (in ruby) on a single server... BTW. travelling is spelled with double LL: traveLLing – jfanals Nov 23 '10 at 22:26
  • Andrew sorry for the spelling correction... I just found out that it can be spelled both ways ;) – jfanals Nov 23 '10 at 22:33
  • @ischnura: It's one of those American English versus Commonwealth English things. I actually tried typing in with two "l"s at first! – Andrew Grimm Nov 23 '10 at 22:50
  • 2
    To clarify: 50 locations for how many users / drivers / salespeople? Does it need to be run in real time, or can this be scheduled to be run at night - to have solutions in the morning? - To be fair - I'm just asking this from the pointview of server load. – konung Nov 23 '10 at 23:45
  • 3
    Is this the old one about the traveling saleman and the farmer's 50 daughters? – the Tin Man Nov 23 '10 at 23:50
  • When Matchu says this is a hard problem, this does not mean the the programming is hard, it means that the best know algorithm takes a very long time to run regardless of the language used. To throw in some numbers - 50 locations will take 49019536704020485392579302011699200000000 times longer than 24 locations. – John La Rooy May 18 '11 at 00:09

6 Answers6

6

This might be what you are looking for:

Warning:

this site gets flagged by firefox as attack site - but I doesn't appear to be. In fact I used it before without a problem

[Check revision history for URL]

rubyquiz seems to be down ( has been down for a bit) however you can still check out WayBack machine and archive.org to see that page: http://web.archive.org/web/20100105132957/http://rubyquiz.com/quiz142.html

konung
  • 6,908
  • 6
  • 54
  • 79
  • The URL that you suggested points to multiple solutions that can be downloaded... I was able to tweak the code of one of the solutions (from Joseph Seaton) to fit my needs. Thanks for again for pointing to that site :) I managed to get a reasonable solution in less than 10 seconds... – jfanals Nov 26 '10 at 18:56
  • You are more than welcome. btw - I highly recommend exploring and trying to solve some of the problems there - will help you become a better ruby programmer. I try to do one problem a week, when I have time. – konung Dec 01 '10 at 15:54
4

Even with the DP solution mentioned in another answer, that's going to require O(10^15) operations. So you're going to have to look at approximate solutions, which are probably acceptable given that you currently do them by hand. Look at http://en.wikipedia.org/wiki/Travelling_salesman_problem#Heuristic_and_approximation_algorithms

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • +1! Particularly look at the PTAS linked to from the Euclidean TSP subsection or described in http://www.cs.princeton.edu/~arora/pubs/tsp.ps (easier to follow description available at http://corelab.ntua.gr/courses/approx-alg/material/Euclidean%20TSP.pdf ). It gives you a poly-time O(1+1/epsilon) approximation algorithm for whatever epsilon you fancy (of course, be wary that the exponent grows with 1/epsilon)! – Yonatan N Nov 24 '10 at 06:54
2

Here are a couple of tricks:
1: Lump locations that are relatively close into one graph, and turn those locations into a single node in your main graph. This lets you be greedy without too much work.
2: Use an approximation algorithm.
2a: My favorite is bitonic tours. They're pretty easy to hack up.
See Update

Here's a py lib with a bitonic tour and here's another
Let me go look for a ruby one. I'm having trouble finding more than just the RGL, which has efficiency issues....

Update
In your case, the minimum spanning tree attack should be effective. I can't think of a case where your cities wouldn't meet the triangle inequality. This means that there should be a relatively sort of kind of almost fast rather decent approximation. Particularly if the distance is euclidean, which I think, again, it must be.

Jake Kurzer
  • 1,532
  • 4
  • 16
  • 25
1

I worked on using meta-heurestic algorithms such as Ant Colony Optimazation to solve TSP problems for the Bays29 (29-city) problem, and it gave me close to optimal solutions in very short time. You can potentially use the same.

I wrote it in Java though, I will link it here anyways, because I am currently working on a port to ruby: Java: https://github.com/mohammedri/ant_colony_java_TSP Ruby: https://github.com/mohammedri/aco-ruby (incomplete) This is the dataset it solves for: https://github.com/jorik041/osmsharp/blob/master/Core/OsmSharp.Tools/Benchmark/TSPLIB/Problems/TSP/bays29.tsp

Keep in mind I am using the Euclidean distance between each city i.e. the straight line distance, I don't think that is ideal in a real life situation considering roads and a city map etc. but it may be a good starting point :)

mr3mo
  • 135
  • 1
  • 10
1

One of the optimized solution is using Dynamic Programming but still very expensive O(2**n), which is not very feasible, unless you use some clustering and distributing computing, ruby or single server won't be very useful for you.

I would recommend you to come up with a greedy criteria instead of using DP or brute force which would be easier to implement.

Once your program ends you can do some memoization, and store the results somewhere for later lookups, which can as well save you some cycles.

in terms of the code, you ll need to implement vertices, edges that have weights.

ie: vertex class which have edges with weights, recursive. than a graph class that will populate the data.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • 1
    Brute force approach is considered impractical for more than 20 points. For 50 he would have to try factorial(50!) permutations - roughly about = 30414093201713378043612608166064768844377641568960512000000000000 for each salesman. – konung Nov 24 '10 at 00:07
0

If you want the cost of the solution produced by the algorithm is within 3/2 of the optimum then you want the Christofides algorithm. ACO and GA don't have a guaranteed cost.

Micromega
  • 12,486
  • 7
  • 35
  • 72