0

Possible Duplicate:
Implementation of A Star (A*) Algorithm in Java

For an assignment I have to implement some search algorithms in order to solve the travelling salesman problem, I understand the problem and I understand how the algorithm works, I simply don't know how to implement it (my Java isn't great) but this should really be the easy part but unfortunately my knowledge of Java isn't good enough to apply what I know about the algorithm.

I was therefore wondering if anyone could provide some hints or tips on how to get started with this or maybe even some good links to read up on, I also have to implement some other algorithms too if there's an easier one to start with id be happy to go with that one! I've had a look on here already but can't seem to find anything which is relevant :/

Any help is much appreciated! Thank you

Community
  • 1
  • 1
thrash
  • 187
  • 1
  • 4
  • 18
  • Basic java tutorials aren't hard or particularly lengthy, and combined with a library such JUNG or something similar to help you with the graph object side of things (Nodes, Edges, etc) you should be up and running fairly quickly. Look for tutorials focusing on Data Structures and Algorithms and you should see plenty of examples to help you. – Quetzalcoatl Dec 07 '12 at 16:49
  • I think I need to use a variety of nodes in order to compare all algorithms, I have been given some text files to test. Oh and I've seen other stuff about A* on here but none of it seems relevant to TSP or if it is then I don't know how to implement it to work on TSP – thrash Dec 07 '12 at 17:12

1 Answers1

0

I'm not an expert, but I don't think the A* can be used to solve the Traveling Salesman Problem. I'm sure you know the TSP is NP-hard, so exact solutions are quite complex and don't exist for every case. The easiest way I know of (and have used) to approximate a solution is whats called 'Simulated Annealing', which is a stochastic method (i.e. random!).

The idea is very simple. You organise the points into a list, which represents a path between all the nodes (this is called a 'tour'). Calculate how long the tour is and save the length. You then pick a random sub-list and simply reverse it. Calculate the new length. If the new list is shorter then keep it, otherwise discard it. Simply repeat these steps 100/1,000/10,000,000 times (depending on the number of points) and you'll have a reasonable approximation.

Example, imagine there are 5 points (pseudocode)...

List tour = [e, b, a, d, c]
int len = tourLength(tour)
List newTour = [e, b] + [d, a] + [c] // a and d have been reversed
int newLen = tourLength(newTour)
if(newLen < len) {
    tour = newTour
}

For 10 points, 100 iterations should yield a good result.

Zutty
  • 5,357
  • 26
  • 31