Advertisement

Genetic Algorithms for TSP

Started by October 07, 2011 05:49 PM
11 comments, last by QuinnJohns 13 years, 1 month ago
I'm trying to code a GA for the Travelling Salesman Problem, would there be any chance someone could give me basic pseudocode? I've looked over a ton of online documentations, but I'm still confused on the basic flow of a system like that? Thanks.
[size="2"][size="1"][size="2"]- Quinn
[size="2"][size="1"][size="2"]Software Developer, Mintrus
For a GA, all you need to do is make each variable a "gene" in a long line of genes. (Think an array, basically.) You simply use the values in the array to solve your problem and measure the results. The swapping of genes is as simple as swapping values from one array to another (not between array locations, mind you.) So, after our passes and grading, we might say "swap AgentA[17..20] with AgentB[17..20]". You now have two, slightly modified versions of Agents A and B.

Was there a reason that you chose GA for TSP?

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Advertisement
You can Gtranslate the following (French <-> English generally works great): http://khayyam.developpez.com/articles/algo/voyageur-de-commerce/genetique/

You can Gtranslate the following (French <-> English generally works great): http://khayyam.devel...erce/genetique/

Well done!

Incidentally, they have been able to solve 16-node TSP in screaming fast time using swarm theory (think ants that report back on their adventures). Read The Perfect Swarm by Fisher for more info.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"


Was there a reason that you chose GA for TSP?


I think I'm with IADaveMark on this one, GAs doesn't strike me as particularly useful for this... it's much less likely to give you the optimal solution (unless it gets really lucky).

Unless there's some kind of hidden logic to the layout of the locations in the kind of environment you're solving that you don't know about and could be teased out by by the GA and applied across many problems, the advantages aren't really very apparent.

Sometimes I'm sure there is such a logic (and in that case, a really good genetic algorithm may be able to figure it out and probably proceed much faster than a random system)... but if this is a more or less random level we're talking about, there may not be anything of the sort that would give a GA an advantage here.

That said, genetic algorithms are just plain fun... so, that could factor in.
[font="arial, verdana, tahoma, sans-serif"]
they have been able to solve 16-node TSP in screaming fast time using swarm theory (think ants that report back on their adventures). Read The Perfect Swarm by Fisher for more info.

A nice tutorial explaining the implementation of an ant colony algorithm to the traveling salesman problem: http://khayyam.devel...ies-de-fourmis/ (sorry, you'll have to Gtranslate it again)

[/font]

[quote name='IADaveMark' timestamp='1318024048' post='4870273']
Was there a reason that you chose GA for TSP?

I think I'm with IADaveMark on this one, GAs doesn't strike me as particularly useful for this... it's much less likely to give you the optimal solution (unless it gets really lucky).
[/quote]

Even though a GA only approach might be inefficient compared to classical heuristics, it can always be combined with these later if needed (that's the advantage of a metaheuristic method). Crossovers might turn out to be useful. That said, I have never looked closely at TSP but there seem to be myriads of papers on TSP + GA, which tends to make me think the OP's goal isn't too quixotic.
Advertisement
Thanks for the help! I've been toying around with different algorithms, to gain a better understanding. I've written so far, Depth First, Bread First, Best First, Insertion Heuristic, and now I'm trying a GA. Using a GA would be pointless for me on a small dataset, but i'm playing with about 100 "city" data points. Thanks again for the help!

Regards,
Quinn
[size="2"][size="1"][size="2"]- Quinn
[size="2"][size="1"][size="2"]Software Developer, Mintrus
This document may interest you: https://louisville.e...lligent/tsp.PDF (mirror: http://www.scribd.co...Algorithms-1998)

Our purpose in this term project is to implement heuristic algorithms and compare and evaluate their respective computational efficiency. Included in this model are greedy, 2-opt, greedy 2-opt, 3-opt, greedy 3-opt, genetic algorithm, simulated annealing, and neural network approach and their improvement versions. The problem size can be adjusted from 2-node up to 10,000-node. Therefore, these algorithms can be evaluated in a wide spectrum of situations[/quote]

comparisonoftspalgorith.png
GAs are terrible local search solvers for TSPs. State of the art solvers use hand-crafted meta-heuristics to direct the search toward promising solutions. These are usually combined with some kind of edge-swapping move operator like Lin-Kernighan or Stem-and-Cycle.

Check out LKH for example; winner of the DIMACS challenge for TSPs.
Check out LKH for example; winner of the DIMACS challenge for TSPs.


Interesting, thanks pithlit!

http://www.akira.ruc.dk/~keld/research/LKH/ :
A simple genetic algorithm has been added. New keyword: POPULATION_SIZE. Tours found by the first POPULATION_SIZE runs constitute an initial population of tours. In each of the remaining runs two tours (parents) from the current population is recombined into a new tour (child) using a variant of the Edge Recombination Crossover (ERX). The parents are chosen with random linear bias towards the best members of the population. The child is used as initial tour for the next run. If this run produces a tour better than the worst tour of the population, then the resulting tour replaces the worst tour. Premature convergence is avoided by requiring that all tours in the population have different costs.[/quote]


[color="#1C2837"]So crossovers turned out to be useful and diversity is used in the fitness function in order to avoid premature convergence (= genetic drift in GA terminology), such as illustrated below:

[color="#1C2837"]eatypicalprogressofanea.png

This topic is closed to new replies.

Advertisement