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 Calories Fullness
Sweet Cookies 120 30
Apple 70 50
Integral Cookies 115 50
Sandwich 450 200
Coca Cola 150 30
Hamburguer 700 150
Strawberry 150 100
Salad + Chicken 300 250
Knapsack Problem (3)
Food Calories Fullness
Chocolat Bar 950 20
Mixed Nuts 850 50
Nachos 600 150
Pine Apple 70 30
Water (2 liters) 0 400
Chicken and Rice 500 250
Milkshake 210 600
Rice and Beans + Eggs 700 400
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)
GA (2)
" ... is a search technique used in computing to find true or approximate solutions to
optimization and search problems. Genetic algorithms are a particular class of evolutionary
algorithms that use techniques inspired by evolutionary biology such as inheritance,
mutation, selection, 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
GA (5)
How it works?
● Evolution starts with a random population, this is called first generation
● In each generation, fitness of every individual is calculated
● Several individuals are selected from the current population base on their fitness.
● These individuals are combined to obtain new individuals.
GA (6)
How it works?
● Some individuals are discarded from the new population (lowest fitness)
● We have a new population, the next generation, this generation is used in the next iteration of the algorithm.
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
GA (10)
Representation of the solution:
● Typical representation of a solution is an array of bits.
● Try to use a fixed length representation, this will facilitate the crossover
● For example in problem, we can represent the solution of our problem as an array of 16 bits.
GA (11)
1 1 1 0 0 1 0 1 1 0 0 0 0 1 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 = don't choose this product1 = choose this product
● Sweet Cookies● Apple● Integral Cookies● Hamburguer● Salad + Chicken● Chocolat Bar ● Chicken and Rice● Rice and Beans + Eggs
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, we work with bigger types to represent a bit array (bytes)
● Save space when we are going to transfer data over the network.
● Efficient use of resources
GA (13)
Bit Vectors:● Used to compress data and encryption
algorithms.● 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 (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.
GA (15)
Fitness Function ● We want to maximize the fullness sensation
given a constraint of an amount of calories.● 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= 1x = 2000/x
GA (17)
Initial Population● Randomly Generated, 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.● An small population can give you a local
maximum, a huge population requires too much computational resources.
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
GA (20)
Reproduction● Crossover, mutation and inversion ● For each new solution, select a pair of
parents (proportional to their fitness)1 0 0 1 1 1 1 0
0 1 2 3 4 5 6 7
0 1 1 1 0 0 1 1
0 1 2 3 4 5 6 7
1 1 1 0 0 1 1 1
0 1 2 3 4 5 6 7
SIMPLEPOINTCROSSOVER
1 0 0 1 0 0 1 1
0 1 2 3 4 5 6 7
Child 1
Child 2
GA (21)
Mutation (low probability )and Inversion (very low probability)
● In the mutation we select a random bit and add 1, discard the overflow.
● In the inversion select a random chain of bits and apply complement to this chain.
GA (22)
1 0 0 1 1 1 1 0
0 1 2 3 4 5 6 7
0 1 1 1 0 0 1 1
0 1 2 3 4 5 6 7
1 1 1 0 0 1 1 1
0 1 2 3 4 5 6 7
SIMPLEPOINTCROSSOVER
1 0 0 1 0 0 1 1
0 1 2 3 4 5 6 7
Child 1
Child 2
1 0 0 1 1 0 1 1
0 1 2 3 4 5 6 7
1 1 1 1 1 0 1 1
0 1 2 3 4 5 6 7
Mutation Inversion