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 readinputfilefilename:
with openfilenamer as file:
data
for line in file:
data.appendlistmapint line.stripsplit
return data
# Fitness function to evaluate the total score of a task assignment
def fitnesschromosome data:
totalscore
for task, person in enumeratechromosome:
totalscore datapersontask
return totalscore
# Selection function using tournament selection
def selectionpopulation fitnesses:
selected random.choicespopulation weightsfitnesses, k
return selected
# Crossover function Order Crossover
def crossoverparent parent:
size lenparent
start, end sortedrandomsamplerangesize
child None size
# Inherit the middle section from parent
childstart:end parentstart:end
# Fill remaining tasks in order from parent
fillpositions i for i in rangesize if childi is None
fillvalues task for task in parent if task not in child
for i value in zipfillpositions, fillvalues:
childi value
return child
# Mutation function: Swap two random tasks
def mutatechromosome mutationrate:
if random.random mutationrate:
i j random.samplerangelenchromosome
chromosomei chromosomej chromosomej chromosomei
# Genetic Algorithm function
def geneticalgorithmdata populationsize generations mutationrate:
numtasks lendata
numpeople lendata
# Generate initial population random permutations of task assignments
population randomsamplerangenumpeople numtasks for in rangepopulationsize
bestsolution None
bestfitness floatinf
# Evolve over generations
for gen in rangegenerations:
# Calculate fitness for all chromosomes
fitnesses fitnesschromo data for chromo in population
# Track the best solution
maxfitness maxfitnesses
if maxfitness bestfitness:
bestfitness maxfitness
bestsolution populationfitnessesindexmaxfitness
# Selection and Crossover
newpopulation
for in rangepopulationsize :
parent parent selectionpopulation fitnesses
child crossoverparent parent
child crossoverparent parent
newpopulation.extendchild child
# Mutation
for chromo in newpopulation:
mutatechromo mutationrate
population newpopulation
# Print progress every generations
if gen :
printfGeneration gen: Best Fitness bestfitness
return bestsolution, bestfitness
# Main execution
if namemain:
# Load data from file assumed to be called 'taskassignmentinput.txt
data readinputfileGAtxttxt
# Run genetic algorithm
bestsolution, bestfitness geneticalgorithmdata
# Output the best solution and its fitness score
printBest Task Assignment:", bestsolution
printBest Fitness Total Score: bestfitness
;
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
