Sunday, July 4, 2010

Genetic Algorithm example: Displaying text 'python' on the console

There's one quote by the great Chinese philosopher Confucius;
I hear and I forget. I see and I remember. I do and I understand.
I was running through the same period while learning genetic algorithm. I must admit that I really am not that good in biology, being a physical group student from the early college time. However Genetic Algorithm is one of the interesting topics, i have ever encountered. I heard about this term in the AI class, saw the relevant topics in internet, went through some books. Excited by the topic, i finally came up with this idea of demonstrating a simple example powered by genetic algorithm. Make sure you have gone through the genetic algorithm chapter at least once because i am not going to explain the entire detail of genetic algorithm but i will rather focus on the example i am going to demonstrate. This program is very much simple, which displays a text 'python' in console, and is coded implementing genetic algorithm.

Pseudo Code
Source Code

Description of the above code:-
We encode the so called genes directly using characters. The first step in problem solving using genetic algorithm is generating a random population of any size, preferably large. This task is accomplished by the generate_population() function. The second step is to find the fitness associated with each member of the population and sort the population according to the fitness. I have calculated the ASCII value difference between the target string and the member of the population to find the fitness. Therefore fit members have less fitness value and are on the top of the population list contrary to the fact that fit members will have large fitness value. However, both are meant to sort the creatures of the population. We have achieved it the other way. We then randomly select two parents and perform the sexual reproduction. This random selection is carried out by selecting random members from the top of the population, indicated by ELITISMSIZE. This kind of selection strategy is called Truncation Selection. Random single crossover point is chosen for reproduction. The child thus reproduced is put under the probable condition of mutation rate. Once children equal to the size of the population is produced, they are placed in the population, in addition to calculating their fitness. This process is carried out for large generations, here 100.
Things are not as complicated as it seems. The code is lengthy because i have put the member along with its fitness value in a list in the list of population.
P.S. It is better to opt for Biased Roulette Wheel Selection for selecting parents. The proposed program still lacks optimization thereby converging on the local optima.

0 comments:

Post a Comment