INTRODUCTION TO
ARTIFICIAL
INTELLIGENCE
Unit 7
Sumaira Saeed Spring 2024 2
Outline
• Evolutionary Computation
• Evolutionary Algorithm
Sumaira Saeed Spring 2024 3
Local Search / Optimization
• In many problems, you are only concerned about the
optimum value and not about the whole path that lead you
to that value.
• So goal state is important but the path that lead to the
goal state is unimportant.
• Sometimes you do not exactly know about the goal node
but you know certain characteristics of the goal node.
Sumaira Saeed Spring 2024 4
Knapsack Problem
• Given a set of items, each with
a weight and a value,
determine the number of each
item to include in a collection
so that the total weight is less
than or equal to a given limit
and the total value is as large
as possible.
Sumaira Saeed Spring 2024 5
Travelling Salesman Problem
• Given a list of cities and the distances between each pair
of cities, what is the shortest possible route that visits
each city exactly once and returns to the origin city?
Sumaira Saeed Spring 2024 6
Other similar problems
• Resource Allocations
• Scheduling Problems
• Factory floor layout
• Job-Shop scheduling
• Vehicle routing problem
• VLSI Design
• .......
• And many more combinatorial optimization problems
Sumaira Saeed Spring 2024 7
Hill Climbing
• General Idea:
• Start wherever
• Always choose the best neighbor
• If no neighbors have better scores than current, quit
• Why can this be a terrible idea?
Sumaira Saeed Spring 2024 8
Local vs Global Optimum?
What is the optimum point?
Sumaira Saeed Spring 2024 9
Local vs Global Optimum?
What do you think about the optimum point now?
Sumaira Saeed Spring 2024 10
Solution Space Landscape
Sumaira Saeed Spring 2024 11
Complex Functions
Rastrigin
Griewank Rosenbrock
Sumaira Saeed Spring 2024 12
Evolution
• Evolution is change in the heritable characteristics
of biological populations over successive generations.
Evolutionary processes give rise to biodiversity at every
level of biological organization, including the levels
of species, individual organisms, and molecules.
Sumaira Saeed Spring 2024 13
Evolution for Problem Solving
• Evolution in itself is a mechanism of incremental search,
whereby more fit solutions to problems propagate to future
generations, and less fit solutions gradually fade away.
• This process of natural selection provides a wonderful
vehicle for finding solutions to difficult multivariate
optimization problems.
Sumaira Saeed Spring 2024 14
Evolutionary Algorithm
(Source: Wikipedia)
• In artificial intelligence, an evolutionary algorithm (EA) is a
subset of evolutionary computation, a generic population-
based meta-heuristic optimization algorithm.
• An EA uses some mechanisms inspired by biological
evolution: reproduction, mutation, recombination, and
selection.
• Candidate solutions to the optimization problem play the
role of individuals in a population, and the fitness function
determines the environment within which the solutions
"live" .
• Evolution of the population then takes place after the
repeated application of the above operators.
Sumaira Saeed Spring 2024 15
Overview of Evolutionary Algorithms
• A parallel search scheme that is inspired by
biological evolution
• EAs have been used successfully for
optimization problems in several fields
• They make little assumptions about the
landscape
• Works for problem where classical schemes fail
such as
– When derivate of a function does not exist
– The function is discontinuous
Sumaira Saeed Spring 2024 16
History
• Several efforts were started in parallel during
1960s to solve a variety of problems using more
or less the same approach
• Evolutionary Strategies (Berlin Technical University)
• Genetic Algorithms (University of Michigan)
• Evolutionary Programming (UCLA)
• During 1990s the above communities agreed to
the term “Evolutionary Algorithms”.
Sumaira Saeed Spring 2024 17
Evolutionary Computing
• Evolutionary Algorithms
• Genetic Algorithms
• Evolutionary Programming
• Evolutionary Strategies
• Genetic Programming
• Swarm Intelligence
• Particle Swarm Optimization
• Ant Colony Optimization
• Other Nature Inspired Models
• Artificial Immune Systems
• Differential Evolution
• Honey Bee Optimization
• Coevolution
Sumaira Saeed Spring 2024 18
General Scheme of EAs
Sumaira Saeed Spring 2024 19
A Typical Evolutionary Algorithm Cycle
• Step 1: Initialize the population randomly or with
potentially good solutions.
• Step 2: Compute the fitness of each individual in the
population.
• Step 3: Select parents using a selection procedure.
• Step 4: Create offspring by crossover and mutation
operators.
• Step 5: Compute the fitness of the new offspring.
• Step 6: Select members of population to die using a
selection procedure.
• Step 7: Go to Step 2 until termination criteria are met.
Sumaira Saeed Spring 2024 20
Important Concepts
• Chromosome (Candidate Solution)
• Fitness Function
• Parent Selection (Pre-selection)
• Crossover (Exploration)
• Mutation (Exploitation)
• Survivor Selection (Post-selection)
Sumaira Saeed Spring 2024 21
Representation: EA Terms
Sumaira Saeed Spring 2024 22
Most common representation of Chromosomes:
• – Binary
• – Integer
• – Real-Valued or Floating-Point
• – Permutation
• – Tree
Sumaira Saeed Spring 2024 23
Example: Discrete Representation (Binary
alphabet)
Representation of an individual can be using discrete values (binary,
integer, or any other system with a discrete set of values).
Following is an example of binary representation.
CHROMOSOME
GENE
Sumaira Saeed Spring 2024 24
Exploitation vs. Exploration
• Any efficient optimization algorithm must use two
techniques to find a global maximum
• Exploration:
• to investigate new and unknown areas in the search space
• Exploitation:
• to make use of knowledge found at points previously visited to help
find better points
• These two requirements are contradictory, and a good
search algorithm must find a tradeoff between the two.
Sumaira Saeed Spring 2024 25
Crossover
• Merges information from parents into offspring
• Choice of what information to merge is stochastic
• Most offspring may be worse, or the same as the parents
• Hope is that some are better by combining elements of
genotypes that lead to good traits
Sumaira Saeed Spring 2024 26
Mutation
• After creation of new individuals via crossover, mutation is
applied usually with a low probability to introduce random
changes into the population
• Replace gene values lost from the population or not initially present
• Evaluate more regions of the search space
• Avoid premature convergence
• Makes the entire search space reachable
•
• If applied at high probability, the offspring will lose their
resemblance to their parents and the ability of the
algorithm to learn from the parents will be lost
Sumaira Saeed Spring 2024 27
Crossover OR Mutation?
• Exploration: Discovering promising areas in the search
space, i.e. gaining information on the problem
• Exploitation: Optimizing within a promising area, i.e. using
information
• There is co-operation AND competition between them
• Crossover is explorative, it makes a big jump to an area
somewhere “in between” two (parent) areas
• Mutation is exploitative, it creates random small
diversions, thereby staying near (in the area of ) the
parent
• Only crossover can combine information from two parents
• Only mutation can introduce new information (alleles)
Sumaira Saeed Spring 2024 28
How to produce new solutions?
Sumaira Saeed Spring 2024 29
One Point Crossover
Sumaira Saeed Spring 2024 30
Two Point Crossover
Sumaira Saeed Spring 2024 31
Two Point Crossover – Example
• Copy randomly selected set from first parent
• Copy rest from second parent in order 1,9,3,8,2
Sumaira Saeed Spring 2024 32
Permutation Representations
• Useful in ordering/sequencing problems
• Task is (or can be solved by) arranging some objects in a
certain order.
• Examples:
• Production scheduling: important thing is which elements are
scheduled before others (order)
• Travelling Salesman Problem (TSP) : important thing is which
elements occur next to each other (adjacency)
• If there are n variables then the representation is as a list
of n integers, each of which occurs exactly once
Sumaira Saeed Spring 2024 33
Insert Mutation for Permutations
• Pick two allele values 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
Sumaira Saeed Spring 2024 34
Swap Mutation for Permutations
• Pick two alleles at random and swap their positions
• Preserves most of adjacency information (4 links broken),
disrupts order more
Sumaira Saeed Spring 2024 35
Selection Procedure
• Selection in evolutionary algorithms is the process of
choosing which individuals reproduce offspring and which
individuals survive to the next generation
• When selection is used to choose which individuals
reproduce, the process is referred to as pre-selection
(parent(s) selection)
• When it is used to select the individuals that survive to the
next generation it is called post-selection (survival
selection)
Sumaira Saeed Spring 2024 36
SELECTION SCHEMES
Sumaira Saeed Spring 2024 37
Fitness Proportional
• Individuals are selected based on their fitness in
proportion to the other individuals in the
population.
• Suffers from selection pressure if an individual
dominates the population.
Sumaira Saeed Spring 2024 38
Fitness Proportional Selection (continued)
• Main idea: better individuals get higher chance
• Chances proportional to fitness
• Implementation: roulette wheel technique
• Assign to each individual a part of the roulette
wheel
• Spin the wheel n times to select n individuals
Sumaira Saeed Spring 2024 39
Rank Based Selection
• Individuals are selected based on their rank rather than
fitness to reduce the bias large fitness values might have
(reduce the selection pressure)
Sumaira Saeed Spring 2024 40
Rank Based Selection (continued)
• First sort the Fitness value of the Population.
• Then if the Population number is 10 then give the
probability of selection to the Population like 0.1,0.2,0.3,
…,1.0 .
• Then calculate cumulative Fitness and make roulette
wheel.
• And the next steps is same as roulette wheel.
Sumaira Saeed Spring 2024 41
Rank Based Selection (continued)
• Rank-based selection schemes can avoid premature
convergence.
• But can be computationally expensive because it sorts the
populations based on fitness value.
• But this method can lead to slower convergence, because
the best chromosomes do not differ so much from other
ones
Sumaira Saeed Spring 2024 42
Solve Example
Sumaira Saeed Spring 2024 43
Binary Tournament
• Binary Tournament
• Two individuals are randomly selected from the population and
compared.
• The one with the highest fitness is selected for reproduction.
• Then another two individuals are randomly selected and the best fit
is kept as the mate to the first parent.
Sumaira Saeed Spring 2024 44
Truncation
• Least useful selection strategy.
• Truncation selection simply retains the fittest x% of the
population. These fittest individuals are duplicated so that
the population size is maintained.
• For example, we might select the fittest 25% from a population of
100 individuals. In this case we would create four copies of each of
the 25 candidates in order to maintain a population of 100
individuals.
• This is an easy selection strategy to implement but it can
result in premature convergence as less fit candidates are
ruthlessly culled without being given the opportunity to
evolve into something better. Nevertheless, truncation
selection can be an effective strategy for certain problems.
Sumaira Saeed Spring 2024 45
Source: https://www.slideshare.net/FatemehKarimi/genetic-algorithm-55733368
Sumaira Saeed Spring 2024 46
Traveling Salesman Problem
• Given a number of
cities and the costs of
traveling from one city
to any other city, what
is the cheapest round-
trip route that visits
each city exactly once
and then returns to the
starting city?
Sumaira Saeed Spring 2024 47
Traveling Salesman Problem – Representation
• Problem:
• Given n cities
• Find a complete tour with minimal length
• Encoding:
• Label the cities 1, 2, … , n
• One complete tour is one permutation
• For n =4 [1,2,3,4], [3,4,2,1] are possible tours
Sumaira Saeed Spring 2024 48
Traveling Salesman Problem – Representation
• Start with A
• A–D–H–F–C–B–G–E A B C D E F G H
A 0 8 3 1 4 9 3 6
• Cost = ?
B 8 0 5 10 11 4 3 6
• Start with E C 3 5 0 8 7 1 5 12
D 1 10 8 0 9 11 6 4
• E–H–F–C–A–D–B–G
E 4 11 7 9 0 5 17 3
• Cost = ? F 9 4 1 11 5 0 4 1
G 3 3 5 6 17 4 0 7
• Start with G
H 6 6 12 4 3 1 7 0
• G–B–F–H–E–A–D–C
• Cost = ?
• Search space is BIG
• For 30 cities there are 30! 1032 possible tours
Sumaira Saeed Spring 2024 49
Step 1 – Initialize The Population
• Suppose the population size is 6
Candidate Solutions
A C B F H D E G
H B G E A C D F
A H G C B D F E
E G B C D HF A
F H A D C B E G
C D B A H E G F
Sumaira Saeed Spring 2024 50
Step 2 – Compute Fitness
Candidate Solutions Fitness
A B C D E F G H
A C B F H D E G 43
A 0 8 3 1 4 9 3 6
H B G E A C D F 52 B 8 0 5 10 11 4 3 6
A H G C B D F E 49 C 3 5 0 8 7 1 5 12
E G B C D HF A 47 D 1 10 8 0 9 11 6 4
F H A D C B E G 49 E 4 11 7 9 0 5 17 3
C D B A H E G F 56 F 9 4 1 11 5 0 4 1
G 3 3 5 6 17 4 0 7
H 6 6 12 4 3 1 7 0
Sumaira Saeed Spring 2024 51
Step 3 – Parent Selection (Binary Tournament)
• First Parent
• 1 vs. 4 (1 is the winner as its fitness is better)
Candidate Solutions Fitness
A C B F H D E G 43
H B G E A C D F 52
A H G C B D F E 49
E G B C D HF A 47
F H A D C B E G 49
C D B A H E G F 56
Parent 1 A C B F H D E G 43
Sumaira Saeed Spring 2024 52
Step 3 – Parent Selection (Binary Tournament)
• Second Parent
• 2 vs. 5 (5 is the winner as its fitness is better)
Candidate Solutions Fitness
A C B F H D E G 43
H B G E A C D F 52
A H G C B D F E 49
E G B C D HF A 47
F H A D C B E G 49
C D B A H E G F 56
Parent 1I F H A D C B E G 49
Sumaira Saeed Spring 2024 53
Step 4a – Crossover
• Crossover to produce two offspring – assume crossover
points are 3 and 5
Parent 1 A C B F H D E G 43
Parent 2 F H A D C B E G 49
Offspring 1 D C B F H E G A
Offspring 2 F H A D C E G B
Sumaira Saeed Spring 2024 54
Step 4b: Mutation
• Swap mutation on the two offspring produced
Offspring 1 DC B F H E G A
Offspring 2 F H A D C E G B
• For Offspring 1, swap 2nd and 4th gene.
• For Offspring 2, swap 2nd and 6th gene.
Offspring 1 D F B C H EGA
Offspring 2 F E A D C HGB
Sumaira Saeed Spring 2024 55
Step 5: Compute Fitness
A B C D E F G H
A 0 8 3 1 4 9 3 6
B 8 0 5 10 11 4 3 6
C 3 5 0 8 7 1 5 12
D 1 10 8 0 9 11 6 4
E 4 11 7 9 0 5 17 3
F 9 4 1 11 5 0 4 1
G 3 3 5 6 17 4 0 7
H 6 6 12 4 3 1 7 0
Offspring 1 D F B C H EGA 55
Offspring 2 F E A D C HGB 40
Sumaira Saeed Spring 2024 56
Step 6: Survivor Selection
Parents Fitnes Children Fitnes
s s
A C B F H D E G D F B C H E G A
H B G E A C D F F E A D C H G B
A H G C B D F E
E G B C D HF A
F H A D C B E G
C D B A H E G F
Sumaira Saeed Spring 2024 57
Survivor Selection
• Most EAs use fixed population size so need a way of
going from (parents + offspring) to next generation
• Suppose the process shown in the previous slides is
repeated twice to generate 4 more offspring.
• We have now a pool of these 10 solutions (6 parents + 4
offspring) and can run binary tournament to select 6
candidates for the next generation.
• These 6 selected candidates will form the parents pool in
the 2nd generation.
Sumaira Saeed Spring 2024 58
Evolutionary Algorithm Cycle
• Step 1: Initialize the population randomly or with
potentially good solutions
• Step 2: Compute the fitness of each individual in the
population
• Step 3: Select parents using a selection procedure
• Step 4: Create offspring by crossover and mutation
operators
• Step 5: Compute the fitness of the new offspring
• Step 6: Select members of population to die using a
selection procedure
• Step 7: Go to Step 2 until termination criteria are met
Sumaira Saeed Spring 2024 59
Algorithm Design
• Design a representation
• Design a way of evaluating an individual
• Design suitable recombination operator(s)
• Design suitable mutation operator(s)
• Decide how to select individuals to be parents
• Decide how to select individuals for the next generation
(how to manage the population)
• Decide how to stop: termination criterion
Sumaira Saeed Spring 2024 60
The 8-Queen Problem
Place 8 queens on an 8x8 chessboard in
such a way that they cannot check each other
Sumaira Saeed Spring 2024 61
The 8-Queen Problem – Representation
Obvious mapping
1 3 5 2 6 4 7 8
Sumaira Saeed Spring 2024 62
The 8-Queen Problem – Fitness Evaluation
• Penalty of one queen
• Number of queens she can check
• Penalty of a configuration
• Sum of the penalties of all queens
• Penalty is to be minimized
• Fitness of a configuration
• Inverse penalty to be maximized
Sumaira Saeed Spring 2024 63
The 8-Queenss Problem – Crossover
• Combining two permutations into two new permutations:
• choose random crossover point
• copy first parts into children
• create second part by inserting values from other parent:
• in the order they appear there
• beginning after crossover point
• skipping values already in child
1 3 526 4 7 8 1 3 542 8 7 6
8 7 654 3 2 1 8 7 624 1 3 5
Sumaira Saeed Spring 2024 64
The 8-Queens Problem – Mutation
• Small variation in one permutation
• swapping values of two randomly chosen positions
1 3 5 2 6 4 7 8 1 3 7 2 6 4 5 8