forked from HIT1190202126/EC_work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.py
223 lines (186 loc) · 6.83 KB
/
Algorithms.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from os import path
import random
import crossOver
import Selection
import mutation
from Population import Population
from Individual import Individual
from TSPProblem import TSPProblem
from Selection import Selection
from crossOver import crossOver
from mutation import Mutation
"""
pmx_crossover + swap_mutation + tournament_selection
population_size: number of individuals
tsp_path: the path which points to the .tsp file
opt_path: the path which points to the .Opt file
batch_size: number of generations
"""
def EA1(population_size, tsp_path, opt_path, batch_size):
mutation_possibility = 6
tsp = TSPProblem(tsp_path, opt_path)
pop = Population(tsp, population_size)
# initial
pop.ini_tourlist()
pop.set_length()
pop.set_adaptability()
log_tour = []
log_len = []
for i in range(batch_size):
if i % 100 == 0:
print("EA1:batch_size:%d" % i)
# optimize 1/3 parents
t = pop.len
parents_pop = Selection().tournament_selection(int(pop.len / 3), 3, tsp, t, pop)
# cross random from parents
child_list = []
while len(child_list) < population_size:
pa1 = random.randint(0, parents_pop.len - 1)
pa2 = random.randint(0, parents_pop.len - 1)
pa1 = parents_pop.tourlist[pa1]
pa2 = parents_pop.tourlist[pa2]
# cross
ch1, ch2 = crossOver(size=0, tsp=tsp).PMX_crossover(pa1, pa2)
child_list.append(ch1)
child_list.append(ch2)
for j in range(population_size):
rate = random.randint(1, 10)
# mutation
if rate <= mutation_possibility:
child_list[j] = Mutation(size=0, tspproblem=tsp).swapMutation(child_list[j])
# make the child population
pop = Population(tsp, population_size)
pop.tourlist = child_list
pop.set_length()
pop.set_adaptability()
if i % 100 == 0:
min_len = pop.tourlist[0].length
min_index = 0
for j in range(1, pop.len):
if pop.tourlist[j].length < min_len:
min_len = pop.tourlist[j].length
min_index = j
print("min_len:%f\n" % min_len)
log_tour += [pop.tourlist[min_index].tour]
if i % 5000 == 0:
log_len += [min_len]
return log_tour, log_len
"""
order crossover + scramble_mutation + elitism_selection
"""
def EA2(population_size, tsp_path, opt_path, batch_size):
mutation_possibility = 6
tsp = TSPProblem(tsp_path, opt_path)
pop = Population(tsp, population_size)
# initial
pop.ini_tourlist()
pop.set_length()
pop.set_adaptability()
log_tour = []
log_len = []
for i in range(batch_size):
if i % 100 == 0:
print("EA2:batch_size:%d" % i)
# optimize 1/3 parents
parents_list = Selection().elitism_selection(int(pop.len / 3), tsp, pop.len, pop)
# temp_list = pop.tourlist
# temp_list.sort(reverse = True)
# print(temp_list[0].calDis())
parents_pop = Population(tsp, pop.len)
parents_pop.tourlist = parents_list
child_list = []
# cross random from parents
if len(parents_list) % 2 == 0:
child_list = parents_list
else:
child_list = parents_list[0:len(parents_list) - 1]
while len(child_list) != population_size:
pa1 = random.randint(0, int(pop.len / 3) - 1)
pa2 = random.randint(0, int(pop.len / 3) - 1)
# print(parents_pop.tourlist)
pa1 = parents_pop.tourlist[pa1] # individual
pa2 = parents_pop.tourlist[pa2]
# cross
ch1, ch2 = crossOver(0, tsp).order_crossover(pa1, pa2)
child_list.append(ch1)
child_list.append(ch2)
for j in range(population_size):
rate = random.randint(1, 10)
# mutation
if rate <= mutation_possibility:
child_list[j] = Mutation(0, tsp).scrambleMutation(child_list[j])
child_list[j].calDis()
# make the child population
pop = Population(tsp, population_size)
pop.tourlist = child_list
pop.set_length()
pop.set_adaptability()
if i % 100 == 0:
min_len = pop.tourlist[0].length
min_index = 0
for k in range(1, pop.len):
if pop.tourlist[k].length < min_len:
min_len = pop.tourlist[k].length
min_index = k
print("min_len:%f\n" % min_len)
log_tour += [pop.tourlist[min_index].tour]
if i % 5000 == 0:
log_len += [min_len]
return log_tour, log_len, pop
"""
order crossover + inversion_mutation + elitism_selection
"""
def EA3(population_size, tsp_path, opt_path, batch_size):
mutation_possibility = 6
tsp = TSPProblem(tsp_path, opt_path)
pop = Population(tsp, population_size)
# initial
pop.ini_tourlist()
pop.set_length()
pop.set_adaptability()
log_tour = []
log_len = []
for i in range(batch_size):
if i % 100 == 0:
print("EA3:batch_size:%d" % i)
# optimize 1/3 parents
parents_list = Selection().elitism_selection(int(pop.len / 3), tsp, pop.len, pop)
parents_pop = Population(tsp, pop.len)
parents_pop.tourlist = parents_list
child_list = []
# cross random from parents
if len(parents_list) % 2 == 0:
child_list = parents_list
else:
child_list = parents_list[0:len(parents_list) - 1]
while len(child_list) != population_size:
pa1 = random.randint(0, int(pop.len / 3) - 1)
pa2 = random.randint(0, int(pop.len / 3) - 1)
pa1 = parents_pop.tourlist[pa1]
pa2 = parents_pop.tourlist[pa2]
# cross
ch1, ch2 = crossOver(0, tsp).order_crossover(pa1, pa2)
child_list.append(ch1)
child_list.append(ch2)
for j in range(population_size):
rate = random.randint(1, 10)
# mutation
if rate <= mutation_possibility:
child_list[j] = Mutation(0, tsp).invertionMutation(child_list[j])
# make the child population
pop = Population(tsp, population_size)
pop.tourlist = child_list
pop.set_length()
pop.set_adaptability()
if i % 100 == 0:
min_len = pop.tourlist[0].length
min_index = 0
for k in range(1, pop.len):
if pop.tourlist[k].length < min_len:
min_len = pop.tourlist[k].length
min_index = k
print("min_len:%f\n" % min_len)
log_tour += [pop.tourlist[min_index].tour]
if i % 5000 == 0:
log_len += [min_len]
return log_tour, log_len, pop