Question: Based on the following code; import random # Read input file function def read _ input _ file ( filename ) : with open (

Based on the following code;
import random
# Read input file function
def read_input_file(filename):
with open(filename,'r') as file:
data =[]
for line in file:
data.append(list(map(int, line.strip().split(','))))
return data
# Fitness function to evaluate the total score of a task assignment
def fitness(chromosome, data):
total_score =0
for task, person in enumerate(chromosome):
total_score += data[person][task]
return total_score
# Selection function using tournament selection
def selection(population, fitnesses):
selected = random.choices(population, weights=fitnesses, k=2)
return selected
# Crossover function (Order Crossover)
def crossover(parent1, parent2):
size = len(parent1)
start, end = sorted(random.sample(range(size),2))
child =[None]* size
# Inherit the middle section from parent1
child[start:end]= parent1[start:end]
# Fill remaining tasks in order from parent2
fill_positions =[i for i in range(size) if child[i] is None]
fill_values =[task for task in parent2 if task not in child]
for i, value in zip(fill_positions, fill_values):
child[i]= value
return child
# Mutation function: Swap two random tasks
def mutate(chromosome, mutation_rate=0.05):
if random.random()< mutation_rate:
i, j = random.sample(range(len(chromosome)),2)
chromosome[i], chromosome[j]= chromosome[j], chromosome[i]
# Genetic Algorithm function
def genetic_algorithm(data, population_size=100, generations=1000, mutation_rate=0.05):
num_tasks = len(data[0])
num_people = len(data)
# Generate initial population (random permutations of task assignments)
population =[random.sample(range(num_people), num_tasks) for _ in range(population_size)]
best_solution = None
best_fitness =-float('inf')
# Evolve over generations
for gen in range(generations):
# Calculate fitness for all chromosomes
fitnesses =[fitness(chromo, data) for chromo in population]
# Track the best solution
max_fitness = max(fitnesses)
if max_fitness > best_fitness:
best_fitness = max_fitness
best_solution = population[fitnesses.index(max_fitness)]
# Selection and Crossover
new_population =[]
for _ in range(population_size //2):
parent1, parent2= selection(population, fitnesses)
child1= crossover(parent1, parent2)
child2= crossover(parent2, parent1)
new_population.extend([child1, child2])
# Mutation
for chromo in new_population:
mutate(chromo, mutation_rate)
population = new_population
# Print progress every 100 generations
if gen %50==0:
print(f"Generation {gen}: Best Fitness ={best_fitness}")
return best_solution, best_fitness
# Main execution
if __name__=="__main__":
# Load data from file (assumed to be called 'task_assignment_input.txt')
data = read_input_file('GAtxt.txt')
# Run genetic algorithm
best_solution, best_fitness = genetic_algorithm(data)
# Output the best solution and its fitness score
print("Best Task Assignment:", best_solution)
print("Best Fitness (Total Score):", best_fitness)
;
Do the following;
Write a report detailing the following:
(a) Describe a chromosome in the initial population and the population size used.
(b) Define the fitness function you used.
(c) Describe the selection method used.
(d) Describe the mutation operator and the mutation rate used.
(e) Describe the crossover operator and the crossover rate used.
(f) Describe the termination criterion.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!