📄 getstbests.m
字号:
% 函数功能: 返回输入种群中的全局最优解及其个数,粒子编号改为 -1
% |--注意:输入的种群必须按照 适应度值 升序排列
function [out_bests, out_bCount]=getStBests(in_stPop, in_comInd, in_jobCount, in_dCInd, in_nDInd, in_fitInd, in_pBInd)
% 输入参数:
% |--in_stPop: 要得到全局最优解的种群,包括解的全部信息 ((popSize + gBCount) × parInfSize)维
% |--in_comInd: 比较的位置
% |--in_jobCount: 作业个数,应等于主函数中的 jobCount
% |--in_dCInd: 被优越数在记录粒子信息的一维数组中的位置,应等于主函数中的 dCInd
% |--in_nDInd: 邻近密度在记录粒子信息的一维数组中的位置,应等于主函数中的 nDInd
% |--in_fitInd: 适应度值在记录粒子信息的一维数组中的位置,应等于主函数中的 fitInd
% |--in_pBInd: 粒子编号在记录粒子信息的一维数组中的位置,应等于主函数中的 pBInd
% 返回参数:
% |--out_bests: 全局最优解,(out_bCount × parInfSize)维
% |--out_bCount: 全局最优解的个数,视具体情况而定
[f_pSize, f_parInfSize]= size(in_stPop);
% in_pBInd = in_fitInd + 1; % 粒子号数在这里没用,在替换差解时用来判断是否是gBest,是就不替换该位置
out_bests(1, 1: in_fitInd) = in_stPop(1, 1: in_fitInd);% 默认排序后 适应度值 最低的为全局最优解
out_bests(1, in_pBInd) = -1;
out_bCount = 1;
%%------------------------------------------%%
% 选出所有的非劣解,判断条件为 适应度值 为 第一个粒子的适应度
stPi=2; % 起始位置
% gBf=in_stPop(1,in_fitInd);
f_bestCom = in_stPop(1, in_comInd); % 最优解的 适应度
while (stPi <= f_pSize ) && (in_stPop(stPi, in_comInd) == f_bestCom) % 在前几个中搜索,只适用于按 适应度值 排完序的
serPi = 1;
findFlag = 0; % 表示没找到相同的序列
while (serPi <= out_bCount) && (findFlag == 0)
if (in_stPop(stPi,1: in_jobCount) == out_bests(serPi, 1: in_jobCount) )
findFlag = 1;
end
serPi = serPi + 1;
end
if (findFlag == 0) && (in_stPop(stPi, in_dCInd) == 0) % 该粒子还没进入 全局最优解,且不是劣解
out_bCount = out_bCount+1;
out_bests(out_bCount, 1: in_fitInd)=in_stPop(stPi, 1: in_fitInd);
out_bests(out_bCount, in_pBInd) = -1;
end
stPi = stPi + 1;
end
% % % % % %%------------------------------------------%%
% % % % % % 根据邻近密度,转化适应度为选择压力,邻近密度越小,即个体越优,选择压力越大
% % % % % % 为防止邻居数为0时,选择概率为100%,统一修正加1
% % % % % f_nDensity = -ones(out_bCount, 1);
% % % % % f_nDensity(:, 1) = out_bests(:, in_nDInd) + 1;
% % % % % f_nDSum = sum(f_nDensity);
% % % % % for (f_gBi = 1: out_bCount)
% % % % % out_bests(f_gBi, in_fitInd) = (f_nDSum - out_bests(f_gBi, in_nDInd)) / f_nDSum;
% % % % % end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -