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
| import numpy as np from numpy import random from copy import deepcopy from tqdm import tqdm import matplotlib.pyplot as plt import warnings warnings.filterwarnings("ignore") np.set_printoptions(threshold=np.inf) np.set_printoptions(suppress=True) plt.rcParams['axes.unicode_minus'] = False plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] def fun(X): x = X.flatten() fx = 0 for i in range(len(x)-1): a = x[i]**2 - 10*np.cos(2*np.pi*x[i]) + 10 fx += a fx += -7*x[-1] return fx
s = np.zeros((1,30)) sub = np.array(s-10).ravel() up = np.array(s+10).ravel() type = np.array(s).ravel()
def dd2(best_x, x): best_x = np.array(best_x) x = np.array(x) c = np.sum(pow(x - best_x, 2), axis=1) d = pow(c, 0.5) return d def new_min(arr): min_data = min(arr) key = np.argmin(arr) return min_data, key def type_x(xx,type,n): for v in range(n): if type[v] == -1: xx[v] = np.maximum(sub[v], xx[v]) xx[v] = np.minimum(up[v], xx[v]) elif type[v] == 0: xx[v] = np.maximum(sub[v], int(xx[v])) xx[v] = np.minimum(up[v], int(xx[v])) else: xx[v] = np.maximum(sub[v], random.randint(0,2)) xx[v] = np.minimum(up[v], random.randint(0,2)) return xx def woa(sub,up,type,nums,det): n = len(sub) num = nums * n x = np.zeros([num, n]) f = np.zeros(num) for s in range(num): for v in range(n): rand_data = np.random.uniform(0,1) x[s, v] = sub[v] + (up[v] - sub[v]) * rand_data x[s, :] = type_x(x[s, :],type,n) f[s] = fun(x[s, :]) best_f, a = new_min(f) best_x = x[a, :] trace = np.array([deepcopy(best_f)]) xx = np.zeros([num, n]) ff = np.zeros(num) Mc = (up - sub) * 0.1 for ii in tqdm(range(det)): d = dd2(best_x, x) d.sort() z = np.exp(-d[1] / np.mean(Mc)) z = max(z, 0.1) yx = [] dx = [] random_rand = random.random(n) for i in range(50): m = [random.choice([-1, 1]) for _ in range(n)] asd = best_x + Mc * z * ((det-ii )/det) * random_rand * m xd = type_x(asd,type,n) if i < 1: dx = deepcopy(xd) else: dx = np.vstack((dx,xd)) yx=np.hstack((yx,fun(xd))) best_t, a = new_min(yx) best_c = dx[a, :] if best_t < best_f: best_f = best_t best_x = best_c w = (ii / det)**3 a = (2 - 2*ii/det)*(1- w) pp=0.7 if ii <= 0.5*det else 0.4 for i in range(num): r1 = np.random.rand() r2 = np.random.rand() A = 2 * a * r1 - a C = 2 * r2 b = 1 l = np.random.uniform(-1,1) p = np.random.rand() if p < pp: if abs(A) >= 1: rand_leader = np.random.randint(0, num) X_rand = x[rand_leader, :] D_X_rand = abs(C * X_rand - x[i, :]) xx[i, :] = w*X_rand - A * D_X_rand xx[i, :] = type_x(xx[i, :],type,n) elif abs(A) < 1: D_Leader = abs(C * best_x - x[i, :]) xx[i, :] = w*best_x - A * D_Leader xx[i, :] = type_x(xx[i, :],type,n) elif p >= pp: D = abs(best_x - x[i, :]) xx[i, :] = D*np.exp(b*l)*np.cos(2*np.pi*l) + (1-w)*best_x xx[i, :] = type_x(xx[i, :],type,n) ff[i] = fun(xx[i, :]) if len(np.unique(ff[:i]))/(i+1) <= 0.1: xx[i,:] = (r1*(best_x-xx[i,:]) + r2*(x[np.random.randint(0,num),:] - xx[i,:])) xx[i, :] = type_x(xx[i, :],type,n) ff[i] = fun(xx[i, :]) F = np.hstack((np.array([best_f]), f, ff)) F, b = np.sort(F,axis=-1,kind='stable'), np.argsort(F) X = np.vstack(([best_x], x, xx))[b, :] f = F[:num] x = X[:num, :] best_f, a = new_min(f) best_x = x[a , :] trace = np.hstack((trace, [best_f])) return best_x,best_f,trace
best_x,best_f,trace = woa(sub,up,type,20,60)
print('最优解为:') print(best_x) print('最优值为:') print(float(best_f))
plt.title('鲸鱼算法') plt.plot(range(1,len(trace)+1),trace, color='r') plt.show()
|