I am learning about travelling salesman problem and I was wondering if any of you know a site where I could get source code for C++? I checked Google but found nothing useful.
The TSP is NP-Hard, exact methods for non-trival instances tend to involve column generation and cutting planes, which means you will need fairly good IP software as well (CPLEX/Gurobi/etc).
If you're looking for a heuristic solution, then again there are many options, are you looking for a construction heuristic, or an optimization metaheuristic such as simulated annealing?
I am of course assuming a you're interested in a deterministic Euclidean symmetric TSP. Which may not be the case?
What I'm trying to say is that there is no simple code to solve the TSP, and in a lot of cases, no code to solve the TSP at all. If you can be much more precise about what you're asking, I may be able to help you.
procedure SALESMAN(C, N, R, l_path)
begin
INSERT_FIRST_NODES(stack);
for numberOfLinks = 2 to N - 1 do
begin
for node = 2 to N do
CREATE LIST(node, stack, list)
NEW STACK ELEMENT(stack, list)
end;
end;
FINISH_LIST(stack, list);
NEW STACK ELEMENT(stack, list)
FIND_SOLUTION(R, stack, l_path)
RELEASE_STACK(stack)
end
This is an algorithm, now I'm searching source code for the implementation of this algorithm. :)
As Kev82 wrote: The TSP is NP-Hard. In effect this means that you cannot find the solution for problems that are not very small. So you can make a brute force method (look at all possible routes, pick the best), but this will be useless outside trivial (=very small) instances.
Alternatively you can use a heuristic method. But in this case there are many, many options to choose from - some easy, some hard, some good, some lousy.
So there is no definitive source code for the algorithm. You need to decide how you wish to approach a problem that even modern computing cannot solve definitively.
I could probably off the top of my head list 50 completely different methods of solving the TSP, and there will be many more than that.
What you've posted is a small part of a pascal implementation of one of them. I have no idea what the algorithm you've posted is, or how it is supposed to work, it doesn't sound to me like you do either, and I think it is very unlikely that anyone else will.
If you are looking for something simple, then the simplest you are going to get will be a greedy construction algorithm, such as nearest unvisited neighbour, algorithms like this are generally crap though. It depends on your data, but the simplest reasonable construction algorithm I can think of would be an angular sort algorithm.
The alternative is to go down the metaheuristic route, the simplest in this case would be a local neighbourhood search (based on a 2-swap neighbourhood) with random restart. This is actually surprisingly good for the effort it requires.
I'm reading the guide for the implementation and it says that it's needed to implement the problem of salesman by backtracing. The input is adjacency matrix which looks like this:
To restate: There is no simple program is C++ or any other language that solves the TSP for arbitrary size.
For small sizes, you can solve the problem by brute force (calculate every route and pick the best). But only for very small sizes. Otherwise, you need to make a choice in heuristics. Google to find implementations.
12 nodes is probably somewhere close to being brute force solvable on a good pc, but I would not recommend it!
Try looking at depth first search. With a proper implementation of the backtracking (ie end when it goes over the current maximum) you should be able to perfectly solve almost all 12 node problems in a reasonable timespan.
BTW I have never worked on the TSP problem, so the above claim that is should be quickly solveable is an intuition...
I have solved a lot of TSPs and I would agree with exiledAussie that with backtracking, even with a naff starting solution, you should be able to solve any size 12 instance in under a second. An exhaustive search will solve up to 10, maybe 11 at a push, within a few minutes.
Do you understand the backtracking algorithm itsself? The code for this is very simple if you understand the algorithm.
But I'm in C++ only for a year, so I do not know exactly how to implementate this and that's the reason I'm searching for the source code. :)