0% found this document useful (0 votes)
8 views

Ga_method Hand Calc

The document provides an overview of Genetic Algorithms (GAs), a probabilistic search and optimization technique inspired by natural selection. It details the working principles of GAs, including population initialization, fitness evaluation, reproduction, crossover, and mutation, along with various selection methods. Additionally, it discusses binary-coded GAs and provides examples of how to implement these concepts in practice.

Uploaded by

mohameed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Ga_method Hand Calc

The document provides an overview of Genetic Algorithms (GAs), a probabilistic search and optimization technique inspired by natural selection. It details the working principles of GAs, including population initialization, fitness evaluation, reproduction, crossover, and mutation, along with various selection methods. Additionally, it discusses binary-coded GAs and provides examples of how to implement these concepts in practice.

Uploaded by

mohameed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Nontraditional optimization

methods
Genetic Algorithms
Nontraditional optimization
methods
Genetic Algorithms
Libyan academy for post graduate Studies
Prof. D. Mostafa Essuri
[email protected]

November - 2022

2
What is Genetic Algorithms
• Genetic Algorithm (GA) is a population-based probabilistic search and
optimization technique.
• works based on the mechanism of natural genetics and Darwin’s principle of
natural selection.
• The GA was introduced by Prof. John Holland of the University of Michigan.
• His book lays the foundation of the GAs.
• It is basically an iterative search technique.
• working based on the concept of probability.
What is Genetic Algorithms
Is an iterative search method that Uses rules of probability
• Several versions of GA are available in the literature, such as:
• Binary-coded GA ,
• real-coded GA ,
• micro-GA ,
• messy-GA , and others.
The working principle of a Simple GA
• A GA starts with a population of initial solutions generated at random.
• The fitness/goodness value (that is, objective function value in case of
a maximization problem) of each solution in the population is
calculated.
• It is important to mention that a GA is generally designed to solve a
maximization problem. Thus, a minimization problem has to be
converted into a corresponding maximization problem as
• discussed below:
The working principle of a Simple GA
How to converted into a corresponding maximization problem

• Durability principle ➔ maximize -f(x)


• If f(x) ~= 0 Maximize 1/𝑓(𝑥)
• If f(x) >= 0 Maximize 1/ ( 1 + 𝑓(𝑥)) or
• Maximize 1/ ( 1 + 𝑓(𝑥)^2)
The population of solutions is then modified using different operators,
namely reproduction (selection) , crossover, mutation, and others.

Reproduction (also known as selection scheme) is utilized


to select the good solutions using their fitness values.
Thus, it forms a mating pool (Parents)consisting of good solutions
probabilistically.

In crossover, there is an exchange of properties between the parents and


as a result of which, new children solutions are created.
• It is important to mention that if the parents are good, the children are
expected to be good.
• Many types of crossover operators are available in the GA-literature,
such as single-point crossover, two-point …
• crossover, multi-point crossover, uniform crossover,
Mutation means a sudden change of parameter.
In a GA-search, it is used for achieving a local change around the current
solution.
Thus, if a solution gets stuck at the local minimum, this operator may help it
to come out of this situation and consequently, it may also jump into the global
basin.

Termination :
• After the reproduction, crossover and mutation are applied to the
whole population of solutions, one generation of a GA is completed.

• Different criteria may be used to terminate the program, such as:


• Maximum number of generations,
• Desired accuracy in the solution, …
Binary-Coded GA
Numerical Example
The working Example of Simple GA
Maximize 𝑦 = 𝑓 𝑥1, 𝑥2 x1,2min <= x1,x2 <= x1,2 max
x1 and x2 are real variables
Step 1: Generate a binary string:
• In this type of GA, the solutions are represented in the form of binary strings composed of 1s and
0s.
• Population size: An initial population of solutions of size N (say N = 100, 200, . . ., depending on
the complexity of the problem) is selected at random.
• The length of a binary string is decided based on a desired precision in the values of the
variables.
• For example, if we need a precision level of ε in the values of a variable x1, we will have to assign
l bits to represent it, where l can be determined using the expression given below.
The working Example of Simple GA
• For example, if we need a precision level of ε in the values of a variable x1, we will have to assign l bits to
represent it, where l can be determined using the expression given below.

• The decoded value:


Let us consider a binary number 1 0 1 1 0. Its decoded value is determined as
1 0 1 1 0
1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 + 0 × 20 = 22.
4 3 2 1

Minimum 0 0 0 0 0
0 × 24 + 0 × 23 + 0 × 2 2 + 0 × 21 + 0 × 20 = 0.

Minimum 1 1 1 1 1
4 3
1 × 2 +1 ×2 +1 ×2 2 1 0
+ 1 × 2 + 1 × 2 = 31.
The working Example of Simple GA
• For example, if we need a precision level of ε in the values of a variable x1, we will have to assign l bits to
represent it, where l can be determined using the expression given below.

For example: 3 bit string


then l =3
which means the string 000 ➔ 0 and 111 ➔ 7

This range of x1 is divided into 2𝑙 -1 parts that is 2^3 -1 = 7

𝑥1𝑚𝑎𝑥 −𝑥1𝑚𝑖𝑛 𝑥1𝑚𝑎𝑥 −𝑥1𝑚𝑖𝑛


approximatlly 𝜖= ➔ we get the above equation ➔ 2𝑙 =
2𝑙 𝜀

More accuracy ➔ more bits ➔ more computation ➔ slower GA


The working Example of Simple GA
• If we use 10 bits for each variable then one Thus, in this problem, the GA-string is
20-bits long.
• Let us also suppose that the initial population of GA-strings created at random is:
binary string is 20-bits
F1
𝒄𝒉𝒓𝒐𝒎𝒐𝒔𝒐𝒎𝒆 F2
• 𝟏𝟎𝟏𝟎𝟎𝟎𝟏𝟏𝟏𝟎 1111000011 ⇒ F F3
𝒔𝒖𝒃𝒔𝒕𝒓𝒊𝒏𝒈 𝒙𝟏 𝒔𝒖𝒃𝒔𝒕𝒓𝒊𝒏𝒈 𝒙𝟐 F4
.
• How many strings ? .. Population size N FN
Generation
• Step 2 Fitness evaluation:
linear mapping rule
• To determine the fitness value of each solution (that is, string), the real values of the variables: 𝑥1 and
𝑥2 are to be calculated first.
• If the minimum and maximum limits of a variable (say 𝑥1 ) and decoded value of the binary sub-string
assigned to represent 𝑥1 are known, its real value can be determined using the linear mapping rule
given below.

• Where 𝑙 is the length of the substring used to represent the variable, D is the decoded value

• Next ➔ Same is applied to 𝑥2 and its real value is obtained

• Next➔ Use the values of 𝑥1 and 𝑥2 to calculate functional f(𝑥1 , 𝑥2 ) for each N chromosomes
• Step 2 Fitness evaluation:
linear mapping rule
• To determine the fitness value of each solution (that is, string), the real values of the variables: 𝑥1 and
𝑥2 are to be calculated first.
• If the minimum and maximum limits of a variable (say 𝑥1 ) and decoded value of the binary sub-string
assigned to represent 𝑥1 are known, its real value can be determined using the linear mapping rule
given below.
• Step 3 - Reproduction:
• All the GA-strings contained in the population may not be equally good in terms of their fitness values calculated in
Step 2.
• In this step, an operator named reproduction is used to select the good ones from the population of strings based on
their fitness information. Several reproduction schemes (also called selection schemes) have been developed by
various investigators. Some of these are

I) Proportionate Selection/Roulette-Wheel Selection - In this scheme, the probability of a string for


being selected in the mating pool is considered to be proportional to its fitness.
• The wheel is rotated/spun for N times and each time.
• only one area is identified by the pointer to be the winner.

𝑓𝑖
𝑝=
σ𝑁
𝑖=1 𝑓𝑖
A Roulette-Wheel used to implement proportionate selection scheme
• Step 3 - Reproduction:
Initial string Individual Probability of
fitness being selected
1000….11111…. 𝒇𝟏 𝒇𝟏
σ𝑵
𝒊=𝟏 𝒇𝒊
000101010…. 𝒇𝟐 𝒇𝟐
σ𝑵
𝒊=𝟏 𝒇𝒊
1010…10010….. 𝒇𝟑 𝒇𝟑
σ𝑵
𝒊=𝟏 𝒇𝒊
…. ….. ….
0011…11111…. 𝒇𝑵 𝒇𝑵
σ𝑵
𝒊=𝟏 𝒇𝒊
In this scheme, a good string may be selected for a number of times.
• Step 3 - Reproduction:

The following procedure may be adopted to implement Roulette-Wheel


selectionscheme in the computer program:
For simplicity, let us assume that there are only four binary strings in the
population of solutions having the fitness values: 1 𝑓 , 𝑓 , 𝑓 𝑎𝑛𝑑 𝑓
2 3 4.

• Arrange the strings in ascending order of their fitness values as f3, f1, f4, f2.
• Let us also assume that their probability values for being selected in the mating
pool are as follows: p1 = 0.15, p2 = 0.6, p3 = 0.05 and p4 = 0.2, according to this
selection scheme.
• Let us consider a random number generator, which generates a number (say r)
in the range of (0.0, 1.0). If r is found to lie in the ranges:
0.0 ≤ r < 0.05, 0.05 ≤ r < 0.2, 0.2 ≤ r < 0.4 and 0.4 ≤ r ≤ 1.0, then the
3ed, 1st, 4th and 2nd string will be selected
for the mating pool, respectively.
• Step 3 - Reproduction:
• II) Ranking method
A GA-search depends on the factors like population diversity and selection
Pressure.
These two factors are inversely related to each other in the sense that if the
selection pressure increases, the population diversity will decrease, and vice-
versa.
% % Rank Ri/sum % %

f1 80 0.8 4 0.4
f1 40 0.4
f2 10 0.1 3 0.3
f2 30 0.3
f3 7 0.07 2 0.2
f3 20 0.2
f4 3 0.03 1 0.1
f4 10 0.1
sum 100 1.0 10 ---
• Step 3 - Reproduction:
• III) Tournament Selection
In this scheme, we select the tournament size n (say 2 or 3 , or 5) at random,
which is a smaller number compared to the population size N (30, 100, or 200).
• We pick n strings from the population, at random and determine the best
one in terms of fitness value. (max. or min)
• The best string is copied into the mating pool and then all n strings are
returned to the population. Thus, in this scheme, only one string is selected
per tournament and N tournaments are to be played to make the size of
mating pool equal to N. Here, there is a chance for a good string to be
copied in the mating pool more than once.
• It is found to be computationally faster than both the fitness-based and
rank-based proportionate selection schemes.
• Step 3 - Reproduction:
• V) Elitism Selection

• This scheme was proposed by Kenneth De Jong [26], in which an elite


string (in terms of fitness) is identified first in a population of strings.
• It is then directly copied into the next generation to ensure its presence.
• This is done because the already found best string may be lost, if it is not
selected during reproduction using any one of the above schemes.
• Step 4 - Crossover:

In crossover, there is an exchange of properties between two parents and as a


result of which, two children solutions are produced.
To carry out this operation, the parents or mating pairs (each pair consists of
two strings) are selected at random from the mating pool.
Thus, N/2 mating pairs are formed from a population of strings of size N.
The parents are checked, whether they will participate in crossover by tossing a
coin, whose probability of appearing head is Pc.
If the head appears, the parent will participate in crossover to produce two
children. Otherwise, they will remain intact in the population.
It is important to mention that Pc is generally kept near to 1.0, so that almost
all the parents can participate in the crossover.
• Step 4 - Crossover:
• I) Single Point Crossover

0101101011|1001101001
0011010010|1110100101

Two children produced due to the single-point crossover are given below.

0101101011|1110100101
0011010010|1001101001
• Step 4 - Crossover:
• Two-point Crossover –
• We select two different crossover sites lying between 1 and L − 1, at random. The parent
strings participating in this crossover are:

101011|10011|010010100
010010|11101|000110011

• Two children strings produced due to the two-point crossover are:

101011|11101|010010100
010010|10011|000110011

• We may not get change if the string is long , say 100… the last part is small
• Step 4 - Crossover:
• Multi-point Crossover –
• In case of multi-point crossover, a number of crossover points (either an odd number or
an even number greater than two) are selected along the length of the strings, at
random.
• The bits lying between alternate pairs of sites are then interchanged.
• The parent strings are shown below, in which five crossover sites are chosen at random.

101|1000|0111|0011|011|10
011|1011|0001|1000|101|00

• Two children strings produced due to the 5-point crossover are:

101|1011|0111|1000|011|00
011|1000|0001|0011|101|10
• Step 4 - Crossover:
• Uniform Crossover
• 4. - Uniform crossover is a more general version of the multi-point crossover. Let us take
an example of the parent strings shown below to explain the principle of uniform
crossover.
10110000111001101110
01110110001100010100

• In this scheme, at each bit position of the parent strings, we toss a coin (with a probability
of 0.5 for appearing head) to determine whether there will be interchanging of the bits. If
the head appears, there will be a swapping of bits among the parent strings, otherwise
they will remain unaltered. Let us assume that the 2-nd, 4-th, 5-th, 8-th, 9-th, 12-th, 15-
th, 18-th and 20-th bit positions are selected for swapping. Thus, the obtained children
solutions will look as follows:
11110000011101001110
00110110101000110100
• Step 4 - Crossover:
• 4. - Uniform Crossover
• Uniform crossover is a more general version of the multi-point crossover. Let us take an
example of the parent strings shown below to explain the principle of uniform crossover.
10110000111001101110
01110110001100010100
• In another implementation of uniform crossover, a random mask (also known as template) consisting of 1s
and 0s of the same string-length as that of the parents is created. The mask is then searched bit-wise
starting from one side.
• At a particular bit-position, if the mask contains 1, then the corresponding bits of parents 1 and 2 will be
copied in the said position of children 1 and 2, respectively.
• On the other hand, if the mask contains 0 at a specific bit-position, then the corresponding bits of parents 1
and 2 will be selected for the said position of children 2 and 1, respectively. Let us consider a mask of 20
bits as given below.

10011101010110101110
The template
• Step 4 - Crossover:
Step 5 - Mutation : In a GA, the concept of biological mutation is modeled artificially to
bring a local change over the current solution.
In mutation, 1 is converted into 0 and vice-versa.
The role of mutation operator is explained with the help of the following fig. , which shows
the plot of an objective function having one local basin and another global basin. Let us
suppose that all the randomly generated initial solutions

Under these circumstances, mutation (which


changes 0 to 1 and vice-versa) can push a string
from the local basin into the global basin (refer to
Fig.) by changing the left-most bit from 0 into 1.
Thus, mutation helps the GA to search the globally
optimal solution.
• Step 4 - Crossover:
Step 5 - Mutation : In a GA, the concept of biological mutation is modeled artificially to bring a local change
over the current solution.
In mutation, 1 is converted into 0 and vice-versa.
The role of mutation operator is explained with the help of the following fig. , which shows the plot of an objective
function having one local basin and another global basin. Let us suppose that all the randomly generated initial solutions.
Problem in left-most bit.. No cross-over can help

Rules for mutation


1. Bring local change over current solution
2. 1 is converted into 0 and vise-versa.
3. It helps GA to overcome local minima problem
4. Mutation probability Pm is kept low , and selected as
0.1/𝐿 ≤ 𝑃𝑚 ≤ 1/𝐿
To implement a bit-wise mutation scheme, a number lying between 0.0
and 1.0 is created using a random number generator at each bit-position.
If this number comes out to be less than or equal to Pm at a particular bit-
position, then that bit will be mutated (that is, 1 will be converted into 0
and vice-versa).
• In general

In short, reproduction selects good strings from the population to form a mating pool and in
crossover, there is an exchange of properties between two parent strings and consequently, two
children strings are created. Mutation brings a local change at a particular bit position of the string.
Once the population of strings is modified using the operators, namely reproduction, crossover and
mutation, one generation of a GA is completed.

The GA runs until the termination criterion (that is, maximum number of generations or a desired
precision in the solution) is reached.
• Cross-over Versus Mutation

• It has been pointed out that mutation is more powerful than crossover in terms of its
disruption capability, whereas crossover outperforms mutation in terms of the
construction capability. Due to this reason, Genetic Algorithm (GA) and Genetic
Programming (GP) rely more on the crossover, whereas in Evolution Strategies (ES) and
Evolutionary Programming (EP), a more weightage is given on the mutation.
working principle of a binary-coded GA
Example one variable function

Maximize y = 𝑥 subjected to
subject to 1.0 ≤ 𝑥 ≤ 16.0

We know that the maximum is at y= 4.0 when x=16


• The initial population of binary-strings (of size N = 6) are created at random.
• We chose 6 bits to represent the variable x so L=6.
• We call the random number generator 36 times to generate 36 bits of 0’s and 1’s ,
• Lets assume the initial population

1 100101
2 011010
3 010110
4 111010
5 101100
6 001101
ID string Decoded Real value Fitness P selection P Expected
value value
F(x) = 𝒙
1 100101 37 9.81 3.13 0.18 1.07
2 011010 26 7.19 2.68 0.15 0.91
3 010110 22 6.24 2.5 0.14 0.85
4 111010 58 14.81 3.85 0.22 1.31
5 101100 44 11.48 3.39 0.19 1.16
6 001101 13 4.09 2.02 0.12 0.69
Sum 17.57 1
average 2.888
Max 3.85
P expected : How many times is individual string is copied into the next generation.
Real or actual selection may be different than expected because it is random
• Selection is based on Roulette-Wheel selection.
• Single-point crossover (pc = 1.0) i.e all strings will participate in the cross over
• A bit-wise mutation (pm = 0.03) very low
Pm * number of bits = 0.03 * 36 = 1.08
Only one bit will be changes
In real application of random search it
Could be different
Create a mating pool
• Selection is based on Roulette-Wheel selection.
• Single-point crossover (pc = 1.0) i.e all strings will participate in the cross over
• A bit-wise mutation (pm = 0.03) very low

Actual Previous Mating Pool Mating Pair Parents Cross over Children Mutation
selection pool single point

1 100101 100101 3 ( 1 with 3) 100101 10|0101 101010 101010


1 011010 011010 6 (2 with 6) 111010 11|1010 110101 110101
0 010110 111010 1 (1 with 3) 011010 011|010 011100 011100
2 111010 111010 5 ( 4 with 5) 101100 101|100 101010 111010
2 101100 101100 4 ( 5 with 4) 111010 11|1010 111100 111100
0 001101 101100 2 ( 6 with 2) 101100 10|1100 101010 101010
• Selection is based on Roulette-Wheel selection.
• Single-point crossover (pc = 1.0) i.e all strings will participate in the cross over
• A bit-wise mutation (pm = 0.03) very low
Actual Previous Decoded Real value Fittness Ps P Estimated Real
selection pool value x F(x) = 𝒙 selection

1 101010 42 11.0 3.32 0.159156 0.954938 0


2 110101 53 13.62 3.69 0.176894 1.061361 1
3 011100 28 7.67 2.77 0.13279 0.79674 0
4 111010 58 14.81 3.85 0.184564 1.107383 2
5 111100 60 15.28 3.91 0.18744 1.12464 2
6 101010 42 11.00 3.32 0.159156 0.954938 1
Sum = 20.86
Av = 3.48
Max = 3.91
Fittnes
Actual Real s
Previou Decoded P Real Mating Mating Cross
selectio value F(x) Ps Parents children Mutation
s pool value Estimated selection pool pair over
n x = 𝒙

1 101010 42 11.0 3.32 0.159156 0.954938 0 110101

2 110101 53 13.62 3.69 0.176894 1.061361 1 111010

3 011100 28 7.67 2.77 0.13279 0.79674 0 111010

4 111010 58 14.81 3.85 0.184564 1.107383 2 111100

5 111100 60 15.28 3.91 0.18744 1.12464 2 111100

6 101010 42 11.00 3.32 0.159156 0.954938 1 101010

Sum = 20.86
Av = 3.48
Max = 3.91
Notes
• Every iteration the average and maximum will improve
How to fix the GA parameters
Constraints handling
In real problems we have constraints
Objective function

Functional constrains (equality or inequality Side constraints


Penalty functional approach
• penalty function approach, method of maintaining a feasible population over infeasible
solutions, approach aiming at preserving feasibility of solutions, approach separating the
objectives and constraints .
• In this approach, the fitness function of a solution (say i−th) 𝑓𝑖 𝑋 is expressed by modifying its
objective function as follows:
𝐹𝑖 𝑋 = 𝑓𝑖 𝑋 ± 𝑃𝑖
- if I is solving a maximization problem the penalty term has to be subtracted.
- if I is solving a minimization problem the penalty term has to be added.
- If no violation 𝑃𝑖 = 0

𝑃𝑖 is penalty term 𝑓𝑖 fitness


Penalty functional approach 𝜑𝑖𝑘 (𝑋) is the violation i.e
Ex if the constraint that is violated
𝐹𝑖 𝑋 = 𝑓𝑖 𝑋 ± 𝑃𝑖 is x1 + x2 < 4
And the ith solution is when x1 =4
Static Penalty:
𝑞 and x2= 3
𝑃𝑖 = 𝐶 ෍ 𝜑𝑖𝑘 (𝑋) 2 Then
𝑘=1 𝜑𝑖𝑘 (𝑥1, 𝑥2) is obtained as:
is penalty term 𝑓𝑖 is the fitness RHS ➔ 3+4 =7 LHS ➔4
𝜑𝑖𝑘 𝑥1, 𝑥2 = 4 − 7 = −3
Pi is penalty of ith solution We therefore square the term to
𝑞

𝐹𝑖 𝑋 = 𝑓𝑖 𝑋 + 𝐶 ෍ 𝜑𝑖𝑘 (𝑋) 2 remove sign.


𝑘=1
Q is number of constraints ,
k is constraints count and
i is solution count

2
where C indicates the user-defined penalty coefficient and 𝜑𝑖𝑘 (𝑋) represents the penalty
term for k-th constraint, corresponding to i-th objective function.

You might also like