Download presentation
Presentation is loading. Please wait.
1
Genetic Algorithms Chih-Hao Lu
2
陸志豪副教授 生物醫學研究所 chlu@mail.cmu.edu.tw http://lulab.cmu.edu.tw 學歷
國立交通大學生物資訊所 博士 專長 結構生物資訊、計算生物學、電腦輔助藥物設計、 演化式計算與機器學習 研究領域 蛋白質區域結構模組與功能預測 蛋白質結構與動力學的相關研究 蛋白質與分子的交互作用相關研究 虛擬藥物篩選
3
Machine Learning Machine learning is the subfield of computer science that gives computers the ability to learn without being explicitly programmed. Machine learning explores the study and construction of algorithms that can learn from and make predictions on data. Optimization problems
4
Approaches Decision tree learning Association rule learning
Artificial neural networks Deep learning Inductive logic programming Support vector machines Clustering Bayesian networks Reinforcement learning Representation learning Similarity and metric learning Sparse dictionary learning Genetic algorithms Rule-based machine learning Learning classifier system
5
Genetic Algorithms - History
Originally developed by John Holland (1975) Got popular in the late 1980’s Inspired by the biological evolution process Uses concepts of “Natural Selection” and “Genetic Inheritance” (Darwin 1859) Can be used to solve a variety of problems that are not easy to solve using other techniques Particularly well suited for hard problems where little is known about the underlying search space Widely-used in business, science and engineering
6
Evolution in the real world
Each cell of a living thing contains chromosomes - strings of DNA Each chromosome contains a set of genes - blocks of DNA Each gene determines some aspect of the organism (like eye colour) A collection of genes is sometimes called a genotype A collection of aspects (like eye colour) is sometimes called a phenotype Reproduction involves recombination of genes from parents and then small amounts of mutation (errors) in copying The fitness of an organism is how much it can reproduce before it dies Evolution based on “survival of the fittest”
7
Genetic Algorithm versus Nature
Environment Optimization problem Individuals living in that environment Feasible solutions Individual’s degree of adaptation to its surrounding environment Solutions quality (fitness function)
8
Genetic Algorithm versus Nature
A population of organisms (species) A set of feasible solutions Selection, recombination and mutation in nature’s evolutionary process Stochastic operators Evolution of populations to suit their environment Iteratively applying a set of stochastic operators on a set of feasible solutions
9
Start with a Dream… Suppose you have a problem
You don’t know how to solve it What can you do? Can you use a computer to somehow find a solution for you? This would be nice! Can it be done?
10
A “blind generate and test” algorithm:
A dumb solution A “blind generate and test” algorithm: Repeat Generate a random possible solution Test the solution and see how good it is Until solution is good enough
11
Can we use this dumb idea?
Sometimes - yes: if there are only a few possible solutions and you have enough time then such a method could be used For most problems - no: many possible solutions with no time to try them all so this method can not be used
12
A “less-dumb” idea Generate a set of random solutions Repeat
Test each solution in the set Remove some bad solutions from set Duplicate some good solutions Make small changes to some of them Until best solution is good enough
13
Genetic Algorithm Generate a set of random solutions (Initialization & Representation) Repeat Test each solution in the set Remove some bad solutions from set Duplicate some good solutions Make small changes to some of them Until best solution is good enough
14
How do you encode a solution?
Obviously this depends on the problem! GA’s often encode solutions as fixed length “bitstrings” (e.g , , ) Each bit represents some aspect of the proposed solution to the problem For GA’s to work, we need to be able to “test” any string and get a “score” indicating how “good” that solution is
15
Silly Example - Drilling for Oil
Imagine you had to drill for oil somewhere along a single 1km desert road Problem: choose the best place on the road that produces the most oil per day We could represent each solution as a position on the road Say, a whole number between [ ]
16
Where to drill for oil? 500 1000 Road Solution2 = 900 Solution1 = 300
17
Digging for Oil The set of all possible solutions [ ] is called the search space In this case it’s just one number but it could be many numbers or symbols Often GA’s code numbers in binary producing a bitstring representing a solution In our example we choose 10 bits which is enough to represent
18
Convert to binary string
512 256 128 64 32 16 8 4 2 1 900 300 1023 In GA’s these encoded strings are sometimes called genotypes or chromosomes and the individual bits are sometimes called genes
19
Summary We have seen how to: represent possible solutions as a number
encoded a number into a binary string generate a score for each number given a function of “how good” each solution is - this is often called a fitness function Our silly oil example is really optimisation over a function f(x) where we adapt the parameter x
20
When choosing an encoding method (Representation) rely on the following key ideas:
Use a data structure as close as possible to the natural representation If possible, ensure that all genotypes correspond to feasible solutions Write appropriate genetic operators as needed If possible, ensure that genetic operators preserve feasibility
21
Genetic Algorithm Generate a set of random solutions (Initialization & Representation) Repeat Test each solution in the set (Evaluation) Remove some bad solutions from set Duplicate some good solutions Make small changes to some of them Until best solution is good enough
22
Evaluation (fitness function)
Solution is only as good as the evaluation function; choosing a good one is often the hardest part Similar-encoded solutions should have a similar fitness
23
Drilling for Oil Solution1 = 300 (0100101100)
Road 1000 O I L 30 5 Location
24
Search Space For a simple function f(x) the search space is one dimensional. But by encoding several values into the chromosome many dimensions can be searched e.g. two dimensions f(x,y) Search space can be visualised as a surface or fitness landscape in which fitness dictates height Each possible genotype is a point in the space A GA tries to move the points to better places (higher fitness) in the the space
25
Fitness landscapes
26
Search Space Obviously, the nature of the search space dictates how a GA will perform A completely random space would be bad for a GA Also GA’s can get stuck in local maxima if search spaces contain lots of these Generally, spaces in which small improvements get closer to the global optimum are good
27
Genetic Algorithm Generate a set of random solutions (Initialization & Representation) Repeat Test each solution in the set (Evaluation) Remove some bad solutions from set (Selection) Duplicate some good solutions (Recombination) Make small changes to some of them (Mutation) Until best solution is good enough
28
Reproduction This is done by selecting two parents during reproduction and combining their genes to produce offspring Two high scoring “parent” bit strings (chromosomes) are selected and with some probability (crossover rate) combined Producing two new offspring (bit strings) Each offspring may then be changed randomly (mutation)
29
Selecting Parents Many schemes are possible so long as better scoring chromosomes more likely selected Score is often termed the fitness “Roulette Wheel” selection can be used: Add up the fitness's of all chromosomes Generate a random number R in that range Select the first chromosome in the population that - when all previous fitness’s are added - gives you at least the value R
30
Example population No. Chromosome Fitness 1 1010011010 2 1111100001 3
4 5 6 7 8
31
Roulette Wheel Selection
1 2 3 4 5 6 7 8 1 2 3 1 3 5 1 2 Random[0..18] = 7 Chromosome4 Parent1 Random[0..18] = 12 Chromosome6 Parent2 18
32
Crossover - Recombination
Parent1 Offspring1 Parent2 Offspring2 Crossover single point - random With some high probability (crossover rate) apply crossover to the parents. (typical values are 0.8 to 0.95)
33
Mutation mutate Offspring1 Offspring1 Offspring2 Offspring2 Original offspring Mutated offspring With some small probability (the mutation rate) flip each bit in the offspring (typical values between 0.1 and 0.001)
34
Genetic Algorithm Generate a set of random solutions (Initialization & Representation) Repeat (Generation) Test each solution in the set (Evaluation) Remove some bad solutions from set (Selection) Duplicate some good solutions (Recombination) Make small changes to some of them (Mutation) Until best solution is good enough (Termination)
35
Termination condition
Examples: A pre-determined number of generations or time has elapsed A satisfactory solution has been achieved No improvement in solution quality has taken place for a pre-determined number of generations
36
Genetic Algorithm Generate a set of random solutions (Representation)
Repeat (Generation) Test each solution in the set (Evaluation) Remove some bad solutions from set (Selection) Duplicate some good solutions (Recombination) Make small changes to some of them (Mutation) Until best solution is good enough (Termination)
37
Many Variants of GA Different kinds of selection (not roulette)
Tournament Elitism, etc. Different recombination Multi-point crossover 3 way crossover etc. Different kinds of encoding other than bit string Integer values Ordered set of symbols Different kinds of mutation
38
Many parameters to set Any GA implementation needs to decide on a number of parameters: Population size (N), mutation rate (m), crossover rate (c) Often these have to be “tuned” based on results obtained - no general theory to deduce good values Typical values might be: N = 50, m = 0.05, c = 0.9
39
Why does crossover work?
A lot of theory about this and some controversy Holland introduced “Schema” theory The idea is that crossover preserves “good bits” from different parents, combining them to produce better solutions A good encoding scheme would therefore try to preserve “good bits” during crossover and mutation
40
Example: the MAXONE problem
Suppose we want to maximize the number of ones in a string of l binary digits Is it a trivial problem? It may seem so because we know the answer in advance However, we can think of it as maximizing the number of correct answers, each encoded by 1, to l yes/no difficult questions
41
Example (cont) An individual is encoded (naturally) as a string of l binary digits The fitness f of a candidate solution to the MAXONE problem is the number of ones in its genetic code We start with a population of n random strings. Suppose that l = 10 and n = 6
42
Example (initialization)
We toss a fair coin 60 times and get the following initial population: s1 = f (s1) = 7 s2 = f (s2) = 5 s3 = f (s3) = 7 s4 = f (s4) = 4 s5 = f (s5) = 8 s6 = f (s6) = 3
43
Example (selection) Next we apply fitness proportionate selection with the roulette wheel method: 𝑓(𝑖) 𝑖 𝑓(𝑖) Individual i will have a probability to be chosen We repeat the extraction as many times as the number of individuals we need to have the same parent population size (6 in our case) 1 Area is Proportional to fitness value 2 n 3 4
44
Example (selection) Suppose that, after performing selection, we get the following population: s1` = (s1) s2` = (s3) s3` = (s5) s4` = (s2) s5` = (s4) s6` = (s5)
45
Example (crossover) For each couple we decide according to crossover probability (for instance 0.6) whether to actually perform crossover or not Suppose that we decide to actually perform crossover only for couples (s1`, s2`) and (s5`, s6`). For each couple, we randomly extract a crossover point, for instance 2 for the first and 5 for the second s1` = s2` = s5` = s6` = Before crossover: After crossover: s1`` = s2`` = s5`` = s6`` =
46
Example (mutation) The final step is to apply random mutation: for each bit that we are to copy to the new population we allow a small probability of error (for instance 0.1) Before applying mutation: s1`` = s2`` = s3`` = s4`` = s5`` = s6`` =
47
Example (mutation) After applying mutation:
s1``` = f (s1``` ) = 6 s2``` = f (s2``` ) = 7 s3``` = f (s3``` ) = 8 s4``` = f (s4``` ) = 5 s5``` = f (s5``` ) = 5 s6``` = f (s6``` ) = 6
48
Example (end) In one generation, the total population fitness changed from 34 to 37, thus improved by ~9% At this point, we go through the same process all over again, until a stopping criterion is met
49
Knapsack problem We want to maximize the total value of objects that we can put in a knapsack of some fixed capacity. A representation of a solution might be an array of bits, where each bit represents a different object, and the value of the bit (0 or 1) represents whether or not the object is in the knapsack. Not every such representation is valid, as the size of objects may exceed the capacity of the knapsack. The fitness of the solution is the sum of values of all objects in the knapsack if the representation is valid, or 0 otherwise.
51
Travelling Salesman Problem (TSP)
A salesman has to find the shortest distance journey that visits a set of cities Assume we know the distance between each city This is known to be a hard problem to solve because the number of possible routes is N! where N = the number of cities There is no simple algorithm that gives the best answer quickly
52
TSP (Representation, Initialization, Evaluation and Selection)
Representation: A vector v = (i1 i2… in) represents a tour (v is a permutation of {1,2,…,n}) Initialization: use either some heuristics, or a random sample of permutations of {1,2,…,n} Evaluation: Fitness f of a solution is the inverse cost of the corresponding tour Selection: We shall use the fitness proportionate selection
53
Recombination: Builds offspring by choosing a sub-sequence of a tour from one parent and preserving the relative order of cities from the other parent and feasibility Example: p1 = ( ) and p2 = ( ) First, the segments between cut points are copied into offspring o1 = (x x x x x) and o2 = (x x x x x)
54
p1 = ( ) p2 = ( ) Recombination: Next, starting from the second cut point of one parent, the cities from the other parent are copied in the same order The sequence of the cities in the second parent is 9 – 3 – 4 – 5 – 2 – 1 – 8 – 7 – 6 After removal of cities from the first offspring we get 9 – 3 – 2 – 1 – 8 This sequence is placed in the first offspring o1 = ( ), and similarly in the second o2 = ( )
55
Mutation (Inversion) The sub-string between two randomly selected points in the path is reversed Example: ( ) is changed into ( ) Such simple inversion guarantees that the resulting offspring is a legal tour
56
Thank you! Chih-Hao Lu
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.