You are on page 1of 30

# Genetic Algorithm

CE-2103

Knapsack Problem (1)

"You have to choose the right food for you , you can only eat 2000 calories a day and you have to maximize the fullness sensation"

Knapsack Problem (2)
Food Sweet Cookies Apple Integral Cookies Sandwich Coca Cola Hamburguer Strawberry Salad + Chicken 120 70 115 450 150 700 150 300 Calories 30 50 50 200 30 150 100 250 Fullness

Knapsack Problem (3) Food Chocolat Bar Mixed Nuts Nachos Pine Apple Water (2 liters) Chicken and Rice Milkshake Rice and Beans + Eggs 950 850 600 70 0 500 210 700 Calories 20 50 150 30 400 250 600 400 Fullness .

Any ideas??? .

Genetic Algorithms (GA) .

GA (1) ● Developed by John Henry Holland (1970's) ● Inspired by biological evolution process. ● Based on concepts like ○ Natural Selection ○ Genetic Inheritance ○ Mutations ● Charles Darwin (1859) .

selection. Genetic algorithms are a particular class of evolutionary algorithms that use techniques inspired by evolutionary biology such as inheritance.GA (2) " . is a search technique used in computing to find true or approximate solutions to optimization and search problems... mutation." . and recombination ...

GA (3) What have to be defined? ● Genetic representation of the solution domain. ● Fitness function to to evaluate the solution domain. .

GA (4) Evolution: ● cell = contains chromosomes (string DNA) ● chromosome = set of genes (blocks DNA) ● genotype = collection of genes ● Reproduction = combination of genes of parents ● mutation = errors during reproduction ● fitness = how much it can reproduce before it dies. ● Survival of the fittest .

. fitness of every individual is calculated ● Several individuals are selected from the current population base on their fitness.GA (5) How it works? ● Evolution starts with a random population. ● These individuals are combined to obtain new individuals. this is called first generation ● In each generation.

this generation is used in the next iteration of the algorithm.GA (6) How it works? ● Some individuals are discarded from the new population (lowest fitness) ● We have a new population. . the next generation.

.GA (7) When it finished? ● reach a maximum number of generations ● there is no change in the genetic material of the poulation ● A suitable solution may or may not have reached.

GA (8) Vocabulary ● Individual: any possible solution ● Population: group of all the individuals ● Search Space: all the possible solutions for a problem ● Chromosome: scheme/blueprint for an individual ● Trait: Aspect of an individual .

GA (9) Vocabulary (2) ● Allele: possible aspects ● Locus: position of a gene in the chromosome ● Genome: Collection of chromosomes for an individual .

we can represent the solution of our problem as an array of 16 bits. this will facilitate the crossover ● For example in problem. ● Try to use a fixed length representation. .GA (10) Representation of the solution: ● Typical representation of a solution is an array of bits.

GA (11) 1 0 1 1 1 2 0 3 0 4 1 5 0 6 1 7 1 8 0 9 0 10 0 11 0 12 1 13 0 14 1 15 ● ● ● ● ● ● ● ● Sweet Cookies Apple Integral Cookies Hamburguer Salad + Chicken Chocolat Bar Chicken and Rice Rice and Beans + Eggs 0 = don't choose this product 1 = choose this product .

GA (12) Bit Vectors: ● Specialized type to work with bit arrays (boolean values) ● No waste of space ● There is not a type "bit" in the programming languages. ● Efficient use of resources . we work with bigger types to represent a bit array (bytes) ● Save space when we are going to transfer data over the network.

● You can represent a Bit Vector as follows: ● this give you a bit vector of 32 positions (4 bytes) ● use bitwise operations to perform operations. .GA (13) Bit Vectors: ● Used to compress data and encryption algorithms.

.GA (14) Fitness Function ● ● ● ● defined over the genetic representation measures the quality of a given solution problem dependent defines which solutions have much more probabilities of survive and reproduce.

● In our example: fitness(solution) = fullness(solution) + calories(solution) fullness(solution) = sum (fullness of each item in the solution) /sum(fullness all the possible items) calories(solution) = x = abs(sum(calories of each item in the solution)-2000) if(x == 0) x= 1 x = 2000/x .GA (15) Fitness Function ● We want to maximize the fullness sensation given a constraint of an amount of calories.

GA (16) Initial Population NO Selection Crossover Terminate? SI Crossover Mutation .

. covering the entire search space. ● Population size depends on the problem (usually has several hundreds or thousands) ● Solution may be seeded in areas where optimal solutions can be found. a huge population requires too much computational resources.GA (17) Initial Population ● Randomly Generated. ● An small population can give you a local maximum.

GA (18) .

GA (19) Selection: ● During each generation you select a part of the population to create a new generation ● Individuals are selected based on their fitness (proportional to the fitness) ● There are other methods to select individuals for example. random .

select a pair of parents (proportional to their fitness) 1 0 0 1 0 2 1 3 1 4 1 5 1 6 0 7 0 0 1 1 1 2 1 3 0 4 0 5 1 6 1 7 SIMPLE POINT CROSSOVER 1 0 0 1 0 2 1 3 0 4 0 5 1 6 1 7 Child 1 Child 2 1 0 1 1 1 2 0 3 0 4 1 5 1 6 1 7 .GA (20) Reproduction ● Crossover. mutation and inversion ● For each new solution.

discard the overflow.GA (21) Mutation (low probability )and Inversion (very low probability) ● In the mutation we select a random bit and add 1. . ● In the inversion select a random chain of bits and apply complement to this chain.

GA (22) 1 0 0 1 0 2 1 3 1 4 1 5 1 1 6 0 1 1 1 0 7 0 2 1 2 1 3 0 3 0 4 0 4 0 5 1 5 0 0 1 6 1 6 1 1 1 7 1 7 1 2 1 3 0 4 0 5 1 6 1 7 Child 1 Child 2 SIMPLE POINT CROSSOVER 0 1 0 1 0 0 1 0 2 1 3 1 4 0 5 1 6 1 7 1 0 1 1 1 2 1 3 1 4 0 5 1 6 1 7 Mutation Inversion .

GA (23) .

GA (24) First Generation 1 0 1 1 1 2 0 3 0 4 1 5 0 6 1 7 1 8 0 9 0 10 0 11 0 12 1 13 0 14 1 15 1.98 1 0 1 1 0 2 1 3 0 4 0 5 1 6 1 7 0 8 0 9 1 10 0 11 1 12 0 13 0 14 0 15 0.44 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 .