-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcs.py
160 lines (125 loc) · 4.71 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
#[2009]-"Cuckoo search via Levy flights"
import numpy as np
from numpy.random import rand
from FS.functionHO import Fun
from FS.__basic import init_position, binary_conversion, boundary
import math
# Levy Flight
def levy_distribution(beta, dim):
# Sigma
nume = math.gamma(1 + beta) * np.sin(np.pi * beta / 2)
deno = math.gamma((1 + beta) / 2) * beta * 2 ** ((beta - 1) / 2)
sigma = (nume / deno) ** (1 / beta)
# Parameter u & v
u = np.random.randn(dim) * sigma
v = np.random.randn(dim)
# Step
step = u / abs(v) ** (1 / beta)
LF = 0.01 * step
return LF
def jfs(xtrain, ytrain, opts):
# Parameters
ub = opts['ub']
lb = opts['lb']
thres = 0.5
Pa = 0.25 # discovery rate
alpha = 1 # constant
beta = 1.5 # levy component
N = opts['N']
max_iter = opts['T']
if 'Pa' in opts:
Pa = opts['Pa']
if 'alpha' in opts:
alpha = opts['alpha']
if 'beta' in opts:
beta = opts['beta']
# Dimension
dim = np.size(xtrain, 1)
if np.size(lb) == 1:
ub = ub * np.ones([1, dim], dtype='float')
lb = lb * np.ones([1, dim], dtype='float')
# Initialize position
X = init_position(lb, ub, N, dim)
# Binary conversion
Xbin = binary_conversion(X, thres, N, dim)
# Fitness at first iteration
fit = np.zeros([N, 1], dtype='float')
Xgb = np.zeros([1, dim], dtype='float')
fitG = float('inf')
for i in range(N):
fit[i,0] = Fun(xtrain, ytrain, Xbin[i,:], opts)
if fit[i,0] < fitG:
Xgb[0,:] = X[i,:]
fitG = fit[i,0]
# Pre
curve = np.zeros([1, max_iter], dtype='float')
t = 0
curve[0,t] = fitG.copy()
print("Generation:", t + 1)
print("Best (CS):", curve[0,t])
t += 1
while t < max_iter:
Xnew = np.zeros([N, dim], dtype='float')
# {1} Random walk/Levy flight phase
for i in range(N):
# Levy distribution
L = levy_distribution(beta,dim)
for d in range(dim):
# Levy flight (1)
Xnew[i,d] = X[i,d] + alpha * L[d] * (X[i,d] - Xgb[0,d])
# Boundary
Xnew[i,d] = boundary(Xnew[i,d], lb[0,d], ub[0,d])
# Binary conversion
Xbin = binary_conversion(Xnew, thres, N, dim)
# Greedy selection
for i in range(N):
Fnew = Fun(xtrain, ytrain, Xbin[i,:], opts)
if Fnew <= fit[i,0]:
X[i,:] = Xnew[i,:]
fit[i,0] = Fnew
if fit[i,0] < fitG:
Xgb[0,:] = X[i,:]
fitG = fit[i,0]
# {2} Discovery and abandon worse nests phase
J = np.random.permutation(N)
K = np.random.permutation(N)
Xj = np.zeros([N, dim], dtype='float')
Xk = np.zeros([N, dim], dtype='float')
for i in range(N):
Xj[i,:] = X[J[i],:]
Xk[i,:] = X[K[i],:]
Xnew = np.zeros([N, dim], dtype='float')
for i in range(N):
Xnew[i,:] = X[i,:]
r = rand()
for d in range(dim):
# A fraction of worse nest is discovered with a probability
if rand() < Pa:
Xnew[i,d] = X[i,d] + r * (Xj[i,d] - Xk[i,d])
# Boundary
Xnew[i,d] = boundary(Xnew[i,d], lb[0,d], ub[0,d])
# Binary conversion
Xbin = binary_conversion(Xnew, thres, N, dim)
# Greedy selection
for i in range(N):
Fnew = Fun(xtrain, ytrain, Xbin[i,:], opts)
if Fnew <= fit[i,0]:
X[i,:] = Xnew[i,:]
fit[i,0] = Fnew
if fit[i,0] < fitG:
Xgb[0,:] = X[i,:]
fitG = fit[i,0]
# Store result
curve[0,t] = fitG.copy()
print("Generation:", t + 1)
print("Best (CS):", curve[0,t])
t += 1
# Best feature subset
Gbin = binary_conversion(Xgb, thres, 1, dim)
Gbin = Gbin.reshape(dim)
pos = np.asarray(range(0, dim))
sel_index = pos[Gbin == 1]
num_feat = len(sel_index)
# Create dictionary
cs_data = {'sf': sel_index, 'c': curve, 'nf': num_feat}
return cs_data