forked from HIT1190202126/EC_work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read_data.py
93 lines (85 loc) · 3.21 KB
/
Read_data.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
import re
class dataLoader:
"""
Loading data , need to specify the type of the data(opt.tour / tsp)
"""
def __init__(self, path, type):
"""
The constructor func
:param path:the location of the opening file
:param type:the type of the file (1 - tsp , 0 -opt)
"""
self.file_path = path
self.dataType = type
def readOpt(self, String0):
"""
read data from opt.tour String
:param String: opt.tour String
:return: Name, COMMENT, TYPE, DIMENSION ,TOUR_SECTION(in List)
"""
Name = re.match(r"NAME : (.*)", String0)[1]
COMMENT = re.search(r"COMMENT : (.*)", String0)[1]
TYPE = re.search(r"TYPE : (.*)", String0)[1]
DIMENSION = re.search(r"DIMENSION : (.*)", String0)[1]
split = String0.split("\n")
Tour = []
for s0 in split:
if (s0 and s0[0] <= '9' and s0[0] >= '0'):
Tour.append(int(s0))
return Name, COMMENT, TYPE, DIMENSION, Tour
def readTsp(self, String0):
"""
read data from opt.tour String
:param String: opt.tour String
:return: Name, COMMENT ,TYPE DIMENSION, EDGE_WEIGHT_TYPE ,NODE_COORD_SECTION(in List[list[int]])
"""
Name = re.match(r"NAME : (.*)", String0)[1]
COMMENT = re.search(r"COMMENT : (.*)", String0)[1]
TYPE = re.search(r"TYPE : (.*)", String0)[1]
DIMENSION = re.search(r"DIMENSION : (.*)", String0)[1]
EDGE_WEIGHT_TYPE = re.search(r"EDGE_WEIGHT_TYPE : (.*)", String0)[1]
NODE_COORD_SECTION = []
split = String0.split("\n")
for s0 in split:
if (s0 and s0[0] <= '9' and s0[0] >= '0'):
one = s0.split(" ")
One = []
One.append(float(one[0]))
One.append(float(one[1]))
One.append(float(one[2]))
if (One != []):
NODE_COORD_SECTION.append(One)
return Name, COMMENT, TYPE, DIMENSION, EDGE_WEIGHT_TYPE, NODE_COORD_SECTION
def Loading(self):
"""
Reading data
:return:
"""
string0 = ""
with open(self.file_path, "r") as f:
string0 = f.read()
if (self.dataType == 0):
# opt
Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION = self.readOpt(string0)
return Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION
elif (self.dataType == 1):
# Tsp
Name, COMMENT, TYPE, DIMENSION, EDGE_WEIGHT_TYPE, NODE_COORD_SECTION = self.readTsp(string0)
return Name, COMMENT, TYPE, DIMENSION, EDGE_WEIGHT_TYPE, NODE_COORD_SECTION
# Test:
if __name__ == "__main__":
location = "dataSet/tsp/eil51.tsp"
Name, COMMENT, TYPE, DIMENSION, EDGE_WEIGHT_TYPE, NODE_COORD_SECTION = dataLoader(location, 1).Loading()
print(Name)
print(COMMENT)
print(TYPE)
print(DIMENSION)
print(EDGE_WEIGHT_TYPE)
print(NODE_COORD_SECTION)
location = "dataSet/opt_tour/eil51.opt.tour"
Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION = dataLoader(location, 0).Loading()
print(Name)
print(COMMENT)
print(TYPE)
print(DIMENSION)
print(TOUR_SECTION)