-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsi_final.py
269 lines (206 loc) · 11.1 KB
/
jsi_final.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
"""JSI_final.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PI-00nODPx92ELdTBLb2EKVIqCJYjKdg
"""
import sys
sys.path.insert(1, 'Sonia')
sys.path.insert(1, 'Shadi')
sys.path.insert(1, 'Justin')
from GAO import *
from CSO import *
from IWO import *
# import sys
# sys.path.insert(1, '../Sonia')
from abc_opt import ABC_OPT
from de import DE
from gso import GSO
from fss_opt import FSS
from pso import *
from gwo_algorithm import *
from firefly_algorithm import *
import numpy as np
from sklearn import svm
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
import random
data = load_digits()
n_samples = len(data.images)
X = data.images.reshape((n_samples, -1))
Y = data['target']
# modle implementation
def fitness_function1(x):
# x[0] = C and x[1] = gamma
clf = svm.SVC(kernel='rbf', C=x[0], gamma=x[1], random_state=42)
scores = cross_val_score(clf, X, Y, cv=5)
return scores.mean()
def fitness_function2(x):
pass
def bubble_sort(lst1, lst2):
# Set a flag to True to indicate that the list is not yet sorted
sorted = False
# Keep looping until the list is sorted
while not sorted:
# Set the flag to True to assume that the list is already sorted
sorted = True
# Iterate over the list and compare adjacent elements
for i in range(len(lst1) - 1):
# If the current element is smaller than the next element, swap them
if lst1[i] < lst1[i + 1]:
lst1[i], lst1[i + 1] = lst1[i + 1], lst1[i]
lst2[i], lst2[i + 1] = lst2[i + 1], lst2[i]
sorted = False
# Return the sorted list
return lst1, lst2
def clip_pop(pop, intervals):
# IF BOUND IS SPECIFIED THEN CLIP 'pop' VALUES SO THAT THEY ARE IN THE SPECIFIED RANGE
return [[random.uniform(lower_bound, upper_bound) if not lower_bound <= x <= upper_bound else x for x, (lower_bound, upper_bound) in zip(sublist, intervals)] for sublist in pop]
class JSI:
def __init__(self, **kwargs):
self.model = kwargs.get('model', 'svm')
self.parameters = kwargs.get('parameters', 2)
self.intervals = kwargs.get('intervals', [[1.0, 100.0], [0.0001, 0.1]])
self.interval_dict = {'c': self.intervals[0], 'gamma': self.intervals[1]}
self.Generations = kwargs.get('Generations', 10)
self.n_pop = kwargs.get('n_pop', 10)
if self.model == 'svm':
self.fitness_function = fitness_function1
elif self.model == 'decision tree':
self.fitness_function = fitness_function2
self.best_pop_from_all=[]
self.best_fit_from_all=[]
self.model_name=[]
self.n = len(self.intervals)
#initialize the population
self.pop = []
if self.intervals is not None:
for i in range(self.n_pop):
x = [random.uniform(interval[0], interval[1]) for interval in self.intervals]
self.pop.append(x) # list of lists, for 2 dim: [[ , ], [ , ], [ , ],...,[ , ]]
self.pop = clip_pop(self.pop, self.intervals)
else:
print('Please determine the intervals for the paremeters')
def run(self):
old_pop_GAO = self.pop.copy()
old_pop_CSO = self.pop.copy()
old_pop_IWO = self.pop.copy()
old_pop_ABC = self.pop.copy()
old_pop_DE = self.pop.copy()
old_pop_GSO = self.pop.copy()
old_pop_FSS = self.pop.copy()
old_pop_firefly = self.pop.copy()
old_pop_gwo = self.pop.copy()
old_pop_pso = self.pop.copy()
#params for fss
#initial step
Sinit = {p: self.interval_dict[p][0] for p in self.interval_dict.keys()}
#final step
Sfinal = {p: self.interval_dict[p][1] for p in self.interval_dict.keys()}
for i in range(self.Generations):
print('iter:',i)
#new_pop_Algo, fit_Algo = Algo(...., pop=old_pop_Algo,...)
#[ , , , ...], [ , , , ...] = Algo(..., pop = [ , , , ...], ...)
new_pop_GAO, fit_GAO = GAO(fitness_function=self.fitness_function, pop=old_pop_GAO, intervals=self.intervals)
new_pop_CSO, fit_CSO = CSO(fitness_function=self.fitness_function, nest=old_pop_CSO, n_pop=self.n_pop, intervals=self.intervals, pa=0.25, beta=1.5)
new_pop_IWO, fit_IWO = IWO(dim=self.parameters, fitness_function=self.fitness_function, n_pop=self.n_pop, pop=old_pop_IWO, intervals=self.intervals, rinitial=2, rfinal=0.1, modulation_index=2, itermax=self.Generations, iter=i)
new_pop_ABC, fit_ABC = ABC_OPT(bounds=self.interval_dict, n_pop=self.n_pop, cycles=1, fitness_function=self.fitness_function, population=None, old_pop = old_pop_ABC)()
print("finished with ABC")
new_pop_DE, fit_DE = DE(cr=0.9, bounds=self.interval_dict, n_pop=self.n_pop, cycles=1, fitness_function=self.fitness_function, population=None, old_pop = old_pop_DE)()
print("finished with DE")
new_pop_GSO, fit_GSO = GSO(rho=0.3, gamma=0.65, s=0.5, rs=0.45, r0=4, betta=0.075, l0=0.25, bounds=self.interval_dict, n_pop=self.n_pop, cycles=1, fitness_function=self.fitness_function, population=None, old_pop = old_pop_GSO)()
print("finished with GSO")
new_pop_FSS, fit_FSS = FSS(Sinit, Sfinal, bounds=self.interval_dict, n_pop=self.n_pop, cycles=1, fitness_function=self.fitness_function, population=None, old_pop = old_pop_FSS)()
print("finished with FFS")
best_firefly, pop_firefly, fit_firefly =firefly_algorithm(fitness_function = self.fitness_function, population =old_pop_firefly, dimensions = self.intervals, max_iter = self.Generations, alpha = 0.5, beta = 0.5, gamma = 0.5)
print("finished with firefly")
best, pop_GWO, fit_gwo = GWO(fitness_function = self.fitness_function, pop_size = self.n_pop, intervals = self.intervals, max_iter = self.Generations)
print("finished with GWO")
best_position, pop_pso, fit_pso = PSO(population = old_pop_pso, fitness_function = self.fitness_function, intervals= self.intervals, max_iter = self.Generations, c1 = 1.5, c2 =1.5)
print("finished with PSO")
#sorting the fitness with the corresponding population
sorted_fit_GAO, new_pop_GAO = bubble_sort(fit_GAO, new_pop_GAO)
sorted_fit_CSO, new_pop_CSO = bubble_sort(fit_CSO, new_pop_CSO)
sorted_fit_IWO, new_pop_IWO = bubble_sort(fit_IWO, new_pop_IWO)
sorted_fit_ABC, new_pop_ABC = bubble_sort(fit_ABC, new_pop_ABC)
sorted_fit_DE, new_pop_DE = bubble_sort(fit_DE, new_pop_DE)
sorted_fit_GSO, new_pop_GSO = bubble_sort(fit_GSO, new_pop_GSO)
sorted_fit_FSS, new_pop_FSS = bubble_sort(fit_FSS, new_pop_FSS)
sorted_fit_firefly, pop_firefly = bubble_sort(fit_firefly, pop_firefly)
sorted_fit_gwo, pop_GWO = bubble_sort(fit_gwo, pop_GWO)
sorted_fit_pso, pop_pso = bubble_sort(fit_pso, pop_pso )
#change the n-1 worst nembers in each of the n population with the
#best member from each of the n-1 other population
new_pop_GAO[self.n_pop-1] = new_pop_CSO[0].copy()
new_pop_GAO[self.n_pop-2] = new_pop_IWO[0].copy()
new_pop_CSO[self.n_pop-1] = new_pop_GAO[0].copy()
new_pop_CSO[self.n_pop-2] = new_pop_IWO[0].copy()
new_pop_IWO[self.n_pop-1] = new_pop_GAO[0].copy()
new_pop_IWO[self.n_pop-2] = new_pop_CSO[0].copy()
new_pop_ABC[self.n_pop-1] = new_pop_DE[0].copy()
new_pop_ABC[self.n_pop-2] = new_pop_GSO[0].copy()
new_pop_ABC[self.n_pop-3] = new_pop_FSS[0].copy()
new_pop_DE[self.n_pop-1] = new_pop_ABC[0].copy()
new_pop_DE[self.n_pop-2] = new_pop_GSO[0].copy()
new_pop_DE[self.n_pop-3] = new_pop_FSS[0].copy()
new_pop_GSO[self.n_pop-1] = new_pop_DE[0].copy()
new_pop_GSO[self.n_pop-2] = new_pop_ABC[0].copy()
new_pop_GSO[self.n_pop-3] = new_pop_FSS[0].copy()
new_pop_FSS[self.n_pop-1] = new_pop_DE[0].copy()
new_pop_FSS[self.n_pop-2] = new_pop_GSO[0].copy()
new_pop_FSS[self.n_pop-3] = new_pop_ABC[0].copy()
pop_firefly[self.n_pop-1] = pop_GWO[0].copy()
pop_firefly[self.n_pop-2] = pop_pso[0].copy()
pop_GWO[self.n_pop-1] = pop_firefly[0].copy()
pop_GWO[self.n_pop-2] = pop_pso[0].copy()
pop_pso[self.n_pop-1] = pop_firefly[0].copy()
pop_pso[self.n_pop-2] = pop_GWO[0].copy()
old_pop_GAO = new_pop_GAO
old_pop_CSO = new_pop_CSO
old_pop_IWO = new_pop_IWO
old_pop_ABC = new_pop_ABC
old_pop_DE = new_pop_DE
old_pop_GSO = new_pop_GSO
old_pop_FSS = new_pop_FSS
old_pop_firefly = pop_firefly
old_pop_gwo = pop_GWO
old_pop_pso = pop_pso
#end of for loop
#after the loop finishes store the best fitness and the corresponding population of each algorithm
self.best_pop_from_all.append(new_pop_GAO[0])
self.best_fit_from_all.append(sorted_fit_GAO[0])
self.model_name.append('GAO')
self.best_pop_from_all.append(new_pop_CSO[0])
self.best_fit_from_all.append(sorted_fit_CSO[0])
self.model_name.append('CSO')
self.best_pop_from_all.append(new_pop_IWO[0])
self.best_fit_from_all.append(sorted_fit_IWO[0])
self.model_name.append('IWO')
self.best_pop_from_all.append(new_pop_ABC[0])
self.best_fit_from_all.append(sorted_fit_ABC[0])
self.model_name.append('ABC')
self.best_pop_from_all.append(new_pop_DE[0])
self.best_fit_from_all.append(sorted_fit_DE[0])
self.model_name.append('DE')
self.best_pop_from_all.append(new_pop_GSO[0])
self.best_fit_from_all.append(sorted_fit_GSO[0])
self.model_name.append('GSO')
self.best_pop_from_all.append(new_pop_FSS[0])
self.best_fit_from_all.append(sorted_fit_FSS[0])
self.model_name.append('FSS')
self.best_pop_from_all.append(pop_firefly[0])
self.best_fit_from_all.append(sorted_fit_firefly[0])
self.model_name.append('Firefly')
self.best_pop_from_all.append(pop_GWO[0])
self.best_fit_from_all.append(sorted_fit_gwo[0])
self.model_name.append('GWO')
self.best_pop_from_all.append(pop_pso[0])
self.best_fit_from_all.append(sorted_fit_pso[0])
self.model_name.append('PSO')
# after haveing the best fitness and the corresponding population of each algorithm
# this function will return the best among all
def best_all(self):
# 3 lists and sortin based on the second one (fitness): [ , , ] , [ , , ], [ , , ]
best = sorted(zip(self.best_pop_from_all, self.best_fit_from_all, self.model_name), key=lambda x: x[1], reverse=True)
# best will be like: [( , , ),( , , ),( , , )]
return best[0][0], best[0][1], best[0][2]