Search code examples
algorithmtraveling-salesmanapproximation

Solving a TSP-related task


I have a problem similar to the basic TSP but not quite the same.

I have a starting position for a player character, and he has to pick up n objects in the shortest time possible. He doesn't need to return to the original position and the order in which he picks up the objects does not matter.

In other words, the problem is to find the minimum-weight (distance) Hamiltonian path with a given (fixed) start vertex.

What I have currently, is an algorithm like this:

best_total_weight_so_far = Inf

foreach possible end vertex:
    add a vertex with 0-weight edges to the start and end vertices
    current_solution = solve TSP for this graph
    remove the 0 vertex

    total_weight = Weight (current_solution)
    if total_weight < best_total_weight_so_far
        best_solution = current_solution
        best_total_weight_so_far = total_weight

However this algorithm seems to be somewhat time-consuming, since it has to solve the TSP n-1 times. Is there a better approach to solving the original problem?


Solution

  • It is a rather minor variation of TSP and clearly NP-hard. Any heuristic algorithm (and you really shouldn't try to do anything better than heuristic for a game IMHO) for TSP should be easily modifiable for your situation. Even the nearest neighbor probably wouldn't be bad -- in fact for your situation it would probably be better that when used in TSP since in Nearest Neighbor the return edge is often the worst. Perhaps you can use NN + 2-Opt to eliminate edge crossings.

    On edit: Your problem can easily be reduced to the TSP problem for directed graphs. Double all of the existing edges so that each is replaced by a pair of arrows. The costs for all arrows is simply the existing cost for the corresponding edges except for the arrows that go into the start node. Make those edges cost 0 (no cost in returning at the end of the day). If you have code that solves the TSP for directed graphs you could thus use it in your case as well.