Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

86
DREAM PLAN IDEA IMPLEMENTATION

Transcript of Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Page 1: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

DREAMDREAM

PLANPLANIDEAIDEA

IMPLEMENTATIONIMPLEMENTATION

Page 2: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Present to:Amirkabir University of Technology (Tehran

Polytechnic) & Semnan University

Genetic algorithms

Dr. Kourosh KianiEmail: [email protected]: [email protected]: www.kouroshkiani.com

Page 3: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Charles Darwin 1809 - 1882

"A man who dares to waste an hour of life has not discovered the value of life"

Page 4: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm
Page 5: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms are a part of evolutionary computing, which is a rapidly growing area of artificial intelligence.

As you can guess, genetic algorithms are inspired by Darwin's theory of evolution. Simply said, problems are solved by an evolutionary process resulting in a best (fittest) solution (survivor) - in other words, the solution is evolved.

Genetic algorithms

Life on earth has evolved to be as it is through the processes of natural selection, recombination and mutation.

Page 6: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Evolutionary computing was introduced in the 1960s by I. Rechenberg in his work "Evolution strategies" (Evolutions strategy in original). His idea was then developed by other researchers. Genetic Algorithms (GAs) were invented by John Holland and developed by him and his students and colleagues. This lead to Holland's book "Adaption in Natural and Artificial Systems" published in 1975.

In 1992 John Koza has used genetic algorithm to evolve programs to perform certain tasks. He called his method "genetic programming" (GP). LISP programs were used, because programs in this language can expressed in the form of a "parse tree", which is the object the GA works on.

History

Page 7: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Biological Background : The cell

• Every animal cell is a complex of many small “factories” working together

• The center of this all is the cell nucleus

• The nucleus contains the genetic information

Page 8: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

• Genetic information is stored in the chromosomes

• Each chromosome is build of DNA

• Chromosomes in humans form pairs

• There are 23 pairs

• The chromosome is divided in parts: genes

• Genes code for properties

• The possibilities of the genes forone property is called: allele

• Every gene has an unique positionon the chromosome: locus

Biological Background : Chromosomes

Page 9: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Every organism has a set of rules, describing how that organism is built up from the tiny building blocks of life.These rules are encoded in the genes of an organism, which in turn are connected together into long strings called chromosomes.Each gene represents a specific characteristic of the organism, like eye color or hair color, and has several different settings. For example, the settings for a hair color gene may be blonde or black. These genes and their settings are usually referred to as an organism's genotype. The physical expression of the genotype - the organism itself - is called the phenotype.When two organisms mate they share their genes. The resultant offspring may end up having half the genes from one parent and half from the other. This process is called recombination. Very occasionally a gene may be mutated. Normally this mutated gene will not affect the development of the phenotype but very occasionally it will be expressed in the organism as a completely new characteristic .

Genetic algorithms

Page 10: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

What Are Genetic What Are Genetic Algorithms?Algorithms?

►A way to employ evolution in the computer

►Search and optimization technique based on variation and selection

Page 11: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Encoding of chromosomes is the first question to ask when starting to solve a problem with GA. Encoding depends on the problem heavily.

This could be as a string of real numbers or, as is more typically the case, a binary bit string.

Binary Encoding Binary encoding is the most common one, mainly because the first research of GA used this type of encoding and because of its relative simplicity. In binary encoding, every chromosome is a string of bits 0 or 1.

Chromosome A

101100101100101011100101

Chromosome B

111111100000110000011111

Components of Genetic algorithms : Encoding

Page 12: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

1 0 1 1 0 1 0 0 0 1 0 1 1

Size Shape Speed

Chromosome A

1 1 1 0 0 1 1 0 0 0 1 10 1

Size Shape Speed

Chromosome B

x y z

0 1 10 1 0 0 1 1 0 1 0 0 1Chromosome A

1 1 1 1 0 1 0 1 1 1 0 1 0

x y z

0 0Chromosome B

Solutions are coded as “chromosomes”

Components of Genetic algorithms : Encoding

1

Page 13: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Given the digits 0 through 9 and the operators +, -, * and /,  find a sequence that will represent a given target number. The operators will be applied sequentially from left to right as you read.

So, given the target number 23, the sequence 6+5*4/2+1 would be one possible solution.If  75.5 is the chosen number then 5/2+9*7-5 would be a possible solution.

First we need to encode a possible solution as a string of bits… a chromosome.So how do we do this? Well, first we need to represent all the different characters available to the solution... that is 0 through 9 and +, -, * and /. This will represent a gene. Each chromosome will be made up of several genes.

Components of Genetic algorithms : Encoding

Page 14: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Four bits are required to represent the range of characters used: 

0: 00001: 00012: 00103: 00114: 01005: 01016: 01107: 01118: 10009: 1001+: 1010-: 1011*: 1100/: 1101

The possible genes 1110 & 1111 will remain unused and will be ignored by the algorithm if encountered.

So now you can see that the solution mentioned above for 23, ' 6+5*4/2+1' would be represented by nine genes like so: 

0110 1010 0101 1100 0100 1101 0010 1010 00016      +     5        *      4      /      2     +      1

These genes are all strung together to form the chromosome:

  011010100101110001001101001010100001

Components of Genetic algorithms : Encoding

Page 15: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

A Quick Word about Decoding 

Because the algorithm deals with random arrangements of bits it is often going to come across a string of bits like this:

 0010001010101110101101110010

 Decoded, these bits represent:

  0010 0010 1010 1110 1011 0111 0010

2        2        +      n/a    -        7        2 

Which is meaningless in the context of this problem! Therefore, when decoding, the algorithm will just ignore any genes which don’t conform to the expected pattern of: number -> operator -> number -> operator …and so on. With this in mind the above ‘nonsense’ chromosome is read (and tested) as:

0010 0010 1010 1110 1011 0111 0010 2        2        +     n/a    -        7        2

 2   +   7

Components of Genetic algorithms : Decoding

Page 16: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

0010 1010 0011 1100 0101 1010 0110 2        +        3     *    5      +       6

Components of Genetic algorithms : Decoding

0: 00001: 00012: 00103: 00114: 01005: 01016: 01107: 01118: 10009: 1001+: 1010-: 1011*: 1100/: 1101

=30

1000 1100 0011 1101 0011 1010 0010 8        *        3     /    3      +       2 =10

0111 1010 0100 1011 0001 1101 0010 ?        ?        ?     ?    ?       ?       ? =?

Page 17: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Permutation encoding can be used in ordering problems, such as travelling salesman problem or task ordering problem. In permutation encoding, every chromosome is a string of numbers that represent a position in a sequence.

Chromosome A 1  5  3  2  6  4  7  9  8

Chromosome B 8  5  6  7  2  3  1  4  9

Example of chromosomes with permutation encoding Permutation encoding is useful for ordering problems. For some types of crossover and mutation corrections must be made to leave the chromosome consistent (i.e. have real sequence in it) for some problems.     Example of Problem: Travelling salesman problem (TSP)The problem: There are cities and given distances between them. Travelling salesman has to visit all of them, but he does not want to travel more than necessary. Find a sequence of cities with a minimal travelled distance. Encoding: Chromosome describes the order of cities, in which the salesman will visit them

Components of Genetic algorithms :

Permutation Encoding

Page 18: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Direct value encoding can be used in problems where some more complicated values such as real numbers are used. Use of binary encoding for this type of problems would be difficult. In the value encoding, every chromosome is a sequence of some values. Values can be anything connected to the problem, such as (real) numbers, chars or any objects.

Chromosome A 1.2324  5.3243  0.4556  2.3293  2.4545

Chromosome B ABDJEIFJDHDIERJFDLDFLFEGT

Chromosome C (back), (back), (right), (forward), (left)

Example of chromosomes with value encoding Value encoding is a good choice for some special problems. However, for this encoding it is often necessary to develop some new crossover and mutation specific for the problem.     Example of Problem: Finding weights for a neural networkThe problem: A neural network is given with defined architecture. Find weights between neurons in the neural network to get the desired output from the network.Encoding: Real values in chromosomes represent weights in the neural network.

Components of Genetic algorithms :

Value Encoding

Page 19: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Crossover and MutationCrossover and Mutation

►Crossover: Operate on selected parent chromosomes to create new offspring.

►Mutation: Randomly changes the offspring resulted from crossover to prevent falling of all solutions into a local optimum.

Page 20: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

CrossoverCrossover

101000000

1011011111

Crossover: single point - random

1011011111

101000000

Parent1

Parent2

Offspring1

Offspring2

Page 21: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

CrossoverCrossover

Single point cross-overSingle point cross-over

Multiple point cross-overMultiple point cross-over

Page 22: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

CrossoverCrossover

parent 1

1 1 0 1 0

1 0 0 0 1

offspring 1 1 1 0 0

1 0 0 1

0

1parent 2

offspring 2

Crossover: two-point - random

Page 23: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

CrossoverCrossover► Assign 'heads' to one parent, 'tails' to the otherAssign 'heads' to one parent, 'tails' to the other► Flip a coin for each gene of the first childFlip a coin for each gene of the first child► Make an inverse copy of the gene for the second Make an inverse copy of the gene for the second

childchild► Inheritance is independent of positionInheritance is independent of position

Crossover: Uniform

Parent 1: 000000000000000000 Parent 2: 111111111111111111 Offspring 1: 010010110001011001 Offspring 2: 101101001110100110

Page 24: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Single arithmetic crossover

• Parents: x1,…,xn and y1,…,yn• Pick a single gene (k) at random, • child1 is:

• reverse for other child. e.g. with = 0.8

CrossoverCrossover

nkkk xxyxx ..., ,)1( , ..., ,1

0.10.1 0.20.2 0.30.3 0.40.4 0.50.5 0.60.6 0.70.7 0.80.8 0.90.9

0.0.33

0.10.1 0.40.4 0.50.5 0.30.3 0.20.2 0.60.6 0.60.6 0.30.3

Parent 1:

Parent 2:

Offspring 1:

Offspring 1:

0.10.1 0.20.2 0.30.3 0.40.4 0.50.5 0.60.6 0.70.7 0.60.6 0.90.9

6.064.016.048.08.0)8.01(6.08.0

0.30.3 0.10.1 0.40.4 0.50.5 0.30.3 0.20.2 0.60.6 0.80.8 0.30.3

8.076.012.064.06.0)8.01(8.08.0

Page 25: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

• Parents: x1,…,xn and y1,…,yn• Pick random gene (k) after this point mix values

child1 is:

• reverse for other child. e.g. with = 0.8

CrossoverCrossoverSimple arithmetic Simple arithmetic crossovercrossover

nx

kx

ky

kxx )1(

ny ..., ,

1)1(

1 , ..., ,

1

0.10.1 0.20.2 0.30.3 0.40.4 0.50.5 0.60.6 0.70.7 0.80.8 0.90.9

0.0.33

0.10.1 0.40.4 0.50.5 0.30.3 0.20.2 0.60.6 0.60.6 0.30.3

Parent 1:

Parent 2:

Offspring 1:

Offspring 1:

0.10.1 0.20.2 0.30.3 0.40.4 0.50.5 0.60.6 0.70.7 0.60.6 0.40.4

6.064.016.048.08.0)8.01(6.08.0

0.30.3 0.10.1 0.40.4 0.50.5 0.30.3 0.20.2 0.60.6 0.80.8 0.80.8

8.076.012.064.06.0)8.01(8.08.0

4.042.018.024.09.0)8.01(3.08.0

8.078.006.072.03.0)8.01(9.08.0

Page 26: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

None - no crossover, offspring is exact copy of parents

CrossoverCrossover

Page 27: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

MutationMutation

1011011111

1010000000

Offspring1

Offspring2

1011001111

1000000000

Offspring1

Offspring2

With some small probability (the mutation rate) flip each bit in the offspring (typical values between 0.1 and 0.001)

mutated

Original offspring Mutated offspring

Page 28: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Adding Mutation: a small number (for real value encoding) is added to (subtracted from) selected values

(1.29  5.68  2.86  4.11  5.55) => (1.29  5.68  2.73  4.22  5.55)

MutationMutation

Order changing mutation - two numbers are selected and exchanged (1 2 3 4 5 6 8 9 7) => (1 8 3 4 5 6 2 9 7)

Page 29: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Create initial population (usually

random)1 1011001011001010111001010101001101100101100101

01110

2 001000101100101011100101010111110110010110000101110

3 001000101100111111100101110111000010010110000101110

4 100000101100101011101111110100110110010000010101110

5 111111111100101011100101011111110110010110010111111

6 001000101100111111111111111000110010010110000101110

7 100000101001100111110010101110111111010011011001001

8 111111111110000000001111101111110100110111111101000

9 000000000000000000000000000000001111111101010101011

10 000000000110011100001010101010100000000000011111111

11 000110110110111100001010101010100000000000011110101

12 111110000110011100001010101010100011111000000000100

13 101010000110011100001010000010100011111000011100101

14 101010000111111111001010000111111111111001111100001

15 101010000101010101010111001010101010101010101010111

16 101010011101010101110111001010101011100100000010101

17 000100001111010101110000001010100001100100000010000

Page 30: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Simple example of Genetic optimization

The chosen problem is to minimize the value of a string of 1’s and 0’s as a binary number. While this is a trivial problem, it will serve to illustrate the operation of the algorithm without the obscuring effects of a complicated objective function.

The genetic algorithm performs minimization in the following steps:

1. A random population of chromosomes are generated.2. The fitness function is applied to each chromosome.3. The selection operation is applied favoring the fittest chromosomes.4. The crossover operation is applied on the selected pairs to generate a new

population.5. Mutation is applied to some offspring6. Repeat steps 2-5

Page 31: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Selection of parents ( reproduction)

In this step the current population is used to produce a second generation of population that, on the average, have a higher objective function.

Selection replicates the most successful solutions found in a population at a rate proportional to their relative quality.

J chromosome

number

Binary chromosome

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 011010 26 0.0385 0.142

2 001011 11 0.0909 0.334

3 110110 54 0.0185 0.068

4 010011 19 0.0526 0.194

5 100111 39 0.0256 0.094

6 010110 22 0.0455 0.168

Total 0.2717 1

Page 32: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Selection of parents ( reproduction)

J chromosome number

Binary chromosom

e

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

Round (Fj*6)=

Copy number

1 011010 26 0.0385 0.142 1

2 001011 11 0.0909 0.334 2

3 110110 54 0.0185 0.068 0

4 010011 19 0.0526 0.194 1

5 100111 39 0.0256 0.094 1

6 010110 22 0.0455 0.168 1

Total 0.2717 1

0.142, 14%

0.334, 34%

0.068, 7%

0.194, 19%

0.094, 9%

0.168, 17%

001011

011010

110110

010011

100111

010110

In the reproduction phase, each chromosome is copied to the second generation a number of times that is proportional to its fractional objective function Fj.

Selected population

011010

001011

001011

010011

100111

010110 Area is Proportional to fitness value

Page 33: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Recombination decomposes two distinct solutions (chromosomes) and then randomly mixes their parts to form novel solutions (chromosomes) .This process is intended to simulate the exchange of genetic material that occurs during biological reproduction. Here, pairs in the breeding population are mated randomly. For simplicity we will mate adjacent pairs. For each pair, a random integer from 1-6 (1- length of chromosome) is chosen. This indicates how many bits on the right end of each chromosome should be exchanged between the two mated chromosomes.

Components of Genetic algorithms : Crossover (Recombination)

sP1 = 011010

sP2 = 001011

Before crossover:

After crossover:sC1 = 011011

sC2 = 001010

sP3 = 001011

sP4 = 010011

sP5 = 100111

sP6 = 010110

sC3 = 000011

sC4 = 011011

sC5 = 100110

sC6 = 010111

Page 34: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Mutation

A surprisingly small role is played by mutation. Biologically, it randomly perturbs thepopulation’s characteristics, thereby preventing evolutionary dead ends. Most mutations are damaging rather than beneficial; therefore, rates must be low to avoid the destruction of species. In the simulation we will assume a mutation rate of on bit per thousand, where a mutation acts to reverse the value of single bit. Since we have only 36 bits in our population, the probability of a mutation is only 0.036; therefore, no bits are reversed in the simulation. By mutation, algorithm explores parts of the space that crossover might miss, and helps prevent premature convergence.

1 1 0 1 1 0 1 1 0 1 0 1 1 1 0 0

1 1 0 1 1 0 1 0 0 1 0 1 1 1 0 0

Before

After

Page 35: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Crossover (Recombination)

J chromosome

number

Binary chromosome

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 011011 27 0.037 0.064

2 001010 10 0.10 0.174

3 000011 3 0.333 0.578

4 011011 27 0.037 0.064

5 100110 38 0.0263 0.046

6 010111 23 0.043 0.075

Total 0.576 1

After One Generation

Page 36: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

0.064, 6%

0.174, 17%

0.578, 59%

0.064, 6%

0.046, 5%

0.075, 7%

000011

001010

011011

100111

010111

011011

Components of Genetic algorithms : Selection of parents ( reproduction)

J chromosome number

Binary chromosom

e

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

Round (Fj*6)=

Copy number

1 011011 27 0.037 0.064 0

2 001010 10 0.10 0.174 1

3 000011 3 0.333 0.578 4

4 011011 27 0.037 0.064 0

5 100110 38 0.0263 0.046 0

6 010111 23 0.043 0.075 1

Total 0.576 1

In the reproduction phase, each chromosome is copied to the second generation a number of times that is proportional to its fractional objective function Fj.

Selected population

001010

000011

000011

000011

000011

010111

Area is Proportional to fitness value

Page 37: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Crossover (Recombination)

sP1 = 001010

sP2 = 000011

Before crossover:

After crossover:sC1 = 001011

sC2 = 000010

sP3 = 000011

sP4 = 000011

sP5 = 000011

sP6 = 010111

sC3 = 000011

sC4 = 000011

sC5 = 000011

sC6 = 010111

Page 38: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Crossover (Recombination)

J chromosome

number

Binary chromosome

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 001011 11 0.091 0.0557

2 000010 2 0.5 0.306

3 000011 3 0.333 0.204

4 000011 3 0.333 0.204

5 000011 3 0.333 0.204

6 010111 23 0.043 0.026

Total 1.633 1

After Two Generation

Page 39: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

0.0557, 6%

0.306, 31%

0.204, 20%

0.204, 20%

0.204, 20%

0.026, 3%

Components of Genetic algorithms : Selection of parents ( reproduction)

J chromosome number

Binary chromosom

e

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

Round (Fj*6)=

Copy number

1 001011 11 0.091 0.0557 0

2 000010 2 0.5 0.306 2

3 000011 3 0.333 0.204 1

4 000011 3 0.333 0.204 1

5 000011 3 0.333 0.204 1

6 010111 23 0.043 0.026 0

Total 1.633 1

In the reproduction phase, each chromosome is copied to the second generation a number of times that is proportional to its fractional objective function Fj.

Selected population

000010

000010

000011

000011

000011

000011Area is Proportional to fitness value

Page 40: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Crossover (Recombination)

sP1 = 000010

sP2 = 000010

Before crossover:

After crossover:sC1 = 000010

sC2 = 000010

sP3 = 000011

sP4 = 000011

sP5 = 000011

sP6 = 000011

sC3 = 000011

sC4 = 000011

sC5 = 000011

sC6 = 000011

Page 41: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Components of Genetic algorithms : Crossover (Recombination)

J chromosome

number

Binary chromosome

Decimal value

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 000010 2 0.5 0.214

2 000010 2 0.5 0.214

3 000011 3 0.333 0.143

4 000011 3 0.333 0.143

5 000011 3 0.333 0.143

6 000011 3 0.333 0.143

Total 16 2.332 1

After Three Generation

Page 42: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

The Evolutionary CycleThe Evolutionary Cycle

Create initial random population

Evaluate fitness of each individual

Termination criteria satisfied ?

Select parents according to fitness

Recombine parents to generate offspring

Mutate offspring

Replace population by new offspring

stopyes

no

Page 43: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Example: the MAXExample: the MAXONEONE problem 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

► 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

Page 44: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

J chromosome

number

Binary chromosome

Oj = number of ones

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 1111010101

7 0.206

2 0111000101

5 0.147

3 1110110101

7 0.206

4 0100010011

4 0.118

5 1110111101

8 0.235

6 0100110000

3 0.088

Total 34 1

Genetic algorithms : Example Example ((initialization)

the MAXONE problemthe MAXONE problem

Page 45: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Selection of parents ( reproduction)

J chromosome number

Binary chromosom

e

Oj = 1/(decimal value)

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

Round (Fj*6)=

Copy number

1 1111010101 7 0.206 1

2 0111000101 5 0.147 1

3 1110110101 7 0.206 1

4 0100010011 4 0.118 1

5 1110111101 8 0.235 1

6 0100110000 3 0.088 0

Total 34 1

Selected population

1111010101

0111000101

1110110101

0100010011

1110111101

1110111101

Page 46: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Next we mate strings for 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

Genetic algorithms : Example Example

((crossover1) the MAXONE problemthe MAXONE problem

Page 47: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

sP1 = 1111010101

sP3 = 1110110101

sp4 = 0100010011

sP5 = 1110111101

Before crossover:

After crossover:

sC1 = 1110110101

sC3 = 1111010101

sC4 = 0100011101

sC5 = 1110110011

Genetic algorithms : Example ( (crossover 2)

the MAXONE problem

Page 48: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Modified population

1110110101

0111000101

1111010101

0100011101

1110110011

1110111101

Before applying mutation: After applying mutation:

Modified population

1110100101

0111000101

1111110100

0100011101

1110110001

1110101111

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)

Genetic algorithms : Example ( (Mutation)

the MAXONE problem

Page 49: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

J chromosome

number

Binary chromosome

Oj = number of ones

Objective function

Fj =Oj / (Total Oj)

Fraction of Total

1 1110100101

6 0.162

2 0111000101

5 0.135

3 1111110100

7 0.189

4 0100011101

5 0.135

5 1110110001

6 0.162

6 1110101111

8 0.216

Total 37 1

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

Genetic algorithms : Example ( (End) the MAXONE problem

Page 50: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

-1 -0.5 0 0.5 1 1.5 2-1

-0.5

0

0.5

1

1.5

2

2.5

3

Genetic algorithms : Optimization of a simple function

The function is defined as:

The problem is to find x from the rang [-1 2] whichMaximizes the function f, i.e., to find x0 such that,

]21[),()( 0 xallforxfxf

It is relatively easy to analyze the function f. The zeros of the first derivative f’ should be determined:

0.1).10sin(.)( xxxf

,2,120

12

0

,2,120

12

.10).10tan(

0).10cos(.10).10sin()(

0

ifori

x

x

ifori

x

xx

xxxxf

i

i

The function f reaches its local

max for xi if i is an odd integer and its local min for xi if i is an even integer. The function reaches its max for:

85.20.1)2

18sin(.85.1)85.1(

85.120

3719

f

x

Page 51: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Optimization of a simple function

Assume that we wish to construct a genetic algorithm: to maximize the function f.Let discuss the major components of such a genetic algorithm in turn.

Page 52: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Presentation (Encoding)

We use a binary vector as a chromosome to present real values of the variable x.The length of the vector depends on the required precision, which, in this example, is six places after the decimal point.The domain of the variable x has length 3; the precision requirement implies that the range [-1 2] should be divided into at least 3.1,000,000=3,000,000 equal size ranges. This means that 22 bits are required as a binary vector (chromosome): 41943042300000022097152 2221

The mapping from a binary vector (chromosome) (b21b20....b0) into a real numberX from the range [-1 2] is straightforward and is completed in to steps:•Convert the binary chromosome (b21b20….b0) from the base 2 to base 10:

'

10

21

0202021 2.)( xbbbb

i

ii

• Find a corresponding real number x :

12

3.0.1

22'

xx

Where -1.0 is the left boundary of the domain and 3 is the length of the domain:

12).10000....10001( 2

imii

ii

abdecimalax

Page 53: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Presentation (Encoding)

Chromosome: 1000101110110101000111

2288967)1111010100011000101110( 2' x

637197.04194303

3.22889670.1 x

12

3.0.1

22'

xx

Of course, the chromosomes (0000000000000000000000) and (1111111111111111111111)Represent boundaries of the domain, -1.0 and 2.0

Page 54: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Initial population

The initialization process is very simple:we create a population of chromosomes, where each chromosome is is a binary vector of 22 bits. All 22 bits for each chromosome are initializedRandomly.

Page 55: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Evaluation function

)()( xfveval Evaluation function eval(*) for binary chromosome v is equivalent to the function f:

As noted earlier, the evaluation plays the role of the environment, rating potential solutionIn terms of their fitness. For example, three chromosomes:

627888.1)0111111100011110000000(

958973.0)0000000001000000001110(

637197.0)1111010100011000101110(

33

22

11

xtodcorresponev

xtodcorresponev

xtodcorresponev

250650.2)()(

078878.0)()(

586345.1)()(

33

22

11

xfveval

xfveval

xfveval

Clearly, the chromosome v3 is the best of the three chromosomes, since its evaluation returns the highest value.

Page 56: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Mutation

721638.1)0111111100011110100000(

627888.1)0111111100011110000000(

3'

3'

33

xtodcorresponev

xtodcorresponev

Assume that the fifth gene from the v3 chromosome was selected for mutation:

082257.0)()(

250650.2)()(

3'

3'

33

xfveval

xfveval

This means that this particular mutation resulted in a significant decrease of the value of the chromosome v3. If the 10th gene was selected for mutation in chromosome v3, then:

630818.1)0111111100011110000001(

627888.1)0111111100011110000000(

3"

3"

33

xtodcorresponev

xtodcorresponev

343555.2)()(

250650.2)()(

3"

3"

33

xfveval

xfveval

Page 57: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Crossover

v2 = 0000001110000000010000

v3 = 1110000000111111000101

Before crossover:

After crossover:v’2 = 0000000000111111000101v’3 = 1110001110000000010000

627888.1)0111111100011110000000(

958973.0)0000000001000000001110(

33

22

xtodcorresponev

xtodcorresponev

250650.2)()(

078878.0)()(

33

22

xfveval

xfveval

666028.1)0000000001001110001110(

998113.0)0111111100010000000000(

3'

3'

2'

2'

xtodcorresponev

xtodcorresponev

459245.2)()(

940865.0)()(

3'

3'

2'

2'

xfveval

xfveval

Note that the second offspring has a better evaluation than both of its parents.

Page 58: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Parameters

For this particular problem we have used the following parameters: Population size: pop_size = 50, probability of crossover Pc =0.25, probability of mutation Pm = 0.01.

Generation number

Evaluation function

1 1.441942

6 2.250003

8 2.250283

9 2.250284

10 2.250363

12 2.328077

39 2.344251

40 2.345087

51 2.738930

99 2.849246

137 2.850217

145 2.850227

In table, we provide the generation number for which we noted an improvement in the evaluation function, together with the value of function. The best chromosome after 150 generations was:

850773.1

)0100010000011111001101(

max

max

xtodcorrespone

v

Page 59: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Optimization of a two variables function

The function is defined as:

Where

).20sin(.).4sin(.5.21),( 221121 xxxxxxf

8.51.41.123 21 xandx

Page 60: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Presentation (Encoding)

We use a binary vector as a chromosome to present real values of the variable x.The length of the vector depends on the required precision, which, in this example, is four places after the decimal point.

The domain of the variable x1 has length 15.1; the precision requirement implies that the range [-3 12.1] should be divided into at least 15.1*10000=151000 equal size ranges. This means that 18 bits are required as a binary vector (chromosome):

26214421510002131072 1817

The domain of the variable x2 has length 1.7; the precision requirement implies that the range [4.1 5.8] should be divided into at least 1.7*10000=17000 equal size ranges. This means that 15 bits are required as a binary vector (chromosome):

32768217000216384 1514

Page 61: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Presentation (Encoding)

The total length of a chromosome (solution vector) is then m = 18 + 15=33 bits; the first 18 bits code x1 and remaining 15 bits (19-33) code x2.Let us consider an example chromosome:

010001001011010000111110010100010

052426.1262143

1.15.703520.3

12

)0.3(1.12).110100000100010010(0.3

1821

decimalx

12).10000....10001( 2

imii

ii

abdecimalax

The first 18 bits: 111110010100010 represents:

755330.532767

7.1.319061.4

12

1.48.5).00101111001010(1.4

1522

decimalx

So the chromosome: 010001001011010000111110010100010 corresponds to (x1, x2)= (1.052426,5.755330). The fitness value for this chromosome is:

f(1.052426,5.755330)=20.252640.

The first 18 bits: 010001001011010000 represents

Page 62: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : Initial population

To optimize the function f using a genetic algorithm, we create a population of pop_size = 20 chromosomes. All 33 bits in all chromosomes are initialized randomly.

110100010111110011110011011100101

100000011000111111111110001010100

101011111010100001110100111010000

101000110111111001111010110101111

011000110100100011111101100111100

110001111101101110000101110111011

001011110010100001010100100100110

110000000100010001000111110111110

000010101000011101101001101000101

000000110111110110101100110011111

011101000011100110000010000011110

100000000111101100010110100000001

111011010110001110100011000011000

011101101111100110101110010001000

011101011111110010101000001010000

101011111100001010011010001110110

010100000111001101001111000110001

101101011101111001000000000100000

010101010001100110111001110001001

111101001101100001111111001101000

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

Page 63: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

7768.387

6669.13),(),(|7573.49359.7|110100010111110011110011011100101

0959.20),(),(|3955.57466.1|100000011000111111111110001010100

4141.15),(),(|1582.58430.3|101011111010100001110100111010000

6961.13),(),(|5713.43675.3|101000110111111001111010110101111

8672.23),(),(|9937.42115.9|011000110100100011111101100111100

0602.30),(),(|0545.50889.11|110001111101101110000101110111011

8762.19),(),(|1513.53359.1|001011110010100001010100100100110

3167.27),(),(|3786.51346.11|110000000100010001000111110111110

0116.15),(),(|2394.43561.9|000010101000011101101001101000101

4106.23),(),(|9960.41300.3|000000110111110110101100110011111

2784.21),(),(|7937.45548.2|011101000011100110000010000011110

1277.16),(),(|3814.57954.0|100000000111101100010110100000001

9597.17),(),(|7030.49106.4|111011010110001110100011000011000

0208.16),(),(|5802.59914.0|011101101111100110101110010001000

1004.18),(),(|3919.48117.1|011101011111110010101000001010000

3411.25),(),(|7344.42551.1|101011111100001010011010001110110

4067.17),(),(|5934.52786.5|010100000111001101001111000110001

5263.19),(),(|3903.45166.2|101101011101111001000000000100000

5800.7),(),(|3802.43484.10|010101010001100110111001110001001

0196.26),(),(|6522.50844.6|111101001101100001111111001101000

21212120

21212119

21212118

21212117

21212116

21212115

21212114

21212113

21212112

21212111

21212110

2121219

2121218

2121217

2121216

2121215

2121214

2121213

2121212

2121211

FitnessTotal

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

xxfxxevalxxv

During the evaluation phase we decode each chromosome and calculate the fitnessFunction values from (x1, x2) values just decoded. We get:

It is clear, that the chromosome v15 is the strongest one, and the chromosome v2 the weakest.

Page 64: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Now we constructs a roulette wheel for the selection process. The total fitness of the population is:

The probability of a selection pi and the cumulative probabilities qi for each chromosome vi (i=1,…20) are:

0000.10352.06822.387/6669.13/)(

9648.00518.06822.387/0959.20/)(

9129.00397.06822.387/4141.15/)(

8732.00353.06822.387/6961.13/)(

8379.00615.06822.387/8672.23/)(

7763.00775.06822.387/0602.30/)(

6988.00513.06822.387/8762.19/)(

6475.00775.06822.387/3167.27/)(

5771.00387.06822.387/0116.15/)(

5384.00604.06822.387/4106.23/)(

4780.00549.06822.387/2784.21/)(

4231.00416.06822.387/1277.16/)(

3815.00463.06822.387/9597.17/)(

3352.00413.06822.387/0208.16/)(

2939.00467.06822.387/1004.18/)(

2472.00654.06822.387/3411.25/)(

1819.00449.06822.387/4067.17/)(

1370.00504.06822.387/5263.19/)(

0866.00195.06822.387/5800.7/)(

0671.00671.06822.387/0196.26/)(

202020

191919

181818

171717

161616

151515

141414

131313

121212

111111

101010

999

888

777

666

555

444

333

222

111

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

qFvevalp

7768.387)(20

1

i

ivevalF

Page 65: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Now we are ready to spin the roulette wheel 20 times; each time we select a single chromosome for a new population. Let us assume that a (random) sequence of 20 numbers from range [0 1] is:

0000.17802.0

9648.07671.0

9129.06446.0

8732.07656.0

8379.00054.0

7763.09834.0

6988.03681.0

6475.02772.0

5771.03890.0

5384.07039.0

4780.04247.0

4231.04948.0

3815.02226.0

3352.07022.0

2939.01717.0

2472.09476.0

1819.05345.0

1370.03087.0

0866.01758.0

0670.05138.0

2020

1919

1818

1717

1616

1515

1414

1313

1212

1111

1010

99

88

77

66

55

44

33

22

11

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

qr

chromosomevSelectqrq 1111110 5384.05138.04780.0

chromosomevSelectqrq 4423 1819.01758.01370.0

chromosomevSelectqrq 7736 1819.03087.01717.0

chromosomevSelectqrq 1111410 5384.05345.04780.0

chromosomevSelectqrq 1919518 9648.09476.09129.0

chromosomevSelectqrq 4463 1819.01717.01370.0

chromosomevSelectqrq 1515714 7763.07022.06988.0

chromosomevSelectqrq 5584 2472.02226.01819.0

chromosomevSelectqrq 1111910 5384.04948.04780.0

chromosomevSelectqrq 1010109 4780.04247.04231.0

chromosomevSelectqrq 15151114 7763.07039.06988.0

chromosomevSelectqrq 99128 4231.03890.03815.0

chromosomevSelectqrq 66135 2939.02772.02472.0

chromosomevSelectqrq 88147 3815.03681.03352.0

chromosomevSelectqrq 20201519 0000.19834.09648.0

chromosomevSelectqrq 11160 0670.00054.00000.0

chromosomevSelectqrq 15151714 7763.07656.06988.0

chromosomevSelectqrq 13131812 6475.06446.05771.0

chromosomevSelectqrq 15151914 7763.07671.06988.0

chromosomevSelectqrq 16162015 8379.07802.07763.0 1620

'

1519'

1318'

1517'

116'

2015'

814'

613'

912'

1511'

1010'

119'

58'

157'

46'

195'

114'

73'

42'

111'

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

vv

1

1

0

0

1

4

0

1

0

3

1

1

1

1

1

1

2

0

0

1

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

Page 66: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

010100000111001101001111110111011

110001111101101110000101000110001

11''

2''

v

v

110001111101101110000101110111011

010100000111001101001111000110001

11'

2'

v

v

Genetic algorithms : CrossoverNow we are ready to apply the crossover operation to the individuals in the new population (vector v’

I ). The probability of crossover Pc=0.25, so we expect that on average 25% of chromosomes (i.e., 4 out of 20) undergo crossover.We proceed in the following way: Let us assume that a (random) sequence of 20 numbers from range [0 1] is:

8269.0

3557.0

2002.0

3892.0

5819.0

7584.0

6745.0

1665.0

8699.0

0315.0

7854.0

6068.0

4012.0

5198.0

9172.0

3490.0

3115.0

6255.0

1519.0

8230.0

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

r

This mean that the chromosomes v’2, v’

11, v’13 and v’

18 were selected for crossover. For each of these two pairs, we generate a random integer number pos from range [1 32] ( 33 is the total length-number of bits-in chromosome). The number pos indicates the position of the crossing point. The first pair of chromosomes is:

And the generated number pos=9.

Page 67: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

011101011111110001000111110111110

110000000100010010101000001010000

18''

13''

v

v

110000000100010001000111110111110

011101011111110010101000001010000

18'

13'

v

v

Genetic algorithms : CrossoverThe second pair of chromosomes is:

And the generated number pos=9.

011000110100100011111101100111100

110001111101101110000101110111011

011101011111110001000111110111110

110001111101101110000101110111011

111101001101100001111111001101000

110100010111110011110011011100101

111011010110001110100011000011000

110000000100010010101000001010000

100000000111101100010110100000001

010100000111001101001111110111011

011101000011100110000010000011110

000000110111110110101100110011111

101011111100001010011010001110110

110001111101101110000101110111011

010100000111001101001111000110001

100000011000111111111110001010100

000000110111110110101100110011111

011101101111100110101110010001000

110001111101101110000101000110001

000000110111110110101100110011111

20'

19'

18''

17'

16'

15'

14'

13''

12'

11''

10'

9'

8'

7'

6'

5'

4'

3'

2''

1'

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

The current version of the population

Page 68: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Genetic algorithms : MutationThe next operator, mutation, is performed on a bit-by-bit basis. The probability of mutation pm=0.01, so we expect that (on average) 1% of bits would undergo mutation. There are m x pop_size =33 x 20 = 660 bits in the whole population; we expect (on average) 6.6 mutations per generation. Every bit has an equal chance to be mutated, so, for every bit in the population, we generate a random number r from the range [0 1]; if r<0.01, we mutate the bit.This means that we have to generate 660 random numbers. In a sample run, 5 of these numbers were smaller than 0.01; the bit number and the random number are listed below:

Bit position

Random number

112 0.0002

349 0.0099

418 0.0088

429 0.0054

602 0.0028

Bit position

Chromosome position

Bit number within chromosome

112 int(112/33) = 4 112-3x33 = 13

349 int(349/33) = 11

349-10x33 = 19

418 int(418/33) = 13

418-12x33 = 22

429 int(429/33) = 13

429-12x33 = 33

602 int(602/33) = 19

602-18x33 = 8

Page 69: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

011000110100100011111101100111100

110001111101101110000101110111011

011101011111110001000111110111110

110001111101101110000101110111011

111101001101100001111111001101000

110100010111110011110011011100101

111011010110001110100011000011000

110000000100010010101000001010000

100000000111101100010110100000001

010100000111001101001111110111011

011101000011100110000010000011110

000000110111110110101100110011111

101011111100001010011010001110110

110001111101101110000101110111011

010100000111001101001111000110001

100000011000111111111110001010100

000000110111110110101100110011111

011101101111100110101110010001000

110001111101101110000101000110001

000000110111110110101100110011111

20'

19'

18''

17'

16'

15'

14'

13''

12'

11''

10'

9'

8'

7'

6'

5'

4'

3'

2''

1'

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

Genetic algorithms : Mutation

Chro

moso

me p

ositio

n

Bit n

um

ber w

ithin

ch

rom

oso

me

4 13

11 19

13 22

13 33

19 8

The current version of the population before mutation

The current version of the population after mutation

011000110100100011111101100111100

110001111101101110000101110111111

011101011111110001000111110111110

110001111101101110000101110111011

111101001101100001111111001101000

110100010111110011110011011100101

111011010110001110100011000011000

111010000100010010101000001010000

100000000111101100010110100000001

010100000111001101001011110111011

011101000011100110000010000011110

000000110111110110101100110011111

101011111100001010011010001110110

110001111101101110000101110111011

010100000111001101001111000110001

100000011000111111111110001010100

000000110111110010101100110011111

011101101111100110101110010001000

110001111101101110000101000110001

000000110111110110101100110011111

20'

19'

18''

17'

16'

15'

14'

13''

12'

11''

10'

9'

8'

7'

6'

5'

4'

3'

2''

1'

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

Page 70: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

We drop primes for modified chromosomes : the population is listed as new vector vi:

011000110100100011111101100111100

110001111101101110000101110111111

011101011111110001000111110111110

110001111101101110000101110111011

111101001101100001111111001101000

110100010111110011110011011100101

111011010110001110100011000011000

111010000100010010101000001010000

100000000111101100010110100000001

010100000111001101001011110111011

011101000011100110000010000011110

000000110111110110101100110011111

101011111100001010011010001110110

110001111101101110000101110111011

010100000111001101001111000110001

100000011000111111111110001010100

000000110111110010101100110011111

011101101111100110101110010001000

110001111101101110000101000110001

000000110111110110101100110011111

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

0497.447

8672.23)9937.4,2116.9()(

6084.27)0545.5,0595.11()(

5911.27)6670.5,1346.11()(

0602.30)0545.5,0890.11()(

0196.26)6522.5,0844.6()(

6670.13)7573.4,9360.7()(

9597.17)7030.4,9106.4()(

6925.22)2099.4,8118.1()(

1278.16)3815.5,7954.0()(

3518.33)7434.4,0886.11()(

2784.21)7937.4,5549.2()(

4107.23)9961.4,1301.3()(

3412.25)7345.4,2552.1()(

0602.30)0545.5,0890.11()(

4067.17)5935.5,2787.5()(

0959.20)3956.5,7466.1()(

4126.23)9961.4,1282.3()(

0208.16)6803.5,9915.0()(

2011.18)0545.5,2790.5()(

4107.23)9961.4,1301.3()(

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

Total

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

Page 71: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Now, we are ready to run the selection process again and apply the genetic operator, evaluate the next generation, etc. After 100 generations the population is:

001010101011110000100001110011001

001111000001010001010001111011000

001010101011110000100001110011001

011010101011110000100001110011001

001010001010010000101001110011001

001010011011010111001011110011010

011010101011110000100001110011001

011010101011111011011101110111101

001110001011010010010001101011000

000110001011000010010001101011000

011010101011111011011101110111101

001110001011010010010001110011000

001110101001010001010001111011000

000110001011010010010001101011000

001010001010010000100001110011001

011010101011111011011101110111101

001010101011110000110001110011000

011010101011111011011101110111101

000010101011110000100011110011001

011010101011110011011101110111101

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

3728.625

9567.32)2424.4,5888.10()(

6697.19)4728.4,5181.11()(

9567.32)2424.4,5888.10()(

9321.32)2425.4,5888.10()(

3595.34)2146.4,5888.10()(

7468.30)6537.4,6066.10()(

9321.32)2425.4,5888.10()(

3166.30)0925.5,1246.11()(

4561.35)42779.4,6311.9()(

4779.35)4279.4,6237.9()(

3166.30)0925.5,1225.11()(

3938.34)4279.4,5748.10()(

3090.23)4528.4,5181.11()(

4586.35)4279.4,6311.9()(

3561.34)2146.4,5888.10()(

3166.30)0925.5,1246.11()(

9331.31)2442.4,5741.10()(

3166.30)0925.5,1246.11()(

8697.26)6674.4,5888.10()(

2985.30)0925.5,12.11()(

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

Total

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

fveval

Page 72: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Hybrid intelligent systemsHybrid intelligent systems

Evolutionary neural networksEvolutionary neural networks

Page 73: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Evolutionary neural networksEvolutionary neural networks

Although neural networks are used for solving a variety of problems, they still have some limitations.

One of the most common is associated with neural network training. The back-propagation learning algorithm cannot guarantee an optimal solution. In real-world applications, the back-propagation algorithm might converge to a set of sub-optimal weights from which it cannot escape. As a result, the neural network is often unable to find a desirable solution to a problem at hand.

Page 74: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Another difficulty is related to selecting an optimal topology for the neural network. The “right” network architecture for a particular problem is often chosen by means of heuristics, and designing a neural network topology is still more art than engineering.

Genetic algorithms are an effective optimisation technique that can guide both weight optimisation and topology selection.

Evolutionary neural networksEvolutionary neural networks

Page 75: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

y

0.91

3

4

5

6

7

8

x1

x3

x22

-0.8

0.4

0.8

-0.7

0.2

-0.2

0.6-0.3 0.1

-0.2

0.9

-0.60.1

0.3

0.5

From neuron:To neuron:

1 2 3 4 5 6 7 8

12345678

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 0

0.9 -0.3 -0.7 0 0 0 0 0 -0.8 0.6 0.3 0 0 0 0 00.1 -0.2 0.2 0 0 0 0 00.4 0.5 0.8 0 0 0 0 00 0 0 -0.6 0.1 -0.2 0.9 0

Chromosome: 0.9 -0.3 -0.7 -0.8 0.6 0.3 0.1 -0.2 0.2 0.4 0.5 0.8 -0.6 0.1 -0.2 0.9

Encoding a set of weights in a chromosomeEncoding a set of weights in a chromosome

Page 76: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

The second step is to define a fitness function for evaluating the chromosome’s performance. This function must estimate the performance of a given neural network. We can apply here a simple function defined by the sum of squared errors.

The training set of examples is presented to the network, and the sum of squared errors is calculated. The smaller the sum, the fitter the chromosome. The genetic algorithm attempts to find a set of weights that minimises the sum of squared errors.

Fitness FunctionFitness Function

Page 77: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

The third step is to choose the genetic operators – crossover and mutation. A crossover operator takes two parent chromosomes and creates a single child with genetic material from both parents. Each gene in the child’s chromosome is represented by the corresponding gene of the randomly selected parent.

A mutation operator selects a gene in a chromosome and adds a small random value between 1 and 1 to each weight in this gene.

weight optimisationweight optimisationCrossover & Mutation

Page 78: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Crossover in weight optimisationCrossover in weight optimisation

3

4

5

y6

x22

-0.3

0.9-0.7

0.5

-0.8

-0.6

Parent 1

x11

-0.2

0.1

0.4

3

4

5

y6

x22

-0.1-0.5

0.2-0.9

0.6

0.3

Parent 2

x11

0.9

0.3

-0.8

0.1 -0.7 -0.6 0.5 -0.8-0.2 0.9 0.4 -0.3 0.3 0.2 0.3 -0.9 0.60.9 -0.5 -0.8 -0.1

0.1 -0.7 -0.6 0.5 -0.80.9 -0.5 -0.8 0.1

3

4

5

y6

x22

-0.1

-0.5-0.7

0.5

-0.8

-0.6

Child

x11

0.9

0.1

-0.8

Page 79: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Mutation in weight optimisationMutation in weight optimisation

Original network3

4

5

y6

x22

-0.3

0.9-0.7

0.5

-0.8

-0.6x11

-0.2

0.1

0.4

0.1 -0.7 -0.6 0.5 -0.8-0.2 0.9

3

4

5

y6

x22

0.2

0.9-0.7

0.5

-0.8

-0.6x11

-0.2

0.1

-0.1

0.1 -0.7 -0.6 0.5 -0.8-0.2 0.9

Mutated network

0.4 -0.3 -0.1 0.2

Page 80: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Can genetic algorithms help us in selecting Can genetic algorithms help us in selecting the network architecture?the network architecture?

The architecture of the network (i.e. the number of neurons and their interconnections) often determines the success or failure of the application. Usually the network architecture is decided by trial and error; there is a great need for a method of automatically designing the architecture for a particular application. Genetic algorithms may well be suited for this task.

Evolutionary neural networks:Evolutionary neural networks:optimal topologyoptimal topology

Page 81: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

The basic idea behind evolving a suitable network architecture is to conduct a genetic search in a population of possible architectures.

We must first choose a method of encoding a network’s architecture into a chromosome.

Evolutionary neural networks:Evolutionary neural networks:optimal topologyoptimal topology

Page 82: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Encoding the network architecture

The connection topology of a neural network can be represented by a square connectivity matrix.

Each entry in the matrix defines the type of connection from one neuron (column) to another (row), where 0 means no connection and 1 denotes connection for which the weight can be changed through learning.

To transform the connectivity matrix into a chromosome, we need only to string the rows of the matrix together.

Page 83: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Encoding of the network topologyEncoding of the network topology

From neuron:To neuron:

1 2 3 4 5 6

123456

0 0 0 0 0 00 0 0 0 0 01 1 0 0 0 01 0 0 0 0 00 1 0 0 0 00 1 1 1 1 0

3

4

5

y6

x22

x11

Chromosome: 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0

Page 84: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

The cycle of evolving a neural network topologyThe cycle of evolving a neural network topology

Neural Network j

Fitness = 117

Neural Network j

Fitness = 117Generation i

Training Data Set 0 0 1.00000.1000 0.0998 0.88690.2000 0.1987 0.75510.3000 0.2955 0.61420.4000 0.3894 0.47200.5000 0.4794 0.33450.6000 0.5646 0.20600.7000 0.6442 0.08920.8000 0.7174 -0.01430.9000 0.7833 -0.10381.0000 0.8415 -0.1794

Child 2

Child 1

CrossoverParent 1

Parent 2

Mutation

Generation (i + 1)

Page 85: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm

Questions? Discussion? Suggestions ?

Page 86: Dr. Kourosh Kiani Genetics-Algorithm Lecture 01 Introduction Genetics Algorithm