450 likes | 465 Views
COMP305. Part II. Genetic Algorithms. Topic 4. Basic Genetic Algorithm. GAs by John Holland. Holland introduced a “population” of binary strings which he called “chromosomes”.
E N D
COMP305. Part II. Genetic Algorithms. Genetic Algorithms
Topic 4. Basic Genetic Algorithm. Genetic Algorithms
GAs by John Holland. • Holland introduced • a “population” of binary strings which he called “chromosomes”. • The “population” evolves using kind of “natural selection” together with the genetics-inspired operators of crossover, mutation, and inversion. • Bits in a “chromosome” represent genes, and each “gene” is an instance of a particular “allele”, 0 or 1. • The selection operator chooses those chromosomes in the population that will be allowed to reproduce, and on average the fitter chromosomes produce more offspring than the less fit ones. • Crossover exchange subparts of two chromosomes • Mutation randomly changes the allele values of some locations in the chromosome. • Inversion reverses the order of a contiguous section of the chromosome rearranging the genes order. Genetic Algorithms
Basic Structure of a Genetic Algorithm. • Randomly generate initial population of nbit strings (“chromosomes”) • Evaluate the fitness of each string in the population • 3. Repeat the following steps until next generation of n individual strings produced • a.Select pair of parent chromosomes from current population • according to their fitness, i.e. chromosomes with higher fitness • are selected more often • b. Apply crossover (with probability) • c. Apply mutation (with probability of occurrence) • 4. Apply generational replacement • 5. Go to 2 or terminate if termination condition met Genetic Algorithms
Basic Structure of a Genetic Algorithm. • Randomly generate initial population of nbit strings (“chromosomes”) • Evaluate the fitness of each string in the population • 3. Repeat the following steps until next generation of n individual strings produced • a.Select pair of parent chromosomes from current population • according to their fitness, i.e. • chromosomes with higher fitness • are selected more often • b. Apply crossover (with probability) • c. Apply mutation (with probability of occurrence) • 4. Apply generational replacement • 5. Go to 2 or terminate if termination condition met • Each iteration in the cycle • produces a new “generation” of chromosomes. • The entire set of generations is called a run. • Typical GA run is from 50 to 500 or more generations. • At the end of a run often there is at least one highly fit chromosome in the population. Genetic Algorithms
Example Implementation of a GA. • Let the length of the string l = 8, and the number of chromosomes in the population (population size) n = 4. • Fitness functionf(x) is equal to the number of ones in the bit string x. • Selection operator. Fitness-proportionate selection, i.e. the number of times an individual is expected to reproduce is equal to its fitness fidivided by the average fitness of the population fNi= fi/ f • Crossover probabilitypc = 0.7 • Mutation probabilitypm = 0.001 • Make a run ofthree generations. Genetic Algorithms
Example of a Genetic Algorithm. • The initial randomly generated population might look like this: • Chromosome Chromosome • index string • 1 00000110 • 2 11101110 • 3 00100000 • n= 4 00110010 Generation 0 l= 8 Genetic Algorithms
Example of a Genetic Algorithm. 2.Evaluate the fitness of each string in the population Chromosome Chromosome Fitness = number of ones index string in the string 1 00000110 f1 = 2 2 11101110 f2= 6 3 00100000 f3 =1 n = 4 00110010 f4 =3 Generation 0 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 1 00000110 f1 = 2 N1= f1 / f = 2/3 1 • 2 11101110 f2= 6 N2 = f2/ f =2 • 3 00100000 f3 =1 N3 = f3/ f =1/3 0 • n = 4 00110010 f4 =3 N4 = f4/ f =1 • Average fitness f = (2+6+1+3)/4 = 3 Generation 0 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 1 00000110 f1 = 2 N1 1 • 2 11101110 f2= 6 N2 = 2 • 3 00100000 f3 =1 N3 0 • n = 4 00110010 f4 =3 N4 =1 • Thus, chromosome number 1 shall be selected one time, • chromosome number 2 shall be selected two times, • chromosome number 3 shall not be selected at all, • chromosome number 4 shall be selected one time. Generation 0 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 1 00000110 f1 = 2 N1 1 • 2 11101110 f2= 6 N2 = 2 • 3 00100000 f3 =1 N3 0 • n = 4 00110010 f4 =3 N4 = 1 • Thus, string number 1 shall be selected one time • string number 2 shall be selected two times • string number 3 shall not be selected at all • string number 4 shall be selected one time Generation 0 Forming pairs of “parents” for crossover: 1st pair: strings2 and 1, 2nd pair: strings 2 and 4 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Chromosome Chromosome Fitness times to be selected index string for reproduction 1 00000110 f1 = 2 N1 1 2 11101110 f2= 6 N2 = 2 3 00100000 f3 =1 N3 0 n = 4 00110010 f4 =3 N4 =1 Pairs of “parents” for crossover: 1st pair: strings2 and 12nd pair: strings 2 and 4 indexchromosomeindex chromosome 2 11101110 2 11101110 1 00000110 4 00110010 Generation 0 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each pair of parental chromosomes. If the random number rproduced by the generator for the pair of “parents” is less or equal to the crossover probability pc, then apply crossover to the “parents” at randomly chosen locus, otherwise the parents do not crossover. If a pair of parents do not undergo crossover, their offspring are their identical copies. Pairs of “parents” for crossover: r = 0.4 < pc => Apply crossover r = 0.8 > pc => No crossover 1st pair: strings2 and 12nd pair: strings 2 and 4 indexchromosomeindex chromosome 2 11101110 2 11101110 1 00000110 4 00110010 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Pairs of “parents” for crossover: r = 0.4 < pc => Apply crossover r = 0.8 > pc => No crossover 1st pair: strings2 and 12nd pair: strings 2 and 4 indexchromosomeindex chromosome 2 11101110 2 11101110 1 00000110 4 00110010 5 11000110 2 11101110 6 00101110 4 00110010 old new Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each new chromosome. If the random number r is less or equal to the mutation probability pm, apply mutation to the chromosome, i.e. flip a bit at randomly chosen locus. Mutation: 1st pair:2nd pair: index chromosome index chromosome r = 0.4 > pm => No mutation r = 0.8 > pm => No mutation 5 11000110 2 11101110 r = 0.1 > pm => No mutation r = 0.0005 < pm => Mutation 6 00101110 4 00110010 7 00110110 old new Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Mutation: 1st pair:2nd pair: index chromosome index chromosome 5 11000110 2 11101110 6 00101110 4 00110010 5 11000110 2 11101110 6 00101110 7 00110110 old new Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational replacement Replacement 1st pair:2nd pair: index chromosome index chromosome 2 11101110 2 11101110 1 00000110 4 00110010 5 11000110 2 11101110 6 00101110 7 00110110 Parents Offspring Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational Replacement Chromosome Chromosome Fitness times to be selected index string for reproduction 1 00000110 f1 = 2 N1 1 2 11101110 f2 = 6 N2 = 2 3 00100000 f3 = 1 N3 0 n = 4 00110010 f4 = 3 N4 =1 2 11101110 5 11000110 6 00101110 7 00110110 Generation 0 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational Replacement Chromosome Chromosome Fitness times to be selected index string for reproduction 1 00000110 f1 = 2 N1 1 2 11101110 f2 = 6 N2 = 2 3 00100000 f3 = 1 N3 0 n = 4 00110010 f4 = 3 N4 =1 2 11101110 5 11000110 Run for the second generation 6 00101110 7 00110110 Generation 0 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. 2.Evaluate the fitness of each string in the population Chromosome Chromosome Fitness = number of ones index string in the string 2 11101110 f1 = 6 5 11000110 f5 = 4 6 00101110 f6 = 4 7 00110110 f7 = 4 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 2 11101110 f2 = 6 N2 = f2 / f = 6/4.5 1 • 5 11000110 f5 = 4 N5 = f5 / f = 4/4.5 1 • 6 00101110 f6 = 4 N6 = f6 / f = 4/4.5 1 • 7 00110110 f7 = 4 N7 = f7 / f = 4/4.5 1 • Average fitness f = (6+4+4+4)/4 = 4.5 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 2 11101110 f2 = 6 N2 1 • 5 11000110 f5 = 4 N5 1 • 6 00101110 f6 = 4 N6 1 • 7 00110110 f7 = 4 N7 1 • Thus, chromosome number 2 shall be selected one time, • chromosome number 5 shall be selected one time, • chromosome number 6 shall be selected one time, and • chromosome number 7 shall be selected one time. Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 2 11101110 f2 = 6 N2 1 • 5 11000110 f5 = 4 N5 1 • 6 00101110 f6 = 4 N6 1 • 7 00110110 f7 = 4 N7 1 • Thus, string number 2 shall be selected one time, • string number 5 shall be selected one time, • string number 6 shall be selected one time, • string number 7 shall be selected one time. Generation 1 Forming pairs of “parents” for crossover: 1st pair: strings2 and 5, 2nd pair: strings 6 and 7 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Chromosome Chromosome Fitness times to be selected index string for reproduction 2 11101110 f2 = 6 N2 1 5 11000110 f5 = 4 N5 1 6 00101110 f6 = 4 N6 1 7 00110110 f7 = 4 N7 1 Pairs of “parents” for crossover: 1st pair: strings2 and 52nd pair: strings 6 and 7 indexchromosomeindex chromosome 2 11101110 6 00101110 5 11000110 7 00110110 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each pair of parental chromosomes. If the random number rproduced by the generator for the pair is less or equal to the crossover probability pc, apply crossover to the “parents” at randomly chosen locus. If a pair of parents do not undergo crossover, their offspring are their identical copies. Pairs of “parents” for crossover: r = 0.6 < pc => Apply crossover r = 0.1 < pc => Apply crossover 1st pair: strings2 and 52nd pair: strings 6 and 7 indexchromosomeindex chromosome 2 11101110 6 00101110 5 11000110 7 00110110 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Pairs of “parents” for crossover: r = 0.6 < pc => Apply crossover r = 0.1 < pc => Apply crossover 1st pair: strings2 and 52nd pair: strings 6 and 7 indexchromosomeindex chromosome 2 111011106 00101110 5 11000110 7 00110110 8 1110011010 00100110 9 1100111011 00111110 old new Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each new chromosome. If the random number r is less or equal to the mutation probability pm, apply mutation to the chromosome, i.e. flip a bit at randomly chosen locus. Mutation: 1st pair:2nd pair: index chromosome index chromosome r = 0.35 > pm => No mutation r = 0.1 > pm => No mutation 8 11100110 10 00100110 r = 0.47 > pm => No mutation r = 0.53 > pm => No mutation9 11001110 11 00111110 Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Mutation: 1st pair:2nd pair: index chromosome index chromosome 8 11100110 10 00100110 9 11001110 11 00111110 8 11100110 10 00100110 9 11001110 11 00111110 old new Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational replacement Replacement 1st pair:2nd pair: index chromosome index chromosome 2 11101110 6 00101110 5 11000110 7 00110110 8 11100110 10 00100110 9 11001110 11 00111110 Parents Offspring Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational Replacement Chromosome Chromosome Fitness times to be selected index string for reproduction 2 11101110 f2 = 6 N2 1 5 11000110 f5 = 4 N5 1 6 00101110 f6 = 4 N6 1 7 00110110 f7 = 4 N7 1 8 11100110 9 11001110 10 00100110 11 00111110 Generation 1 Generation 2 Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational Replacement Chromosome Chromosome Fitness times to be selected index string for reproduction 2 11101110 f2 = 6 N2 1 5 11000110 f5 = 4 N5 1 6 00101110 f6 = 4 N6 1 7 00110110 f7 = 4 N7 1 8 11100110 9 11001110 Run for the third generation 10 00100110 11 00111110 Generation 1 Generation 2 Genetic Algorithms
Example of a Genetic Algorithm. 2.Evaluate the fitness of each string in the population Chromosome Chromosome Fitness = number of ones index string in the string 8 11100110 f8 = 5 9 11001110f9 = 5 10 00100110 f10 = 3 11 00111110f11 = 5 Generation 2 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 8 11100110 f8 = 5 N8 = f8 / f = 5/4.5 1 • 9 11001110f9 = 5N9 = f9 / f = 5/4.5 1 • 10 00100110 f10 = 3N10 = f10 / f = 3/4.5 0 • 11 00111110 f11 = 5N11 = f11 / f = 5/4.5 2 • Average fitness f = (5+5+3+5)/4 = 4.5 Generation 2 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 8 11100110 f8 = 5 N8 1 • 9 11001110f9 = 5N9 1 • 10 00100110 f10 = 3N10 0 • 11 00111110 f11 = 5N11 2 • Thus, chromosome number 8 shall be selected one time, • chromosome number 9 shall be selected one time, • chromosome number 10 shall not be selected at all, and • chromosome number 11 shall be selected two times. Generation 2 Genetic Algorithms
Example of a Genetic Algorithm. • 3a.Select pair of parent chromosomes from current population • according to their fitness • Chromosome Chromosome Fitness times to be selected • index string for reproduction • 8 11100110 f8 = 5 N8 1 • 9 11001110f9 = 5N9 1 • 10 00100110 f10 = 3N10 0 • 11 00111110 f11 = 5N11 2 • Thus, string number 8 shall be selected one time • string number 9 shall be selected one time • string number 10 shall not be selected at all • string number 11 shall be selected two times Generation 2 Forming pairs of “parents” for crossover: 1st pair: strings8 and 11, 2nd pair: strings 9 and 11 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Chromosome Chromosome Fitness times to be selected index string for reproduction 8 11100110 f8 = 5 N8 1 9 11001110f9 = 5N9 1 10 00100110 f10 = 3N10 0 11 00111110 f11 = 5N11 2 Pairs of “parents” for crossover: 1st pair: strings8 and 112nd pair: strings 9 and 11 indexchromosomeindex chromosome 8 11100110 9 11001110 11 00111110 11 00111110 Generation 1 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each pair of parental chromosomes. If the random number rproduced by the generator for the pair is less or equal to the crossover probability pc, apply crossover to the “parents” at randomly chosen locus. If a pair of parents do not undergo crossover, their offspring are their identical copies. Pairs of “parents” for crossover: r = 0.83 > pc => No crossover r = 0.54 < pc => Apply crossover 1st pair: strings8 and 112nd pair: strings 9 and 11 indexchromosomeindex chromosome 8 11100110 9 11001110 11 00111110 11 00111110 Genetic Algorithms
Example of a Genetic Algorithm. 3b.Apply crossover (with probability) pc = 0.7 Pairs of “parents” for crossover: r = 0.83 > pc => No crossover r = 0.54 < pc => Apply crossover 1st pair: strings8 and 112nd pair: strings 9 and 11 indexchromosomeindex chromosome 8 11100110 9 11001110 11 00111110 11 00111110 8 11100110 12 11011110 11 00111110 13 00101110 old new Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Use a generator producing random numbers in the range between 0 and 1 to get a random number rfor each new chromosome. If the random number r is less or equal to the mutation probability pm, apply mutation to the chromosome, i.e. flip a bit at randomly chosen locus. Mutation: 1st pair:2nd pair: index chromosome index chromosome r = 0.5 > pm => No mutation r = 0.61 > pm => No mutation 8 11100110 12 11011110 r = 0.0008 < pm => Mutation r = 0.353 > pm => No mutation 11 00111110 13 00101110 14 01111110 old new Genetic Algorithms
Example of a Genetic Algorithm. 3c.Apply mutation (with probability of occurrence) pm = 0.001 Mutation: 1st pair:2nd pair: index chromosome index chromosome 8 11100110 12 11011110 11 00111110 13 00101110 8 11100110 12 11011110 14 01111110 13 00101110 old new Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational replacement Replacement 1st pair:2nd pair: index chromosome index chromosome 8 11100110 9 11001110 11 00111110 11 00111110 8 11100110 12 11011110 14 01111110 13 00101110 Parents Offspring Genetic Algorithms
Example of a Genetic Algorithm. 4.Apply generational Replacement Chromosome Chromosome Fitness times to be selected index string for reproduction 8 11100110 f8 = 5 N8 1 9 11001110f9 = 5N9 1 10 00100110 f10 = 3N10 0 11 00111110 f11 = 5N11 2 8 11100110 12 11011110 The third generation 13 00101110 14 01111110 STOP Generation 2 Generation 3 Genetic Algorithms
Example of a Genetic Algorithm. 2.Evaluate the fitness of each string in the population Chromosome Chromosome Fitness = number of ones index string in the string 8 11100110 f8 = 5 12 11011110 f12 = 6 13 00101110f13 = 4 14 01111110 f14 = 6 Average fitness f = (5+6+4+6)/4 = 5.25 Generation 3 Genetic Algorithms
Example of a Genetic Algorithm. 2.Evaluate the fitness of each string in the population Chromosome Chromosome Fitness = number of ones index string in the string 8 11100110 f8 = 5 12 11011110 f12 = 6 13 00101110f13 = 4 14 01111110 f14 = 6 f Average fitness f = (5+6+4+6)/4 = 5.25 The average fitness in the population of possible solutions increases with every generation. 01 2 3 Generation № Generation 3 5.0 3.0 1.0 Genetic Algorithms