xold = initialization(SearchAgents_no, dim, ub, lb).T
ng = SearchAgents_no
tmax = Max_iteration
nv = dim
xav = np.mean(xold, axis=0)
fav = fobj(xav)
fold = np.apply_along_axis(fobj, 1, xold)
cg_curve = np.zeros(Max_iteration)
fitness_values_history = []
evaluations_history = []
for t in range(Max_iteration):
if 1 - t / tmax >= 0.89:
fb = 1 - (t / tmax) * np.exp(-t / tmax)
elif 0.55 <= 1 - t / tmax < 0.89:
fb = (1 - 2 / 3 * (t / tmax) * np.exp(-t / tmax)) * np.exp(-t / tmax)
elif 0.34 <= 1 - t / tmax < 0.55:
fb = (1 - 5 / 8 * (t / tmax) * np.exp(-t / tmax)) * np.exp(-t / tmax)
elif 0.21 <= 1 - t / tmax < 0.34:
fb = (1 - 3 / 8 * (t / tmax) * np.exp(-t / tmax)) * np.exp(-t / tmax)
elif 0.13 <= 1 - t / tmax < 0.21:
fb = (1 - 2 / 8 * (t / tmax) * np.exp(-t / tmax)) * np.exp(-t / tmax)
else:
fb = (1 - 1 / 13 * (t / tmax) * np.exp(-t / tmax)) * np.exp(-t / tmax)
a = np.argsort(fold)
if fav < fold[a[ng - 1]]:
fold[a[ng - 1]] = fav
xold[a[ng - 1], :] = xav
xav = np.mean(xold, axis=0)
fav = fobj(xav)
u = np.random.permutation(ng)
xnew = np.zeros((ng, nv))
fnew = np.zeros((ng, 1))
for r in range(ng):
if fold[u[0, r]] >= fav and fav >= fold[r]:
for i in range(nv):
xnew[r, i] = xold[r, i] + fb * np.random.rand() * (xav[i] - xold[u[0, r], i] - xold[r, i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
elif fold[u[0, r]] >= fold[r] and fold[r] >= fav:
for i in range(nv):
xnew[r, i] = xav[i] + fb * np.random.rand() * (xold[r, i] - xold[u[0, r], i] - xav[i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
elif fav >= fold[r] and fold[r] >= fold[u[0, r]]:
for i in range(nv):
xnew[r, i] = xold[u[0, r], i] + fb * np.random.rand() * (xold[r, i] - xav[i] - xold[u[0, r], i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
elif fav >= fold[u[0, r]] and fold[u[0, r]] >= fold[r]:
for i in range(nv):
xnew[r, i] = xold[r, i] + fb * np.random.rand() * (xold[u[0, r], i] - xav[i] - xold[r, i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
elif fold[r] >= fold[u[0, r]] and fold[u[0, r]] >= fav:
for i in range(nv):
xnew[r, i] = xav[i] + fb * np.random.rand() * (xold[u[0, r], i] - xold[r, i] - xav[i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
elif fold[r] >= fav and fav >= fold[u[0, r]]:
for i in range(nv):
xnew[r, i] = xold[r, i] + fb * np.random.rand() * (xav[i] - xold[u[0, r], i] - xold[r, i]) / 3
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
fnew[r] = fobj(xnew[r])
if fnew[r] > fold[r]:
fnew[r] = fold[r]
xnew[r] = xold[r]
fold = fnew
xold = xnew
a = np.argsort(fold)
for r in range(ng):
for i in range(nv):
yu = 0.62 * np.random.rand()
y = yu * (-xold[a[ng - 1], i] + xold[a[0], i])
xnew[r, i] = xold[r, i] + y
# check upper and lower bounds
if xnew[r, i] > ub[0, i]:
xnew[r, i] = ub[0, i]
if xnew[r, i] < lb[0, i]:
xnew[r, i] = lb[0, i]
fnew[r] = fobj(xnew[r])
if fnew[r] > fold[r]:
fnew[r] = fold[r]
xnew[r] = xold[r]
fold = fnew
xold = xnew
a = np.argsort(fold)
cg_curve[t] = a[0]
print('At iteration', t, 'the elite fitness is', a[0])
Best_pos = xold[a[0]]
Best_score = a[0]
fitness_values_history.append(a[0])
evaluations_history.append(t * ng)
return evaluations_history,fitness_values_history, Best_pos, Best_score检查代码