"""
快速非支配排序
"""
import random
import numpy as np
def nonDominationSort(pops, fits):
"""快速非支配排序算法
Params:
pops: 种群,nPop * nChr 数组
fits: 适应度, nPop * nF 数组
Return:
ranks: 每个个体所对应的等级,一维数组
"""
nPop = pops.shape[0]
nF = fits.shape[1] # 目标函数的个数
ranks = np.zeros(nPop, dtype=np.int32)
nPs = np.zeros(nPop) # 每个个体p被支配解的个数
sPs = [] # 每个个体支配的解的集合,把索引放进去
for i in range(nPop):
iSet = [] # 解i的支配解集
for j in range(nPop):
if i == j:
continue
isDom1 = fits[i] <= fits[j]
isDom2 = fits[i] < fits[j]
# 是否支配该解-> i支配j
if sum(isDom1) == nF and sum(isDom2) >= 1:
iSet.append(j)
# 是否被支配-> i被j支配
if sum(~isDom2) == nF and sum(~isDom1) >= 1:
nPs[i] += 1
sPs.append(iSet) # 添加i支配的解的索引
r = 0 # 当前等级为 0, 等级越低越好
indices = np.arange(nPop)
while sum(nPs==0) != 0:
rIdices = indices[nPs==0] # 当前被支配数为0的索引
ranks[rIdices] = r
for rIdx in rIdices:
iSet = sPs[rIdx]
nPs[iSet] -= 1
nPs[rIdices] = -1 # 当前等级的被支配数设置为负数
r += 1
return ranks
# 拥挤度排序算法
def crowdingDistanceSort(pops, fits, ranks):
"""拥挤度排序算法
Params:
pops: 种群,nPop * nChr 数组
fits: 适应度, nPop * nF 数组
ranks:每个个体对应的等级,一维数组
Return:
dis: 每个个体的拥挤度,一维数组
"""
nPop = pops.shape[0]
nF = fits.shape[1] # 目标个数
dis = np.zeros(nPop)
nR = ranks.max() # 最大等级
indices = np.arange(nPop)
for r in range(nR+1):
rIdices = indices[ranks==r] # 当前等级种群的索引
rPops = pops[ranks==r] # 当前等级的种群
rFits = fits[ranks==r] # 当前等级种群的适应度
rSortIdices = np.argsort(rFits, axis=0) # 对纵向排序的索引
rSortFits = np.sort(rFits,axis=0)
fMax = np.max(rFits,axis=0)
fMin = np.min(rFits,axis=0)
n = len(rIdices) # 当前等级的个体数量
for i in range(nF):
orIdices = rIdices[rSortIdices[:,i]] # 当前操作元素的原始位置
j = 1
while n > 2 and j < n-1:
if fMax[i] != fMin[i]:
dis[orIdices[j]] += (rSortFits[j+1,i] - rSortFits[j-1,i]) / \
(fMax[i] - fMin[i])
else:
dis[orIdices[j]] = np.inf
j += 1
dis[orIdices[0]] = np.inf
dis[orIdices[n-1]] = np.inf
return dis
# 测试非支配排序算法的正确性
if __name__ == "__main__":
y1 = np.arange(1,5).reshape(4,1)
y2 = 5 - y1
fit1 = np.concatenate((y1,y2),axis=1)
y3 = 6 - y1
fit2 = np.concatenate((y1,y3),axis=1)
y4 = 7 - y1
fit3 = np.concatenate((y1,y4),axis=1)
fit3 = fit3[:2]
fits = np.concatenate((fit1,fit2,fit3), axis=0)
pops = np.arange(fits.shape[0]).reshape(fits.shape[0],1)
random.seed(123)
# 打乱数组
indices = np.arange(fits.shape[0])
random.shuffle(indices)
fits = fits[indices]
pops = pops[indices]
print(indices)
# 首先测试非支配排序算法
ranks = nonDominationSort(pops, fits)
print('ranks:', ranks)
# 测试拥挤度排序算法
dis = crowdingDistanceSort(pops,fits,ranks)
print("dis:", dis)
没有合适的资源?快使用搜索试试~ 我知道了~
三种多目标优化算法pythonNSGA2 MOPSO MODE
共28个文件
py:28个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 54 浏览量
2024-03-26
14:09:59
上传
评论 1
收藏 24KB ZIP 举报
温馨提示
三种多目标优化算法【python】NSGA2 MOPSO MODE 三种多目标优化算法【python】NSGA2 MOPSO MODE 三种多目标优化算法【python】NSGA2 MOPSO MODE 三种多目标优化算法【python】NSGA2 MOPSO MODE 三种多目标优化算法【python】NSGA2 MOPSO MODE
资源推荐
资源详情
资源评论
收起资源包目录
三种多目标优化算法【python】.zip (28个子文件)
三种多目标优化算法【python】
NSGA2
nonDominationSort.py 4KB
select1.py 938B
optSelect.py 1KB
NSGA2.py 2KB
crossover.py 997B
initPops.py 155B
function.py 1KB
fitness.py 487B
test.py 3KB
mutate.py 604B
MODE
nonDominationSort.py 4KB
select1.py 1004B
crossover.py 516B
MODE.py 2KB
function.py 1KB
fitness.py 533B
test.py 3KB
initPop.py 185B
mutate.py 583B
MOPSO
updatePBest.py 442B
getGBest.py 3KB
updateArchive.py 2KB
initPops.py 745B
MOPSO.py 2KB
function.py 926B
fitness.py 530B
test.py 3KB
checkArchive.py 1KB
共 28 条
- 1
资源评论
资源存储库
- 粉丝: 4503
- 资源: 392
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 筷手引流工具.apk
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功