The Generation Game – A Simple Genetic Algorithm
Last night my housemate Hayley and I were talking about the Data Mining and Decision Systems module we took last semester, during that discussion the concept of genetic algorithms came up.
In the computer science field of artificial intelligence, a genetic algorithm (GA) is a search heuristic that mimics the process of natural selection, except that GAs use a goal-oriented targeted search and natural selection isn’t a search at all
Hayley explained the concept to me using the idea of an animal that is more likely to survive in its environment by blending in with the colour of its surroundings. So tonight, as a nice change from revision and work on my Final Year Project, I had a go at developing such an algorithm — with a somewhat humorous undertone. The result of this undertaking is a little tech demo called “The Generation Game – A Simple Genetic Algorithm”.
In the demo it is advantageous for a sheep to be green, in order to fit in with its field. However at the beginning of the game the ten sheep in the initial flock are of completely random colours.
Turns are taken, in order by
- A Flock of Sheep, which breeds, producing 1 new sheep for every two sheep in the flock (if there is a left over sheep it doesn’t breed). Sheep produced by the mating process are the median colour of its two parents.
- A Wolfpack which depending on its hunting ability can eat 10% – 60% of the heard in any given turn
The closer a sheep is to the colour green, the less likely it is the be a casualty of a wolf attack (however there are other contributing factors, and a green sheep can still be killed).
As you can see from the gif image above if the initial flock has a green sheep amongst its ranks then evolution takes place (survival of the fittest, like how Darwin described it) and within just 20 generations the flock consists of only green sheep.
Another interesting situation is when there is no green sheep in the initial flock.
As you can see from the example above which starts with no green sheep the flock gradually becomes a light brown colour, this is the closest colour the flock could get to green with the genes avaliable in its gene pool.
This is just a simple tech demo (written in C# with XNA) to prove to myself I understood the concept, but it worked out quite well and I think its cool. I’ll be cleaning up the code and adding some comments, so be sure to check out the repository containing the program on GitHub.
Thats all for now,