猎人猎物优化算法
猎人猎物优化算法
连续空调间的优化算法
import numpy as np
from tqdm import tqdm # 进度条设置
import random
from matplotlib import rcParams
import math
import matplotlib; matplotlib.use('TkAgg')
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
config = {
"font.family": 'serif',
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
class HPO(object):
def __init__(self, m, T, lb, ub, R, C):
self.M= m # 种群个数
self.T = T # 迭代次数
self.lb = lb
self.ub = ub
self.R = R #行
self.C = C #列
self.b = 0.1 #调节参数
def init_x(self):
x = np.random.uniform(self.lb, self.ub, (self.M, self.R, self.C))
return x
def fitness(self,x):
"""
(x1-50)**2+(x2-50)**2
:param x:
:return:
"""
# result = (x[0] - 50) ** 2 + (x[1] - 50) ** 2
result = x[:, :, 0] ** 2 + x[:, :, 1] ** 2
return result
def main(self):
x = self.init_x()
fitness = self.fitness(x)
fitness_best = fitness.min()
x_num = list(fitness).index(fitness_best)
x_best = x[x_num]
fitness_best_list = []
fitness_best_list.append(fitness_best)
for it in range(self.T):
C = 1-it*(0.98/self.T)
R1 = np.random.choice([0, 1], (self.M, self.R, self.C))
R3 = np.random.choice([0, 1], (self.M, self.R, self.C))
R2 = np.random.rand(self.M)
kbest = round(self.M*C)
#R1 IDX Z
R1 = R1-C
R1[R1 <=0] = 0
R1[R1 > 0] = 1
# R_IDX (p==0) ~IDX = R1
R_IDX = R1.copy()
R_IDX = 1-R_IDX
z = np.zeros((self.M, self.R, self.C))
for i in range(self.M):
z[i] = R2[i] * R_IDX[i]
z += R3*R1
# #u
# u=np.zeros((self.R,self.C))
# for i in range(self.M):
# u += x[i]
# u = u/self.M
#
# #Deuc
# Deuc_par = x-u
# # print(Deuc_par)
# Deuc = np.zeros(self.M)
# for i in range(self.M):
# Deuc[i] = np.sum(Deuc_par[i]**2)
# Deuc = np.sqrt(Deuc)
# max_index = np.argmax(Deuc)
# P_pos = x[max_index]
# x_new = np.zeros((self.M, self.R, self.C))
# x_new=x+0.5*(2*C*z*P_pos-x+2*(1-C)*z*u-x)
#
# #kbest
# kbest=round(self.M*C)
R5 = np.random.rand()
x_new = np.zeros((self.M, self.R, self.C))
if 0<self.b:
# u
u = np.zeros((self.R, self.C))
for i in range(self.M):
u += x[i]
u = u / self.M
# Deuc
Deuc_par = x - u
# print(Deuc_par)
Deuc = np.zeros(self.M)
for i in range(self.M):
Deuc[i] = np.sqrt(np.sum(Deuc_par[i] ** 2))
kbest = round(self.M * C)
Deuc_max = np.max(sorted((Deuc)[:kbest]))
Deuc = Deuc[:kbest]
num_par = np.where(Deuc == Deuc_max)
num = num_par[0][0]
P_pos = x[num]
x_new = x + 0.5 * (2 * C * z * P_pos - x + 2 * (1 - C) * z * u - x)
else:
R4 = np.random.rand()
for i in range(self.M):
x_new[i] = x_best+C*z*np.cos(2*np.pi*R4)*(x_best-x[i])
x = x_new
fitness = self.fitness(x)
fitness_best_new = fitness.min()
if fitness_best_new <= fitness_best:
fitness_best = fitness_best_new
x_num = list(fitness).index(fitness_best)
x_best = x[x_num]
else:
fitness_best = fitness_best
fitness_best_list.append(fitness_best)
print(fitness_best)
plt.plot(fitness_best_list, label='迭代曲线')
plt.xlabel('迭代次数')
plt.ylabel('适应度')
plt.legend()
plt.show()
if __name__ == '__main__':
hpo = HPO(5,10,-10,10,1,2)
hpo.main()