Jump to content
  • Advertisement
Sign in to follow this  
QuinnJohns

Genetic Algorithms for TSP

This topic is 2617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!