|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
#技术风云榜#基于非支配排序的多目标PSO算法MATLAB实现
* W6 m j( a& D$ E$ l( K6 \" {$ f" C- b! @& ]1 r+ L" A
这一篇是Xue Bing在一区cybernetics发的论文,里面提出了两个多目标PSO特征选择算法,一个是NSPSO另一个是CMDPSO。
/ d3 Y: Y7 Z' u
T/ r! H7 j( s' G0 p& q伪代码
" j( L+ U: k% G" J7 W, p! t1 A! r5 `( }5 R
. \# V a+ x$ }0 ], q
0 ?4 P1 [* v2 d* [0 S4 W: `6 i! I5 p0 w7 Z8 T0 ^
具体流程) e }- P4 W6 B1 \% O4 ]# Z
- ①划分数据集为测试集和训练集
- ②初始化PSO算法
- ③迭代开始
- ④计算两个目标值(论文中是特征数和错误率)
- ⑤非支配排序
- ⑥拥挤距离度量并排序
- ⑥对每个粒子从第一前沿面选择一个粒子作为gbest,更新当前粒子
- ⑦调整粒子群
- ⑧迭代结束返回! }, s w9 |( u' Z& I) d4 Q# _
# f! @9 ]0 ^' T6 ]
MATLAB实现:9 ?* F8 G4 K8 F% ]5 t
NSPSO:3 A/ k0 X- n7 X% c2 ~
2 \5 A; X9 v, R
注意其中FSKNN是我的问题的评价函数,包含两个目标值,都存入到pfitness中
8 h! T5 }1 T" B, ^, G% {
( f, X. \& K* \9 O: o7 p7 U2 ]- function [solution,time,pop,pfitness,site,LeaderAVE] = NSPSO(train_F,train_L)
- tic
- global maxFES
- dim = size(train_F,2);
- FES = 1;
- sizep = 30;
- pop = rand(sizep,dim);
- popv = rand(sizep,dim);
- pfitness = zeros(sizep,2);
- LeaderAVE = zeros(1,2);
- while FES <maxFES
- Off_P = zeros(sizep,dim);
- Off_V = zeros(sizep,dim);
- ofitness = zeros(sizep,2);
- for i=1:sizep
- [pfitness(i,1),pfitness(i,2)] = FSKNN(pop(i,:),i,train_F,train_L);
- end
- Front = NDSort(pfitness(:,1:2),sizep);
- [~,rank] = sortrows([Front',-CrowdingDistance(pfitness,Front)']);
- LeaderSet = rank(1:10);
- solution = pfitness(LeaderSet,:);
- LeaderAVE(1) = mean(solution(:,1));
- LeaderAVE(2) = mean(solution(:,2));
- for i = 1:sizep
- good = LeaderSet(randperm(length(LeaderSet),1));
- r1 = rand(1,dim);
- r2 = rand(1,dim);
- Off_V(i,:) = r1.*popv(i,:) + r2.*(pop(good,:)-pop(i,:));
- Off_P(i,:) = pop(i,:) + Off_V(i,:);
- end
- for i=1:sizep
- [ofitness(i,1),ofitness(i,2)] = FSKNN(Off_P(i,:),i,train_F,train_L);
- end
- temppop = [pop;Off_P];
- tempv = [popv;Off_V];
- tempfiness = [pfitness;ofitness];
- [FrontNO,MaxFNO] = NDSort(tempfiness(:,1:2),sizep);
- Next = false(1,length(FrontNO));
- Next(FrontNO<MaxFNO) = true;
- PopObj = tempfiness;
- fmax = max(PopObj(FrontNO==1,:),[],1);
- fmin = min(PopObj(FrontNO==1,:),[],1);
- PopObj = (PopObj-repmat(fmin,size(PopObj,1),1))./repmat(fmax-fmin,size(PopObj,1),1);
- % Select the solutions in the last front
- Last = find(FrontNO==MaxFNO);
- del = Truncation(PopObj(Last,:),length(Last)-sizep+sum(Next));
- Next(Last(~del)) = true;
- % Population for next generation
- pop = temppop(Next,:);
- popv = tempv(Next,:);
- pfitness = tempfiness(Next,:);
- fprintf('GEN: %2d Error: %.4f F:%.2f\n',FES,LeaderAVE(1),LeaderAVE(2));
- FES = FES + 1;
- end
- [FrontNO,~] = NDSort(pfitness(:,1:2),sizep);
- site = find(FrontNO==1);
- solution = pfitness(site,:);
- LeaderAVE(1) = mean(solution(:,1));
- LeaderAVE(2) = mean(solution(:,2));
- toc
- time = toc;
- end
c( ]% j; W2 V
2 x- T( Q4 M4 j" _6 O$ e* i5 ]# o) j* `8 ~$ e$ {# v6 k
拥挤距离代码:
' H, H& x5 U# s' |6 {" a) n/ j! i/ x1 M! R8 g
- function CrowdDis = CrowdingDistance(PopObj,FrontNO)
- % Calculate the crowding distance of each solution front by front
- % Copyright 2015-2016 Ye Tian
- [N,M] = size(PopObj);
- CrowdDis = zeros(1,N);
- Fronts = setdiff(unique(FrontNO),inf);
- for f = 1 : length(Fronts)
- Front = find(FrontNO==Fronts(f));
- Fmax = max(PopObj(Front,:),[],1);
- Fmin = min(PopObj(Front,:),[],1);
- for i = 1 : M
- [~,Rank] = sortrows(PopObj(Front,i));
- CrowdDis(Front(Rank(1))) = inf;
- CrowdDis(Front(Rank(end))) = inf;
- for j = 2 : length(Front)-1
- CrowdDis(Front(Rank(j))) = CrowdDis(Front(Rank(j)))+(PopObj(Front(Rank(j+1)),i)-PopObj(Front(Rank(j-1)),i))/(Fmax(i)-Fmin(i));
- end
- end
- end
- end
1 _) `" I9 Q' b7 ^# K. b
8 |8 k# Q/ ^+ @, n( I. r0 l* r$ o/ P1 l( {
Truncation.m代码:
5 s; ~9 L2 { M5 m6 S; y, B6 d( t" U R7 S: h: F M
- function Del = Truncation(PopObj,K)
- % Select part of the solutions by truncation
- N = size(PopObj,1);
- %% Truncation
- Distance = pdist2(PopObj,PopObj);
- Distance(logical(eye(length(Distance)))) = inf;
- Del = false(1,N);
- while sum(Del) < K
- Remain = find(~Del);
- Temp = sort(Distance(Remain,Remain),2);
- [~,Rank] = sortrows(Temp);
- Del(Remain(Rank(1))) = true;
- end
- end
8 o; A8 M1 y+ S& ], S* [ - a S$ D+ d5 h! Q/ m! s7 a% S
% m0 }" Z0 x" W1 n9 X& l2 n
6 i: E+ z! V. q2 `* o |
|