Using GA’s to Solve Problems Amy Hoover What is a Genetic Algorithm? • Genetic Algorithm – “a search technique … to find exact or approximate solutions to optimization and search problems.” -Wikipedia Components of a GA • Chromosome – How we represent the individual • Fitness function – How we rate the individual • Mutation • Crossover How do GAs work? • Randomize initial population (of 5) – – – – – – Note: These were arbitraily picked A = 10111 B = 11100 C = 00110 D = 00001 E = 11011 • Evaluate the fitness of each individual (A, B, C, D, E) in the population – A = 10111 – B = 11100 – C = 00110 – D = 00001 – E = 11011 (fitness = 1+ 0 + 1 + 1+ 1 = 4) (fitness = 1 + 1 + 1+ 0 + 0 = 3) (fitness = 0 + 0 + 1+ 1 + 0 = 2) (fitness = 0 + 0 + 0+ 0 + 1 = 1) (fitness = 1 + 1 + 0+ 1 + 1 = 4) • Repeat – Select parents • Who would we choose from the previous generation? (A and E) – Mate the parents, they plus their offspring form next generation • How do we mate them? • What will the next generation look like? Next Generation • • • • • A = 10111 (parent 1 clone) E = 11011 (parent 2 clone) F=? G=? H=? • Which generation are F, G, and H from? Next Generation • • • • • A = 10111 (parent 1) E = 11011 (parent 2) F=? G=? H=? • Which generation are F, G, and H from? • How can we make F, G, H? Crossover and Mutation • Let’s make F, G, H (Gen 1 from initial generation 0) • • • • • A = 10111 (parent 1) E = 11011 (parent 2) F = 11111 (Mutate A) G = 11011(Crossover AE 2nd 3rd bits of E on A, also clone E ) H = 10011 (Crossover AE 3rd and 4th of E) • Which generation are F, G, and H from? • How can we make F, G, H? Calculate the fitness of each individual • • • • • A = 10111 (parent 1) E = 11011 (parent 2) F = 11111 (Mutate A) G = 11011 (Crossover AE) H = 10011 (Crossover AE) Calculate the fitness of each individual • • • • • A = 10111 = 4 E = 11011 = 4 F = 11111 = 5 G = 11011 = 4 H = 10011 = 3 • Select parents (F,G) • Repeat – Select parents (A, E, F, G, H) • How do we pick the parents? – Pick the top 2 from A, E, F, G, H – Mate parents (crossover, mutation) – Calculate fitness – (Repeat Select parents…) Example • Problem: I want to maximize the number of 0’s in a bit string – (I want A = 00000) • Let’s work through the psuedo code Example • Problem: I want to maximize the number of 0s in a bit string – (I want A = 00000) • Step 1: Randomize the initial population – A = 01101 – B = 11011 – C = 01010 – D = 01111 – E = 11111 Example • Initial Population (gen 0) – A = 01101 – B = 11011 – C = 01010 – D = 01111 – E = 11111 • Calculate fitness (recall 0’s are good, 1’s are bad, we want to reward good genes!) Example • Initial Population – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – B = 11011 = 0 + 0 + 1 + 0 + 0 = 1 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – D = 01111 = 1 + 0 + 0 + 0 +0 = 1 – E = 11111 = 0 + 0 + 0 + 0 +0 = 0 • Reward 0’s, not 1’s Example • Initial Population – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – B = 11011 = 0 + 0 + 1 + 0 + 0 = 1 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – D = 01111 = 1 + 0 + 0+ 0+ 0 = 1 – E = 11111 = 0 + 0 + 0 + 0 + 0 = 0 Example • Who should our parents be? (A and C) – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – B = 11011 = 0 + 0 + 1 + 0 + 0 = 1 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – D = 01111 = 1 + 0 + 0+ 0+ 0 = 1 – E = 11111 = 0 + 0 + 0 + 0 + 0 = 0 Example • Who should our parents be? – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 • What next? Mating! – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – F = 01100 = 3 – G = 01110 = 2 – H = 01011 = 2 Calculate Fitness! – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – F = 01100 = 3 – G = 01110 = 2 – H = 01011 = 2 Pick the top two! – A = 01101 = 1 + 0 + 0 + 1 + 0 = 2 – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – F = 01100 = 3 – G = 01110 = 2 – H = 01011 = 2 – Parents, C and F Start Over! – C = 01010 = 1 + 0 + 1 + 0 + 1 = 3 – F = 01100 = 3 –G=? –H=? –I=?