Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Genetic Algorithm for Solving Equations: A Step-by-Step Guide, Cheat Sheet of Applied Computing

Soft computing notes from Chitkara univeristy

Typology: Cheat Sheet

2020/2021

Uploaded on 05/07/2023

aman-kumar-90
aman-kumar-90 🇮🇳

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Example:
Suppose there is equality a + 2b + 3c + 4d = 30, genetic algorithm will be used to find the value
of a, b, c, and d that satisfy the above equation. First, we should formulate the objective function,
for this problem the objective is minimizing the value of function f(x) where f(x) = ((a + 2b + 3c
+ 4d) - 30). Since there are four variables in the equation, namely a, b, c, and d, we can compose
the chromosome as follow: To speed up the computation, we can restrict that the values of
variables a, b, c, and d are integers between 0 and 30.
Step 1. Initialization
For example, we define the number of chromosomes in population are 6, then we generate
random value of gene a, b, c, d for 6 chromosomes.
Chromosome[1] = [a ;b ;c ;d] = [12 ;05 ;23; 08]
Chromosome[2] = [a ;b ;c ;d] = [02 ;21 ;18 ;03]
Chromosome[3] = [a ;b ;c ;d] = [10 ;04 ;13 ;14]
Chromosome[4] = [a ;b ;c ;d] = [20 ;01 ;10 ;06]
Chromosome[5] = [a ;b ;c ;d] = [01 ;04 ;13 ;19]
Chromosome[6] = [a ;b ;c ;d] = [20 ;05 ;17 ;01]
Step 2. Evaluation
We compute the objective function value for each chromosome produced in initialization step:
F_obj[1] = Abs(( 12 + 2*05 + 3*23 + 4*08 ) - 30) = Abs((12 + 10 + 69 + 32 ) - 30)
= Abs(123 - 30) = 93
F_obj[2] = Abs((02 + 2*21 + 3*18 + 4*03) - 30) = Abs((02 + 42 + 54 + 12) - 30)
= Abs(110 - 30) = 80
F_obj[3] = Abs((10 + 2*04 + 3*13 + 4*14) - 30) = Abs((10 + 08 + 39 + 56) - 30)
= Abs(113 - 30) = 83
F_obj[4] = Abs((20 + 2*01 + 3*10 + 4*06) - 30) = Abs((20 + 02 + 30 + 24) - 30)
= Abs(76 - 30) = 46
pf3
pf4
pf5

Partial preview of the text

Download Genetic Algorithm for Solving Equations: A Step-by-Step Guide and more Cheat Sheet Applied Computing in PDF only on Docsity!

Example: Suppose there is equality a + 2b + 3c + 4d = 30, genetic algorithm will be used to find the value of a, b, c, and d that satisfy the above equation. First, we should formulate the objective function, for this problem the objective is minimizing the value of function f(x) where f(x) = ((a + 2b + 3c

  • 4d) - 30). Since there are four variables in the equation, namely a, b, c, and d, we can compose the chromosome as follow: To speed up the computation, we can restrict that the values of variables a, b, c, and d are integers between 0 and 30. Step 1. Initialization For example, we define the number of chromosomes in population are 6, then we generate random value of gene a, b, c, d for 6 chromosomes. Chromosome[1] = [a ;b ;c ;d] = [12 ;05 ;23; 08] Chromosome[2] = [a ;b ;c ;d] = [02 ;21 ;18 ;03] Chromosome[3] = [a ;b ;c ;d] = [10 ;04 ;13 ;14] Chromosome[4] = [a ;b ;c ;d] = [20 ;01 ;10 ;06] Chromosome[5] = [a ;b ;c ;d] = [01 ;04 ;13 ;19] Chromosome[6] = [a ;b ;c ;d] = [20 ;05 ;17 ;01] Step 2. Evaluation We compute the objective function value for each chromosome produced in initialization step: F_obj[1] = Abs(( 12 + 205 + 323 + 408 ) - 30) = Abs((12 + 10 + 69 + 32 ) - 30) = Abs(123 - 30) = 93 F_obj[2] = Abs((02 + 221 + 318 + 403) - 30) = Abs((02 + 42 + 54 + 12) - 30) = Abs(110 - 30) = 80 F_obj[3] = Abs((10 + 204 + 313 + 414) - 30) = Abs((10 + 08 + 39 + 56) - 30) = Abs(113 - 30) = 83 F_obj[4] = Abs((20 + 201 + 310 + 406) - 30) = Abs((20 + 02 + 30 + 24) - 30) = Abs(76 - 30) = 46

F_obj[5] = Abs((01 + 204 + 313 + 419) - 30) = Abs((01 + 08 + 39 + 76) - 30) = Abs(124 - 30) = 94 F_obj[6] = Abs((20 + 205 + 317 + 401) - 30) = Abs((20 + 10 + 51 + 04) - 30) = Abs(85 - 30) = 55 Step 3. Selection

  1. The fittest chromosomes have higher probability to be selected for the next generation. To compute fitness probability, we must compute the fitness of each chromosome. To avoid divide by zero problem, the value of F_obj is added by 1. Fitness[1] = 1 / (1+F_obj[1]) = 1 / 94 = 0. Fitness[2] = 1 / (1+F_obj[2]) = 1 / 81 = 0. Fitness[3] = 1 / (1+F_obj[3]) = 1 / 84 = 0. Fitness[4] = 1 / (1+F_obj[4]) = 1 / 47 = 0. Fitness[5] = 1 / (1+F_obj[5]) = 1 / 95 = 0. Fitness[6] = 1 / (1+F_obj[6]) = 1 / 56 = 0. Total = 0.0106 + 0.0123 + 0.0119 + 0.0213 + 0.0105 + 0.0179 = 0.0845 The probability for each chromosome is formulated by: P[i] = Fitness[i] / Total P[1] = 0.0106 / 0.0845 = 0. P[2] = 0.0123 / 0.0845 = 0. P[3] = 0.0119 / 0.0845 = 0. P[4] = 0.0213 / 0.0845 = 0. P[5] = 0.0105 / 0.0845 = 0. P[6] = 0.0179 / 0.0845 = 0. From the probabilities above we can see that Chromosome 4 that has the highest fitness, this chromosome has highest probability to be selected for next generation chromosomes. For the selection process we use roulette wheel, for that we should compute the cumulative probability values:

Chromosome[2] = [10;04;13;14] Chromosome[3] = [12;05;23;08] Chromosome[4] = [20;05;17;01] Chromosome[5] = [10;04;13;14] Chromosome[6] = [20;01;10;06] In this example, we use one-cut point, i.e. randomly select a position in the parent chromosome then exchanging sub-chromosome. Parent chromosome which will mate is randomly selected and the number of mate Chromosomes is controlled using crossover_rate (ρc) parameters. For random number R above, parents are Chromosome[1], Chromosome[4] and Chromosome[5] will be selected for crossover. Chromosome[1] >< Chromosome[4] Chromosome[4] >< Chromosome[5] Chromosome[5] >< Chromosome[1] After chromosome selection, the next process is determining the position of the crossover point. This is done by generating random numbers between 1 to (length of Chromosome – 1). In this case, generated random numbers should be between 1 and 3. After we get the crossover point, parents Chromosome will be cut at crossover point and its gens will be interchanged. For example, we generated 3 random number and we get: C[1] = 1 C[2] = 1 C[3] = 2 Then for first crossover, second crossover and third crossover, parent’s gens will be cut at gen number 1, gen number 1 and gen number 3 respectively, e.g. Chromosome[1] = Chromosome[1] >< Chromosome[4] = [02;21;18;03] >< [20;05;17;01] = [02;05;17;01] Chromosome[4] = Chromosome[4] >< Chromosome[5]

= [20;05;17;01] >< [10;04;13;14]

= [20;04;13;14]

Chromosome[5] = Chromosome[5] >< Chromosome[1] = [10;04;13;14] >< [02;21;18;03] = [10;04;18;03] Thus, Chromosome population after experiencing a crossover process: Chromosome[1] = [02;05;17;01] Chromosome[2] = [10;04;13;14] Chromosome[3] = [12;05;23;08] Chromosome[4] = [20;04;13;14] Chromosome[5] = [10;04;18;03] Chromosome[6] = [20;01;10;06] Step 5. Mutation Number of chromosomes that have mutations in a population is determined by the mutation_rate parameter. Mutation process is done by replacing the gen at random position with a new value. The process is as follows. First, we must calculate the total length of gen in the population. In this case the total length of gen is total_gen = number_of_gen_in_Chromosome * number of populations = 4 * 6 = 24 Mutation process is done by generating a random integer between 1 and total_gen (1 to 24). If generated random number is smaller than mutation_rate(ρm) variable then marked the position of gen in chromosomes. Suppose we define ρm 10%, it is expected that 10% (0.1) of total_gen in the population that will be mutated: number of mutations = 0.1 * 24 = 2.4 ≈ 2 Suppose generation of random number yield 12 and 18 then the chromosome which have mutation are Chromosome number 3 gen number 4 and Chromosome 5 gen number 2. The value of mutated gens at mutation point is replaced by random number between 0-30. Suppose generated random number are 2 and 5 then Chromosome composition after mutation are:

Chromosome[5] = [10;05;18;03] Chromosome[6] = [20;01;10;06] These new Chromosomes will undergo the same process as the previous generation of Chromosomes such as evaluation, selection, crossover and mutation and at the end it produces new generation of Chromosome for the next iteration. This process will be repeated until a predetermined number of generations. For this example, after running 50 generations, best chromosome is obtained: Chromosome = [07; 05; 03; 01] This means that: a = 7, b = 5, c = 3, d = 1 If we use the number in the problem equation: a + 2b + 3c + 4d = 30 7 + (2 * 5) + (3 * 3) + (4 * 1) = 30 We can see that the value of variable a, b, c and d generated by genetic algorithm can satisfy that equality.