This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
AICup/code/aco/io_tsp.py

71 lines
2.6 KiB
Python
Raw Normal View History

2019-10-23 19:07:20 +00:00
import numpy as np
from matplotlib import pyplot as plt
2020-12-21 14:46:50 +00:00
from aco.utils import distance_euc
2019-10-23 19:07:20 +00:00
2020-09-28 07:30:21 +00:00
class ProblemInstance:
2019-10-23 19:07:20 +00:00
def __init__(self, name_tsp):
2020-09-28 09:56:36 +00:00
self.exist_opt = False
2020-09-25 11:20:28 +00:00
self.optimal_tour = None
2020-09-28 10:13:53 +00:00
self.dist_matrix = None
2020-10-19 15:25:30 +00:00
2019-10-23 19:07:20 +00:00
# read raw data
2020-10-19 15:25:30 +00:00
self.file_name = name_tsp
file_object = open(self.file_name)
2019-10-23 19:07:20 +00:00
data = file_object.read()
file_object.close()
self.lines = data.splitlines()
# store data set information
self.name = self.lines[0].split(' ')[2]
self.nPoints = np.int(self.lines[3].split(' ')[2])
2019-10-31 15:54:17 +00:00
self.best_sol = np.float(self.lines[5].split(' ')[2])
2019-10-23 19:07:20 +00:00
# read all data points and store them
self.points = np.zeros((self.nPoints, 3))
for i in range(self.nPoints):
line_i = self.lines[7 + i].split(' ')
2019-11-04 07:05:30 +00:00
self.points[i, 0] = int(line_i[0])
2019-10-23 19:07:20 +00:00
self.points[i, 1] = line_i[1]
self.points[i, 2] = line_i[2]
self.create_dist_matrix()
2020-12-21 14:46:50 +00:00
if self.file_name in ["./problems/eil76.tsp",
"./problems/kroA100.tsp",
"../problems/eil76.tsp",
2020-10-19 15:25:30 +00:00
"../problems/kroA100.tsp"]:
2019-11-04 05:47:15 +00:00
self.exist_opt = True
2020-10-19 15:25:30 +00:00
file_object = open(self.file_name.replace(".tsp", ".opt.tour"))
2019-11-04 05:19:11 +00:00
data = file_object.read()
file_object.close()
lines = data.splitlines()
# read all data points and store them
2019-11-04 05:59:47 +00:00
self.optimal_tour = np.zeros(self.nPoints, dtype=np.int)
2019-11-04 05:19:11 +00:00
for i in range(self.nPoints):
2019-11-04 05:31:15 +00:00
line_i = lines[5 + i].split(' ')
2019-11-04 05:56:13 +00:00
self.optimal_tour[i] = int(line_i[0]) - 1
2019-10-23 19:07:20 +00:00
def print_info(self):
2020-09-28 10:13:53 +00:00
print("\n\n#############################")
2019-10-23 19:07:20 +00:00
print('name: ' + self.name)
print('nPoints: ' + str(self.nPoints))
print('best_sol: ' + str(self.best_sol))
2020-09-28 09:56:36 +00:00
print('exist optimal: ' + str(self.exist_opt))
2019-10-23 19:07:20 +00:00
2020-10-19 15:25:30 +00:00
def plot_data(self, show_numbers=False):
2019-10-23 19:07:20 +00:00
plt.figure(figsize=(8, 8))
plt.title(self.name)
plt.scatter(self.points[:, 1], self.points[:, 2])
2020-10-12 09:43:27 +00:00
if show_numbers:
for i, txt in enumerate(np.arange(self.nPoints)): # tour_found[:-1]
plt.annotate(txt, (self.points[i, 1], self.points[i, 2]))
2019-10-23 19:07:20 +00:00
plt.show()
def create_dist_matrix(self):
self.dist_matrix = np.zeros((self.nPoints, self.nPoints))
for i in range(self.nPoints):
for j in range(i, self.nPoints):
2020-09-25 09:15:15 +00:00
self.dist_matrix[i, j] = distance_euc(self.points[i][1:3], self.points[j][1:3])
2019-10-23 19:11:32 +00:00
self.dist_matrix += self.dist_matrix.T