Swarm Unit2
Swarm Unit2
Evolutionary Computing
• Evolutionary computing is a area of computer science which is inspired by the
process of natural evolution.
Unit 2
Evolutionary Algorithms
By
Dr. Naeem Ahmad
Assistant Professor
NIT Raipur, India
1 2
5 6
1
19-08-2024
Survival of Fittest
Millions of
years Reproduction by
selected population
8
7
Chromosomes Genes
• Genetic information is encoded in • Visible properties like eye color or fur color can be determined by genes.
chromosomes.
• Each chromosomes is built of
DNA.
• Chromosomes are divided into
several parts called genes.
• Genes are the properties of
species.
• Genes are the functional units of
inheritance encoding phenotypic
characteristics.
9 10
2
19-08-2024
• In reproduction two gametes conjugate • Given a quality function is to be maximized, we can randomly creates a set of
candidate solution.
to zygote which will become a new
• Apply quality function to these and find the fitness value (higher means better)
individual.
• On the basis of fitness values, some of the better candidates are chosen to seed the
• Crossover leads to a new genotype. next generation.
• This can be done by applying recombination and/or mutation.
• These create a set of new candidates (offsprings) which compete the old candidates
based on their fitness value, for a place in next generation.
13 14
15 16
17 18
3
19-08-2024
19 20
Start
Initialize
Evaluate Solution
No
Optimum Selection
Solution?
Yes Crossover
Stop
Mutation
22
Pseudocode GA Process
Begin • Encode potential solution in terms of chromosomes like data structure.
INITIALIZE population with random candidate solution • Select parent on the basis of fitness of the solutions to produce
Evaluate each solution (calculate fittest function) offsprings for next generation, which contain the characteristics of the
REPEAT UNTIL (Termination condition satisfied) { both parent.
Selection • Employ recombination operators (Selection, Crossover, Mutation)
Crossover repeatedly to preserve the good portion of the string.
Mutation • Good portions of the string usually lead to optimal or near optimal
Calculate fittest function solution. The method is applied over a desired number of generations.
} • If well designed, population will converge faster.
End
23 24
4
19-08-2024
25 26
27 28
Parent 2 Child 2
29 30
5
19-08-2024
31 32
33 34
6
19-08-2024
feasible one
Feasible Space • Legality of a chromosomes
oChromosomes represent a solution to a problem.
• Uniqueness of a mapping (between chromosomes and
1-n mapping
solution to a problem)
Coding Space
oOf these mappings, one to one mapping is highly desirable with
Solution Space
n-1 mapping one chromosome representing only one solution to a problem.
1-1 mapping
37 38
Initialization Initialization
• Create initial population of solutions • Population of solutions
o Through a process of randomization • Fitness of solutions are evaluated (= objective function)
o Through a local search procedure
Solution No. Fitness Value
o Selecting only feasible solution
1 0 1 0 0 1 0 1 1 0 1 0 1 13.2783
0 1 1 0 1 0 1 0 0 1 0 1 2 20.3749
• Consider a problem: Minimize F(x1,x2,x3)
0 0 1 0 1 0 1 1 1 1 0 0 3 19.8302
• With binary encoding, we have 0 1 0 1 0 0 1 0 0 0 1 1 4 52.9405
1 0 0 0 1 0 1 0 1 0 0 1 5 25.8302
1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1 1 0 0 0 0 1 1 6 36.0282
0 0 1 0 1 0 1 1 0 1 1 0 7 70.9202
x1 x2 x3 0 1 1 1 1 0 0 1 1 1 0 1 8 38.9022
0 1 0 1 0 1 0 1 1 0 0 1 9 29.0292
1 0 0 0 1 1 1 1 1 1 0 0 10 21.9292
39 40
41 42
7
19-08-2024
Reproduction Reproduction
• For the example of optimization problem Solution Parent Selected Offsprings
o Let the crossover probability be 0.8 Selected
Ok for
Solution No. Chromosomes Random value [0,1] Crossover ? 2 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1
1 1 0 1 0 0 1 0 1 1 0 1 0 0.9502 >0.8 No 3 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 0 0
2 0 1 1 0 1 0 1 0 0 1 0 1 0.2191 <0.8 Yes
3 0 0 1 0 1 0 1 1 1 1 0 0 0.4607 <0.8 Yes 4 0 1 0 1 0 0 1 0 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1
4 0 1 0 1 0 0 1 0 0 0 1 1 0.6081 <0.8 Yes 7 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 0
5 1 0 0 0 1 0 1 0 1 0 0 1 0.8128 >0.8 No
6 1 0 1 1 1 1 0 0 0 0 1 1 0.9256 >0.8 No 8 0 1 1 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 0 1 1 1 0 1
7 0 0 1 0 1 0 1 1 0 1 1 0 0.7779 <0.8 Yes 10 1 0 0 0 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 0 0
8 0 1 1 1 1 0 0 1 1 1 0 1 0.4596 <0.8 Yes
9 0 1 0 1 0 1 0 1 1 0 0 1 0.9817 >0.8 No
10 1 0 0 0 1 1 1 1 1 1 0 0 0.7784 <0.8 Yes
43 44
Reproduction Reproduction
• Mutation operation (based on mutation probability) • For optimization problem
• Each bit of every individual is modified with probability P m. Minimize f(x1,x2,x3)
0 1 1 0 1 0 1 1 0 1 0 1
• Main operator for global search (looking at new areas of search space) • Let Pm = 1/12 = 0.083
0.12 0.57 0.62 0.31 0.01 0.73 0.83 0.63 0.02 0.26 0.94 0.63
• Pm is usually small {0.0001, …., 0.01}. • Generate a random number [0,1]
• Rule of thumb Pm = 1/ (no of bits in chromosome) for each bit
Mutation
• Select bit having probability less
than Pm.
• Interchanging the bits with each 0 1 1 0 0 0 1 1 1 1 0 1
other.
45 46
47 48
8
19-08-2024
49 50
53 54
9
19-08-2024
GA-Example 1 GA-Example 1
• Mutation probability Pm = 1/5 = 0.2 (We can also consider 0.01, 0.1 etc.)
String Offsprings Offsprings X value Fitness
• Generate random number for all 20 genes. no. after after mutation 𝒇 𝒙 = 𝒙𝟐 + 𝟏
crossover
String No. Random numbers 1 01100 01100 12 145
2 11001 11100 28 785
1 0.6787 0.7577 0.7431 0.3922 0.6555 3 11010 11010 26 677
2 0.7712 0.7060 0.1318 0.2769 0.0462 4 10000 10000 16 257
3 0.5971 0.8235 0.6948 0.3171 0.9502 Sum σ 𝒇𝒊 1864
Average 𝒇𝒂𝒗𝒈 466
4 0.3644 0.4387 0.3816 0.7655 0.7952
Maximum 785
• Genes in the above highlighted places are to be flipped (0 to 1 and 1 to 0). Best in this iteration (final)
55 56
59 60
10
19-08-2024
GA-Example-2 GA-Example-2
Mating pool After crossover Iteration 2
Crossover between string
String Details 3 and 4 String Details String Details Objective function Fitness Expected
1 1-2-3-4 1 1-2-3-4 value (D) function Count
Prt 1: 3–1–4–2 value (100-D)
2 2-4-1-3 2 3-1-4-2
Prt 2: 4–3–2–1 1 1-2-3-4 20+9+23+7 = 59 41 1.14 1
3 3-1-4-2 3 1-3-2-4
2 3-1-4-2 13+25+12+6 = 56 44 1.23 2
4 4-3-2-1 4 2-1-4-3
Ch 1: 1–3–2–4 3 1-3-2-4 20+15+19+7 = 61 39 1.03 1
Ch 2: 2–1–4–3 4 2-1-4-3 14+25+12+30 = 81 19 0.53 0
ftotal = 143
favg = 35.75
61 62
63 64
𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡 𝑠𝑖 𝑥𝑖 ≤ 50
𝑖=0 • Chromosome A: 101100
• What should be the fitness function? • Chromosome B : 110111
65 66
11
19-08-2024
• Parent 1: 101 100; Parent 2: 110 111 Child 1: 101 111; Child 2: 110 100
67
12