Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

6/4/24, 3:45 PM Untitled8

In [ ]: #Name: SAYEED BIN KABIR


#Id No: 211003003080
#Course: B.Tech CSE-AI
#Batch: BCS-AI 3B
#Semester: VI
#Subject: AI LAB

In [35]: import random

POPULATION_SIZE = 20
CHROMOSOME_LENGTH = 10
MUTATION_PROBABILITY = 0.2
GENERATIONS = 100

def binary_to_int(binary_string):
return int(binary_string, 2)

def int_to_binary(integer, length=5):


return format(integer, f'0{length}b')

def initialize_population():
population = []
for _ in range(POPULATION_SIZE):
a = random.randint(0, 5)
b = random.randint(0, 5)
chromosome = int_to_binary(a) + int_to_binary(b)
population.append(chromosome)
return population

def fitness(chromosome):
a = binary_to_int(chromosome[:5])
b = binary_to_int(chromosome[5:])
return abs(100 - (5 * a + 4 * b))

def selection(population):
sorted_population = sorted(population, key=lambda chromo: fitness(chromo))
return sorted_population[:4]

def crossover(parent1, parent2):


point = random.randint(1, CHROMOSOME_LENGTH - 1)
child1 = parent1[:point] + parent2[point:]
child2 = parent2[:point] + parent1[point:]
return child1, child2

def mutate(chromosome):
mutated_chromosome = ""
for bit in chromosome:
if random.random() < MUTATION_PROBABILITY:
mutated_chromosome += '0' if bit == '1' else '1'
else:
mutated_chromosome += bit
return mutated_chromosome

def genetic_algorithm():
population = initialize_population()

for generation in range(GENERATIONS):


fitness_values = [fitness(chromosome) for chromosome in population]
localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 1/6
6/4/24, 3:45 PM Untitled8
print(f"Generation {generation+1} Errors: {fitness_values}")
selected_population = selection(population)

new_population = []
for parent in selected_population:
for _ in range(5):
new_population.append(parent)

next_generation = []
while len(next_generation) < POPULATION_SIZE:
parent1 = random.choice(new_population)
parent2 = random.choice(new_population)
child1, child2 = crossover(parent1, parent2)
next_generation.append(mutate(child1))
if len(next_generation) < POPULATION_SIZE:
next_generation.append(mutate(child2))

population = next_generation

final_fitness_values = [fitness(chromosome) for chromosome in population]


print("Final Population Errors: ", final_fitness_values)
for chromo in population:
a = binary_to_int(chromo[:5])
b = binary_to_int(chromo[5:])
print(f"Chromosome: {chromo}, a: {a}, b: {b}, Error: {fitness(chromo)}")

genetic_algorithm()

localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 2/6
6/4/24, 3:45 PM Untitled8
Generation 1 Errors: [64, 60, 67, 87, 80, 80, 64, 70, 100, 70, 55, 81, 81, 91, 80, 7
9, 68, 69, 75, 84]
Generation 2 Errors: [67, 76, 24, 34, 48, 58, 49, 20, 25, 76, 21, 17, 60, 51, 60, 38,
77, 16, 71, 60]
Generation 3 Errors: [15, 52, 25, 1, 35, 18, 17, 89, 46, 65, 32, 56, 15, 164, 71, 52,
56, 60, 75, 21]
Generation 4 Errors: [23, 9, 7, 40, 41, 36, 55, 19, 97, 15, 7, 39, 19, 23, 61, 41, 4,
25, 17, 55]
Generation 5 Errors: [25, 49, 13, 15, 37, 12, 123, 56, 39, 12, 25, 48, 57, 49, 4, 58,
16, 21, 7, 7]
Generation 6 Errors: [73, 28, 15, 24, 121, 57, 52, 9, 44, 12, 84, 35, 41, 25, 13, 61,
15, 25, 130, 82]
Generation 7 Errors: [32, 28, 85, 4, 69, 5, 31, 79, 63, 41, 29, 31, 14, 26, 22, 67, 1
5, 35, 33, 16]
Generation 8 Errors: [76, 18, 58, 15, 138, 20, 0, 15, 159, 16, 44, 80, 16, 53, 30, 3
4, 1, 34, 64, 67]
Generation 9 Errors: [0, 159, 141, 11, 71, 15, 22, 92, 33, 32, 15, 7, 1, 95, 71, 45,
63, 80, 95, 11]
Generation 10 Errors: [10, 43, 163, 6, 21, 29, 39, 67, 46, 0, 11, 61, 56, 15, 10, 35,
1, 64, 0, 72]
Generation 11 Errors: [70, 24, 68, 39, 84, 3, 11, 44, 3, 3, 32, 20, 14, 28, 74, 34, 1
4, 91, 86, 19]
Generation 12 Errors: [47, 125, 133, 87, 3, 29, 16, 80, 83, 17, 9, 65, 80, 77, 26, 1,
67, 77, 27, 117]
Generation 13 Errors: [41, 23, 11, 6, 7, 145, 3, 51, 59, 7, 75, 47, 95, 1, 64, 20, 5
1, 4, 23, 40]
Generation 14 Errors: [24, 51, 27, 1, 20, 76, 13, 0, 34, 76, 65, 59, 6, 55, 30, 22,
1, 36, 38, 44]
Generation 15 Errors: [128, 15, 13, 9, 25, 12, 1, 0, 78, 21, 140, 63, 11, 65, 64, 43,
26, 12, 31, 40]
Generation 16 Errors: [79, 80, 7, 5, 60, 7, 81, 36, 45, 71, 35, 21, 39, 95, 85, 31, 1
05, 33, 20, 53]
Generation 17 Errors: [12, 76, 105, 45, 16, 25, 44, 52, 27, 89, 49, 27, 45, 3, 105, 2
0, 11, 5, 20, 21]
Generation 18 Errors: [55, 72, 4, 11, 32, 26, 3, 29, 25, 22, 84, 3, 3, 59, 44, 35, 3
7, 5, 84, 27]
Generation 19 Errors: [68, 63, 5, 83, 63, 61, 2, 147, 83, 4, 1, 27, 63, 34, 56, 2, 5
1, 13, 64, 19]
Generation 20 Errors: [6, 24, 2, 63, 2, 87, 30, 58, 42, 2, 45, 39, 122, 12, 150, 32,
42, 79, 166, 22]
Generation 21 Errors: [62, 5, 58, 83, 23, 26, 34, 10, 63, 50, 6, 50, 34, 2, 82, 98, 7
2, 26, 38, 34]
Generation 22 Errors: [23, 10, 18, 10, 56, 86, 7, 22, 68, 74, 56, 26, 64, 11, 3, 42,
5, 14, 23, 90]
Generation 23 Errors: [43, 7, 6, 39, 6, 17, 15, 10, 18, 25, 44, 48, 32, 42, 18, 67,
3, 65, 32, 2]
Generation 24 Errors: [7, 79, 20, 14, 15, 22, 2, 14, 9, 42, 32, 15, 64, 7, 158, 38, 2
2, 3, 2, 78]
Generation 25 Errors: [9, 58, 12, 32, 76, 25, 9, 11, 7, 31, 15, 54, 27, 36, 7, 18, 4
6, 67, 33, 70]
Generation 26 Errors: [2, 45, 14, 9, 1, 5, 89, 10, 79, 37, 95, 17, 61, 83, 57, 23, 9
5, 23, 7, 19]
Generation 27 Errors: [97, 55, 87, 73, 31, 7, 37, 49, 2, 74, 25, 34, 41, 1, 19, 43, 3
1, 17, 34, 30]
Generation 28 Errors: [13, 95, 41, 37, 11, 44, 17, 19, 15, 19, 6, 171, 38, 42, 7, 35,
3, 1, 50, 83]
Generation 29 Errors: [1, 6, 32, 2, 26, 10, 5, 59, 1, 8, 71, 9, 78, 5, 101, 11, 29, 1
5, 40, 37]
Generation 30 Errors: [80, 11, 123, 1, 36, 10, 55, 7, 94, 53, 107, 55, 37, 0, 2, 127,
31, 37, 5, 1]

localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 3/6
6/4/24, 3:45 PM Untitled8
Generation 31 Errors: [32, 7, 14, 72, 0, 4, 56, 40, 15, 80, 40, 97, 2, 1, 2, 50, 25,
32, 8, 24]
Generation 32 Errors: [10, 6, 80, 10, 87, 26, 40, 2, 40, 2, 9, 10, 4, 26, 119, 12, 4,
38, 45, 26]
Generation 33 Errors: [68, 1, 2, 2, 50, 21, 10, 76, 86, 72, 22, 64, 58, 3, 1, 60, 0,
106, 100, 6]
Generation 34 Errors: [97, 5, 95, 21, 80, 71, 14, 39, 16, 43, 0, 5, 32, 28, 42, 99, 3
1, 1, 24, 65]
Generation 35 Errors: [47, 49, 31, 25, 15, 37, 10, 16, 83, 37, 77, 17, 85, 0, 35, 14
7, 10, 11, 5, 16]
Generation 36 Errors: [36, 80, 45, 70, 6, 34, 94, 95, 2, 74, 5, 54, 40, 15, 28, 75, 1
0, 31, 21, 28]
Generation 37 Errors: [86, 90, 45, 7, 1, 10, 10, 90, 13, 2, 15, 30, 42, 14, 22, 2, 3
8, 13, 56, 7]
Generation 38 Errors: [6, 111, 8, 67, 71, 54, 18, 10, 7, 86, 2, 38, 55, 38, 1, 41, 3
8, 30, 75, 10]
Generation 39 Errors: [1, 58, 75, 31, 1, 44, 58, 70, 2, 2, 1, 1, 32, 15, 30, 65, 2, 7
5, 22, 43]
Generation 40 Errors: [81, 18, 39, 3, 37, 73, 25, 5, 153, 17, 89, 39, 25, 1, 79, 11,
25, 79, 1, 1]
Generation 41 Errors: [63, 79, 11, 95, 155, 5, 5, 41, 11, 99, 1, 71, 63, 5, 19, 33, 5
5, 50, 49, 17]
Generation 42 Errors: [5, 5, 25, 29, 49, 41, 3, 138, 1, 148, 15, 15, 43, 3, 15, 79,
1, 11, 72, 5]
Generation 43 Errors: [31, 30, 1, 7, 10, 27, 65, 32, 99, 78, 63, 3, 75, 45, 66, 62,
3, 7, 71, 31]
Generation 44 Errors: [12, 9, 70, 1, 1, 2, 7, 25, 7, 71, 7, 67, 83, 83, 1, 19, 35, 7,
16, 7]
Generation 45 Errors: [63, 79, 1, 11, 155, 80, 21, 14, 1, 13, 2, 50, 71, 95, 74, 1,
9, 3, 18, 144]
Generation 46 Errors: [11, 5, 50, 65, 11, 21, 67, 110, 19, 66, 1, 43, 26, 54, 39, 28,
5, 21, 74, 1]
Generation 47 Errors: [107, 11, 103, 51, 15, 17, 3, 43, 21, 6, 11, 65, 31, 5, 87, 61,
79, 16, 14, 21]
Generation 48 Errors: [30, 66, 2, 83, 17, 9, 11, 32, 37, 17, 41, 2, 9, 90, 31, 7, 65,
171, 9, 126]
Generation 49 Errors: [9, 26, 44, 11, 63, 84, 50, 5, 26, 18, 65, 31, 30, 102, 53, 90,
42, 13, 23, 9]
Generation 50 Errors: [18, 55, 7, 23, 59, 13, 31, 15, 85, 5, 51, 23, 53, 53, 5, 62, 1
5, 16, 31, 7]
Generation 51 Errors: [35, 84, 17, 33, 47, 71, 58, 96, 74, 7, 15, 2, 121, 32, 65, 53,
137, 68, 3, 110]
Generation 52 Errors: [30, 12, 20, 90, 77, 58, 3, 27, 6, 31, 30, 25, 74, 57, 129, 73,
49, 65, 17, 54]
Generation 53 Errors: [42, 28, 46, 18, 8, 1, 86, 38, 65, 61, 77, 31, 6, 2, 58, 105, 1
3, 37, 23, 25]
Generation 54 Errors: [26, 48, 72, 32, 25, 10, 66, 10, 24, 94, 8, 8, 17, 2, 116, 13,
70, 54, 67, 62]
Generation 55 Errors: [61, 20, 54, 84, 5, 30, 50, 37, 75, 22, 130, 1, 42, 90, 52, 52,
8, 40, 72, 40]
Generation 56 Errors: [89, 39, 49, 0, 16, 67, 121, 84, 1, 4, 100, 47, 12, 28, 13, 74,
49, 19, 96, 23]
Generation 57 Errors: [84, 4, 31, 50, 12, 67, 51, 96, 39, 17, 91, 21, 44, 16, 28, 52,
62, 20, 0, 159]
Generation 58 Errors: [46, 2, 20, 8, 12, 40, 76, 22, 20, 3, 124, 48, 16, 12, 20, 80,
96, 8, 136, 20]
Generation 59 Errors: [30, 105, 20, 29, 22, 47, 26, 74, 56, 3, 40, 4, 69, 3, 34, 8, 5
1, 1, 12, 77]
Generation 60 Errors: [20, 79, 44, 85, 11, 8, 5, 49, 24, 51, 39, 7, 30, 37, 6, 25, 9
7, 7, 20, 83]

localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 4/6
6/4/24, 3:45 PM Untitled8
Generation 61 Errors: [164, 43, 90, 33, 71, 44, 58, 38, 45, 73, 22, 26, 74, 21, 3, 5
1, 3, 57, 135, 5]
Generation 62 Errors: [3, 1, 3, 61, 5, 7, 127, 3, 87, 34, 5, 20, 20, 35, 38, 60, 20,
33, 1, 9]
Generation 63 Errors: [9, 63, 87, 78, 49, 7, 98, 65, 14, 98, 50, 83, 119, 11, 11, 7,
25, 65, 66, 47]
Generation 64 Errors: [37, 13, 7, 19, 7, 59, 7, 31, 104, 11, 95, 19, 21, 79, 34, 59,
66, 27, 11, 101]
Generation 65 Errors: [15, 17, 55, 23, 13, 109, 118, 75, 47, 19, 49, 23, 39, 25, 19,
93, 65, 25, 35, 7]
Generation 66 Errors: [13, 16, 29, 37, 15, 65, 13, 24, 24, 97, 37, 0, 93, 17, 91, 3,
9, 45, 60, 29]
Generation 67 Errors: [67, 37, 24, 4, 23, 13, 64, 20, 69, 11, 47, 15, 69, 7, 9, 20, 2
3, 95, 9, 72]
Generation 68 Errors: [23, 7, 31, 49, 121, 19, 13, 87, 23, 1, 50, 50, 11, 19, 78, 37,
51, 57, 23, 23]
Generation 69 Errors: [33, 71, 1, 41, 68, 25, 121, 53, 7, 115, 1, 101, 11, 13, 31, 2
1, 63, 27, 32, 49]
Generation 70 Errors: [19, 1, 13, 69, 7, 11, 8, 24, 5, 27, 71, 7, 1, 5, 23, 69, 11, 8
1, 9, 21]
Generation 71 Errors: [1, 26, 1, 31, 75, 61, 84, 7, 77, 11, 95, 31, 23, 11, 17, 123,
41, 11, 15, 79]
Generation 72 Errors: [11, 75, 23, 65, 103, 25, 11, 67, 45, 39, 7, 75, 59, 68, 95, 6
5, 87, 5, 12, 29]
Generation 73 Errors: [19, 33, 93, 25, 11, 75, 26, 5, 87, 11, 5, 5, 67, 119, 2, 22, 6
8, 61, 1, 7]
Generation 74 Errors: [78, 3, 86, 14, 61, 57, 11, 12, 30, 27, 61, 21, 80, 5, 30, 58,
37, 42, 33, 1]
Generation 75 Errors: [63, 10, 27, 85, 19, 17, 23, 63, 86, 27, 11, 1, 23, 61, 42, 83,
47, 25, 21, 85]
Generation 76 Errors: [73, 58, 27, 12, 17, 17, 58, 42, 163, 66, 53, 98, 123, 95, 85,
5, 104, 33, 25, 39]
Generation 77 Errors: [52, 13, 12, 45, 59, 73, 76, 76, 67, 6, 20, 58, 57, 91, 63, 37,
59, 52, 5, 95]
Generation 78 Errors: [6, 1, 87, 78, 35, 5, 32, 70, 7, 10, 21, 10, 11, 90, 47, 63, 1
4, 2, 62, 38]
Generation 79 Errors: [22, 118, 79, 27, 30, 90, 3, 42, 2, 14, 65, 96, 79, 10, 6, 10,
39, 42, 15, 10]
Generation 80 Errors: [14, 7, 66, 1, 3, 100, 11, 6, 2, 6, 61, 46, 66, 48, 26, 23, 6,
22, 34, 8]
Generation 81 Errors: [57, 32, 29, 56, 17, 111, 67, 3, 20, 68, 15, 29, 48, 22, 59, 1
6, 3, 19, 29, 11]
Generation 82 Errors: [15, 47, 12, 15, 20, 119, 27, 1, 5, 24, 4, 59, 25, 31, 25, 50,
73, 7, 11, 11]
Generation 83 Errors: [97, 35, 67, 55, 9, 26, 5, 5, 5, 72, 26, 15, 39, 7, 25, 7, 20,
7, 11, 19]
Generation 84 Errors: [31, 31, 22, 5, 43, 9, 21, 62, 49, 13, 75, 59, 15, 5, 43, 99, 7
9, 65, 54, 19]
Generation 85 Errors: [35, 33, 47, 50, 6, 23, 20, 45, 77, 7, 24, 33, 15, 107, 27, 39,
81, 59, 75, 56]
Generation 86 Errors: [45, 26, 2, 27, 59, 2, 7, 29, 87, 33, 125, 49, 52, 12, 10, 16,
10, 20, 63, 80]
Generation 87 Errors: [144, 27, 30, 66, 15, 26, 62, 87, 70, 50, 30, 58, 46, 12, 59, 8
3, 7, 42, 170, 142]
Generation 88 Errors: [19, 25, 19, 69, 12, 3, 16, 20, 83, 11, 115, 4, 18, 67, 25, 20,
29, 54, 41, 0]
Generation 89 Errors: [51, 11, 144, 51, 29, 61, 16, 36, 63, 41, 9, 12, 4, 44, 3, 2, 7
6, 81, 6, 8]
Generation 90 Errors: [12, 2, 12, 15, 70, 32, 58, 56, 18, 43, 2, 22, 36, 16, 44, 14,
50, 46, 14, 67]

localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 5/6
6/4/24, 3:45 PM Untitled8
Generation 91 Errors: [62, 2, 1, 6, 52, 82, 80, 72, 59, 44, 59, 46, 14, 7, 6, 48, 18,
96, 34, 16]
Generation 92 Errors: [63, 0, 22, 47, 83, 26, 50, 38, 26, 12, 6, 2, 153, 34, 7, 4, 1,
4, 43, 106]
Generation 93 Errors: [62, 34, 18, 1, 70, 9, 17, 48, 4, 39, 32, 62, 110, 4, 44, 57, 1
2, 34, 2, 131]
Generation 94 Errors: [12, 33, 11, 89, 33, 11, 29, 42, 50, 33, 12, 49, 1, 73, 4, 4, 2
5, 71, 2, 87]
Generation 95 Errors: [147, 60, 33, 53, 104, 2, 2, 14, 25, 4, 21, 62, 84, 4, 112, 94,
5, 26, 27, 68]
Generation 96 Errors: [74, 60, 12, 74, 40, 52, 32, 13, 66, 2, 6, 0, 8, 36, 91, 68, 9
2, 46, 2, 4]
Generation 97 Errors: [12, 14, 32, 25, 52, 49, 62, 40, 22, 42, 52, 46, 9, 6, 17, 30,
56, 32, 80, 0]
Generation 98 Errors: [87, 88, 0, 71, 17, 10, 38, 91, 18, 44, 30, 116, 36, 130, 26, 1
2, 174, 14, 8, 97]
Generation 99 Errors: [12, 94, 55, 107, 20, 125, 136, 64, 10, 16, 5, 4, 45, 10, 48, 2
0, 122, 18, 87, 116]
Generation 100 Errors: [5, 60, 14, 18, 50, 51, 32, 22, 8, 14, 58, 39, 46, 76, 25, 81,
31, 27, 120, 63]
Final Population Errors: [48, 56, 5, 19, 16, 27, 24, 12, 21, 12, 23, 40, 85, 9, 22,
71, 4, 58, 4, 60]
Chromosome: 0110010110, a: 12, b: 22, Error: 48
Chromosome: 0010000110, a: 4, b: 6, Error: 56
Chromosome: 0010110100, a: 5, b: 20, Error: 5
Chromosome: 0010101110, a: 5, b: 14, Error: 19
Chromosome: 0110000110, a: 12, b: 6, Error: 16
Chromosome: 0110100010, a: 13, b: 2, Error: 27
Chromosome: 0000010011, a: 0, b: 19, Error: 24
Chromosome: 0100010010, a: 8, b: 18, Error: 12
Chromosome: 0011101011, a: 7, b: 11, Error: 21
Chromosome: 0110000111, a: 12, b: 7, Error: 12
Chromosome: 0000110010, a: 1, b: 18, Error: 23
Chromosome: 0010011110, a: 4, b: 30, Error: 40
Chromosome: 1110101010, a: 29, b: 10, Error: 85
Chromosome: 0000111010, a: 1, b: 26, Error: 9
Chromosome: 0011010111, a: 6, b: 23, Error: 22
Chromosome: 1001110011, a: 19, b: 19, Error: 71
Chromosome: 0010010101, a: 4, b: 21, Error: 4
Chromosome: 1001010001, a: 18, b: 17, Error: 58
Chromosome: 0010010101, a: 4, b: 21, Error: 4
Chromosome: 0000001010, a: 0, b: 10, Error: 60

localhost:8889/nbconvert/html/Downloads/AILAB/Untitled8.ipynb?download=false 6/6

You might also like