You are on page 1of 73

Artificial Intelligence

Dr. Basem H. Ahmed & Dr. Mohammed A. Awadallah

First semester 2022/2023


1
Chapter 4
Evolutionary Computation
Genetic Algorithm

2
Genetic Algorithm

3
Can evolution be intelligent?
➢Intelligence can be defined as the capability of a system to adapt
its behavior to ever-changing environment.

➢According to Alan Turing, the form or appearance of a system is


irrelevant to its intelligence.

➢Evolutionary computation simulates evolution on a computer.


• The result of such a simulation is a series of optimization algorithms,
usually based on a simple set of rules.
• Optimization iteratively improves the quality of solutions until an
optimal or at least feasible solution is found.

4
Can evolution be intelligent?
➢The behavior of an individual organism is an inductive inference about
some yet unknown aspects of its environment. If over successive
generations, the organism survives, we can say that this organism is
capable of learning to predict changes in its environment.

➢The evolutionary approach is based on:


• Darwin’s classical theory of evolution,
• Weismann’s theory of natural selection, and
• Mendel’s concept of genetics.

➢Evolutionary computation, is an umbrella term that combines genetic


algorithms, evolution strategies and genetic programming.
5
Simulation of natural evolution
• Evolutionary computation is based on processes of reproduction,
mutation, competition and selection.

• The power to reproduce appears to be an essential property of life.

• The power to mutate is also guaranteed in any living organism that reproduces
itself in a continuously changing environment.

• Processes of competition and selection normally take place in the natural


world, where expanding populations of different species are limited by a finite
space.

6
Simulation of natural evolution
• Evolution can be seen as a process leading to the maintenance of a
population’s ability to survive and reproduce in a specific environment.
This ability is called evolutionary fitness.

• Evolutionary fitness can also be viewed as a measure of the organism’s


ability to anticipate changes in its environment.

• The fitness, or the quantitative measure of the ability to predict


environmental changes and respond adequately, can be considered as
the quality that is optimized in natural life.

7
How is a population with increasing fitness generated?

➢Let us consider a population of rabbits. Some rabbits are faster than


others, and we may say that these rabbits possess superior fitness,
because they have a greater chance of avoiding foxes, surviving and
then breeding.

➢If two parents have superior fitness, there is a good chance that a
combination of their genes will produce an offspring with even higher
fitness.
• Over time the entire population of rabbits becomes faster to meet their
environmental challenges in the face of foxes.

8
Simulation of natural evolution

➢All methods of evolutionary computation simulate natural evolution by


creating a population of individuals, evaluating their fitness, generating
a new population through genetic operations, and repeating this process
a number of times.

➢We will start with Genetic Algorithms (GAs) as most of the other
evolutionary algorithms can be viewed as variations of genetic
algorithms.

9
Genetic Algorithm
➢In the early 1970s, John Holland introduced the concept of genetic
algorithms.

➢His aim was to make computers do what nature does. Holland was
concerned with algorithms that manipulate strings of binary digits.

➢Each artificial “chromosome” consists of a number of “genes”, and


each gene is represented by 0 or 1:

1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1
10
Genetic Algorithm
➢Nature has an ability to adapt and learn without being told what to do.
In other words, nature finds good chromosomes blindly. GAs are
designed to do the same.
➢Two mechanisms link a GA to the problem it is solving: encoding and
fitness evaluation.
➢The GA uses a measure of fitness of individual chromosomes to carry
out reproduction.
➢As reproduction takes place, the crossover operator exchanges parts of
two single chromosomes, and the mutation operator changes the gene
value in some randomly chosen location of the chromosome.

11
Genetic Algorithm
➢A genetic algorithm (or GA) 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 crossover (also called recombination).

➢Genetic algorithms are implemented as a computer simulation in which


a population of abstract representations (called chromosomes or the
genotype or the genome) of candidate solutions (called individuals,
creatures, or phenotypes) to an optimization problem evolves toward
better solutions.
12
Genetic Algorithm

➢The evolution usually starts from a population of randomly generated


individuals and happens in generations.

➢In each generation, the fitness of every individual in the population is


evaluated, multiple individuals are selected from the current population
(based on their fitness), and modified (recombined and possibly
mutated) to form a new population.

13
Genetic Algorithm

➢The new population is then used in the next iteration of the algorithm.

➢Commonly, the algorithm terminates when either a maximum number


of generations has been produced, or a satisfactory fitness level has
been reached for the population.

➢If the algorithm has terminated due to a maximum number of


generations, a satisfactory solution may or may not have been reached.

14
Genetic Algorithm vs. Natural Evolution:

15
Procedural steps of Genetic Algorithm

➢Step 1: Represent the problem variable domain as a chromosome of a


fixed length, choose the size of a chromosome population N, the
crossover probability pc and the mutation probability pm.

➢Step 2: Define a fitness function to measure the performance, or


fitness, of an individual chromosome in the problem domain.
• The fitness function establishes the basis for selecting
• chromosomes that will be mated during reproduction.

16
Procedural steps of Genetic Algorithm
➢Step 3: Randomly generate an initial population of chromosomes of
size N:
x1, x2 , . . . , xN
➢ Step 4: Calculate the fitness of each individual chromosome:
f (x1), f (x2), . . . , f (xN)
➢ Step 5: Select a pair of chromosomes for mating from the current
population.
• Parent chromosomes are selected with a probability related to their
fitness.
17
Procedural steps of Genetic Algorithm
➢ Step 6: Create a pair of offspring chromosomes by applying the genetic
operators - crossover and mutation.
➢ Step 7: Place the created offspring chromosomes in the new population.
➢ Step 8: Repeat Step 5 until the size of the new chromosome
population becomes equal to the size of the initial population, N.
➢ Step 9: Replace the initial (parent) chromosome population with the
new (offspring) population.
➢ Step 10: Go to Step 4, and repeat the process until the termination criterion
is satisfied.
18
Procedural steps of Genetic Algorithm

19
Procedural steps of Genetic Algorithm

20
Genetic Algorithm

➢ GA represents an iterative process. Each iteration is called a generation. A


typical number of generations for a simple GA can range from 50 to over 500. The
entire set of generations is called a run.

➢ Because GAs use a stochastic search method, the fitness of a population may
remain stable for a number of generations before a superior chromosome appears.

➢ A common practice is to terminate a GA after a specified number of generations


and then examine the best chromosomes in the population. If no satisfactory
solution is found, the GA is restarted.

21
Case study
➢ A simple example will help us to understand how a GA works.
➢ Let us find the maximum value of the function (15x - x2) where
parameter x varies between 1 and 15. For simplicity, we may assume
that x takes only integer values.
➢ Thus, chromosomes can be built with only four genes:

22
Case study
➢ Suppose that:
• The size of the chromosome population N is 6,

• The crossover probability pc equals 0.7,

• The mutation probability pm equals 0.001.

➢ The fitness function in our example is defined by:


f(x) = 15x - x2

23
Case study

24
Case study

• In natural selection, only the fittest species can survive, breed, and
thereby pass their genes on to the next generation.
• GAs use a similar approach, but unlike nature, the size of the
chromosome population remains unchanged from one generation to
the next.
• The last column in the previous table shows the ratio of the individual
chromosome’s fitness to the population’s total fitness. This ratio
determines the chromosome’s chance of being selected for crossover
(mating).
• The chromosome’s average fitness improves from one generation to
the next.

25
Case study - Roulette wheel selection

• The most commonly used chromosome selection techniques is the


roulette wheel selection (or Proportionate selection).

26
Case study - Roulette wheel selection

• To select a chromosome, generate a random number r in the range [0,1], and


select the first chromosome with cumulative fitness >=r.
• For example, for r = 0.45, select X4 and for 0.82 select X6.
27
Case study - Roulette wheel selection

28
Other selection schemes

• Tournament Selection
• Tournament selection consists in randomly selecting k individuals;
the parameter k is called the size of the tournament group.

• A tournament is then applied to the k members of the group to


select the best one.

• To select N individuals, the tournament procedure is then carried


out N times.

29
Other selection schemes
• Tournament Selection
• Example : a tournament of size 3 is performed. Three solutions are
picked randomly from the population. The best solution from the
picked individuals is then selected.

30
Other selection schemes
• Linear ranking Selection

where
• N is the size of the population;
• SP is the selection pressure (1.0 < SP ≤ 2.0), and
• r(i) is the rank associated with the individual i.

31
Other selection schemes
• Linear ranking Selection

32
Crossover operator
• Crossover mimics biological recombination
• Some portion of genetic material is swapped between chromosomes.
• Typically the swapping produces an offspring.

• Two parents produce two offspring.

• In terms of crossover, “superior” individuals should have more


opportunities to reproduce to ensure that offspring contain genetic
material of the best individuals.

• Crossover leads to effective combination of schemata (sub-


solutions on different chromosomes)
33
Crossover operator
• Crossover operators:
➢1-point Crossover

➢n-point Crossover

➢Uniform Crossover

34
Crossover operator
• 1-point Crossover
o Choose a random point on the two parents
o Split parents at this crossover point
o Create children by exchanging tails

35
Crossover operator
• 1-point Crossover
o MCQ: Given a binary string 1101001100101101 and another binary string
yxyyxyxxyyyxyxxy in which the values 0 and 1 are denoted by x and y.
The offspring that results from applying 1-point crossover on two strings at a
randomly selected recombination point is
a) yxxyyyxyxxy11010 and yxyyx01100101101
b) 11010yxxyyyxyxxy and yxyyx01100101101
c) 11010yxxyyyxyxxy and 01100101101yxyyx
d) None of the above

36
Crossover operator
• n-point Crossover
o Choose n random crossover points,
o Split along those points,
o Glue parts, alternating between parents,
o Generalization of 1 point (still some positional bias).

37
Crossover operator
• Uniform Crossover
o Corresponding bit positions are randomly exchanged between the two parents to
produce two offspring.
o Assign “heads” to one parent, “tails” to the other,
o Flip a coin for each gene of the first child,
o Make an inverse copy of the gene for the second child.

38
Mutation operator
• Mutation represents a change in the gene.

• Mutation is a background operator. Its role is to provide a guarantee


that the search algorithm is not trapped on a local optimum.

• The mutation operator flips a randomly selected gene in a


chromosome.

• The mutation probability is quite small in nature, and is kept low for
GAs, typically in the range between 0.001 and 0.01.

39
Mutation operator
• Alter each gene independently with a probability pm

• pm is called the mutation rate:

• For each gene, generate a random number r in [0,1]


• If pm > r alter the gene.

40
Mutation operator

41
The genetic algorithm cycle

42
Genetic algorithms: case study
➢Suppose it is desired to find the maximum of the “peak” function of two
variables:

• where parameters x and y vary between -3 and 3.

➢The first step is to represent the problem’s variables as a chromosome-


parameters x and y as a concatenated binary string:

43
Genetic algorithms: case study
• We also choose the size of the chromosome population, for
instance 6, and randomly generatean initial population.
• The next step is to calculate the fitness of each chromosome. This
is done in two stages.
• First, a chromosome, that is a string of 16 bits, is partitioned into two
8-bit strings:

• Second, these strings are converted from binary (base 2) to decimal


(base 10):

44
Genetic algorithms: case study
• Now the range of integers that can be handled by 8-bits, that is the
range from 0 to (28 - 1), is mapped to the actual range of parameters x
and y, that is the range from -3 to 3:

• To obtain the actual values of x and y, we multiply their decimal


values by 0.0235294 and subtract 3 from the results:

45
Genetic algorithms: case study
• Using decoded values of x and y as inputs in the mathematical
function, the GA calculates the fitness of each chromosome.

• To find the maximum of the “peak” function, we will use crossover


with the probability equal to 0.7 and mutation with the probability
equal to 0.001.

• As we mentioned earlier, a common practice in GAs is to specify the


number of generations.
• Suppose the desired number of generations is 100. That is, the GA will create
100 generations of 6 chromosomes before stopping.

46
Genetic algorithms: case study
• Chromosome locations on the surface of the “peak” function: initial
population

47
Genetic algorithms: case study
• Chromosome locations on the surface of the “peak” function: first
generation

48
Genetic algorithms: case study
• Chromosome locations on the surface of the “peak” function: local
maximum

49
Genetic algorithms: case study
• Chromosome locations on the surface of the “peak” function: global
maximum

50
Genetic algorithms: case study
• Performance graphs for 100 generations of 6 chromosomes: local
maximum

51
Genetic algorithms: case study
• Performance graphs for 100 generations of 6 chromosomes: global
maximum

52
Genetic algorithms: case study
• Performance graphs for 20 generations of 60 chromosomes

53
2nd Case study: maintenance scheduling
• Maintenance scheduling problems are usually solved using a
combination of search techniques and heuristics.

• These problems are complex and difficult to solve.

• They are NP-complete and cannot be solved by combinatorial search


techniques.

• Scheduling involves competition for limited resources, and is


complicated by a great number of badly formalized constraints.

54
2nd Case study: maintenance scheduling

Steps of GA:
1. Specify the problem, define constraints and optimum criteria;
2. Represent the problem domain as a chromosome;
3. Define a fitness function to evaluate the chromosome performance;
4. Construct the genetic operators;
5. Run the GA and tune its parameters.

55
2nd Case study: maintenance scheduling
Scheduling of 7 units in 4 equal intervals

➢The problem constraints:


• The maximum loads expected during four intervals are 80, 90, 65 and 70 MW;
• Maintenance of any unit starts at the beginning of an interval and finishes at
the end of the same or adjacent interval. The maintenance cannot be aborted or
finished earlier than scheduled;
• The net reserve of the power system must be greater or equal to zero at any
interval.

➢The optimum criterion is the maximum of the net reserve at any


maintenance period.
56
2nd Case study: maintenance scheduling
• Unit data and maintenance requirements

57
2nd Case study: maintenance scheduling
• Unit gene pools

• Chromosome for the scheduling problem

58
2nd Case study: maintenance scheduling
• Computing Fitness

• Interval 1: 0x20+0x15+0x35+1x40+0x15+0x15+1x10 = 50
• Interval 2: 1x20+0x15+0x35+0x40+1x15+0x15+0x10 = 35
lost capacity
• Interval 3: 1x20+1x15+0x35+0x40+0x15+1x15+0x10 = 50
• Interval 4: 0x20+1x15+1x35+0x40+0x15+0x15+0x10 = 50
• Next, compute the reserve capacity:
Reserve capacity = Total capacity - lost capacity – maximum load
• Interval 1: 150 – 50 – 80 = 20
• Interval 2: 150 – 35 – 90 = 25
• Interval 3: 150 – 50 – 65 = 35
• Interval 4: 150 – 50 – 70 = 30
59
2nd Case study: maintenance scheduling
• Computing Fitness

• Positive value for each chromosome indicate legal chromosome.


• The chromosome fitness is the minimum positive value,
• If the fitness is negative, then the value is zero.

60
2nd Case study: maintenance scheduling
• The crossover operator

61
2nd Case study: maintenance scheduling
• The mutation operator

62
2nd Case study: maintenance scheduling
• Performance graphs and the
best maintenance schedules
created in a population of 20
chromosomes

a) 50 generation

63
2nd Case study: maintenance scheduling
• Performance graphs and the
best maintenance schedules
created in a population of 20
chromosomes

b) 100 generation

64
2nd Case study: maintenance scheduling
• Performance graphs and the
best maintenance
schedules created in a
population of 100
chromosomes.

a) Mutation rate is 0.001

65
2nd Case study: maintenance scheduling
• Performance graphs and the
best maintenance
schedules created in a
population of 100
chromosomes.

b) Mutation rate is 0.01

66
GA for solving Sudoku

a) Problem b) Solution
67
GA for solving Sudoku
• A Sudoku puzzle is completed by filling in all of the empty cells with
numerals 1 to 9, but no row or column and no 3 x 3 sub-block (the
sub-blocks are bound by heavy lines) may contain more than one of
any numeral

68
GA for solving Sudoku
• Initial Solution:

• Assign empty cells of each solution


with a random number from 1 to 9
without any repetitions in the subgrid

69
GA for solving Sudoku
• Fitness function

▪ The fitness function is built based on the idea of frequency calculation of an integer in an
array.
▪ For example, in the array: [2, 3,3, 4, 4, 4], the frequency of 4 is 3, the frequency of 3 is 2, and
the frequency of 2 is 1.
▪ Thus, for the occurrence of each integer, if the frequency exceeds 1, then the calculated error
value is incremented by 1.
▪ This process is repeated for both row and column.

▪ In the above equation, (freqr(y) − 1) denotes the row error, (freqc(y) − 1) denotes the column
error, y represents an integer between 1 and 9, and F represents the fitness value.

▪ The total error calculation is done by performing the summation of row error and column error
considering subgrid error as zero since no repetition is allowed in a subgrid, and the total error
is subtracted from 100 to get the amount of fitness.
▪ In our described method, the chromosome with a fitness value of 100 is assumed as the best
individual.
70
GA for solving Sudoku
• Crossover: (two-point crossover)

71
GA for solving Sudoku
• Mutation operation

72
Other Optimization techniques

Metaheuristics

Evolutionary- Chemical-
Physics-based Human-based Swarm-based
based based

Genetic Algorithm Simulated annealing Harmony search Artificial chemical Particle swarm optimization
Evolutionary Ray optimization Football game-inspired process Ant colony optimization
programming Black hole algorithm algorithm Chemical reaction Krill herd algorithm
Differential evolution Electromagnetic field Political Optimizer optimization Sparrow search algorithm
Biogeography-Based optimization Coronavirus herd Gases Brownian grey wolf optimizer
Optimizer Multi-verse optimizer immunity optimizer motion optimization Rat swarm optimizer
Equilibrium optimizer Ali Baba and the forty Chemotherapy science Crow Search Algorithm
thieves algorithm Elephant herding optimisation

73

You might also like