|
人工鱼群算法多目标优化案例分析
百度网盘链接:
视频1:https://pan.baidu.com/s/1fG7plmMccZz-LCa-98FpUw
视频2:https://pan.baidu.com/s/1Y1oTOVNr5htLn2L7PkVLQg
视频3:https://pan.baidu.com/s/1cxgKRYgiOEk3lb_pvwRqvA
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1\
主程序如下:- % 基于AFSA算法的多目标函数寻优
- clc,clear,close all
- warning off
- format longG
- % web('halcom.cn')
- addpath(genpath('./TestCode'))
- global nObj nVar
- % 算法参数
- maxiter = 50; % 迭代次数
- sizepop = 100; % 种群数量
- try_number = 10; % 种群个体最优try次数
- visual = 1; % 可视距离 = 感知距离
- delta = 0.618; % 拥挤度因子
- step = 0.5; % 步长
- repository = 100; % 非支配解种群数量
- nGrid=12; % 每个目标函数解集域的扩展网格大小,单周期数
- alpha=0.1; % 非支配解的解集的扩展因子
- beta=2; % 非支配解的选择因子
- %% 适应度函数
- % testfun = @fun_ZDT1;
- % maxiter = 5; % 迭代次数
- % nObj = 2; % 目标个数
- % nVar = 30; % 未知量个数
- % popmin = zeros(1, nVar); % 最小取值范围
- % popmax = ones(1, nVar); % 最大取值范围
- % testfun = @fun_ZDT4;
- % maxiter = 50; % 迭代次数
- % nObj = 2; % 目标个数
- % nVar = 10; % 未知量个数
- % popmin = zeros(1, nVar); % 最小取值范围
- % popmax = ones(1, nVar); % 最大取值范围
- % testfun = @fun_DTLZ1;
- % maxiter = 50; % 迭代次数
- % nObj = 3; % 目标个数
- % nVar = 10; % 未知量个数
- % popmin = zeros(1, nVar); % 最小取值范围
- % popmax = ones(1, nVar); % 最大取值范围
- testfun = @fun_DTLZ2;
- maxiter = 50; % 迭代次数
- nObj = 3; % 目标个数
- nVar = 12; % 未知量个数
- popmin = zeros(1, nVar); % 最小取值范围
- popmax = ones(1, nVar); % 最大取值范围
- % testfun = @fun_SRN;
- % maxiter = 50; % 迭代次数
- % nObj = 2; % 目标个数
- % nVar = 2; % 未知量个数
- % popmin = -20*ones(1, nVar); % 最小取值范围
- % popmax = 20*ones(1, nVar); % 最大取值范围
- % testfun = @fun_TNK;
- % maxiter = 50; % 迭代次数
- % nObj = 2; % 目标个数
- % nVar = 2; % 未知量个数
- % popmin = 0*ones(1, nVar); % 最小取值范围
- % popmax = pi*ones(1, nVar); % 最大取值范围
- %% 初始化种群
- x.Pos=[]; % 未知量的解
- x.fitness=[]; % 粒子适应度值
- x.Best.Pos=[]; % 最优解
- x.Best.fitness=[]; % 最优解对应的适应度值--目标函数值
- x.IsDominated=[]; % 是否非支配解,支配解=1, 非支配解=0
- x.GridIndex=[]; % 非支配解的网格化参数值-索引总循环数(并排目标函数的序列化内)
- x.GridSubIndex=[]; % 每一个非支配解的网格化索引值(扩展取值范围内)
- pop=repmat(x,sizepop,1); % 初始化的种群
- clear x
- for i=1:sizepop
- for j=1:nVar
- pop(i).Pos(j) = unifrnd(popmin(j),popmax(j),1);
- end
- % 适应度函数
- pop(i).fitness = testfun(pop(i).Pos);
- % 种群个体最优
- pop(i).Best.Pos=pop(i).Pos;
- % 种群最优适应度值
- pop(i).Best.fitness=pop(i).fitness;
- end
- % 确定种群之间是否存在支配解
- pop=JudgePopDomination(pop);
- % 然后获取非支配解
- rep=pop(~[pop.IsDominated]);
- % 扩展非支配解的适应度函数取值范围
- Grid=CreateGrids(rep,nGrid,alpha);
- % 计算扩展非支配解的GridIndex
- for i=1:numel(rep)
- rep(i)=FindGridIndex(rep(i),Grid);
- end
- %% AFSA算法的多目标函数寻优主程序
- for i=1:maxiter
- popFSA=[];
- popFSA = [pop.Pos];
- popFSA = [reshape(popFSA,nVar,sizepop)]';
- fitness=[];
- fitness = [pop.fitness];
- fitness = [reshape(fitness,nObj,sizepop)]';
-
- for j=1:sizepop
- Xi = popFSA(j,:); % 当前的种群
- Yi = fitness(j,:); % 当前种群对应的适应度值
- % 聚群行为
- [Xc,Yc] = FSA_center(testfun, Xi, Yi, popFSA,fitness, popmin, popmax, sizepop, visual, step, try_number, delta);
- % 追尾行为
- [Xs,Ys] = FSA_scatter(testfun, Xi,Yi, popFSA,fitness, popmin, popmax, sizepop, visual, step, try_number, delta);
- % 是否受支配
- if Domination2(Yc,Ys)
- pop(j).Pos = Xc;
- pop(j).fitness = Yc;
- else
- pop(j).Pos = Xs;
- pop(j).fitness = Ys;
- end
-
- % 是否受支配
- if Domination(pop(j),pop(j).Best)
- pop(j).Best.Pos=pop(j).Pos;
- pop(j).Best.fitness=pop(j).fitness;
- else
- if rand<0.5
- pop(j).Best.Pos=pop(j).Pos;
- pop(j).Best.fitness=pop(j).fitness;
- end
- end
-
- end
-
- % 增加非支配解
- rep =[rep; pop(~[pop.IsDominated])];
- % 非支配解是否有支配解
- rep=JudgePopDomination(rep);
- % 删除支配解
- rep=rep(~[rep.IsDominated]);
-
- % 扩展非支配解的适应度函数取值范围
- Grid=CreateGrids(rep,nGrid,alpha);
- for k=1:numel(rep)
- rep(k)=FindGridIndex(rep(k),Grid);
- end
-
- if nObj==2
- figure(1);
- Plotfitness(pop,rep);
- pause(0.001);
- elseif(nObj==3)
- figure(1);
- Plotfitness3(pop,rep);
- pause(0.001);
- end
- end
- % 确定种群之间是否存在支配解
- pop=JudgePopDomination(pop);
- rep = uniqueRep(rep);
- if nObj==2
- figure(1);
- Plotfitness(pop,rep);
- elseif( nObj==3)
- figure(1);
- Plotfitness3(pop,rep);
- end
- % web('halcom.cn')
- rmpath(genpath('./TestCode'))
复制代码 优化后的追尾行为函数:
- function [Xt,Yt] = FSA_scatter(testfun, Xi, Yi, pop, fitness, popmin,popmax, sizepop, visual, step, try_number, delta )
- % 追尾行为
- % popmin1 = popminmax1(1); popmax1 = popminmax1(2); % x1
- % popmin2 = popminmax2(1); popmax2 = popminmax2(2); % x2
- % Xi = pop(j,:); % 当前的种群
- % Yi = fitness(j); % 当前种群对应的适应度值
- % for k=1:sizepop
- % d(k) = norm( Xi-pop(k,:) ,2);
- % end
- d = zeros( sizepop,1 );
- for i=1:size(fitness, 2)
- [va,vb] = sort(fitness(:, i));
- for k=1:sizepop
- if(vb(k)==sizepop)
- d(k) = d(k) + abs( fitness(vb(k)-1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- elseif( vb(k)==1 )
- d(k) = d(k) + abs( fitness(vb(k)+1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- elseif(vb(k)>1 && vb(k)<sizepop )
- d(k) = d(k) + abs( fitness(vb(k)+1, i) - fitness(vb(k)-1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- end
- end
- end
- [a,b] = find( d>0 & d<visual );
- if(~isempty(b))
- Xc = zeros( size(pop(1,:)) );
- Ymax = 1000000*ones( size(fitness,2), 1 );
- Xmax = Xc;
- for k=1:length(b)
- Xc = pop(a(k),:);
- Yc = testfun( Xc ); % 适应度值
- % 是否受支配
- if Domination2(Yc,Ymax)
- % if Yc>Ymax
- Ymax = Yc;
- Xmax = Xc;
- end
- end
- % if Ymax/length(b) > delta*Yi
- % 是否受支配
- if Domination2(Ymax/length(b),delta*Yi')
- Xt = Xi + ( Xmax - Xi )./(norm(Xmax-Xi,2)+eps) .* step .*rand( size( Xi ) );
- Yt = testfun( Xt ); % 适应度值
- else
- % 觅食行为
- [Xt,Yt] = FSA_find(testfun, Xi, Yi, popmin, popmax, visual, step, try_number );
- end
- else
- % 觅食行为
- [Xt,Yt] = FSA_find(testfun, Xi, Yi, popmin, popmax, visual, step, try_number );
- end
复制代码 优化后的聚群行为函数如下:
- function [Xt,Yt] = FSA_center(testfun, Xi, Yi, pop, fitness, popmin, popmax, sizepop, visual, step, try_number, delta )
- % 聚群行为
- % popmin1 = popminmax1(1); popmax1 = popminmax1(2); % x1
- % popmin2 = popminmax2(1); popmax2 = popminmax2(2); % x2
- % Xi = pop(j,:); % 当前的种群
- % Yi = fitness(j); % 当前种群对应的适应度值
- % for k=1:sizepop
- % d(k) = norm( Xi-pop(k,:) ,2);
- % end
- d = zeros( sizepop,1 );
- for i=1:size(fitness, 2)
- [va,vb] = sort(fitness(:, i));
- for k=1:sizepop
- if(vb(k)==sizepop)
- d(k) = d(k) + abs( fitness(vb(k)-1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- elseif( vb(k)==1 )
- d(k) = d(k) + abs( fitness(vb(k)+1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- elseif(vb(k)>1 && vb(k)<sizepop )
- d(k) = d(k) + abs( fitness(vb(k)+1, i) - fitness(vb(k)-1, i) )./( max(fitness(:,i))-min(fitness(:,i))+eps );
- end
- end
- end
- [a,b] = find( d>0 & d<visual );
- if(~isempty(b))
- Xc = zeros( size(pop(1,:)) );
- for k=1:length(b)
- Xc = Xc+pop(b(k),:);
- end
- Xc = Xc./length(b);
- Yc = testfun( Xc ); % 适应度值
- % if Yc/length(b) > delta*Yi
- % 是否受支配
- if Domination2(Yc/length(b),delta*Yi')
- Xt = Xi + ( Xc - Xi )./(norm(Xc-Xi,2)+eps) .* step .*rand( size( Xi ) );
- Yt = testfun( Xt ); % 适应度值
- else
- % 觅食行为
- [Xt,Yt] = FSA_find(testfun, Xi, Yi, popmin, popmax, visual, step, try_number );
- end
- else
- % 觅食行为
- [Xt,Yt] = FSA_find(testfun, Xi, Yi, popmin, popmax, visual, step, try_number );
- end
复制代码 快速排序法构造非支配集:
- function pop = JudgePopDomination(pop)
- sizepop = numel(pop); % 种群数量
- for i=1:sizepop
- pop(i).IsDominated=0;
- end
- for i=1:sizepop-1
- for j=i+1:sizepop
- b1 = Domination(pop(i),pop(j));
- b2 = Domination(pop(j),pop(i));
- if b1==1
- pop(j).IsDominated=1;
- end
- if b2==1
- pop(i).IsDominated=1;
- end
- end
- end
复制代码 结果如下:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|