d: 维度
lower: 下界
upper: 上界
def bat_algorithm(objfun, N_pop=20, N_gen=1000, A=0.5, r=0.5,
Qmin=0, Qmax=2, d=10, lower=-2, upper=2):
N_iter = 0 # Total number of function evaluations
#=速度上下限============
Lower_bound = lower * np.ones((1,d))
Upper_bound = upper * np.ones((1,d))
Q = np.zeros((N_pop, 1)) # 频率
v = np.zeros((N_pop, d)) # 速度
S = np.zeros((N_pop, d))
#=初始化种群、初始解===
Sol = np.random.uniform(Lower_bound, Upper_bound, (N_pop, d))
Fitness = objfun(Sol)
Sol = np.zeros((N_pop, d))
Fitness = np.zeros((N_pop, 1))
for i in range(N_pop):
Sol[i] = np.random.uniform(Lower_bound, Upper_bound, (1, d))
Fitness[i] = objfun(Sol[i])
#找出初始最优解=======
fmin = min(Fitness)
Index = list(Fitness).index(fmin)
best = Sol[Index]
#开始迭代=
for t in range(N_gen):
#====对所有蝙蝠/解决方案进行循环 ======
for i in range(N_pop):