import numpy as np
from ypstruct import structure
def run(problem, params):
# 函数信息
costfunc = problem.costfunc
nvar = problem.nvar
varmin = problem.varmin
varmax = problem.varmax
# 参数信息
maxit = params.maxit
npop = params.npop
nonlooker = params.nonlooker
limit = int(np.round(0.6*nvar*npop))
a = params.a
# 空的蜂群结构
empty_bee = structure()
empty_bee.position = None
empty_bee.cost = None
# 临时蜂群结构
newbee = structure()
newbee.position = None
newbee.cost = None
# 初始化全局最优解
bestsol = empty_bee.deepcopy()
bestsol.cost = np.inf
# 种群初始化
pop = empty_bee.repeat(npop)
for i in range(npop):
pop[i].position = np.random.uniform(varmin, varmax, nvar)
pop[i].cost = costfunc(pop[i].position)
if pop[i].cost < bestsol.cost:
bestsol = pop[i].deepcopy()
# 初始化每个个体的抛弃次数
count = np.empty(npop)
# 记录每一代中全局最优个体目标函数值
bestcost = np.empty(maxit)
# 人工蜂群算法主循环
for it in range(maxit):
# 引领蜂
for i in range(npop):
# 随机选择k,不等于i
K = np.append(np.arange(0,i),np.arange(i+1,npop))
k = K[np.random.randint(K.size)]
# 定义加速系数
phi = a * np.random.uniform(-1, 1, nvar)
# 新的蜜蜂位置
newbee.position = pop[i].position + phi * (pop[i].position - pop[k].position)
# 计算新蜜蜂目标函数值
newbee.cost = costfunc(newbee.position)
# 通过比较目标函数值,更新第i个蜜蜂的位置
if newbee.cost < pop[i].cost:
pop[i] = newbee.deepcopy()
else:
count[i] += 1
# 计算适应度值和选择概率
fit = np.empty(npop)
meancost = np.mean([pop[i].cost for i in range(npop)])
for i in range(npop):
fit[i] = np.exp(-pop[i].cost/meancost) #将目标函数值转换为适应度值
probs = fit / np.sum(fit)
# 跟随蜂
for m in range(nonlooker):
# 通过轮盘赌的方式选择蜜源
i = roulette_wheel_selection(probs)
# 随机选择k,不等于i
K = np.append(np.arange(0, i), np.arange(i + 1, npop))
k = K[np.random.randint(K.size)]
# 定义加速系数
phi = a * np.random.uniform(-1, 1, nvar)
# 新的蜜蜂位置
newbee.position = pop[i].position + phi * (pop[i].position - pop[k].position)
# 计算新蜜蜂目标函数值
newbee.cost = costfunc(newbee.position)
# 通过比较目标函数值,更新第i个蜜蜂的位置
if newbee.cost < pop[i].cost:
pop[i] = newbee.deepcopy()
else:
count[i] += 1
# 侦察蜂
for i in range(npop):
if count[i] > limit:
pop[i].position = np.random.uniform(varmin, varmax, nvar)
pop[i].cost = costfunc(pop[i].position)
count[i] = 0
# 更新全局最优解
for i in range(npop):
if pop[i].cost < bestsol.cost:
bestsol = pop[i].deepcopy()
# 存储每一代全局最优解的目标函数值
bestcost[it] = bestsol.cost
# 展示迭代信息
print("Iteration {}: Best Cost = {}".format(it, bestcost[it]))
# 返回值
out = structure()
out.pop = pop
out.bestsol = bestsol
out.bestcost = bestcost
return out
def roulette_wheel_selection(p):
c = np.cumsum(p)
r = sum(p) * np.random.rand()
ind = np.argwhere(r <= c)
return ind[0][0]
【人工蜂群算法】人工蜂群算法附Python完整代码
149 浏览量
2023-07-19
20:30:02
上传
评论
收藏 2KB ZIP 举报
希望早日退休的程序猿
- 粉丝: 300
- 资源: 51
最新资源
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
- for daily read
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈