Question: Modify the program below, to get the best path which are connected as per diagram. Currently program below gives me best path where one node

Modify the program below, to get the best path which are connected as per diagram. Currently program below gives me best path where one node to next as no straight connection. For example, one such Best Path my program giving is: ['pu','pe','t','v','g','n'] where t( tambaram) has no straight connection with v(velachery) Please correct my genetic algorithm given
Genetic Algorithm for the attached problem is as follow (It will run without error),
import random
# Node coordinates as provided
node_coordinates ={
'pu': (0,0),
'pe': (1,1),
't': (2,2),
'v': (3,3),
'g': (4,4),
'n': (5,5)
}
# Function to calculate Manhattan distance between two points
def manhattan_distance(point1, point2):
x1, y1= node_coordinates[point1]
x2, y2= node_coordinates[point2]
return abs(x1- x2)+ abs(y1- y2)
# Function to generate the initial population of paths
def generate_initial_population(graph, population_size):
initial_population =[]
for _ in range(population_size):
path = list(graph.keys())
random.shuffle(path)
initial_population.append(path)
return initial_population
# Function to calculate fitness of a path
def calculate_fitness(path):
total_distance = sum(manhattan_distance(path[i], path[i+1]) for i in range(len(path)-1))
return 1/ total_distance if total_distance >0 else float('inf')
# Genetic Algorithm components
def roulette_wheel_selection(population, fitnesses):
total_fitness = sum(fitnesses)
pick = random.uniform(0, total_fitness)
current =0
for individual, fitness in zip(population, fitnesses):
current += fitness
if current > pick:
return individual
def crossover(parent1, parent2, split='single'):
if split == 'single':
point = random.randint(1, len(parent1)-2)
child1= parent1[:point]+[gene for gene in parent2 if gene not in parent1[:point]]
child2= parent2[:point]+[gene for gene in parent1 if gene not in parent2[:point]]
else: # Two-point crossover
point1, point2= sorted(random.sample(range(1, len(parent1)-1),2))
child1= parent1[:point1]+[gene for gene in parent2[point1:point2] if gene not in parent1[:point1]]+ parent1[point2:]
child2= parent2[:point1]+[gene for gene in parent1[point1:point2] if gene not in parent2[:point1]]+ parent2[point2:]
return child1, child2
def mutate(path, mutation_rate=0.01):
for i in range(len(path)):
if random.random() mutation_rate:
swap_with = random.randint(0, len(path)-1)
path[i], path[swap_with]= path[swap_with], path[i]
return path
# Main Genetic Algorithm function
def genetic_algorithm(graph, population_size, generations, mutation_rate=0.01):
population = generate_initial_population(graph, population_size)
for _ in range(generations):
fitnesses =[calculate_fitness(individual) for individual in population]
new_population =[]
while len(new_population) population_size:
parent1= roulette_wheel_selection(population, fitnesses)
parent2= roulette_wheel_selection(population, fitnesses)
child1, child2= crossover(parent1, parent2)
new_population.extend([mutate(child1, mutation_rate), mutate(child2, mutation_rate)])
population = new_population[:population_size]
return max(population, key=calculate_fitness)
# Example graph as provided
graph ={
'pu': [('pe',8),('t',9)],
'pe': [('pu',8),('t',11),('v',6),('g',7)],
'v': [('g',14),('pe',6)],
'g': [('pe',7),('t',10),('n',12),('v',14)],
'n': [('g',12),('t',15)],
't': [('n',15),('g',10),('pe',11),('pu',9)]
}
# Running the Genetic Algorithm
best_path = genetic_algorithm(graph, population_size=10, generations=100)
print("Best Path:", best_path)
print("Fitness:", calculate_fitness(best_path))
 Modify the program below, to get the best path which are

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 Databases Questions!