Genetic Algorithm
Questions:
Q 1) Using a genetic algorithm find the minimum value for the following function
𝑭(𝒙) = 𝒙𝟐 − 𝟏𝟎𝒙 , 0 ≤ x ≤ 10
Where is 6 chromosome for each generation and the length of a chromosome 4-bits?
Solution:
Step 1: Represent the problem variable domain as a chromosome of a fixed length
# of chromosome in each generation N= 6
length of chromosome is 4bit
Step 2: Define a fitness function
It’s F(x )
Step 3: Randomly generate an initial population of chromosomes of size N=6
Decoded
integer
Chromosome string Chromosome label
4 0 1 0 0 1X
1 0 0 0 1 2X
5 0 1 0 1 3X
9 1 0 0 1 4X
2 0 0 1 0 5X
7 0 1 1 1 6X
Step 4: Calculate the fitness of each individual chromosome and compute ratio
The Islamic University of Gaza
Faculty of Engineering
Dept. of Computer Engineering
ECOM5039:Artificial Intelligence
Eng. Ibraheem Lubbad
التي تعطي اقل قيمة xالمطلوب في السؤال إيجاد قيمة انفي المرحلة التالية , بما الكرموسوماحتمالية اختيار تمثل ratioقيمة
00و 0بين في الفترة
.على المجموع الكرموسوم قسمة قيمة chromosomeطريقة حسابها لكل
Ratio % Chromosome fitness Chromosome label
80.02 F( X1 )=-24 X1
2..8 -9 X2
.0.8. -25 X3
2..8 -9 X4
08.02 -0. X5
8.80
.
-21 X6
Sum = -104
تاخذ اكبر احتمال (اقل قيمة اي ) فان القيمة التي الها اعلى سالبية لذالك بسالب fitness الحظ كل كل قيم
Step 5: Select a pair of chromosomes for mating from the current population
لذا احتمالية كرموسوملكل ratio, حسب قيم Roulette wheel selectionعن طريق عملية االختيار تتم بشكل العشوائي
.اكبر الرقم االقل اختيار
Step 6: Create a pair of offspring chromosomes by applying the genetic operators - crossover and
mutation
geneticفي المرحلة السابقة يتم تطبيق عليهم احدي عمليات ال chromosemeبعد ان تم اختيار زوج من
Crossover او mutation
جدا ضئيلةوتحدث بنسبة cross over ليةتكون بانقالب جين واحد من الكرموسوم بعد عم ) الطفرة (: mutationعملية
لجيل افراد في ا ليصبحوا بعد ذالك ان الكرموسوم ما بعد النقطة في تبادل اجزاء عندها يتم تحديد نقطة يتم crossoverفي عملية
الجديد
X 0: 0 1 0 0 X’0: 0 1 0 1
X 4: 1 0 0 1 X’4 1 0 0 0
Step 7: Place the created offspring chromosomes in the new population
Step 8: Repeat Step 5:
.لحتى يتم توليد جميع الكرمسومات الجبل الجديد التي يجب ان يكون عددها
X 0: 0 1 0 0 X’0: 0 0 0 0
X 8: 0 0 0 0 X’8 :0 0 0 0
,X 0: 0 1 0 0 X’’0: 0 1 0 0
X 8: 0 0 0 0 X’’8 : 0 0 0 0
Step 9: Replace the initial (parent) chromosome population with the new (offspring) population
Decoded
integer
Chromosome string Chromosome label
5 0 1 0 1 X1
8 1 0 0 0 X2
4 0 0 0 0 X3
3 0 0 0 0 X4
7 0 1 0 1 X5
1 0 0 0 0 X6
Step 01:
x للقيم الجديدة وتكرار العمليات حتى إيجاد قيمة fittnes وحساب قيمة الرابعة بعد ايجاد قيم الجيل الجديد يتم الرجوع للخطوة
خالل النهائي اي اقل قيمة لدالة يكون الحل بعد إجراء العمليات السابقة عدة مرات اقل قيمة , في هذا السؤالعندها الدالة التي تعطي
x= 5 عند الفترة المحددة
الهدف من السؤال فهم االلية التي يتم من خاللها حساب المطلوب
Q 2) Using a genetic algorithm find the minimum value for the following function
F(𝑥1, 𝑥2)= (𝑥1 2 + 𝑥2 − 11 )2 + (𝑥1 + 𝑥2
2 + −7 )2 , 0 ≤ 𝑥1 , 𝑥2 ≤ 6
Where is 6 chromosome for each generation and the length of a chromosome 8 bits for each X’s ?
Solution:
Step 1: Represent the problem variable domain as a chromosome of a fixed length
Step 2: Define a fitness function it’s F(𝑥1, 𝑥2)
,𝑥1( الدالة نستخدماقل قيمة إليجاد x1x , 2 قيم لقياس مقدار مالئمةيعني 𝑥2F(
Step 3: Randomly generate an initial population of chromosomes of size
Chromosome string Chromosome label
1000 0001 0011 1011 Ch1
1101 1100 0010 1101 Ch2
1011 0011 1010 1000 Ch3
0001 1000 0001 0111 Ch4
0111 0111 1101 0100 Ch5
0110 1011 1101 1101 Ch6
Step 4: Calculate the fitness and compute ratio of each individual chromosome
2X بت ل 2و 1X بت ل 2نقسم الكرموسوم لقسمين لكرموسوم اوال fitnessاب لحس
For a chromosome ch1 :
10 = 1000 0001 =(129) 1x
10= 0011 1011 =(187)2x
X1 X2
actual range of parameters x0 and x2, is the range from 0 to 6 ,but chromosome can handel 8 bit
(0-255), so we need to map integer value that we can handel it by 8 – bit to actual value
Step size = 6
256−1 = 0.0235294
X1=129 * 0.0235294= 3.0352926
X2=187 * 0.0235294= 4.3999978
Chromosome fitness = f(x1, x2)
Chromosome fitness Actual value X2 Actual value X1 Ch string Ch label
244 4.4 3.04
1000 0001 0011 1011 Ch1
286 .00 6 8.08 1101 1100 0010 1101 Ch2
278 0..8 ..80 1011 0011 1010 1000 Ch3
140 0.8. 0.8. 0001 1000 0001 0111 Ch4
432 ...9 .8 8 0111 0111 1101 0100 Ch5
509 .8 2 8.82 0110 1011 1101 1101 Ch6
Sum= 022.
دية لحساب يبالطريقة التقل, لكن هنا القيم كلها موجبة ستاخذ احتمالية اكبر القيم االقل قيمة بمعني minimumايجاد في السؤال مطلوب
ratio من خالل المتمم لها من المجموع الكلي ,فان القيم االقل تاحذ اقل احتمالية , لذا نحتاج لعكس االحتمالية
بالطريقة التقليدية ratio, نحسب قيم ثم
Ratio % Ch fitness Actual value X2 Actual value X1
(1645/9445)*100=17.4
2
1889-244 =1645 244 4.4 3.04
16.97 1603 286 .00 6 8.08
17.06 1611 278 0..8 ..80
18.52 1749 140 0.8. 0.8.
15.43 1457 432 ...9 .8 8
14.61 1380 509 .8 2 8.82
Sum=9445
Sum=022.
255
5
0
6
0
Step 5: Select a pair of chromosomes for mating from the current population
Step 6: Create a pair of offspring chromosomes by applying the genetic operators - crossover and
mutation
ch2: 1101 1100 0 010 1101 ch11: 1101 1100 0101 1101
ch6: 0110 1011 1 101 1101 ch22: 0110 1011 1010 1101
Step 7: Place the created offspring chromosomes in the new population
ch3: 1011 0011 1010 1000 ch33: 1101 0111 1101 0100
ch5: 0111 0111 1101 0100 ch44: 0110 0011 1010 1000
ch2: 1011 0011 1010 1000 ch55: 1011 0011 1010 1100 ch55: 1011 0011 1110 1100
ch4: 0111 0111 1101 0100 ch66: 0111 0111 1101 0000
Ch117%
Ch217%
Ch317%
Ch419%
Ch515%
Ch615%
ROULETTE WHEEL SELECTION
mutation
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
Ch string Ch label
1101 1100 0101 1101 Ch1
0110 1011 1010 1101 Ch2
1101 0111 1101 0100 Ch3
0110 0011 1010 1000 Ch4
1011 0011 1110 1100 Ch5
0111 0111 1101 0000 Ch6
Step 01:
الجديد , بنكرر العمليات ثانية من الخطوة الرابعة لجيل بعد ما جبنا ا
31x , =22x =د عنخالل الفترة المحدد اقل قيمة للدلة الحل النهائي في هذا السؤال
End