体智能算法-北极熊算法(PBO)-python代码实现附详细注释
import numpy as nppython基础代码注释
arraylist排序sort对象import random
import matplotlib.pyplot as plt
np.set_printoptions(suppress=False)
'''
北极熊算法
浮冰漂移:全局搜索
具体捕猎:局部搜索
author:sugarMei
date:6/25
version:1.1
'''
# 全局参数设定
'''
M:种最⼤容量
m:种当前规模⼤⼩
N:⽬标函数解的维度
T:迭代次数
V:最⼤视野距离
X:解集合
P:初始化种数量百分⽐
模拟串口使用printf函数
smart分析案例范文K:动态调整规模参数
B:变量限制范围
'''
绿对照表图片大全# 问题1:迭代陷于局部最优
M, N, T, V, P, K, B =100,20,100,0.3,0.75,0.25,[-10,10]
m =int(np.floor(M * P))
X = np.zeros((M, N))
# Generate a population consisting of 75%M bears at random
# 论⽂提出的基础的算法是random 随机⽣成的
def Func(x):
# Rastragin f4
# return np.sum(x[:] ** 2 - 10 * np.cos(2 * np.pi * x[:]) + 10)
# weierstrass
# return np.sum((x + 0.5) ** 2)
# Sphere f9
# return np.sum(x ** 2)
# Dixon-Price f1
return(x[0]-1)**2+sum((i +1)*(2* x[i]**2- x[i -1])**2for i in range(1,len(x)))
# Griewank f2
# return
# Rosenbrock f5
# Styblinski - Tang f11
# return
# Weierstrass f12
# return 0.5 * np.sum(x ** 4 - 16 * (x ** 2) + 5 * x)
fits = np.zeros(M)
def move(index, angle, radius, action):
# print('index, angle, radius, action, N', index, angle, radius, action, N)
temp = np.zeros(N)
for j in range(N):
# print("j:", j)
result =0
# 计算sin部分的累加值
for k in range(j):
result += np.sin(angle[k])
if'add'.__eq__(action):
if'add'.__eq__(action):
temp[j]= X[index][j]+ radius *(result + np.cos(angle[j])) else:
temp[j]= X[index][j]- radius *(result + np.cos(angle[j])) return temp
res = np.zeros(50)
# main loop
for _ in range(1):
# 初始化
m =int(np.floor(M * P))
X = np.zeros((M, N))
# 随机初始化
X[:m]= np.random.random((m, N))*(B[1]- B[0])+ B[0]
f_best = np.inf
x_best =0
for i in range(m):
fits[i]= Func(X[i])
arg = np.argsort(fits[:m])
if fits[arg[0]]< f_best:
f_best = fits[arg[0]]
x_best = X[arg[0]]
# 测试⼀次迭代T次
t =0
while t < T:
# area search
# 视野半径后期变⼤跳出局部最优
# 对种每⼀只北极熊
for i in range(m):
# 局部搜索
angles = np.random.random(N)*2* np.pi
sta = np.random.random()* V
angle0 = np.random.random()* np.pi /2
r =4* sta * np.cos(angle0)* np.sin(angle0)
# using the sign of add
视图定义tmp = move(i, angles, r,'add')
if Func(tmp)< Func(X[i]):
X[i]= tmp
else:
tmp = move(i, angles, r,'minus')
if Func(tmp)< Func(X[i]):
X[i]= tmp
# 全局搜索
# 决策北极熊的位置
w = np.sqrt(np.sum((x_best - X[i])**2))
alpha = np.random.random()
gamma = np.random.random()* w
tmp = X[i]+ np.sign(w)* alpha + gamma
if Func(tmp)< Func(X[i]):
X[i]= tmp
# 统计当前适应度值
for i in range(m):
fits[i]= Func(X[i])
arg = np.argsort(fits[:m])
if fits[arg[0]]< f_best:
f_best = fits[arg[0]]
x_best = X[arg[0]]
# 种的动态变化
k = np.random.random()
k = np.random.random()
# 种繁衍
if m < M -1and k >(1- K):
# 从前10%中随机抽取⼀个个体与最好个体交配⽣成新的个体
idx = random.randint(1, np.floor(m *0.1)+1)
X[m]=(x_best + X[arg[idx]])/2
m +=1
# 最弱个体死亡
if m > M *0.5and k < K:
# arg中排序最后的为最弱个体
# 死亡之后后⾯的所有个体往前移动⼀个位置
X[arg[-1]:m]= X[arg[-1]+1:m +1]
m -=1
print(t," 当前最⼩适应度值:", f_best,'个体为:', x_best,'种数量:', m) t +=1
print("第", _,'次测试',"最⼩适应度值:", f_best,'个体为:', x_best)
res[_]= f_best
print('100个体、100次迭代、其他参数⼀致')
print('f4 函数的测试平均结果为:', np.mean(res))
print('标准⽅差:', np.std(res, ddof=1))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论