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

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI

Artificial Intelligence and Decision Support

PROF. DR. ARSLAN


INSTRUCTOR SHAUKAT

STUDENT BILAL AHMAD

REG. NO. 345624

DEGREE 42

SYNDICATE A
DEPARTMENT Computer Engineering

SUBMITTED TO: DR. ARSLAN SHAUKAT


SUBMITTED BY: BILAL AHMAD
Part I:
Write a script (program) to solve the 5 antenna problem explained below using
Genetic Algorithm. Randomly generate a population of 20
individuals/chromosomes. Evaluate their fitness and then select, crossover and
mutate. Run it for many generations/iterations until you find the individual with
best/maximum fitness.
CODE:
import random

# ANSI color escape codes


RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
ENDC = "\033[0m"

def mutation(selected_chromosomes):
worst_chromosome_index =
selected_chromosomes.index(min(selected_chromosomes, key=lambda x: x[-1]))
mutated_chromosome = selected_chromosomes[worst_chromosome_index].copy()

for i in range(5):
if mutated_chromosome[i][1] - 1 >= 1:
mutated_chromosome[i] = (mutated_chromosome[i][0],
mutated_chromosome[i][1] - 1)

selected_chromosomes[worst_chromosome_index] = mutated_chromosome
return selected_chromosomes

def calculate_fitness(chromosomes):
fitness = []

for chromosome in chromosomes:


temp = set(chromosome)

for gene in chromosome:


temp.add((gene[0] - 1, gene[1]))
temp.add((gene[0], gene[1] - 1))
temp.add((gene[0] + 1, gene[1]))
temp.add((gene[0], gene[1] + 1))

fitness_value = min(len(temp), 23) # Limit the fitness value to a


maximum of 23
fitness.append(fitness_value)

return fitness
def select_chromosomes(fitness, chromosomes):
sorted_indices = sorted(range(len(fitness)), key=lambda k: fitness[k],
reverse=True)
selected_chromosomes = [chromosomes[sorted_indices[0]].copy(),
chromosomes[sorted_indices[1]].copy()]

for i in range(2, 20, 2):


parent1 = chromosomes[sorted_indices[i]].copy()
parent2 = chromosomes[sorted_indices[i + 1]].copy()

child1 = parent1[:3] + parent2[3:]


child2 = parent2[:3] + parent1[3:]

selected_chromosomes.extend([child1.copy(), child2.copy()])

return selected_chromosomes

# Driver code
chromosomes = [[(random.randint(1, 6), random.randint(1, 6)) for _ in
range(5)] for _ in range(20)]

for generation in range(1000):


fitness_values = calculate_fitness(chromosomes)
selected_chromosomes = select_chromosomes(fitness_values, chromosomes)
mutated_chromosomes = mutation(selected_chromosomes)
chromosomes = mutated_chromosomes

print("All chromosomes with their respective fitness value")

print()
for i in range(20):
color = GREEN if fitness_values[i] == max(fitness_values) else YELLOW
print(f"{RED}Chromosome {i + 1}:{ENDC} {color}{chromosomes[i]}{ENDC} |
{color}Fitness: {fitness_values[i]}{ENDC}")

best_chromosome_index = fitness_values.index(max(fitness_values))
print()
print("The best chromosome after 1000 iterations is:",
chromosomes[best_chromosome_index])
print("Fitness Value: ", max(fitness_values))
OUTPUT:

You might also like