果蝇优化算法:优化ZScore模型 python实现

果蝇优化算法:优化ZScore模型 python实现

数据集:

0.016 0.177 0.4208 1.038 0.022 0
1.0957 0.19 0.2224 0.816 0.933 1
0.2543 0.206 0.5264 0.4 0.015 1
1.2257 0.224 0.3272 1.05 1.049 1
0.3872 0.228 0.2256 0.98 0.998 1
1.6066 0.231 0.2832 1.054 1.009 1
1.1594 0.252 0.3344 0.946 0.987 0
0.3424 0.26 0.3408 0.196 0.126 1
0.8604 0.261 0.2616 0.994 0.996 0
0.5107 0.264 0.2352 0.888 0.004 1
0.8981 0.271 0.1536 0.688 0.857 1
0.0878 0.275 0.2072 0.732 0.908 1
0.2384 0.297 0.2336 0.036 0.099 0
0.8591 0.308 0.4208 0.132 1.031 1
0.5861 0.309 0.1656 0.836 0.896 1
0.6329 0.311 -0.24 -0.134 0.611 0
0.6173 0.311 0.1256 0.714 0.848 0
0.7811 0.326 0.2896 0.834 0.909 1
0.3937 0.329 0.2552 0.024 0.493 0
0.1211 0.055 0.3744 1.142 1.077 1

代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties


#######果蝇算法######
##初始化果蝇参数
popsize = 20        #果蝇种群规模
maxgen = 100        #果蝇最大迭代次数
R = 1               #果蝇飞行半径
D = 5              #优化变量个数

# 读取数据的函数
TXY = np.loadtxt('e:\\TXY.txt')
# 计算这个TXY举证的行数还有列数
row= np.shape(TXY)[0]            
col=np.shape(TXY)[1]

# 将函数来进行分块,然后来进行之后的操作
set = row/5
row1 = row - set

# tr和t1来得到最优z-score系数
tr = TXY[0:row,:col-1]
t1 = TXY[0:row,col-1]

t_value = TXY[:,col-1]

# te和t2来检查优化效果
te = TXY[int(row1):,:col-1]
t2 = TXY[0:int(row1),col-1]

# 计算这个距离的容量
Dist = np.zeros([popsize,D])
# 味道浓度判定值
S = np.zeros([popsize,D])
# 优化参数的结果存放值
bestS = np.zeros([1,D])
# 味道气味值
Smell = np.zeros([popsize,1])

#果蝇种群里面的每一个果蝇需要存放的一个坐标位置,有五个种群,进行5个参数来进行优化参数的作用
X = np.zeros([popsize,D])
Y = np.zeros([popsize,D])

# 每次迭代之后,保存的结果
fitness = np.zeros([maxgen,1])

# 需要计算的保存的最有果蝇的位置
XBest = np.zeros([maxgen,D])
YBest = np.zeros([maxgen,D])

#赋予果蝇群体初始位置,五个种群的位置
num=0
for i in range(row):
    res = TXY[i,0]*1.2+1.4*TXY[i,1]+3.3*TXY[i,2]+0.6*TXY[i,3]+1.0*TXY[i,4]
    if res>2.675:
        if int(t_value[i])==1:
            num+=1
    else:
        if int(t_value[i])==0:
            num+=1
pre = num/row
now=0

# 由于随机化的原因,收敛不到这个寻优的效果,然后现在是直接找到超过的,那么代表这个可以了
while True:
    #初始化种群的位置
    X_axis = np.random.rand(1,D)
    Y_axis = np.random.rand(1,D)
    #果蝇寻优开始,利用嗅觉寻找食物
    for i in range(popsize):
        X[i,:] = X_axis + R*(2*np.random.rand(1,D)-1)
        Y[i,:] = Y_axis + R*(2*np.random.rand(1,D)-1)
        #计算距离Dist
        Dist[i,:] = np.sqrt(X[i,:]**2+Y[i,:]**2)
        #计算味道浓度的倒数作为味道浓度判定值
        S[i,:] = 1/Dist[i,:]
        #带入味道浓度函数中求出味道浓度值
        yc = S[i,0] * tr[:,0] + S[i,1] * tr[:,1] + S[i,2]* tr[:,2] + S[i,3] * tr[:,3] +  S[i,4] * tr[:,4];
        yy = yc - t1;
        # 每5个果蝇飞行一次计算一次均方根误差
        g = 0
        for j in range(row):
            g = g + yy[j]**2;
        Smell[i] = (g/row)**0.5; #将每次的和原来数据的均方根误差,当作气味值
    #找出味道值最小的,即最接近预测结果的气味值,及其下标
    Smellbest,index = np.min(Smell),np.argmin(Smell)
    bestSmell = Smellbest
    #保留最佳味道浓度处的果蝇
    X_axis = X[int(index),:]
    Y_axis = Y[int(index),:]
    #果蝇种群进入迭代寻优
    for j in range(maxgen):
        for i in range(popsize):
            X[i,:] = X_axis + R*(2*np.random.rand(1,D)-1)
            Y[i,:] = Y_axis + R*(2*np.random.rand(1,D)-1)
            #计算距离Dist
            Dist[i,:] = np.sqrt(X[i,:]**2+Y[i,:]**2)
            #计算味道浓度的倒数作为味道浓度判定值
            S[i,:] = 1/Dist[i,:]
            #带入味道浓度函数中求出味道浓度值
            yc = S[i,0] * tr[:,0] + S[i,1] * tr[:,1] + S[i,2]* tr[:,2] + S[i,3] * tr[:,3] +  S[i,4] * tr[:,4];
            yy = yc - t1;
            g = 0
            for k in range(row):
                g = g + yy[k]**2;
            Smell[i] = (g/row)**0.5; 

        Smellbest,index = np.min(Smell),np.argmin(Smell)
        if Smellbest < bestSmell:
            bestSmell = Smellbest
            bestS = S[index]
            X_axis = X[int(index),:]
            Y_axis = Y[int(index),:]
        fitness[j] = bestSmell
        XBest[j] = X_axis
        YBest[j] = Y_axis
    #计算当前预测的一个过程,
    num=0
    for i in range(row):
        res = TXY[i,0]*bestS[0]+bestS[1]*TXY[i,1]+bestS[2]*TXY[i,2]+bestS[3]*TXY[i,3]+bestS[4]*TXY[i,4]
        if res>0.5:
            if int(t_value[i])==1:
                num+=1
        else:
            if int(t_value[i])==0:
                num+=1 
    now = num/row
    if now>pre:
        break;

print("最后迭代的趋近值RMSE:")
print(bestSmell)
print("优化之后的参数:")
print(bestS)

print('之前的预测正确率:')
print(pre)
print('之后的预测正确率:')
print(now)




font_set = FontProperties(fname=r"c:\\windows\\fonts\\simsun.ttc", size=15)
plt.figure(1)
plt.plot(range(maxgen),fitness)
plt.xlabel('迭代次数',fontproperties=font_set)
plt.ylabel('RMSE',fontproperties=font_set)

plt.figure(2)
plt.plot(XBest,YBest,'r*')
plt.xlabel(u'X_axis',fontproperties=font_set)
plt.ylabel(u'Y_axis',fontproperties=font_set)
plt.show()

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值