0% found this document useful (0 votes)
17 views6 pages

AI EXP 7 Anu

ewff

Uploaded by

dekitov522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

AI EXP 7 Anu

ewff

Uploaded by

dekitov522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Bharati Vidyapeeth (Deemed to be University)

Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

Experiment - 7
Name: Anoop Khude Roll No: 5

Subject: AI Class/Batch: TY/CSE-B 1

Date of Performance: Date of Submission:

AIM
Write a program to implement travelling salesman problem using hill climbing algorithm.

Theory/Procedure/Algorithm

The Traveling Salesman Problem (TSP) is a classic example where a salesman must
visit a set of cities exactly once and return to the starting point while minimizing the
total distance travelled.
The TSP stands as one of the best known problems when it comes to work
with NP-hard problems, which implies that no known algorithm exists to
solve it in polynomial time. The problem can be summarized as follows :
"Given a set of cities and the cost of travel (or distance) between each
possible pairs, the TSP, is to find the best possible way of visiting all the
cities and returning to the starting point that minimize the travel cost (or
travel distance)." The exact solution to this problem with n cities can only be
determined through evaluating (n-1)!/2 possibilities.

Traveling Salesman Problem (TSP)


The Traveling Salesman Problem (TSP) is a well-known combinatorial optimization
problem that has been extensively studied in operations research and computer
science. The objective is to determine the shortest possible route that visits each city
once and returns to the origin city. Due to its NP-hard nature, exact solutions are
computationally expensive for large datasets, making heuristic and metaheuristic
approaches like Hill Climbing and Simulated Annealing attractive alternatives.
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

Hill Climbing Algorithm

Hill Climbing is a local search algorithm that iteratively moves towards the direction of
increasing improvement, aiming to find a better solution in the search space. Starting
from a random initial solution, it generates neighboring solutions and selects the one
with the lowest distance. The process repeats until no better neighbors are found.
Steps for Implementing Hill Climbing
1. Initialize the current route and calculate its distance.
2. Iterate to find the best neighboring route until no improvement is found.
3. Return the best route and its distance .

Algorithm :
 Distance Calculation: The calculate_distance function computes the total distance for a given tour
using the distance matrix.
 Initial Solution Generation: The generate_initial_solution function creates a randomized initial
tour.
 Hill Climbing Process:

 Generate all neighboring tours by swapping pairs of cities.


 Evaluate each neighbor's distance.
 If a neighbor has a shorter distance, adopt that tour; otherwise, stop the process.

 Example: The last section demonstrates how to set up a distance matrix and call the hill climbing
function, printing the results.
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

Code
import random
import math

# Function to calculate the total distance of the tour


def calculate_distance(tour, distance_matrix):
total_distance = 0
for i in range(len(tour)):
total_distance += distance_matrix[tour[i]][tour[(i + 1) % len(tour)]]
return total_distance

# Function to generate a random initial solution (tour)


def generate_initial_solution(num_cities):
solution = list(range(num_cities))
random.shuffle(solution)
return solution

# Hill Climbing Algorithm


def hill_climbing(distance_matrix):
num_cities = len(distance_matrix)
current_solution = generate_initial_solution(num_cities)
current_distance = calculate_distance(current_solution, distance_matrix)

while True:
neighbors = []
for i in range(num_cities):
for j in range(i + 1, num_cities):
# Create a neighbor by swapping two cities
neighbor = current_solution[:]
neighbor[i], neighbor[j] = neighbor[j], neighbor[i]
neighbors.append(neighbor)

# Find the best neighbor


best_neighbor = None
best_distance = float('inf')
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

for neighbor in neighbors:


distance = calculate_distance(neighbor, distance_matrix)
if distance < best_distance:
best_distance = distance
best_neighbor = neighbor

# If the best neighbor is better than the current solution, move to the neighbor
if best_distance < current_distance:
current_solution = best_neighbor
current_distance = best_distance
else:
break # No better neighbors, exit the loop

return current_solution, current_distance

# Example usage
if __name__ == "__main__":
# Distance matrix example (symmetric)
distance_matrix = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]

best_tour, best_distance = hill_climbing(distance_matrix)


print("Best tour:", best_tour)
print("Best distance:", best_distance)
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

OUTPUT:

Conclusion

Hence, this program effectively demonstrates the implementation of TSP using hill climbing
algorithm.
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi Mumbai

Department of Computer Science and


Engineering

Assessment

Timely Submission Presentation Understanding Total


Sign
(7) (06) (12) (25)

You might also like