- Please use python version >= 3.6 because typing and f-strings are used, you can check by running
python3 --version
- Please make sure you have
numpy
installed, you can do it by runningpip3 install numpy
- If you have Git installed, go on and clone the repository:
git clone https://github.com/senyaoh/genetic_algorithm_string_evolution.git
- To execute the program, simply run inside the repository folder:
python3 ga_string_evolution.py
This is a demostration of genetic algorthm by evolving random strings into a target string.
We first create a given population of random strings of the length of the target string.
We evaluate the fitness of a given string by comparing the character at the same index with the target string, if it matches, the fitness increased by 1, highest fitness is the length of the string, which means every character matches.
We then calculate the survival probability of each string by dividing the fitness of the string by the sum of fitness of all strings in current generation.
We randomly pick 2 strings based on their survival probability, the higher the probability the more likely they get picked for crossover.
We take the selected strings and combine them into a new string by randomly pick a character from one the string at each index.
Step 2 & 3 will be repeated for as many times as the size of the defined population to create a new generation.
We randomly pick strings in the new generation based on the given mutation rate and give them a mutation. We mutate a string by randomly select one character in the string and change it into another random character.
We repeat the above steps until a string that matches the target string is created.