-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabu.py
108 lines (90 loc) · 4.21 KB
/
tabu.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
import random as rd
from copy import deepcopy
from cnn_helper import cnn_default_hyperparameters, cnn_get_random_neighbouring_solution, cnn_objective, file_name
import time
class TS:
def __init__(self, initial_solution, neighbour_gen_fun, objective_fun, tabu_length, seed=-1):
if (seed >= 0):
rd.seed(seed)
self.tabu_list = []
self.tabu_conditions = []
self.diversification = False
self.num_neighbours = 5
self.tabu_length = tabu_length
self.Initial_solution = initial_solution
self.neighbour_gen_fun = neighbour_gen_fun
self.objective_fun = objective_fun
self.Best_solution, self.Best_objvalue = self.TSearch()
def get_neighbours_and_evaluate(self, solution):
neighbours = []
diffs = []
for i in range(self.num_neighbours):
while True:
# is_tabu = False
neighbour, diff = self.neighbour_gen_fun(solution, rd, True)
while neighbour in neighbours:
neighbour, diff = self.neighbour_gen_fun(solution, rd, True)
if neighbour not in self.tabu_list:
if (diff in self.tabu_conditions) and (not self.diversification):
print('Tabu Solution')
continue
else:
break
neighbours.append(neighbour)
diffs.append(diff)
assert len(neighbours) == self.num_neighbours
best_neighbour = {}
best_accuracy = 0
for neighbour, diff in zip(neighbours, diffs):
val = self.objective_fun(neighbour)
if val > best_accuracy:
best_accuracy = val
best_neighbour = deepcopy(neighbour)
elif val < 0.1:
self.tabu_conditions.append(diff)
return best_neighbour, best_accuracy, neighbours
def TSearch(self):
'''The implementation Tabu search algorithm with short-term memory and pair_swap as Tabu attribute.
'''
# Parameters:
tenure =self.tabu_length
best_solution = self.Initial_solution
best_objvalue = self.objective_fun(best_solution)
current_objvalue = 0
print("#"*30, "Short-term memory TS with Tabu Tenure: {}\nInitial Solution: {}, Initial Objvalue: {}".format(tenure, best_solution, best_objvalue), "#"*30, sep='\n\n')
Terminate = 0
count = 0
while Terminate < 100:
print('\n\n### Iteration: {}### Tabu List {}, Diversification {}, Current_Objvalue: {}, Best_Objvalue: {}'.format(Terminate, len(self.tabu_list), self.diversification, current_objvalue, best_objvalue))
current_solution, current_objvalue, neighbours = self.get_neighbours_and_evaluate(best_solution)
self.tabu_list = self.tabu_list + neighbours
while len(self.tabu_list) > self.tabu_length:
del self.tabu_list[0]
while len(self.tabu_conditions) > 5:
del self.tabu_conditions[0]
if current_objvalue > best_objvalue:
best_objvalue = current_objvalue
best_solution = current_solution
count = 0
self.diversification = False
self.tabu_length = tenure
Terminate += 1
elif (current_objvalue < best_objvalue) and (current_objvalue != -1):
count += 1
Terminate += 1
if count == 10:
self.diversification = True
self.tabu_length = tenure*2
if count == 20:
break
print('#'*50, "Performed iterations: {}".format(Terminate), "Best found Solution: {} , Objvalue: {}".format(best_solution,best_objvalue), sep="\n")
return best_solution, best_objvalue
start_time = time.time()
test = TS(cnn_default_hyperparameters, cnn_get_random_neighbouring_solution, cnn_objective, tabu_length = 100)
end_time = time.time()
with open(file_name, 'a+') as f:
print('-' * 50, file=f)
print(end_time-start_time, file=f)
print(test.Best_solution, file=f)
print(test.Best_objvalue, file=f)
print(test.Best_objvalue, test.Best_solution, end_time-start_time)