Download presentation
Presentation is loading. Please wait.
1
Chapter 14. Genetic Algorithms
Comp3710 Artificial Intelligence Computing Science Thompson Rivers University
2
Course Outline Part I – Introduction to Artificial Intelligence
Part II – Classical Artificial Intelligence, and Searching Knowledge Representation Searching Search Methodologies Advanced Search Genetic Algorithms (relatively new study area) Knowledge Represenation and Automated Reasoning Propositinoal and Predicate Logic Inference and Resolution for Problem Solving Rules and Expert Systems Part III – Machine Learning Part IV – Advanced Topics TRU-COMP3710 Genetic Algorithms
3
Chapter Objectives TRU-COMP3710 Genetic Algorithms
4
Chapter Outline Introduction – Why do we use Genetic Algorithms?
Representations – How to represent a problem The Algorithm Fitness – How to evaluate if a chromosome is good or bad Crossover – How to generate the next generation Mutation – How to escape local maxima Termination Criteria – When do we have to stop the algorithm? Examples – What can we learn from experiences? Why genetic algorithms work? Non-standard GAs Messy genetic algorithms Predators and coevolution Project ideas TRU-COMP3710 Genetic Algorithms
5
Reference Artificial Intelligence Illuminated, Ben Coppin, Jones and Bartlett Illuminated Series Introduction to Evolutionary Computing Genetic Algorithms, A. E. Eiben and J. E. Smith Java Genetic Programming Package GA Playground Very interesting Demo Traveling salesman problem Genetic programming Maze Marchers – TRU-COMP3710 Genetic Algorithms
6
1. Introduction TRU-COMP3710 Genetic Algorithms
7
Natural Evolution Darwin : The Origin of Species
Natural evolution has produced many optimal forms sharks, ichthyosaurs, and dolphins – very good at swimming albatross wings – very good at soaring Important principles used by natural evolution Assortment (diversity): recombination of genetic material Selection (cumulative improvement): survival of the fittest Random mutation (random walk) In this class, I am not taking any side of Evolutionism and Creationism. TRU-COMP3710 Genetic Algorithms
8
WordEvol: Cumulative Selection
Cumulative selection with simple random selection: randomly chooses a character for the first letter in the string, then randomly choose a character for second letter, and so on. [Q] What do you think of this idea? [Q] When we consider one trial as one generation, how many generations would it take to find the correct word? COMPUTER D C K P O R MPUTER D A S Z B F Y T X Q I P K R TRU-COMP3710 Genetic Algorithms
9
COMPUTER FHGLJGHP FHGLJG P A FHCLJGHP FHGLTGHP . . . FHGLJGHR
Cumulative selection with randomness over multiple offspring: 1. generates a random string 2. if it doesn’t match, 2.1 generates a new generation of mutant offspring 3. selects the single offspring most like the target 4. if it doesn’t match, go to 2.1 COMPUTER A B C D E F G H I J K L M N O P Q R S T U V W X Y Z FHGLJGHP 7 FHGLJG P A [Q] What do you think of this idea? [Q] When we consider the steps 2 and 3 as one generation, how many generations would it take to find the correct word? [Q] Diversity? Cumulative improvement? Random walk? 3 FHCLJGHP 5 FHGLTGHP 8 FHGLJGHR TRU-COMP3710 Genetic Algorithms
10
Genetic Algorithm – quick view
Topics Genetic Algorithm – quick view Developed: USA in the 1970’s Early names: J. Holland, K. DeJong, D. Goldberg Typically applied to: discrete optimization Attributed features: not too fast good heuristic for combinatorial problems Special Features: Traditionally emphasizes combining information from good parents (crossover) many variants, e.g., reproduction models, operators TRU-COMP3710 Genetic Algorithms
11
2. Representation How to represent a solution (or state) for a given problem? Genetic techniques can be applied with a range of representations. Usually, we have a population of chromosomes (individuals) that are Usually a string of bits or numbers A complete representation of solution Each chromosome (individual) consists of a number of genes. Other representations are equally valid. Nowadays it is generally accepted that it is better to encode numerical variables directly as Integers Floating point variables Permutation representation: sorting; traveling salesman problem TRU-COMP3710 Genetic Algorithms
12
Topics Encoding (representation of a solution) is very important. Genotype space = {0,1}L Phenotype space Encoding (representation) Decoding (inverse representation) Actual observed properties Full hereditary information TRU-COMP3710 Genetic Algorithms
13
Each individual is a goal candidate.
Topics 3. The Algorithm The algorithm is used as follows: Generate a random population of chromosomes (, the first generation); While (the termination criteria are not satisfied) Determine the fitness of each chromosome (individual); Apply crossover to selected chromosomes from the current generation to generate a new population of chromosomes (the next generation); Fitter chromosomes have more chances to survive to the next generation. (Survival of the fittest) Each pair of parents produces two offspring and those offspring replace the parents in the next generation. Apply mutation to a new population; A little bit of mutation is applied to offspring. [Q] The representation of chromosomes (individuals)? [Q] The size of the population? [Q] The crossover rate?: ~0.8 => the parents can survive in the n.g. ? [Q] The mutation rate?: ~0.001 Each individual is a goal candidate. TRU-COMP3710 Genetic Algorithms
14
4. Fitness and Selection Fitness is an important concept in genetic algorithms. The fitness of a chromosome determines how likely it is that it will reproduce. Fitness is usually measured in terms of how well the chromosome solves some goal problem. Fitness can also be subjective. TRU-COMP3710 Genetic Algorithms
15
E. g. , sorting (Just for explanation
E.g., sorting (Just for explanation. There are many good deterministic sorting algorithms.) [Q] Chromosome for the sorting problem of numbers? Representation of solutions? ( ), ( ), ... If the genetic algorithm is to be used to sort numbers, then the fitness of a chromosome will be determined by how close to a correct sorting it produces. Fitness idea 1: For each element in a list, count the number of right side elements that are bigger than the element; and add the counts. ( ) -> = 3; ( ) -> ??? = 4 Fitness idea 2: Count the number of pairs of adjacent elements, in which the left element is smaller than the right element. ( ) -> 1; ( ) -> ??? [Q] Which idea looks better? TRU-COMP3710 Genetic Algorithms
16
Selection B A C Fitness(A) = 3 Fitness(B) = 1 Fitness(C) = 2
Roulette wheel selection Age based selection … A C 1/6 = 17% 3/6 = 50% B 2/6 = 33% Fitness(A) = 3 Fitness(B) = 1 Fitness(C) = 2 [Q] How to implement? [50 | 67(=50+17) | 100=(67+33)] Random floating point number in [0, 100) [0, 50) => A, [50, 67) => ?, [67, 100) => C TRU-COMP3710 Genetic Algorithms
17
What is roulette_wheel[fitnesses.length-1]?
var fitnesses = []; ... var total = 0; for (var i = 0; i < fitnesses.length; i++) total += fitnesses[i]; var fitness_ratios = []; for (var i = 0; i < fitnesses.length; i++); fitness_ratios[i] = ???; var roulette_wheel = []; roulette_wheel[0] = fitness_ratios[0]; for (var i = 1; i < fitness_ratios.length; i++) ???; What is roulette_wheel[fitnesses.length-1]? TRU-COMP3710 Genetic Algorithms
18
Topics var r; r = Math.random(); // r is in [0, 1); var i; for (i = 0; i < roulette_wheel.length; i++) ??? TRU-COMP3710 Genetic Algorithms
19
5. Crossover Single-point crossover is applied as follows:
Select a random crossover point. Break each chromosome into two parts, splitting at the crossover point. Recombine the broken chromosomes by combining the front of one with the back of the other, and vice versa, to produce two new chromosomes. TRU-COMP3710 Genetic Algorithm
20
Single-point crossover
More likely to keep together genes that are near each other. Can never keep together genes from opposite ends of string. This is known as Positional Bias. [Q] What if all the individuals have wrong genes at the left side? Can be exploited if we know about the structure of our problem, but this is not usually the case. [Q] How to solve? TRU-COMP3710 Genetic Algorithm
21
[Q] Can this idea solve the positional bias problem?
Idea 1: Usually, crossover is applied with one crossover point, but can be applied with more, such as in two-point crossover that has two crossover points: [Q] Can this idea solve the positional bias problem? Idea 2: Uniform crossover involves using a probability to select which genes to use from chromosome 1, and which from chromosome 2. Single offspring can be produced by uniform crossover … TRU-COMP3710 Genetic Algorithm
22
[Q] Do we always have to generate new offspring?
Topics [Q] Do we always have to generate new offspring? Cloning is possible with the crossover rate of ~0.8. [Q] what does this mean? TRU-COMP3710 Genetic Algorithm
23
6. Mutation A unary operator – applies to one chromosome.
Topics 6. Mutation A unary operator – applies to one chromosome. Randomly selects some bits (genes) to be “flipped” 1 => 0 and 0 =>1 Mutation is usually applied with a low probability, such as 1 in 1000. [Q] Why is mutation necessary??? Mutation-only-GA is possible, crossover-only-GA would not work. Crossover makes a big jump to an area somewhere “in between” two (parent) areas. Mutation creates random small diversions, thereby staying near (in the area of ) the parent. To hit the optimum you often need a ‘lucky’ mutation. TRU-COMP3710 Genetic Algorithm
24
Topics 7. Termination Criteria A genetic algorithm is run over a number of generations until the termination criteria are reached. Typical termination criteria are: Stop after a fixed number of generations. Stop when a chromosome reaches a specified fitness level. Stop when a chromosome succeeds in solving the problem, within a specified tolerance. Human judgement can also be used in some more subjective cases. TRU-COMP3710 Genetic Algorithm
25
8. Examples Optimization of a mathematical function
Traveling salesman problem Maze marcher Sorting TRU-COMP3710 Genetic Algorithm
26
E.g. - optimizing a mathematical function
A genetic algorithm can be used to find the highest integer value for f(x) = sin (x) within [0, 15]. [Q] How do you want to encode the problem? Variables? Representation of solutions? Each chromosome consists of 4 bits, to represent the values of x from 0 to 15. E.g., (1,0,0,1) [Q] How to measure fitness of a chromosome? 0 (f(x) = –1) to 100 (f(x) = 1). [Q] How? Fitness(x) = 50 * (f(x) + 1) = 50 * (sin (x) + 1) By applying the genetic algorithm it takes just a few generations to find the optimal solution for f(x). TRU-COMP3710 Genetic Algorithm
27
The first generation The fitness function
The fitness function Fitness(x) = 50 * (f(x) + 1) = 50 * (sin(x) + 1), and fitness values are … Using the random numbers between 0 and 100, and the fitness ratio, chromosomes are selected to produce offspring. [Q] HOW??? Roulette wheel selection [Q] What if only the fittest chromosome is used to produce the next generation? Chromosome Fitness(x) Fitness ratio 1001 70.61 0.463 0011 57.06 0.374 1010 22.80 0.149 0101 2.05 0.134 70.61 / ( ) TRU-COMP3710 Genetic Algorithm
28
Selection based on fitness ratios
8. Selection based on fitness ratios Crossover 10| |11 => 10| |11 => ??? [Q] The next generation and fitness values? Chromosome Fitness(x) Fitness ratio 1011 0001 92.07 0.481 1000 99.47 0.519 TRU-COMP3710 Genetic Algorithm
29
E.g. - traveling salesman problem
Given n cities, find a complete tour with the minimal length. [Q] Is the problem a CSP? [Q] How to encode? Representation of solutions? Label the cities 1, 2, … , n, and ? One complete tour is one permutation. (But, for n = 4, [1,2,3,4] and [3,?,?,?] are the same.) Search space is really BIG: For 30 cities there are 30! 1032 possible tours. Demo Many web sites; TRU-COMP3710 Genetic Algorithm
30
1 2 3 4 5 Fitness [Q] Do we know a minimal path?
[Q] Then, what kind of fitness function can be used? Let’s assume that for each pair of two cities, the shortest path is already determined. [Q] Can we determine the length of a tour? For (1, 2, 3, 4, 5), the length = D(1, 2) + D(2, 3) + D(3, 4) + D(4, 5) + D(5, 1) The shorter, the better. [Q] Let a, b, c, d are the lengths of 4 tours. Then fitness ratios??? TRU-COMP3710 Genetic Algorithm
31
Fitness [Q] Let a, b, c, d are the lengths of 4 tours. Then fitness ratios??? E.g., a = 10, b=20, c=30, d=40 (The smaller, the better.) Idea 1: Like the example of n-queens puzzle Let x = a + b + c + d; y = (x – a) + (x – b) + (x – c) + (x – d) The fitness ratio of a can be defined as (x – a) / y. => x = 100; y = = 300 => 9/30, 8/30, 7/30, 6/30 not much distinctive Idea 2: Let x = a^2 + b^2 + c^2 + d^2; y = (x – a^2) + (x – b^2) + (x – c^2) + (x – d^2) The fitness ratio of a can be defined as (x – a^2) / y. => x = 3000; y = = 9000 => 29/90, 26/90, 21/90, 16/90 still not much distinctive Idea 3: Let x = 1/a + 1/b + 1/c + 1/d The fitness ratio of a can be defined as (1/a) / x. (The larger the better) => 12/25, 6/25, 4/25, 3/25 => a has better chance than the ideas 1 and 2. TRU-COMP3710 Genetic Algorithm
32
Example 4 cities – 1, 2, 3, 4 Distances
4 individuals – [1, 2, 3, 4], [1, 3, 2, 4], [2, 4, 3, 1], [4, 1, 2, 3] Fitness using the Idea 3 in the previous slide? Lengths – 70, 100, 70, 70 x = 1/70 + 1/ /70 + 1/70 = 37/700 fitness1 = (1/70)/(37/700) = 10/37, fitness2 = 7/37, fitness3 = 10/37, fitness4 = 10/37 Roulette wheel? 10 20 30 TRU-COMP3710 Genetic Algorithm
33
Crossover “Normal” crossover operators will often lead to inadmissible solutions. Idea 1: a) Copy randomly selected set from first parent b) Copy rest from second parent while preserving the order in second parent Any problem??? Why??? [Q] The other one??? TRU-COMP3710 Genetic Algorithm
34
Idea 2: Random single point crossover Other part comes from the other parent while preserving the order following the last city in the selected part TRU-COMP3710 Genetic Algorithm
35
How to mutate? Idea 1: Insert Mutation for permutations
Pick two cities at random Move the second to follow the first, shifting the rest along to accommodate Note that this preserves most of the order and the adjacency information Idea 2: Swap mutation for permutations Pick two cities at random and swap their positions Preserves most of adjacency information (4 links broken), disrupts order more Inversion Scramble ... TRU-COMP3710 Genetic Algorithm
36
Termination criterion?
8. Termination criterion? TRU-COMP3710 Genetic Algorithm
37
8. E.g. – maze marchers TRU-COMP3710 Genetic Algorithm
38
E.g. – sorting for experiment
Chromosome ??? Lists E.g., ( ), ( ), ... Fitness ??? Fitness idea 1: For each element in a list, count the number of right side elements that are bigger than the element; and add the counts. ( ) -> = 3; ( ) -> = 4 Fitness idea 2: Count the number of pairs of adjacent elements, in which the left element is smaller than the right element. ( ) -> 1; ( ) -> 1 [Q] Crossover ??? [Q] Mutation ??? Termination criteria ??? TRU-COMP3710 Genetic Algorithm
39
[Q] Crossover ??? [Q] Mutation ??? Termination criteria ???
[Q] Simple crossover ??? ( ) and ( ) -> ??? ( ) and ( )? [Q] Then? Idea 1: Can we use the crossover in the traveling salesman problem? ( ) and ( ) // problem? Idea 2: A bit different from the above crossover, so that the elements coming from the spouse have the same order. ( ) and ( ) Can you evaluate them to see an improvement? Idea … [Q] Mutation ??? Termination criteria ??? TRU-COMP3710 Genetic Algorithm
40
Mutation ??? Termination criteria ??? 8. Topics
Idea 1: Exchange of adjacent elements that are randomly chosen ( ) -> ??? Mutation rate ??? Termination criteria ??? Idea 1: Until the algorithm finds the sorted list Idea 2: Limited number of generations Idea 3: Until the overall fitness reaches a certain threshold Idea 3: Until the difference of overall fitness becomes smaller than a threshold Idea 4: Combination of the above ideas TRU-COMP3710 Genetic Algorithm
41
9. Why does GA work well? In many cases, genetic algorithms quickly produce optimal or near-optimal solutions to combinatorial problems that would otherwise be impractical to solve. Why do genetic algorithms work well? It is possible to explain genetic algorithms by comparison with natural evolution: small changes that occur on a selective basis combined with reproduction will tend to improve the fitness of the population over time. TRU-COMP3710 Genetic Algorithm
42
The Building Block Hypothesis
9. The Building Block Hypothesis Topics Genetic algorithms manipulate short, low-order, high fitness schemata in order to find optimal solutions to problems. These short, low-order, high fitness schemata are known as building blocks. Hence genetic algorithms work well when small groups of genes that are close together represent useful features in the chromosomes. (Try to recall TSP.) This tells us that it is important to choose a correct representation. TRU-COMP3710 Genetic Algorithm
43
10. Non-Standard GAs Genetic algorithms can be deceived by fit building blocks that happen not to combine to give the optimal solutions. Deception can be avoided by inversion: this involves reversing the order of a randomly selected group of bits within a chromosome. The inversion rate is ~0.001. Other ideas Messy GAs Co-evolution TRU-COMP3710 Genetic Algorithm
44
Messy Genetic Algorithms
An alternative to standard genetic algorithms, which avoids deception. Each bit in the chromosome is represented as a (position, value) pair. For example: ((1,0), (2,1), (4,0)) In this case, the third bit is undefined, which is allowed with MGAs. Underspecified bits are filled with bits taken from a template chromosome. The template chromosome is usually the best performing chromosome from the previous generation. A bit can also overspecified: ((1,0), (2,1), (3,1), (3,0), (4,0)) Overspecified bits are usually dealt with by working from left to right and using the first value specified for each bit. TRU-COMP3710 Genetic Algorithm
45
Messy Genetic Algorithms
10. Messy Genetic Algorithms MGAs use splice and cut instead of crossover. Splicing involves simply joining two chromosomes together: ((1,0), (3,0), (4,1), (6,1)) ((2,1), (3,1), (5,0), (7,0), (8,0)) ((1,0), (3,0), (4,1), (6,1), (2,1), (3,1), (5,0), (7,0), (8,0)) Cutting involves splitting a chromosome into two: ((1,0), (3,0), (4,1)) ((6,1), (2,1), (3,1), (5,0), (7,0), (8,0)) TRU-COMP3710 Genetic Algorithm
46
10. Co-Evolution Topics In the real world, the presence of predators is responsible for many evolutionary developments. Similarly, in many artificial life systems, introducing “predators” produces better results. Example This process is known as co-evolution. For example, Ramps, which were evolved to sort numbers: “parasites” were introduced which produced sets of numbers that were harder to sort, and the ramps produced better results. Fish tank TRU-COMP3710 Genetic Algorithm
47
Project idea Topics Genetic algorithm for Local search for
The n-queens problem The class scheduling problem Local search for The traveling salesman problem TRU-COMP3710 Advanced Search
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.