def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 b = 1 # constant N = opts['N'] max_iter = opts['T'] if 'b' in opts: b = opts['b'] # 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 (WOA):", curve[0, t]) t += 1 while t < max_iter: # Define a, linearly decreases from 2 to 0 a = 2 - t * (2 / max_iter) for i in range(N): # Parameter A (2.3) A = 2 * a * rand() - a # Paramater C (2.4) C = 2 * rand() # Parameter p, random number in [0,1] p = rand() # Parameter l, random number in [-1,1] l = -1 + 2 * rand() # Whale position update (2.6) if p < 0.5: # {1} Encircling prey if abs(A) < 1: for d in range(dim): # Compute D (2.1) Dx = abs(C * Xgb[0, d] - X[i, d]) # Position update (2.2) X[i, d] = Xgb[0, d] - A * Dx # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {2} Search for prey elif abs(A) >= 1: for d in range(dim): # Select a random whale k = np.random.randint(low=0, high=N) # Compute D (2.7) Dx = abs(C * X[k, d] - X[i, d]) # Position update (2.8) X[i, d] = X[k, d] - A * Dx # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {3} Bubble-net attacking elif p >= 0.5: for d in range(dim): # Distance of whale to prey dist = abs(Xgb[0, d] - X[i, d]) # Position update (2.5) X[i, d] = dist * np.exp(b * l) * np.cos( 2 * np.pi * l) + Xgb[0, d] # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness 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] # Store result curve[0, t] = fitG.copy() print("Generation:", t + 1) print("Best (WOA):", 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 woa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return woa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 max_local_iter = 10 # maximum iteration for local search N = opts['N'] max_iter = opts['T'] if 'maxLt' in opts: max_local_iter = opts['maxLt'] # 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) # Pre fit = np.zeros([N, 1], dtype='float') Xf = np.zeros([1, dim], dtype='float') fitF = float('inf') # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitF: Xf[0, :] = X[i, :] fitF = fit[i, 0] #--- Opposition based learning Xo = opposition_based_learning(X, lb, ub, thres, N, dim) #--- Binary conversion Xobin = binary_conversion(Xo, thres, N, dim) #--- Fitness fitO = np.zeros([N, 1], dtype='float') for i in range(N): fitO[i, 0] = Fun(xtrain, ytrain, Xobin[i, :], opts) if fitO[i, 0] < fitF: Xf[0, :] = Xo[i, :] fitF = fitO[i, 0] #--- Merge opposite & current population, and select best N XX = np.concatenate((X, Xo), axis=0) FF = np.concatenate((fit, fitO), axis=0) #--- Sort in ascending order ind = np.argsort(FF, axis=0) for i in range(N): X[i, :] = XX[ind[i, 0], :] fit[i, 0] = FF[ind[i, 0]] curve = np.zeros([1, max_iter], dtype='float') t = 0 # Store result curve[0, t] = fitF.copy() print("Iteration:", t + 1) print("Best (ISSA):", curve[0, t]) t += 1 while t < max_iter: # Compute coefficient, c1 (2) c1 = 2 * np.exp(-(4 * t / max_iter)**2) for i in range(N): # First leader update if i == 0: for d in range(dim): # Coefficient c2 & c3 [0 ~ 1] c2 = rand() c3 = rand() # Leader update (1) if c3 >= 0.5: X[i, d] = Xf[0, d] + c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) else: X[i, d] = Xf[0, d] - c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Salp update elif i >= 1: for d in range(dim): # Salp update by following front salp (3) X[i, d] = (X[i, d] + X[i - 1, d]) / 2 # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitF: Xf[0, :] = X[i, :] fitF = fit[i, 0] #--- Local search algorithm Lt = 0 temp = np.zeros([1, dim], dtype='float') temp[0, :] = Xf[0, :] while Lt < max_local_iter: #--- Random three features RD = np.random.permutation(dim) for d in range(3): index = RD[d] #--- Flip the selected three features if temp[0, index] > thres: temp[0, index] = temp[0, index] - thres else: temp[0, index] = temp[0, index] + thres #--- Binary conversion temp_bin = binary_conversion(temp, thres, 1, dim) #--- Fitness Fnew = Fun(xtrain, ytrain, temp_bin[0, :], opts) if Fnew < fitF: fitF = Fnew Xf[0, :] = temp[0, :] Lt += 1 # Store result curve[0, t] = fitF.copy() print("Iteration:", t + 1) print("Best (ISSA):", curve[0, t]) t += 1 # Best feature subset Gbin = binary_conversion(Xf, 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 issa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return issa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 CR = 0.8 # crossover rate MR = 0.01 # mutation rate N = opts['N'] max_iter = opts['T'] if 'CR' in opts: CR = opts['CR'] if 'MR' in opts: MR = opts['MR'] # 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 X = binary_conversion(X, thres, N, dim) # Fitness at first iteration fit = np.zeros([N, 1], dtype='float') Xgb = np.zeros([1, dim], dtype='int') fitG = float('inf') for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, X[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 (GA):", curve[0, t]) t += 1 while t < max_iter: # Probability inv_fit = 1 / (1 + fit) prob = inv_fit / np.sum(inv_fit) # Number of crossovers Nc = 0 for i in range(N): if rand() < CR: Nc += 1 x1 = np.zeros([Nc, dim], dtype='int') x2 = np.zeros([Nc, dim], dtype='int') for i in range(Nc): # Parent selection k1 = roulette_wheel(prob) k2 = roulette_wheel(prob) P1 = X[k1, :].copy() P2 = X[k2, :].copy() # Random one dimension from 1 to dim index = np.random.randint(low=1, high=dim - 1) # Crossover x1[i, :] = np.concatenate((P1[0:index], P2[index:])) x2[i, :] = np.concatenate((P2[0:index], P1[index:])) # Mutation for d in range(dim): if rand() < MR: x1[i, d] = 1 - x1[i, d] if rand() < MR: x2[i, d] = 1 - x2[i, d] # Merge two group into one Xnew = np.concatenate((x1, x2), axis=0) # Fitness Fnew = np.zeros([2 * Nc, 1], dtype='float') for i in range(2 * Nc): Fnew[i, 0] = Fun(xtrain, ytrain, Xnew[i, :], opts) if Fnew[i, 0] < fitG: Xgb[0, :] = Xnew[i, :] fitG = Fnew[i, 0] # Store result curve[0, t] = fitG.copy() print("Generation:", t + 1) print("Best (GA):", curve[0, t]) t += 1 # Elitism XX = np.concatenate((X, Xnew), axis=0) FF = np.concatenate((fit, Fnew), axis=0) # Sort in ascending order ind = np.argsort(FF, axis=0) for i in range(N): X[i, :] = XX[ind[i, 0], :] fit[i, 0] = FF[ind[i, 0]] # Best feature subset Gbin = Xgb[0, :] Gbin = Gbin.reshape(dim) pos = np.asarray(range(0, dim)) sel_index = pos[Gbin == 1] num_feat = len(sel_index) # Create dictionary ga_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return ga_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 alpha = 1 # constant beta0 = 1 # light amplitude gamma = 1 # absorbtion coefficient theta = 0.97 # control alpha N = opts['N'] max_iter = opts['T'] if 'alpha' in opts: alpha = opts['alpha'] if 'beta0' in opts: beta0 = opts['beta0'] if 'gamma' in opts: gamma = opts['gamma'] if 'theta' in opts: theta = opts['theta'] # 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 (FA):", curve[0, t]) t += 1 while t < max_iter: # Alpha update alpha = alpha * theta # Rank firefly based on their light intensity ind = np.argsort(fit, axis=0) FF = fit.copy() XX = X.copy() for i in range(N): fit[i, 0] = FF[ind[i, 0]] X[i, :] = XX[ind[i, 0], :] for i in range(N): # The attractiveness parameter for j in range(N): # Update moves if firefly j brighter than firefly i if fit[i, 0] > fit[j, 0]: # Compute Euclidean distance r = np.sqrt(np.sum((X[i, :] - X[j, :])**2)) # Beta (2) beta = beta0 * np.exp(-gamma * r**2) for d in range(dim): # Update position (3) eps = rand() - 0.5 X[i, d] = X[i, d] + beta * (X[j, d] - X[i, d]) + alpha * eps # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Binary conversion temp = np.zeros([1, dim], dtype='float') temp[0, :] = X[i, :] Xbin = binary_conversion(temp, thres, 1, dim) # fitness fit[i, 0] = Fun(xtrain, ytrain, Xbin[0, :], opts) # best update 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 (FA):", 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 fa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return fa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 CR = 0.9 # crossover rate F = 0.5 # factor N = opts['N'] max_iter = opts['T'] if 'CR' in opts: CR = opts['CR'] if 'F' in opts: F = opts['F'] # 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 (DE):", curve[0, t]) t += 1 while t < max_iter: V = np.zeros([N, dim], dtype='float') U = np.zeros([N, dim], dtype='float') for i in range(N): # Choose r1, r2, r3 randomly, but not equal to i RN = np.random.permutation(N) for j in range(N): if RN[j] == i: RN = np.delete(RN, j) break r1 = RN[0] r2 = RN[1] r3 = RN[2] # mutation (2) for d in range(dim): V[i, d] = X[r1, d] + F * (X[r2, d] - X[r3, d]) # Boundary V[i, d] = boundary(V[i, d], lb[0, d], ub[0, d]) # Random one dimension from 1 to dim index = np.random.randint(low=0, high=dim) # crossover (3-4) for d in range(dim): if (rand() <= CR) or (d == index): U[i, d] = V[i, d] else: U[i, d] = X[i, d] # Binary conversion Ubin = binary_conversion(U, thres, N, dim) # Selection for i in range(N): fitU = Fun(xtrain, ytrain, Ubin[i, :], opts) if fitU <= fit[i, 0]: X[i, :] = U[i, :] fit[i, 0] = fitU 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 (DE):", 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 de_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return de_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 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
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 N = opts['N'] max_iter = opts['T'] # 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) # Pre fit = np.zeros([N, 1], dtype='float') Xf = np.zeros([1, dim], dtype='float') fitF = float('inf') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitF: Xf[0, :] = X[i, :] fitF = fit[i, 0] # Store result curve[0, t] = fitF.copy() print("Iteration:", t + 1) print("Best (ESSA):", curve[0, t]) t += 1 for i in range(N): # First leader update if i == 0: for d in range(dim): #--- random number between [0, 50] r1 = 50 * rand() #--- compute c1 (5) c1 = 2 * np.exp(-((4 * r1 * t) / max_iter)**2) # Coefficient c2 & c3 [0 ~ 1] c2 = rand() c3 = rand() # Leader update (1) if c3 >= 0.5: X[i, d] = Xf[0, d] + c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) else: X[i, d] = Xf[0, d] - c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Salp update elif i >= 1: for d in range(dim): #--- random number between [0, 1] r2 = rand() #--- Salp update by following front salp (6) X[i, d] = r2 * (X[i, d] + X[i - 1, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Best feature subset Gbin = binary_conversion(Xf, 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 essa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return essa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 beta = 1.5 # levy component P = 0.8 # switch probability N = opts['N'] max_iter = opts['T'] if 'P' in opts: P = opts['P'] 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 (FPA):", curve[0, t]) t += 1 while t < max_iter: Xnew = np.zeros([N, dim], dtype='float') for i in range(N): # Global pollination if rand() < P: # Levy distribution (2) L = levy_distribution(beta, dim) for d in range(dim): # Global pollination (1) Xnew[i, d] = X[i, d] + L[d] * (X[i, d] - Xgb[0, d]) # Boundary Xnew[i, d] = boundary(Xnew[i, d], lb[0, d], ub[0, d]) # Local pollination else: # Different flower j, k in same species R = np.random.permutation(N) J = R[0] K = R[1] # Epsilon [0 to 1] eps = rand() for d in range(dim): # Local pollination (3) Xnew[i, d] = X[i, d] + eps * (X[J, d] - X[K, 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 (FPA):", 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 fpa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return fpa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 N = opts['N'] max_iter = opts['T'] # 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) # Pre fit = np.zeros([N, 1], dtype='float') Xgb = np.zeros([1, dim], dtype='float') fitG = float('inf') Xpb = np.zeros([N, dim], dtype='float') fitP = float('inf') * np.ones([N, 1], dtype='float') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitP[i, 0]: Xpb[i, :] = X[i, :] fitP[i, 0] = fit[i, 0] if fitP[i, 0] < fitG: Xgb[0, :] = Xpb[i, :] fitG = fitP[i, 0] # Store result curve[0, t] = fitG.copy() print("Iteration:", t + 1) print("Best (BBPSO):", curve[0, t]) t += 1 for i in range(N): for d in range(dim): #--- Mean mu = (Xpb[i, d] + Xgb[0, d]) / 2 #--- Standard deviation sd = abs(Xpb[i, d] - Xgb[0, d]) #--- Gaussian random number X[i, d] = sd * np.random.randn() + mu # 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 bbpso_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return bbpso_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 N = opts['N'] max_iter = opts['T'] # 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 (JA):", curve[0,t]) t += 1 while t < max_iter: Xnew = np.zeros([N, dim], dtype='float') # Identify best & worst in population idx_max = np.argmax(fit) Xw = X[idx_max,np.newaxis,:].copy() idx_min = np.argmin(fit) Xb = X[idx_min,np.newaxis,:].copy() for i in range(N): for d in range(dim): # Random numbers r1 = rand(); r2 = rand(); # Position update (1) Xnew[i,d] = X[i,d] + r1 * (Xb[0,d] - abs(X[i,d])) - r2 * (Xw[0,d] - abs(X[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 (JA):", 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 ja_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return ja_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 N = opts['N'] max_iter = opts['T'] # 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 X = binary_conversion(X, thres, N, dim) # Pre fit = np.zeros([N, 1], dtype='float') Xf = np.zeros([1, dim], dtype='int') fitF = float('inf') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, X[i, :], opts) if fit[i, 0] < fitF: Xf[0, :] = X[i, :] fitF = fit[i, 0] # Store result curve[0, t] = fitF.copy() print("Iteration:", t + 1) print("Best (TVBSSA):", curve[0, t]) t += 1 # Compute coefficient, c1 (3) c1 = 2 * np.exp(-(4 * t / max_iter)**2) #--- update number of leaders (12) L = np.ceil(N * (t / (max_iter + 1))) for i in range(N): #--- leader update if i < L: for d in range(dim): # Coefficient c2 & c3 [0 ~ 1] c2 = rand() c3 = rand() # Leader update (2) if c3 >= 0.5: Xn = Xf[0, d] + c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) else: Xn = Xf[0, d] - c1 * ( (ub[0, d] - lb[0, d]) * c2 + lb[0, d]) #--- transfer function Tx = transfer_function(Xn) #--- binary update (11) if rand() < Tx: X[i, d] = 0 else: X[i, d] = 1 #--- Salp update else: for d in range(dim): # Salp update by following front salp (4) Xn = (X[i, d] + X[i - 1, d]) / 2 # Boundary Xn = boundary(Xn, lb[0, d], ub[0, d]) #--- Binary conversion if Xn > thres: X[i, d] = 1 else: X[i, d] = 0 # Best feature subset Gbin = Xf[0, :] Gbin = Gbin.reshape(dim) pos = np.asarray(range(0, dim)) sel_index = pos[Gbin == 1] num_feat = len(sel_index) # Create dictionary tvbssa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return tvbssa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 Mp = 0.5 # mutation probability N = opts['N'] max_iter = opts['T'] if 'Mp' in opts: Mp = opts['Mp'] # 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 X = binary_conversion(X, thres, N, dim) # Fitness at first iteration fit = np.zeros([N, 1], dtype='float') Xalpha = np.zeros([1, dim], dtype='int') Xbeta = np.zeros([1, dim], dtype='int') Xdelta = np.zeros([1, dim], dtype='int') Falpha = float('inf') Fbeta = float('inf') Fdelta = float('inf') for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, X[i, :], opts) if fit[i, 0] < Falpha: Xalpha[0, :] = X[i, :] Falpha = fit[i, 0] if fit[i, 0] < Fbeta and fit[i, 0] > Falpha: Xbeta[0, :] = X[i, :] Fbeta = fit[i, 0] if fit[i, 0] < Fdelta and fit[i, 0] > Fbeta and fit[i, 0] > Falpha: Xdelta[0, :] = X[i, :] Fdelta = fit[i, 0] # Pre curve = np.zeros([1, max_iter], dtype='float') t = 0 curve[0, t] = Falpha.copy() print("Iteration:", t + 1) print("Best (TMGWO):", curve[0, t]) t += 1 while t < max_iter: # Coefficient decreases linearly from 2 to 0 (3.5) a = 2 - t * (2 / max_iter) for i in range(N): for d in range(dim): # Parameter C (3.4) C1 = 2 * rand() C2 = 2 * rand() C3 = 2 * rand() # Compute Dalpha, Dbeta & Ddelta (3.7 - 3.9) Dalpha = abs(C1 * Xalpha[0, d] - X[i, d]) Dbeta = abs(C2 * Xbeta[0, d] - X[i, d]) Ddelta = abs(C3 * Xdelta[0, d] - X[i, d]) # Parameter A (3.3) A1 = 2 * a * rand() - a A2 = 2 * a * rand() - a A3 = 2 * a * rand() - a # Compute X1, X2 & X3 (3.7 -3.9) X1 = Xalpha[0, d] - A1 * Dalpha X2 = Xbeta[0, d] - A2 * Dbeta X3 = Xdelta[0, d] - A3 * Ddelta # Update wolf (3.6) Xn = (X1 + X2 + X3) / 3 #--- transfer function Xs = transfer_function(Xn) #--- update position (4.3.2) if rand() < Xs: X[i, d] = 0 else: X[i, d] = 1 # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, X[i, :], opts) if fit[i, 0] < Falpha: Xalpha[0, :] = X[i, :] Falpha = fit[i, 0] if fit[i, 0] < Fbeta and fit[i, 0] > Falpha: Xbeta[0, :] = X[i, :] Fbeta = fit[i, 0] if fit[i, 0] < Fdelta and fit[i, 0] > Fbeta and fit[i, 0] > Falpha: Xdelta[0, :] = X[i, :] Fdelta = fit[i, 0] curve[0, t] = Falpha.copy() print("Iteration:", t + 1) print("Best (TMGWO):", curve[0, t]) t += 1 #--- two phase mutation: first phase # find index of 1 idx = np.where(Xalpha == 1) idx1 = idx[1] Xmut1 = np.zeros([1, dim], dtype='int') Xmut1[0, :] = Xalpha[0, :] for d in range(len(idx1)): r = rand() if r < Mp: Xmut1[0, idx1[d]] = 0 Fnew1 = Fun(xtrain, ytrain, Xmut1[0, :], opts) if Fnew1 < Falpha: Falpha = Fnew1 Xalpha[0, :] = Xmut1[0, :] #--- two phase mutation: second phase # find index of 0 idx = np.where(Xalpha == 0) idx0 = idx[1] Xmut2 = np.zeros([1, dim], dtype='int') Xmut2[0, :] = Xalpha[0, :] for d in range(len(idx0)): r = rand() if r < Mp: Xmut2[0, idx0[d]] = 1 Fnew2 = Fun(xtrain, ytrain, Xmut2[0, :], opts) if Fnew2 < Falpha: Falpha = Fnew2 Xalpha[0, :] = Xmut2[0, :] # Best feature subset Gbin = Xalpha[0, :] Gbin = Gbin.reshape(dim) pos = np.asarray(range(0, dim)) sel_index = pos[Gbin == 1] num_feat = len(sel_index) # Create dictionary tmgwo_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return tmgwo_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 N = opts['N'] max_iter = opts['T'] # 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') Xalpha = np.zeros([1, dim], dtype='float') Xbeta = np.zeros([1, dim], dtype='float') Xdelta = np.zeros([1, dim], dtype='float') Falpha = float('inf') Fbeta = float('inf') Fdelta = float('inf') for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < Falpha: Xalpha[0, :] = X[i, :] Falpha = fit[i, 0] if fit[i, 0] < Fbeta and fit[i, 0] > Falpha: Xbeta[0, :] = X[i, :] Fbeta = fit[i, 0] if fit[i, 0] < Fdelta and fit[i, 0] > Fbeta and fit[i, 0] > Falpha: Xdelta[0, :] = X[i, :] Fdelta = fit[i, 0] # Pre curve = np.zeros([1, max_iter], dtype='float') t = 0 curve[0, t] = Falpha.copy() print("Iteration:", t + 1) print("Best (GWO):", curve[0, t]) t += 1 while t < max_iter: # Coefficient decreases linearly from 2 to 0 a = 2 - t * (2 / max_iter) for i in range(N): for d in range(dim): # Parameter C (3.4) C1 = 2 * rand() C2 = 2 * rand() C3 = 2 * rand() # Compute Dalpha, Dbeta & Ddelta (3.5) Dalpha = abs(C1 * Xalpha[0, d] - X[i, d]) Dbeta = abs(C2 * Xbeta[0, d] - X[i, d]) Ddelta = abs(C3 * Xdelta[0, d] - X[i, d]) # Parameter A (3.3) A1 = 2 * a * rand() - a A2 = 2 * a * rand() - a A3 = 2 * a * rand() - a # Compute X1, X2 & X3 (3.6) X1 = Xalpha[0, d] - A1 * Dalpha X2 = Xbeta[0, d] - A2 * Dbeta X3 = Xdelta[0, d] - A3 * Ddelta # Update wolf (3.7) X[i, d] = (X1 + X2 + X3) / 3 # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < Falpha: Xalpha[0, :] = X[i, :] Falpha = fit[i, 0] if fit[i, 0] < Fbeta and fit[i, 0] > Falpha: Xbeta[0, :] = X[i, :] Fbeta = fit[i, 0] if fit[i, 0] < Fdelta and fit[i, 0] > Fbeta and fit[i, 0] > Falpha: Xdelta[0, :] = X[i, :] Fdelta = fit[i, 0] curve[0, t] = Falpha.copy() print("Iteration:", t + 1) print("Best (GWO):", curve[0, t]) t += 1 # Best feature subset Gbin = binary_conversion(Xalpha, 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 gwo_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return gwo_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 b = 1 # constant N = opts['N'] max_iter = opts['T'] if 'b' in opts: b = opts['b'] # 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] #--- Opposition based learning Xo = opposition_based_learning(X, lb, ub, thres, N, dim) #--- Binary conversion Xobin = binary_conversion(Xo, thres, N, dim) #--- Fitness fitO = np.zeros([N, 1], dtype='float') for i in range(N): fitO[i, 0] = Fun(xtrain, ytrain, Xobin[i, :], opts) if fitO[i, 0] < fitG: Xgb[0, :] = Xo[i, :] fitG = fitO[i, 0] #--- Merge opposite & current population, and select best N XX = np.concatenate((X, Xo), axis=0) FF = np.concatenate((fit, fitO), axis=0) #--- Sort in ascending order ind = np.argsort(FF, axis=0) for i in range(N): X[i, :] = XX[ind[i, 0], :] fit[i, 0] = FF[ind[i, 0]] # Pre curve = np.zeros([1, max_iter], dtype='float') t = 0 curve[0, t] = fitG.copy() print("Generation:", t + 1) print("Best (OBWOA):", curve[0, t]) t += 1 while t < max_iter: # Define a, linearly decreases from 2 to 0 (14) a = 2 - t * (2 / max_iter) for i in range(N): # Parameter A (13) A = 2 * a * rand() - a # Paramater C (13) C = 2 * rand() # Parameter r1, random number in [0,1] r1 = rand() # Parameter l, random number in [-1,1] l = -1 + 2 * rand() # Whale position update (15) if r1 < 0.5: # {1} Encircling prey if abs(A) < 1: for d in range(dim): # Compute D (12) Dx = abs(C * Xgb[0, d] - X[i, d]) # Position update (12) X[i, d] = Xgb[0, d] - A * Dx # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {2} Search for prey elif abs(A) >= 1: for d in range(dim): # Select a random whale k = np.random.randint(low=0, high=N) # Compute D (16) Dx = abs(C * X[k, d] - X[i, d]) # Position update (16) X[i, d] = X[k, d] - A * Dx # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {3} Bubble-net attacking elif r1 >= 0.5: for d in range(dim): # Distance of whale to prey (11) dist = abs(X[i, d] - Xgb[0, d]) # Position update (11) X[i, d] = dist * np.exp(b * l) * np.cos( 2 * np.pi * l) + Xgb[0, d] # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness 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] #--- Opposition based learning Xo = opposition_based_learning(X, lb, ub, thres, N, dim) #--- Binary conversion Xobin = binary_conversion(Xo, thres, N, dim) #--- Fitness fitO = np.zeros([N, 1], dtype='float') for i in range(N): fitO[i, 0] = Fun(xtrain, ytrain, Xobin[i, :], opts) if fitO[i, 0] < fitG: Xgb[0, :] = Xo[i, :] fitG = fitO[i, 0] # Store result curve[0, t] = fitG.copy() print("Generation:", t + 1) print("Best (OBWOA):", curve[0, t]) t += 1 #--- Merge opposite & current population, and select best N XX = np.concatenate((X, Xo), axis=0) FF = np.concatenate((fit, fitO), axis=0) #--- Sort in ascending order ind = np.argsort(FF, axis=0) for i in range(N): X[i, :] = XX[ind[i, 0], :] fit[i, 0] = FF[ind[i, 0]] # 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 obwoa_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return obwoa_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 w = 0.9 # inertia weight c1 = 2 # acceleration factor c2 = 2 # acceleration factor N = opts['N'] max_iter = opts['T'] if 'w' in opts: w = opts['w'] if 'c1' in opts: c1 = opts['c1'] if 'c2' in opts: c2 = opts['c2'] # 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, Vmax, Vmin = init_velocity(lb, ub, N, dim) # Pre fit = np.zeros([N, 1], dtype='float') Xgb = np.zeros([1, dim], dtype='float') fitG = float('inf') Xpb = np.zeros([N, dim], dtype='float') fitP = float('inf') * np.ones([N, 1], dtype='float') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i,0] = Fun(xtrain, ytrain, Xbin[i,:], opts) if fit[i,0] < fitP[i,0]: Xpb[i,:] = X[i,:] fitP[i,0] = fit[i,0] if fitP[i,0] < fitG: Xgb[0,:] = Xpb[i,:] fitG = fitP[i,0] # Store result curve[0,t] = fitG.copy() print("Iteration:", t + 1) print("Best (PSO):", curve[0,t]) t += 1 for i in range(N): for d in range(dim): # Update velocity r1 = rand() r2 = rand() V[i,d] = w * V[i,d] + c1 * r1 * (Xpb[i,d] - X[i,d]) + c2 * r2 * (Xgb[0,d] - X[i,d]) # Boundary V[i,d] = boundary(V[i,d], Vmin[0,d], Vmax[0,d]) # Update position X[i,d] = X[i,d] + V[i,d] # Boundary X[i,d] = boundary(X[i,d], lb[0,d], ub[0,d]) # 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 pso_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return pso_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 alpha = 2 # constant N = opts['N'] max_iter = opts['T'] if 'alpha' in opts: alpha = opts['alpha'] # 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) # Pre fit = np.zeros([N, 1], dtype='float') Xdb = np.zeros([1, dim], dtype='float') fitD = float('inf') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitD: Xdb[0, :] = X[i, :] fitD = fit[i, 0] # Store result curve[0, t] = fitD.copy() print("Iteration:", t + 1) print("Best (SCA):", curve[0, t]) t += 1 # Parameter r1, decreases linearly from alpha to 0 (3.4) r1 = alpha - t * (alpha / max_iter) for i in range(N): for d in range(dim): # Random parameter r2 & r3 & r4 r2 = (2 * np.pi) * rand() r3 = 2 * rand() r4 = rand() # Position update (3.3) if r4 < 0.5: # Sine update (3.1) X[i, d] = X[i, d] + r1 * np.sin(r2) * abs(r3 * Xdb[0, d] - X[i, d]) else: # Cosine update (3.2) X[i, d] = X[i, d] + r1 * np.cos(r2) * abs(r3 * Xdb[0, d] - X[i, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Best feature subset Gbin = binary_conversion(Xdb, 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 sca_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return sca_data
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 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
def jfs(xtrain, ytrain, opts): # Parameters ub = 1 lb = 0 thres = 0.5 beta = 1.5 # levy component N = opts['N'] max_iter = opts['T'] 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) # Pre fit = np.zeros([N, 1], dtype='float') Xrb = np.zeros([1, dim], dtype='float') fitR = float('inf') curve = np.zeros([1, max_iter], dtype='float') t = 0 while t < max_iter: # Binary conversion Xbin = binary_conversion(X, thres, N, dim) # Fitness for i in range(N): fit[i, 0] = Fun(xtrain, ytrain, Xbin[i, :], opts) if fit[i, 0] < fitR: Xrb[0, :] = X[i, :] fitR = fit[i, 0] # Store result curve[0, t] = fitR.copy() print("Iteration:", t + 1) print("Best (HHO):", curve[0, t]) t += 1 # Mean position of hawk (2) X_mu = np.zeros([1, dim], dtype='float') X_mu[0, :] = np.mean(X, axis=0) for i in range(N): # Random number in [-1,1] E0 = -1 + 2 * rand() # Escaping energy of rabbit (3) E = 2 * E0 * (1 - (t / max_iter)) # Exploration phase if abs(E) >= 1: # Define q in [0,1] q = rand() if q >= 0.5: # Random select a hawk k k = np.random.randint(low=0, high=N) r1 = rand() r2 = rand() for d in range(dim): # Position update (1) X[i, d] = X[k, d] - r1 * abs(X[k, d] - 2 * r2 * X[i, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) elif q < 0.5: r3 = rand() r4 = rand() for d in range(dim): # Update Hawk (1) X[i, d] = (Xrb[0, d] - X_mu[0, d]) - r3 * (lb[0, d] + r4 * (ub[0, d] - lb[0, d])) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # Exploitation phase elif abs(E) < 1: # Jump strength J = 2 * (1 - rand()) r = rand() # {1} Soft besiege if r >= 0.5 and abs(E) >= 0.5: for d in range(dim): # Delta X (5) DX = Xrb[0, d] - X[i, d] # Position update (4) X[i, d] = DX - E * abs(J * Xrb[0, d] - X[i, d]) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {2} hard besiege elif r >= 0.5 and abs(E) < 0.5: for d in range(dim): # Delta X (5) DX = Xrb[0, d] - X[i, d] # Position update (6) X[i, d] = Xrb[0, d] - E * abs(DX) # Boundary X[i, d] = boundary(X[i, d], lb[0, d], ub[0, d]) # {3} Soft besiege with progressive rapid dives elif r < 0.5 and abs(E) >= 0.5: # Levy distribution (9) LF = levy_distribution(beta, dim) Y = np.zeros([1, dim], dtype='float') Z = np.zeros([1, dim], dtype='float') for d in range(dim): # Compute Y (7) Y[0, d] = Xrb[0, d] - E * abs(J * Xrb[0, d] - X[i, d]) # Boundary Y[0, d] = boundary(Y[0, d], lb[0, d], ub[0, d]) for d in range(dim): # Compute Z (8) Z[0, d] = Y[0, d] + rand() * LF[d] # Boundary Z[0, d] = boundary(Z[0, d], lb[0, d], ub[0, d]) # Binary conversion Ybin = binary_conversion(Y, thres, 1, dim) Zbin = binary_conversion(Z, thres, 1, dim) # fitness fitY = Fun(xtrain, ytrain, Ybin[0, :], opts) fitZ = Fun(xtrain, ytrain, Zbin[0, :], opts) # Greedy selection (10) if fitY < fit[i, 0]: fit[i, 0] = fitY X[i, :] = Y[0, :] if fitZ < fit[i, 0]: fit[i, 0] = fitZ X[i, :] = Z[0, :] # {4} Hard besiege with progressive rapid dives elif r < 0.5 and abs(E) < 0.5: # Levy distribution (9) LF = levy_distribution(beta, dim) Y = np.zeros([1, dim], dtype='float') Z = np.zeros([1, dim], dtype='float') for d in range(dim): # Compute Y (12) Y[0, d] = Xrb[0, d] - E * abs(J * Xrb[0, d] - X_mu[0, d]) # Boundary Y[0, d] = boundary(Y[0, d], lb[0, d], ub[0, d]) for d in range(dim): # Compute Z (13) Z[0, d] = Y[0, d] + rand() * LF[d] # Boundary Z[0, d] = boundary(Z[0, d], lb[0, d], ub[0, d]) # Binary conversion Ybin = binary_conversion(Y, thres, 1, dim) Zbin = binary_conversion(Z, thres, 1, dim) # fitness fitY = Fun(xtrain, ytrain, Ybin[0, :], opts) fitZ = Fun(xtrain, ytrain, Zbin[0, :], opts) # Greedy selection (10) if fitY < fit[i, 0]: fit[i, 0] = fitY X[i, :] = Y[0, :] if fitZ < fit[i, 0]: fit[i, 0] = fitZ X[i, :] = Z[0, :] # Best feature subset Gbin = binary_conversion(Xrb, 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 hho_data = {'sf': sel_index, 'c': curve, 'nf': num_feat} return hho_data