-
Notifications
You must be signed in to change notification settings - Fork 11
/
CS.py
219 lines (180 loc) · 5.89 KB
/
CS.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
import math
import numpy as np
import random
import timeit
from threading import Thread
import functools
dist_ar = [] # 거리표(global)
# limit_time = 36 # 제한시간(global)
cities_count = 0 # 도시 수(global)
dots_list = [] # 도시 리스트(global)
# Hyper Parameter
limits = (60) * 36/60 # 제한시간
nestCOUNT = 10 # 해집단 내 둥지 갯수
# 시간제한 데코레이터
def timeout(seconds_before_timeout):
def deco(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, seconds_before_timeout))]
def newFunc():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = Thread(target=newFunc)
t.daemon = True
try:
t.start()
t.join(seconds_before_timeout)
except Exception as e:
print('error starting thread')
raise e
ret = res[0]
if isinstance(ret, BaseException):
raise ret
return ret
return wrapper
return deco
# 거리표 제작(param : 문제 경로) : dist_df
def make_distArray(str):
global dist_ar
global limit_time
global cities_count
global dots_list
reader = open(str, mode='rt', encoding='utf-8')
dots_list = reader.read().split("\n") # ['x1 y1', 'x2 y2', 'x3 y3' ... 'xn yn']
cities_count = int(dots_list.pop(0))
limit_time = float(dots_list.pop())
x_list = [] # ['x1', 'x2', 'x3' ... 'xn']
y_list = [] # ['y1', 'y2', 'y3' ... 'yn']
for i in range(cities_count):
temp = dots_list[i].split(" ")
x_list.append(float(temp[0]))
y_list.append(float(temp[1]))
dist_ar = []
for n in range(cities_count):
temp = []
for m in range(cities_count):
temp.append(round((math.sqrt(((x_list[m] - x_list[n]) ** 2) + ((y_list[m] - y_list[n]) ** 2))), 2))
dist_ar.append(temp)
dist_ar = np.array(dist_ar)
print(dist_ar)
# 거리표를 이용한 적합도 매칭 함수
def cal_fit(stri):
fit = 0
for steps in range(len(stri) - 1):
fit += dist_ar[stri[steps], stri[steps + 1]]
return fit
def levyFlight(u) :
# u의 세제곱근분의 1
return math.pow(u,-1.0/3.0)
def randF() :
return random.uniform(0.0001,0.9999)
def levySwap(route, i, j) :
temp = route[i]
route[i] = route[j]
route[j] = temp
return route
def levyTwoOpt(nest, a, c) :
nest = nest[:]
new_nest = levySwap(nest, a, c)
return new_nest
def levyDoublebridge(nest, a, b, c, d) :
nest = nest[:]
new_nest = levySwap(nest, a, b)
new_nest = levySwap(new_nest, b, d) # ??
return new_nest
def CS() :
generation = 0 # 현재 세대
egg = [] # temp chromosome
egg_fit = [] # temp fitness
pa = int(0.2 * nestCOUNT) # a fraction of worse nests
# Initialize
for i in range(nestCOUNT):
egg.append(random.sample(range(0, cities_count), cities_count))
for i in range(nestCOUNT):
egg_fit.append(round(cal_fit(egg[i]), 5))
populations = np.array([egg, egg_fit])
populations = populations.T
print('초기화 최대 해 : \n', populations[0, 0], "\n", populations[0, 1])
while 1:
generation += 1
populations = populations[np.argsort(populations[:, 1])]
# Get a cuckoo randomly by levy flight
cuckooNest = populations[random.randint(0, nestCOUNT-1), 0]
if(levyFlight(randF())>2) :
cuckooNest = levyDoublebridge(cuckooNest, random.randint(0,cities_count-1), random.randint(0,cities_count-1)
, random.randint(0,cities_count-1), random.randint(0,cities_count-1))
else :
cuckooNest = levyTwoOpt(cuckooNest, random.randint(0,cities_count-1), random.randint(0,cities_count-1))
randomNestIndex = random.randint(0, nestCOUNT-1)
# Evaluate and replace
if(populations[randomNestIndex, 1] > cal_fit(cuckooNest)) :
populations[randomNestIndex, 0] = cuckooNest
populations[randomNestIndex, 1] = cal_fit(cuckooNest)
# Pa of worse nests are abandoned and new ones built
for i in range(nestCOUNT-pa, nestCOUNT) :
populations[i, 0] = random.sample(range(0, cities_count), cities_count)
populations[i, 1] = cal_fit(populations[i,0])
populations = populations[np.argsort(populations[:, 1])]
print(generation, '세대 최적 해 : \n', populations[0, 0], "\n", populations[0, 1])
@timeout(limits)
def start_CS(stri) :
make_distArray(stri)
CS()
try :
start = timeit.default_timer()
start_CS("2opt_dots/2opt_cycle100.in")
stop = timeit.default_timer()
print(stop - start)
except :
stop = timeit.default_timer()
print(stop - start)
'''
//질문
1. Pc는 뭐고 왜 줬을까?
2. 왜 버리지 않고 twoopt를 줬을까?? -> 준게 더 좋긴하넹?
3. doublebridge 알고리즘이 이해가 잘안가네???
//비교
numGA : numpy GA
numCS : numpy CS
시간제한 : 36s
타겟 : 2opt_cycle100.in
numGA : generation / fitness (chromosome수 : 10)
3287/1990
3272/2107
3290/2299
3310/2253
3298/1967
numGA : generation / fitness (chromosome수 : 50)
1137/1847
1143/1707
1133/2006
1141/1823
1141/2022
numCS : generation / fitness (nest 수 : 10)
55677/1655
57063/1589
53056/1678
52588/1757
53212/1869
numCS : generation / fitness (nest 수 : 20)
67396/1741
69157/1744
71023/1782
70852/1830
70993/1722
numCS : generation / fitness (nest 수 : 50)
32880/2361
32861/2373
32744/2308
32523/2281
32267/2417
enhanced numCS : generation / fitness (nest 수 : 10)
151389/1470
152319/1609
164071/1579
181645/1418
182080/1594
'''