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

Tutorial Problems GA

The document discusses the application of Genetic Algorithms (GA) to solve various optimization problems, including a mathematical function and a supply chain problem. It details the processes of initialization, selection, crossover, and mutation within the GA framework, providing specific examples and calculations for each step. Additionally, it presents an example of the Asymmetric Traveling Salesman Problem (ATSP) to illustrate the use of GA in routing optimization.

Uploaded by

Shubham Jakhmola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial Problems GA

The document discusses the application of Genetic Algorithms (GA) to solve various optimization problems, including a mathematical function and a supply chain problem. It details the processes of initialization, selection, crossover, and mutation within the GA framework, providing specific examples and calculations for each step. Additionally, it presents an example of the Asymmetric Traveling Salesman Problem (ATSP) to illustrate the use of GA in routing optimization.

Uploaded by

Shubham Jakhmola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Genetic Algorithm

Using GA to solve the following problem


𝑓 𝑥 = 𝑥2
Where x is permitted to vary between 0 and 31
Population size =4
Solution
• Using base 2 arithmetic, we have 2 digits to work with.
• With a five-bit (binary digit) unsigned integer (because 25 = 32) we
can obtain numbers between 0 (00000) and 31 (11111) with a well-
defined objective function and coding, we now simulate a single
generation of a genetic algorithm with reproduction, crossover and
mutation.
Initialisation:
Random population of size 4 by tossing a coin 20 times
Selection
Expected Actual
Initial x Value
pselect Count
population (Unsign count
String No. 𝒇 𝒙 = 𝒙𝟐 𝒇𝒊
𝒇𝒊 from
(Randomly ed
σ𝒇 Roulette
generated integer) 𝒇𝒂𝒗𝒈
wheel
1 01101 13 169 0.14 0.58 1
2 11000 24 576 0.49 1.97 2
3 01000 8 64 0.06 0.22 0
4 10011 19 361 0.31 1.23 1
Sum 1170 1 4 4
Average 293 0.25 1 1
Max 576 0.49 1.97 2
Selection
• Select the mating pool of the next generation by
spinning the weighted Roulette wheel four times.
• Strings 1 and 4 receive one copy in the mating
pool.
• String 2 received two copies.
• String 3 received no copies as shown in Table 1

• Compare this with the expected number of copies


(n-pselecti)
• The best get more copies, the average stays even,
and the worst die-off.
Crossover
Crossover
Mating pool After Mate New
String Site x- 𝒇 𝒙
Reproduction (Cross (Randomly Populati
No. (Randomly value = 𝒙𝟐
site shown) Selected) on
Selected)
1 0110|1 2 4 01100 12 144
2 1100|0 1 4 11001 25 625
3 11|000 4 2 11011 27 729
4 10|011 3 2 10000 16 256
Sum 1754
Average 439
Max 729
Mutation
• Mutation probability: 1/5=0.2

String
Random numbers
No.
1 0.63 0.51 0.16 0.79 0.11
2 0.88 0.45 0.40 1.00 0.98
3 0.93 0.95 0.59 0.95 0.06
4 0.33 0.11 0.75 0.92 0.05
Mutation
Offspring After Offspring after
String No. x-value 𝐟 𝐱 = 𝐱𝟐
Crossover mutation
1 01100 01001 9 81
2 11001 11001 25 625
3 11011 11010 26 576
4 10000 11001 25 625
Sum 1907
Average 476.75
Maximum 625
No improvement in the solution, so iterate gain.
Solve using GA

𝑓 𝑥 = 𝑥 2
+1
0 < 𝑥 < 27
Problem 2
𝑀𝑎𝑥𝑖𝑚𝑖𝑧𝑒 𝑌 = 800 − 62.83 2𝐷 + 0.91𝐷−0.2
0 < 𝐷 < 6.3
Use GA to solve the above problem.
Solution:
GA works with a set of solutions.
Each solution is represented by a string of binary variables,
corresponding to the chromosomes in genetics.
Selection
Initial Expected Actual Count
D-values (1 Fitness pselect
String population count from
decimal Function 𝒇𝒊
No. randomly 𝒇𝒊 Roulette
accuracy) (Y=f) σ𝒇
generated 𝒇 wheel
1 0.000011 0.3 689.6 0.41 1.73 2
2 010100 2.0 498.9 0.29 1.25 1
3 011110 3.0 377.1 0.22 0.945 1
4 110010 5.0 130.3 0.07 0.33 0
Sum 1695.9 1 4.255 4
Average 423.9 0.25 1.06375 1
Max 689.6 0.41 1.73 2
Selection
p-select
String 1 String 2 String 3 String 4
String 4
7%

String 3
22%
String 1
42%

String 2
29%
Single point Crossover
Mate Crossover New
Mating pool After D-
String (Random Site Population
Reproduction val 𝒀=𝒇 𝒙
No. ly (Randomly after
(Cross site shown) ue
Selected) Selected) crossover
1 0.0000|11 4 4 0.000010 0.2 696
2 000|011 3 3 000100 0.4 681.1
3 010|100 2 3 010011 1.9 511
4 0111|10 1 4 011111 3.1 364.9
Sum 2253.0
Average 563.3
Max 696
Mutation
Taking mutation probability 0.05
String
Random numbers
No.
1 0.33 0.44 0.35 0.08 0.83 0.41
2 0.34 0.97 0.54 0.56 0.30 0.40
3 0.07 0.04 0.29 0.27 0.14 0.55
4 0.57 0.27 0.74 0.30 0.99 0.08
Mutation
Offspring After Offspring after
String No. x-value 𝒀=𝒇 𝒙
Crossover mutation
1 0.000010 0.000010 0.03125
2 000100 000100 4
3 010011 000011 3
4 011111 011111 31
Sum
Average
Maximum
Problem 3
Consider a supply chain of a computer manufacturing industry.
• Demand of each customer zone is satisfied exactly by one production plant
• Each production plant supplies exactly to one customer zone
Customer Customer Customer Customer
zone 1 zone 2 zone 3 zone 4
Plant 1 20 25 17 22
Plant 2 14 9 19 6
Plant 3 13 15 23 30
Plant 4 8 25 12 7
Distance in km matrix (as shown)
Objective function Min. Distance
Problem 3
Selection: expected count (=𝑓ൗ𝑓𝑎𝑣𝑔 ) String No. Iteration 1 Iteration 2
1 0.98 0.47
Crossover procedure
2 0.57 0.55
Before crossover After crossover
3 0.45 0.88
abcd dbca
4 0.93 0.93
ijkl ljki
Selection criteria:
Crossover probability (𝐶𝑝 = 0.8) Iteration 1:
String 2 and String 3 are selected for crossover as
Random number used for crossover their random numbers are less than 𝐶𝑝
as shown in Table Iteration 2:
String 1 and String 2 are selected for crossover as
their random numbers are less than 𝐶𝑝
Customer Customer Customer Customer

Solution: Plant 1
zone 1
20
zone 2
25
zone 3
17
zone 4
22
Plant 2 14 9 19 6
Max. Distance (100-D) Plant 3 13 15 23 30
Plant 4 8 25 12 7
Initial Population
String Details Objective Function Fitness Function Value Expected count
Value (D) (100-D)
1 1-2-3-4 25+19+30+8=82 100-82=18 0.55≈ 0
2 2-4-1-3 6+8+17+15=46 100-46=54 1.66≈ 2
3 3-1-4-2 13+22+25+19=79 100-79=21 0.646≈ 1
4 4-3-2-1 12+15+14+22=63 100-63=37 1.13≈ 1
𝑓𝑡𝑜𝑡𝑎𝑙 = 130 4
𝑓𝑎𝑣𝑔 = 32.5
Solution:
Mating Pool After crossover
String Details Crossover between String 2 String Details
and String 3
1 2-4-1-3 1 2-4-1-3
P1: 2-4-1-3
2 2-4-1-3 P2: 3-1-4-2 2 3-4-1-2
3 3-1-4-2 3 2-1-4-3
4 4-3-2-1 Ch1: 3-4-1-2 4 4-3-2-1
Ch2: 2-1-4-3
Customer Customer Customer Customer
zone 1 zone 2 zone 3 zone 4
Solution: Plant 1 20 25 17 22
Plant 2 14 9 19 6
Plant 3 13 15 23 30
Iteration 2
Plant 4 8 25 12 7

String Details Objective Function Fitness Function Value (100- Expected count
Value (D) D)
1 1-2-3-4 20+9+23+7=59 100-59=41 1.11≈ 1
2 3-1-4-2 13+25+12+6=56 100-56=44 1.23≈ 2
3 1-3-2-4 20+15+19+7=61 100-61=39 1.03≈ 1
4 2-1-4-3 14+25+12+30=81 100-81=19 0.53≈ 0
𝑓𝑡𝑜𝑡𝑎𝑙 = 143
𝑓𝑎𝑣𝑔 = 35.75
Customer Customer Customer Customer
zone 1 zone 2 zone 3 zone 4
Solution: Plant 1 20 25 17 22
Plant 2 14 9 19 6
Plant 3 13 15 23 30
Iteration 3 Plant 4 8 25 12 7

String Details Objective Function Fitness Function Value Expected count


Value (D) (100-D)
1 2-1-4-3 14+25+12+30=81 100-81=19 0.54≈ 0
2 4-2-3-1 8+9+23+22=62 100-62=38 1.08≈ 1
3 3-1-4-2 13+25+12+6=56 100-56=44 1.25≈ 2
4 1-3-2-4 20+15+19+7=61 100-61=39 1.11≈ 1
𝑓𝑡𝑜𝑡𝑎𝑙 = 140
𝑓𝑎𝑣𝑔 = 35

Maximum Fitness value is 44 Best solution is 56


Example of GA

• Asymmetric traveling salesman problem (ATSP): Find the routing


of this problem by GA. Total generation = 2 , Population= 4
• Crossover and Mutation rate = 0.8 and 0.2 respectively.
• One-point crossover and swap mutation
From/To City 1 City 2 City 3 City 4 City 5
City 1 0 4 7 9 12
City 2 3 0 8 12 15
City 3 7 9 0 17 12
City 4 8 12 13 0 16
City 5 12 6 15 18 0
Example of GA

Step 1: Initial Solution Step 2: Evaluation

V1 2 4 3 5 1 • σ(𝐷2,4 , 𝐷4,3 , 𝐷3,5 , 𝐷5,1 , 𝐷1,2 ) =


V2 1 3 5 4 2
12 + 13 + 12 + 12 + 4 = 53
• V2 = 52, V3 = 53, V4 = 57
V3 4 3 5 1 2
V2 is 1-3-5-4-2 and objective
V4 3 1 5 4 2
function = 52
Example of GA

Step 3: Crossover Step 4: Mutation


One-Cut point crossover and crossover rate= 0.8 Swap Mutation and mutation rate = 0.2

Population random Population random


V1 & V2 0.3
V1 0.50
V3 & V4 0.9
V2 0.31
V3 0.06
V1 2 4 3 5 1 V1 2 4 5 4 2 V4 0.18
V2 1 3 5 4 2 V2 1 3 3 5 1
V3 4 3 5 1 2 V3 4 1 5 3 2

V1 2 4 5 1 3 V4 3 1 5 4 2 V4 5 1 3 4 2

V2 1 3 2 5 4
Example of GA

Parent Offspring
Step 5: Evaluation
V1 2 4 3 5 1 V1 2 4 5 1 3

V2 1 3 5 4 2 V2 1 3 2 5 4

V3 4 3 5 1 2 V3 4 1 5 3 2

V4 3 1 5 4 2 V4 5 1 3 4 2

• V1 is 4-1-5-3-2 and the objective function


= ( D , D , D , D , D ) =12+16+12+7+9=56
41 15 53 32 24

• V2=57, V3=56, V4=63


• V1 route = 2-4-5-1-3 and the objective function = 56
Example of GA

Step 6: Selection Roulette wheel selection


- (F)
1 1 Population Objective Eval(Vk) Pk Qk
eval (v1 ) = = 0.0179 , eval (v2 ) = = 0.0175 ,
56 57 function
1 1
eval (v3 ) = = 0.0179 , eval (v4 ) = = 0.0159
56 63 V1 56 0.0179 0.258 0.258
F = 0.0179+0.0175+0.0179+0.0159 = 0.069 V2 57 0.0175 0.254 0.512
- pk V3
0.0179 0.0175 56 0.0179 0.258 0.770
p1 = = 0.258 , p2 = = 0.254 ,
0.069 0.069 V4 63 0.0159 0.230 1.000
0.0179 0.0159
p3 = = 0.258 , p4 = = 0.230 F= 0.069
0.069 0.069
- qk
q1 = 0.258 , q2 = 0.258 + 0.254 = 0.512 , q3 = 0.258 + 0.254 + 0.258 = 0.770 ,
q1 = 0.258 + 0.254 + 0.258 + 0.230 = 1.00
Example of GA
0
Step 6: Selection (Cont.)
V4=0.230 V1=0.258

0.770
0.258

V3=0.258 V2=0.254

Random Value (r) Select 0.512


V1 4 1 5 3 2
Chromosome
1 0.58 V3 V2 2 4 5 1 3
2 0.12 V1
V3 5 1 3 4 2
3 0.96 V4
4 0.76 V3
V4 4 1 5 3 2
Tutorial-2
Q9. A company manufactures two products, A and B, on two machines. Whatever is
manufactured is sold in the market, as the market for the product is good. The
capacities of the machines are limited to produce daily 80 units of product A and 60
units of product B. The raw material supply required for the product is limited to produce
600 units per day. The labour required is 160 man-days, and the organization has 160
men on the roll. The production of A requires one man-day hour of labour and that of
product B is 2-man day hour. The company’s objective is to maximize the profit if the
sales price relationships are as given below.

Product Unit price Quantity Cost function


demand
A P1 1500-5P1 200a + 0.1a2
B P2 3800-10P2 300b + 0.1 b2

In the above table a and b are the number of units of A and B produced, respectively.
This can also be written as: a = 1500-5P1 and b=3800-10P2
Solution

Let 𝑅 be the revenue and 𝐶 the cost of production, so that profit


𝑃𝑟𝑜𝑓𝑖𝑡 = 𝑅𝑒𝑣𝑒𝑛𝑢𝑒 − 𝐶𝑜𝑠𝑡
𝑅 = 𝑃1 𝑎 + 𝑃2 𝑏 = 300 − 0.2𝑎 𝑎 + 380 − 0.1𝑏 𝑏
𝑅 = 300𝑎 − 0.2𝑎2 + 380𝑏 − 0.1𝑏 2
and
𝐶 = 200𝑎 + 0.1𝑎2 + 300𝑏 + 0.1𝑏 2
Therefore, Maximize Z = 𝑅 − 𝐶 = 100𝑎 + 0.3𝑎2 + 80𝑏 − 0.2𝑏 2
To check 𝑃1 and 𝑃2 , compute din the problem let us construct
𝐴 ≤ 80, 𝑏 < 60, 5𝑎 + 6𝑏 ≤ 600, 𝑎 + 2𝑏 ≤ 160,
𝑎, 𝑏 ≥ 0
Testing Convex or Concave

You might also like