Friday, 15 August 2014

python - What is wrong with my Genetic Algorithm -



python - What is wrong with my Genetic Algorithm -

i trying understand how genetic algorithms work. larn attempting write on on;however, knowledge limited , not sure if doing right.

the purpose of algorithm see how long take half of herd infected disease if half of population infected. illustration came in head not sure if viable example.

some feedback on how can improve knowledge nice.

here code:

import random def disease(): herd = [] generations = 0 pos = 0 x in range(100): herd.append(random.choice('01')) print herd same = all(x == herd[0] x in herd) while same == false: same = all(x == herd[0] x in herd) animal in herd: try: if pos != 0: after = herd[pos+1] before = herd[pos-1] if after == before , after == '1' , before == '1' , 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 infect members of herd." if __name__ == "__main__": disease()

your code not implement geneticalgorithm. suggest start first open source library understand how work before implement own (if needed)

to have genetic algorithm need following:

1- objective function trying minimize

2- chromosome representation (e.g. real value) model decision variables in objective function. target find best chromosome minimize objective function

3- initial population of chromosomes start search (can random)

4- genetic operators i.e. selection, crossover , mutation apply current population next generation

5- iterate until reach stopping criterion e.g. maximum number of generation or desired fitness value

this brief description genetic algorithm implementation should have.

python algorithm genetic-algorithm genetic

No comments:

Post a Comment