You are on page 1of 9

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 50

Obtained Marks:

Evolutionary Computing
Assignment # 1

Submitted To: Dr. Zulfiqar Ali.


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Abdul Sami.


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg. Number: 21108101.


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Theoretical Part:
 Pseudocode for the solution of given problem by explaining each step by
using GA in your own words:

 Prominent parts/functions of the genetic algorithm:

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

 Parameters and assumptions used in the implementation:

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

 Names of functions, classes, or packages used in the implementation:

Implementation Part:
a) Applying midpoint crossover for the next generation of candidates:
Code:
import random

board_size = 8
num_queens = 8

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

def fitness(chromosome):

non_attacking_pairs = 0
for i in range(num_queens):
for j in range(i+1, num_queens):
if chromosome[i] != chromosome[j] and abs(chromosome[i] -
chromosome[j]) != j - i:
non_attacking_pairs += 1
return non_attacking_pairs

def selection(population):

fitness_sum = sum([fitness(chromosome) for chromosome in population])


selection_probs = [fitness(chromosome) / fitness_sum for chromosome in
population]
selected_indices = random.choices(range(len(population)),
weights=selection_probs, k=2)
return population[selected_indices[0]], population[selected_indices[1]]

def crossover(parent1, parent2):

midpoint = random.randint(0, num_queens-1)


child1 = parent1[:midpoint] + parent2[midpoint:]
child2 = parent2[:midpoint] + parent1[midpoint:]
return child1, child2

def mutation(chromosome):

gene_index = random.randint(0, num_queens-1)


new_gene = random.randint(0, board_size-1)
chromosome[gene_index] = new_gene
return chromosome

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

population_size = 100
population = [[random.randint(0, board_size-1) for _ in range(num_queens)] for
_ in range(population_size)]

max_generations = 1000
for generation in range(max_generations):

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


best_chromosome = population[fitness_scores.index(max(fitness_scores))]
print("Generation:", generation+1, "Best fitness:", max(fitness_scores), "Best
chromosome:", best_chromosome)

parent1, parent2 = selection(population)

child1, child2 = crossover(parent1, parent2)

child1 = mutation(child1)
child2 = mutation(child2)

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


worst_indices = sorted(range(len(fitness_scores)), key=lambda k:
fitness_scores[k])[:2]
population[worst_indices[0]] = child1
population[worst_indices[1]] = child2

b) Applying mutation for the next generation of candidates and providing the
diversity in the solution:

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Code:
import random

board_size = 8
num_queens = 8

def fitness(chromosome):

non_attacking_pairs = 0
for i in range(num_queens):
for j in range(i+1, num_queens):
if chromosome[i] != chromosome[j] and abs(chromosome[i] -
chromosome[j]) != j - i:
non_attacking_pairs += 1
return non_attacking_pairs

def selection(population):

fitness_sum = sum([fitness(chromosome) for chromosome in population])


selection_probs = [fitness(chromosome) / fitness_sum for chromosome in
population]
selected_indices = random.choices(range(len(population)),
weights=selection_probs, k=2)
return population[selected_indices[0]], population[selected_indices[1]]

def crossover(parent1, parent2):

midpoint = random.randint(0, num_queens-1)


child1 = parent1[:midpoint] + parent2[midpoint:]
child2 = parent2[:midpoint] + parent1[midpoint:]
return child1, child2

def mutation(chromosome):

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

gene_index = random.randint(0, num_queens-1)


new_gene = random.randint(0, board_size-1)
chromosome[gene_index] = new_gene
return chromosome

population_size = 100
population = [[random.randint(0, board_size-1) for _ in range(num_queens)] for
_ in range(population_size)]

max_generations = 1000
for generation in range(max_generations):

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


best_chromosome = population[fitness_scores.index(max(fitness_scores))]
print("Generation:", generation+1, "Best fitness:", max(fitness_scores), "Best
chromosome:", best_chromosome)

parent1, parent2 = selection(population)

child1, child2 = crossover(parent1, parent2)

child1 = mutation(child1)
child2 = mutation(child2)

population.append(child1)
population.append(child2)

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


worst_indices = sorted(range(len(fitness_scores)), key=lambda k:
fitness_scores[k])[:2]
population.pop(worst_indices[0])

EC BS(AI)- SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

population.pop(worst_indices[1])

Features of the solution:

 Maximum Fitness value: 27


 Number of Generations: 521
 Best chromosome for the final solution: [3,1,7,2,6,6,0,5] (each number
represents the row where a queen is placed in a column)

The End.

EC BS(AI)- SZABIST-ISB

You might also like