Genetic Algorithms in C#
The code below is a 3.5 upgrade to Barry Lapthorn’s original C# example found on codeproject. I’ve .NET’ised it a bit more (inline with the Framework Guidelines book) and given it a facelift using a Func for the fitness function and autogenerated properties. It should also run a lot faster without the boxing and unboxing required by ArrayList, as it now employs List
I’m in the middle of writing my own version of a C# Genetic Algorithm example, however unlike Barry’s example mine uses bit strings to represent the genes in the genomes. I’m also planning on giving a bit background information to GAs in the article, based on my knowledge from a recent university course - compressing an explanation into several paragraphs will be an interesting test of the completeness of my knowledge.
As with Barry’s original example you use it like so (with the delegate fluff now removed thanks to the v3 compiler):
And the 3 classes needed are GA, Genome and GenomeComparer: