• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Genetic Algorithms for TSP

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Quinn Johns  Members

Posted 07 October 2011 - 11:49 AM

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.
- Quinn
Software Developer, Mintrus

Posted 07 October 2011 - 03:47 PM

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-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
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!"

### #3Franck Dernoncourt  Members

Posted 07 October 2011 - 11:39 PM

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

Posted 07 October 2011 - 11:57 PM

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-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
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!"

### #5driftingSpaceMan  Members

Posted 08 October 2011 - 03:00 AM

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.

### #6Franck Dernoncourt  Members

Posted 08 October 2011 - 12:16 PM

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)

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).

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.

### #7Quinn Johns  Members

Posted 08 October 2011 - 01:26 PM

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
- Quinn
Software Developer, Mintrus

### #8Franck Dernoncourt  Members

Posted 09 October 2011 - 01:47 AM

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

### #9pithlit  Members

Posted 11 October 2011 - 07:01 PM

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.

### #10Franck Dernoncourt  Members

Posted 12 October 2011 - 02:04 AM

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.

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:

### #11BearishSun  Members

Posted 12 October 2011 - 06:48 AM

I did a project for college a few years back, it has an option to solve TSP using a GA.

Here are the binaries and the source, I hope it helps:
http://www.mediafire.com/file/jc0yka96f63b05x/TSP-Genetic.rar

UI is in Croatian but it shouldn't be too hard to figure out. Code is in English.

I didn't find GA too useful for TSP, it was mostly included as a proof of concept.

### #12pithlit  Members

Posted 12 October 2011 - 05:05 PM

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:

I think the GA is just used as a diversification mechanism here: an alternative meta-heuristic that picks new neighbourhoods to search in. IIRC the local search itself is using an exchange operator that ranks edges based on how close they are to being part of the Minimum Spanning Tree of all cities.

### #13Quinn Johns  Members

Posted 12 October 2011 - 10:43 PM

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:

I think the GA is just used as a diversification mechanism here: an alternative meta-heuristic that picks new neighbourhoods to search in. IIRC the local search itself is using an exchange operator that ranks edges based on how close they are to being part of the Minimum Spanning Tree of all cities.

Anyways, it took me a little less than two days to write the GA, mutations, crossovers, etc. Pretty excited about the results. Thanks for the input everyone.
- Quinn
Software Developer, Mintrus

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.