-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest1.py
97 lines (79 loc) · 3 KB
/
test1.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
from Algorithms import EA1, EA2, EA3
from Individual import Individual
from Population import Population
from TSPProblem import TSPProblem
from Read_data import dataLoader
population_size = 10
tsp_path = "dataSet/tsp/eil51.tsp"
opt_path = "dataSet/opt_tour/eil51.opt.tour"
batch_size = 20000
with open("eil51.txt", 'w') as f:
with open('ei51_log.txt', 'w') as f2:
f.write("filepath:%s\n" % tsp_path)
f2.write("filepath:%s\n" % tsp_path)
for population_size in (10, 20):
f.write("\n")
f2.write("\n")
f.write("population_size:%d\n" % population_size)
f2.write("population_size:%d\n" % population_size)
f.write("ans:\n")
tsp = TSPProblem(tsp_path, opt_path)
ans = Individual(TSPProblem(tsp_path, opt_path))
Name, COMMENT, TYPE, DIMENSION, TOUR_SECTION = dataLoader(opt_path, 0).Loading()
ans.tour = TOUR_SECTION
ans.calDis()
f.write(str(ans.length))
f.write("\n")
print("ans:"+str(ans.length))
print("pop_size:%d" % population_size)
# test EA1
print("TestEA1")
f.write("test EA1:\n")
f2.write("test EA1:\n")
log_tour, log_len = EA1(population_size, tsp_path, opt_path, batch_size)
for tour in log_tour:
tour_new = [str(x) for x in tour]
s = ' '.join(tour_new)
f2.write(s)
f2.write("\n")
i = 1
for len in log_len:
f.write("batch_size:%d\n" % (i * 5000))
f.write(str(len))
f.write("\n\n")
i += 1
# test ea2
print("TestEA2")
f.write("test EA2:\n")
f2.write("test EA2:\n")
log_tour, log_len, pop = EA2(population_size, tsp_path, opt_path, batch_size)
for tour in log_tour:
tour_new = [str(x) for x in tour]
s = ' '.join(tour_new)
f2.write(s)
f2.write("\n")
i = 1
for len in log_len:
f.write("batch_size:%d\n" % (i * 5000))
f.write(str(len))
f.write("\n\n")
i += 1
# test ea3
print("TestEA3")
f.write("test EA3:\n")
f2.write("test EA3:\n")
log_tour, log_len, pop = EA3(population_size, tsp_path, opt_path, batch_size)
for tour in log_tour:
tour_new = [str(x) for x in tour]
s = ' '.join(tour_new)
f2.write(s)
f2.write("\n")
i = 1
for len in log_len:
f.write("batch_size:%d\n" % (i * 5000))
f.write(str(len))
f.write("\n\n")
i += 1
print(1)
f.write("***********************************************")
f2.write("***********************************************")