Skip to content

Commit ba697cd

Browse files
author
rangerix
committed
Golden Ratio Based optimization
1 parent e6552b1 commit ba697cd

File tree

1 file changed

+355
-0
lines changed

1 file changed

+355
-0
lines changed

goldenRatio.py

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
import numpy as np
2+
import pandas as pd
3+
import random
4+
import math,time,sys
5+
from matplotlib import pyplot
6+
from datetime import datetime
7+
from sklearn.neighbors import KNeighborsClassifier
8+
from sklearn.model_selection import train_test_split
9+
10+
#==================================================================
11+
def sigmoid1(gamma): #convert to probability
12+
if gamma < 0:
13+
return 1 - 1/(1 + math.exp(gamma))
14+
else:
15+
return 1/(1 + math.exp(-gamma))
16+
17+
def sigmoid1i(gamma): #convert to probability
18+
gamma = -gamma
19+
if gamma < 0:
20+
return 1 - 1/(1 + math.exp(gamma))
21+
else:
22+
return 1/(1 + math.exp(-gamma))
23+
24+
def sigmoid2(gamma):
25+
gamma /= 2
26+
if gamma < 0:
27+
return 1 - 1/(1 + math.exp(gamma))
28+
else:
29+
return 1/(1 + math.exp(-gamma))
30+
31+
def sigmoid3(gamma):
32+
gamma /= 3
33+
if gamma < 0:
34+
return 1 - 1/(1 + math.exp(gamma))
35+
else:
36+
return 1/(1 + math.exp(-gamma))
37+
38+
def sigmoid4(gamma):
39+
gamma *= 2
40+
if gamma < 0:
41+
return 1 - 1/(1 + math.exp(gamma))
42+
else:
43+
return 1/(1 + math.exp(-gamma))
44+
45+
46+
def Vfunction1(gamma):
47+
return abs(np.tanh(gamma))
48+
49+
def Vfunction2(gamma):
50+
val = (math.pi)**(0.5)
51+
val /= 2
52+
val *= gamma
53+
val = math.erf(val)
54+
return abs(val)
55+
56+
def Vfunction3(gamma):
57+
val = 1 + gamma*gamma
58+
val = math.sqrt(val)
59+
val = gamma/val
60+
return abs(val)
61+
62+
def Vfunction4(gamma):
63+
val=(math.pi/2)*gamma
64+
val=np.arctan(val)
65+
val=(2/math.pi)*val
66+
return abs(val)
67+
68+
def initialize(popSize,dim):
69+
population=np.zeros((popSize,dim))
70+
minn = 1
71+
maxx = math.floor(0.8*dim)
72+
if maxx<minn:
73+
minn = maxx
74+
75+
for i in range(popSize):
76+
random.seed(i**3 + 10 + time.time() )
77+
no = random.randint(minn,maxx)
78+
if no == 0:
79+
no = 1
80+
random.seed(time.time()+ 100)
81+
pos = random.sample(range(0,dim-1),no)
82+
for j in pos:
83+
population[i][j]=1
84+
85+
# print(population[i])
86+
return population
87+
88+
def fitness(solution, trainX, testX, trainy, testy):
89+
cols=np.flatnonzero(solution)
90+
val=1
91+
if np.shape(cols)[0]==0:
92+
return val
93+
clf=KNeighborsClassifier(n_neighbors=5)
94+
train_data=trainX[:,cols]
95+
test_data=testX[:,cols]
96+
clf.fit(train_data,trainy)
97+
val=1-clf.score(test_data,testy)
98+
99+
#in case of multi objective []
100+
set_cnt=sum(solution)
101+
set_cnt=set_cnt/np.shape(solution)[0]
102+
val=omega*val+(1-omega)*set_cnt
103+
return val
104+
105+
def allfit(population, trainX, testX, trainy, testy):
106+
x=np.shape(population)[0]
107+
acc=np.zeros(x)
108+
for i in range(x):
109+
acc[i]=fitness(population[i],trainX,testX,trainy,testy)
110+
#print(acc[i])
111+
return acc
112+
113+
def toBinary(solution,dimension):
114+
# print("continuous",solution)
115+
Xnew = np.zeros(np.shape(solution))
116+
for i in range(dimension):
117+
temp = Vfunction3(abs(solution[i]))
118+
119+
random.seed(time.time()+i)
120+
if temp > random.random(): # sfunction
121+
Xnew[i] = 1
122+
else:
123+
Xnew[i] = 0
124+
# if temp > 0.5: # vfunction
125+
# Xnew[i] = 1 - abs(solution[i])
126+
# else:
127+
# Xnew[i] = abs(solution[i])
128+
# print("binary",Xnew)
129+
return Xnew
130+
131+
def toBinaryX(solution,dimension,oldsol,trainX, testX, trainy, testy):
132+
Xnew = np.zeros(np.shape(solution))
133+
Xnew1 = np.zeros(np.shape(solution))
134+
Xnew2 = np.zeros(np.shape(solution))
135+
for i in range(dimension):
136+
temp = sigmoid1(abs(solution[i]))
137+
random.seed(time.time()+i)
138+
r1 = random.random()
139+
if temp > r1: # sfunction
140+
Xnew1[i] = 1
141+
else:
142+
Xnew1[i] = 0
143+
144+
temp = sigmoid1i(abs(solution[i]))
145+
if temp > r1: # sfunction
146+
Xnew2[i] = 1
147+
else:
148+
Xnew2[i] = 0
149+
150+
fit1 = fitness(Xnew1,trainX,testX,trainy,testy)
151+
fit2 = fitness(Xnew2,trainX,testX,trainy,testy)
152+
fitOld = fitness(oldsol,trainX,testX,trainy,testy)
153+
if fit1<fitOld or fit2<fitOld:
154+
if fit1 < fit2:
155+
Xnew = Xnew1.copy()
156+
else:
157+
Xnew = Xnew2.copy()
158+
return Xnew
159+
# else: CROSSOVER
160+
Xnew3 = Xnew1.copy()
161+
Xnew4 = Xnew2.copy()
162+
for i in range(dimension):
163+
random.seed(time.time() + i)
164+
r2 = random.random()
165+
if r2>0.5:
166+
tx = Xnew3[i]
167+
Xnew3[i] = Xnew4[i]
168+
Xnew4[i] = tx
169+
fit1 = fitness(Xnew3,trainX,testX,trainy,testy)
170+
fit2 = fitness(Xnew4,trainX,testX,trainy,testy)
171+
if fit1<fit2:
172+
return Xnew3
173+
else:
174+
return Xnew4
175+
# print("binary",Xnew)
176+
177+
178+
#==================================================================
179+
def goldenratiomethod(dataset,popSize,maxIter):
180+
181+
#---------------------------------------------------------------------
182+
#I know I should not put not it here, but still ...
183+
df=pd.read_csv(dataset)
184+
(a,b)=np.shape(df)
185+
print(a,b)
186+
data = df.values[:,0:b-1]
187+
label = df.values[:,b-1]
188+
dimension = np.shape(data)[1] #particle dimension
189+
#---------------------------------------------------------------------
190+
191+
cross = 5
192+
test_size = (1/cross)
193+
trainX, testX, trainy, testy = train_test_split(data, label,stratify=label ,test_size=test_size)
194+
195+
196+
clf=KNeighborsClassifier(n_neighbors=5)
197+
clf.fit(trainX,trainy)
198+
val=clf.score(testX,testy)
199+
whole_accuracy = val
200+
print("Total Acc: ",val)
201+
202+
x_axis = []
203+
y_axis = []
204+
population = initialize(popSize,dimension)
205+
BESTANS = np.zeros(np.shape(population[0]))
206+
BESTACC = 1000
207+
208+
start_time = datetime.now()
209+
210+
for currIter in range(1,maxIter):
211+
212+
fitList = allfit(population,trainX,testX,trainy,testy)
213+
y_axis.append(min(fitList))
214+
x_axis.append(currIter)
215+
worstInx = np.argmax(fitList)
216+
fitWorst = max(fitList)
217+
Xworst = population[worstInx].copy()
218+
219+
Xave = population.sum(axis=0)
220+
Xave = np.divide(Xave,popSize)
221+
# for x in Xave:
222+
# print("%.2f"%x,end=',')
223+
# print()
224+
XaveBin= toBinary(Xave,dimension)
225+
FITave = fitness(XaveBin, trainX, testX, trainy, testy)
226+
if FITave<fitWorst:
227+
population[worstInx] = XaveBin.copy()
228+
fitList[worstInx] = FITave
229+
230+
231+
232+
for i in range(popSize):
233+
Xi = population[i].copy()
234+
j = i
235+
while j == i:
236+
random.seed(time.time()+j)
237+
j = random.randint(0, popSize-1)
238+
Xj = population[j].copy()
239+
FITi = fitList[i]
240+
FITj = fitList[j]
241+
242+
Xave = population.sum(axis=0)
243+
Xave = np.subtract(Xave,population[i])
244+
Xave = np.subtract(Xave,population[j])
245+
Xave = np.divide(Xave,(popSize-2))
246+
XaveBin = toBinary(Xave,dimension)
247+
FITave = fitness(XaveBin, trainX, testX, trainy, testy)
248+
# print(i,j,FITi,FITj,FITave)
249+
Xbest = np.zeros(np.shape(Xi))
250+
Xmedium = np.zeros(np.shape(Xi))
251+
Xworst = np.zeros(np.shape(Xi))
252+
253+
if FITi < FITj < FITave:
254+
Xbest = Xi.copy()
255+
Xmedium = Xj.copy()
256+
Xworst = Xave.copy()
257+
elif FITi < FITave < FITj:
258+
Xbest = Xi.copy()
259+
Xmedium = Xave.copy()
260+
Xworst = Xj.copy()
261+
elif FITj < FITi < FITave:
262+
Xbest = Xj.copy()
263+
Xmedium = Xi.copy()
264+
Xworst = Xave.copy()
265+
elif FITj < FITave < FITi:
266+
Xbest = Xj.copy()
267+
Xmedium = Xave.copy()
268+
Xworst = Xi.copy()
269+
elif FITave < FITi < FITj:
270+
Xbest = Xave.copy()
271+
Xmedium = Xi.copy()
272+
Xworst = Xj.copy()
273+
elif FITave < FITj < FITi:
274+
Xbest = Xave.copy()
275+
Xmedium = Xj.copy()
276+
Xworst = Xi.copy()
277+
278+
Xt = np.subtract(Xmedium,Xworst)
279+
T = currIter/maxIter
280+
Ft = (golden/(5**0.5)) * (golden**T - (1 - golden)**T)
281+
random.seed(19*time.time() + 10.01)
282+
Xnew = np.multiply(Xbest,(1-Ft)) + np.multiply(Xt,random.random()*Ft)
283+
Xnew = toBinaryX(Xnew,dimension,population[i],trainX, testX, trainy, testy)
284+
FITnew = fitness(Xnew, trainX, testX, trainy, testy)
285+
# if FITnew < fitList[i]:
286+
# print(i,j,"updated2")
287+
population[i] = Xnew.copy()
288+
fitList[i] = FITnew
289+
290+
#second phase
291+
worstInx = np.argmax(fitList)
292+
fitWorst = max(fitList)
293+
Xworst = population[worstInx].copy()
294+
bestInx = np.argmin(fitList)
295+
fitBest = min(fitList)
296+
Xbest = population[bestInx].copy()
297+
for i in range(popSize):
298+
Xi = population[i].copy()
299+
random.seed(29*time.time() + 391.97 )
300+
Xnew = np.add(Xi , np.multiply(np.subtract(Xbest,Xworst),random.random()*(1/golden)) )
301+
Xnew = toBinaryX(Xnew,dimension,population[i],trainX, testX, trainy, testy)
302+
FITnew = fitness(Xnew, trainX, testX, trainy, testy)
303+
# if FITnew < fitList[i]:
304+
fitList[i] = FITnew
305+
population[i] = Xnew.copy()
306+
307+
if fitList[i]< BESTACC:
308+
BESTACC = fitList[i]
309+
BESTANS = population[i].copy()
310+
311+
# pyplot.plot(x_axis,y_axis)
312+
# pyplot.show()
313+
# bestInx = np.argmin(fitList)
314+
# fitBest = min(fitList)
315+
# Xbest = population[bestInx].copy()
316+
cols = np.flatnonzero(BESTANS)
317+
val = 1
318+
if np.shape(cols)[0]==0:
319+
return Xbest
320+
clf = KNeighborsClassifier(n_neighbors=5)
321+
train_data = trainX[:,cols]
322+
test_data = testX[:,cols]
323+
clf.fit(train_data,trainy)
324+
val = clf.score(test_data,testy)
325+
return BESTANS,val
326+
327+
328+
329+
330+
#==================================================================
331+
golden = (1 + 5 ** 0.5) / 2
332+
popSize = 10
333+
maxIter = 10
334+
omega = 1
335+
datasetList = ["BreastEW"]
336+
datasetList = ["Breastcancer", "BreastEW", "CongressEW", "Exactly", "Exactly2", "HeartEW", "Ionosphere", "KrVsKpEW", "Lymphography", "M-of-n", "PenglungEW", "Sonar", "SpectEW", "Tic-tac-toe", "Vote", "WaveformEW", "Wine", "Zoo"]
337+
338+
for dataset in datasetList:
339+
accuList = []
340+
featList = []
341+
for count in range(10):
342+
if (dataset == "WaveformEW" or dataset == "KrVsKpEW") and count>2:
343+
break
344+
print(count)
345+
answer,testAcc = goldenratiomethod("csvUCI/"+dataset+".csv",popSize,maxIter)
346+
print(testAcc,answer.sum())
347+
accuList.append(testAcc)
348+
featList.append(answer.sum())
349+
inx = np.argmax(accuList)
350+
best_accuracy = accuList[inx]
351+
best_no_features = featList[inx]
352+
print(dataset,"best:",accuList[inx],featList[inx])
353+
354+
with open("result_GRx.csv","a") as f:
355+
print(dataset,"%.2f" % (100*best_accuracy),best_no_features,file=f)

0 commit comments

Comments
 (0)