I am trying to understand how genetic algorithms work. As with everything, I learn by attempting to write something on my own;however, my knowledge is very limited and I am not sure if I am doing this right.
The purpose of this algorithm is to see how long it will take half of a herd to be infected by a disease if half of that population are already infected. It is just an example I came up with in my head so I am not sure if this would even be a viable example.
Some feedback on how I can improve my knowledge would be nice.
Here is the code:
import random def disease(): herd =  generations = 0 pos = 0 for x in range(100): herd.append(random.choice('01')) print herd same = all(x == herd for x in herd) while same == False: same = all(x == herd for x in herd) for animal in herd: try: if pos != 0: after = herd[pos+1] before = herd[pos-1] if after == before and after == '1' and before == '1' and animal == '0': print "infection at", pos herd[pos] = '1' #print herd pos += 1 except IndexError: pass pos = 0 generations += 1 random.shuffle(herd) #print herd print "Took",generations,"generations to infect all members of herd." if __name__ == "__main__": disease()
I am looking for comments based on the logic of this algorithm. What can be improved, and what has to be improved to make this a true genetic algorithm if it isn't one already.