REPORT
REPORT
Neighbor Heuristic
1. Introduction
The Two Salesman Problem (2-TSP) is a variant of the classical Traveling Salesman Problem
(TSP), where the goal is to determine two separate tours for two salesmen such that the total
distance traveled is minimized. This report details the implementation of a heuristic solution to
the 2-TSP using the Nearest Neighbor algorithm for tour generation and a simple tour splitting
strategy.
2. Methodology
The solution to the 2-TSP involves the following key steps:
1. Reading Input: Load city coordinates from a file.
2. Distance Calculation: Compute a distance matrix representing the Euclidean distances
between each pair of cities.
3. Tour Generation: Use the Nearest Neighbor algorithm to generate an initial tour
starting from a randomly chosen city.
4. Tour Splitting: Divide the generated tour into two approximately equal parts for the two
salesmen.
5. Tour Length Calculation: Compute the total length of each tour.
6. Writing Output: Save the results to an output file in a specified format.
3. Implementation Details
3.1 Reading Input
The read_input function reads city coordinates from an input file and stores them in a list of
tuples, where each tuple contains a city ID and its (x, y) coordinates.
def read_input(file_name):
cities = []
with open(file_name, 'r') as f:
for line in f:
city_id, x, y = map(int, line.strip().split())
cities.append((city_id, x, y))
return cities
def create_distance_matrix(cities):
n = len(cities)
dist_matrix = [[0]*n for _ in range(n)]
for i in range(n):
for j in range(i+1, n):
dist = calculate_distance(cities[i], cities[j])
dist_matrix[i][j] = dist
dist_matrix[j][i] = dist
return dist_matrix
def split_tours(tour):
half = len(tour) // 2
return tour[:half], tour[half:]
4. Main Function
The main function orchestrates the overall process by invoking the aforementioned functions in
sequence.
# Split the tour into two parts for the two salesmen
tour1, tour2 = split_tours(tour)
5. Conclusion
This report presents a heuristic solution to the Two Salesman Problem (2-TSP) using the
Nearest Neighbor algorithm for initial tour generation and a simple splitting strategy for
dividing the tour between two salesmen. The implementation efficiently reads city coordinates,
computes a distance matrix, generates tours, splits them, calculates tour lengths, and writes the
results to an output file. This approach provides a practical method for solving the 2-TSP,
suitable for moderate-sized datasets.