forked from HIT1190202126/EC_work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSPProblem.py
98 lines (87 loc) · 3.18 KB
/
TSPProblem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re
import Read_data
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
class TSPProblem:
"""
The class which represents the TSP problem.
Enable the construction of TSP problems from the files of the symmetric traveling salesperson problem of the TSPlib
"""
def __init__(self, pathTsp, pathOpt):
"""
The Constructor of the TSPproblem
:param pathTsp: the path which points to the .tsp file
:param pathOpt: the path which points to the .Opt file
"""
'''
Name : file name
Comment : The Comment of the file
Dimension : The num of cities
EdgeWeightType : Type of the edge
Edges : List of Edges [[num-order src dst],.....]
'''
Name, COMMENT, TYPE, DIMENSION, EDGE_WEIGHT_TYPE, NODE_COORD_SECTION = Read_data.dataLoader(pathTsp,
1).Loading()
self.Name = Name
self.Comment = COMMENT
self.DIMENSION = int(DIMENSION)
self.EdgeWeightType = EDGE_WEIGHT_TYPE
self.POS = NODE_COORD_SECTION
Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION = Read_data.dataLoader(pathOpt, 0).Loading()
if (int(DIMENSION) != self.DIMENSION):
print("Dim Error!!")
exit()
self.OptSelction = TOUR_SECTION
#print("Initialization Complete")
def print_info(self):
"""
print the Info of the Tsp-problem to the CMD window
"""
print("-------- TSP problem Info --------")
print("Name:\t", self.Name)
print("Comment:\t", self.Comment)
print("EdgesWeghtType:\t", self.EdgeWeightType)
print("--------Cities--------")
for t in self.POS:
print("City: ", t[0], "\t<", t[1], "\t", t[2], ">")
print("-------- OptPath --------\n", self.OptSelction)
def plot(self, route):
"""
Connect the points from route,and draw it out
:param route: the point seq ,List<int>
:return:
"""
fig, ax0 = plt.subplots()
# label out the points
for e0 in self.POS:
ax0.scatter(e0[1], e0[2], c='b', s=20, alpha=0.5)
# connet the cities
StartingPoint = int(route[0])
i = 0
for k in route:
if i == 0:
i = 1
dst = self.POS[k - 1]
else:
src = dst
dst = self.POS[k - 1]
ax0.add_line(Line2D([src[1], dst[1]], [src[2], dst[2]], linewidth=1, color='r'))
plt.plot()
plt.show()
def getSelfWay(self):
"""
get our Way by calling our methods
:return: List of Cities
"""
pass
def plotOpt(self):
self.plot(self.OptSelction)
def get_opt_tour(self,path0):
Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION = Read_data.dataLoader(path0, 0).Loading()
self.OptSelction = TOUR_SECTION
print("get OPT !")
if __name__ == "__main__":
location1 = "dataSet/tsp/pcb442.tsp"
location2 = "dataSet/opt_tour/pcb442.opt.tour"
TSP0 = TSPProblem(location1, location2)
TSP0.plotOpt()