-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathba.py
139 lines (112 loc) · 4.24 KB
/
ba.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
#[2010]-"A new metaheuristic bat-inspired algorithm"
import numpy as np
from numpy.random import rand
from FS.functionHO import Fun
from FS.__basic import init_position, binary_conversion, boundary
def jfs(xtrain, ytrain, opts):
# Parameters
ub = opts['ub']
lb = opts['lb']
thres = 0.5
fmax = 2 # maximum frequency
fmin = 0 # minimum frequency
alpha = 0.9 # constant
gamma = 0.9 # constant
A_max = 2 # maximum loudness
r0_max = 1 # maximum pulse rate
N = opts['N']
max_iter = opts['T']
if 'fmax' in opts:
fmax = opts['fmax']
if 'fmin' in opts:
fmin = opts['fmin']
if 'alpha' in opts:
alpha = opts['alpha']
if 'gamma' in opts:
gamma = opts['gamma']
if 'A' in opts:
A_max = opts['A']
if 'r' in opts:
r0_max = opts['r']
# 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 & velocity
X = init_position(lb, ub, N, dim)
V = np.zeros([N, dim], dtype='float')
# 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 (BA):", curve[0,t])
t += 1
# Initial loudness [1 ~ 2] & pulse rate [0 ~ 1]
A = np.random.uniform(1, A_max, N)
r0 = np.random.uniform(0, r0_max, N)
r = r0.copy()
while t < max_iter:
Xnew = np.zeros([N, dim], dtype='float')
for i in range(N):
# beta [0 ~1]
beta = rand()
# frequency (2)
freq = fmin + (fmax - fmin) * beta
for d in range(dim):
# Velocity update (3)
V[i,d] = V[i,d] + (X[i,d] - Xgb[0,d]) * freq
# Position update (4)
Xnew[i,d] = X[i,d] + V[i,d]
# Boundary
Xnew[i,d] = boundary(Xnew[i,d], lb[0,d], ub[0,d])
# Generate local solution around best solution
if rand() > r[i]:
for d in range (dim):
# Epsilon in [-1,1]
eps = -1 + 2 * rand()
# Random walk (5)
Xnew[i,d] = Xgb[0,d] + eps * np.mean(A)
# 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 (rand() < A[i]) and (Fnew <= fit[i,0]):
X[i,:] = Xnew[i,:]
fit[i,0] = Fnew
# Loudness update (6)
A[i] = alpha * A[i]
# Pulse rate update (6)
r[i] = r0[i] * (1 - np.exp(-gamma * t))
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 (BA):", 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
ba_data = {'sf': sel_index, 'c': curve, 'nf': num_feat}
return ba_data