果蝇算法最早的文献是由台湾华夏科技大学的潘文超教授于2011年提出来的。该算法是基于果蝇觅食行为的仿生学原理而提出的一种新兴群体智能优化算法,被称为果蝇优化算法(Fruit Fly Optimization Algorithm, FOA)。通过模拟果蝇利用敏锐的嗅觉和视觉进行捕食的过程,FOA实现对解空间的群体迭代搜索。此算法原理易懂、操作简单、易于实现,并具有较强的局部搜索能力。
多目标果蝇算法,根据, 步骤如下:
1.设定待优化函数
设定待优化函数表达式和参数等。
2.设置果蝇算法参数
设置果蝇数popsize, 迭代次数maxgen.
3. 初始化果蝇位置
在定义域范围内随机设定一个果蝇的位置p0, 采用均匀分布的方法, 随机产生popsize个果蝇的位置.
4. 快速非支配排序
目的是将解集中的个体按照它们的非支配程度进行分类。以下是快速非支配排序的基本流程:
(1)初始化:
对于解集中的每个个体,设置两个参数:np(被该个体支配的个体数量)和sp(支配该个体的个体集合)。
初始化前沿面集合(即非支配层级)为空。
(2)计算支配关系:
遍历解集中的每个个体p,对于解集中的每个其他个体q:
如果个体p在所有目标上都优于或等于个体q,并且在至少一个目标上严格优于q,则p支配q,将q加入到p的sp集合中,并增加q的np计数。
如果个体q在所有目标上都优于或等于个体p,并且在至少一个目标上严格优于p,则q支配p。
遍历完成后,所有np为0的个体构成了第一前沿面(即它们不被任何其他个体支配)。
(3)构建前沿面:
创建一个空的前沿面集合,并将所有np=0的个体加入到当前前沿面中。
对于当前前沿面中的每个个体,遍历其sp集合中的每个个体,将这些个体的np值减1。如果某个个体的np值变为0,则将其加入到下一个前沿面的候选集合中。
(4)重复上述步骤,直到候选集合为空,此时完成了一层前沿面的构建。
继续构建下一个前沿面,直到所有个体都被分配到某个前沿面中。
(5)分配前沿面层级:
给每个前沿面分配一个层级值,第一前沿面的层级最低(通常为1),随着前沿面的递增,层级值也递增。
每个个体都被赋予其所在前沿面的层级值,这个层级值表示了个体的非支配程度。
(6)输出:
输出每个个体的前沿面层级以及所属的前沿面。
这个过程最终将解集中的个体分层,其中每一层都是当前层中个体的非支配集。第一层的个体是最优的,因为它们不被任何其他个体支配,第二层的个体只被第一层的个体支配,以此类推。这种分层结构有助于在多目标优化问题中识别出最优解集。
5. 构造第一前沿面
根据4. 快速非支配排序的结果提取出第一前沿面的所有果蝇。
6. 对前沿每个果蝇进行放飞
对前沿每个果蝇进行放飞,得到新解。以p0为中心, 采用均匀分布的方法, 随机产生新的果蝇的位置。
7.计算目标函数值
8.找出不重复的非支配解
9.重复4~8直至满足迭代终止条件。
流程图如下:
完整代码见: https://download.csdn.net/download/corn1949/89217761
MATLAB部分主程序如下:
%% mFOA 多目标果蝇算法
clc;close all;clear all;warning off;%清除变量
rand('seed', 100);
randn('seed', 100);
format long g;
global objnumber varinumber;
global C1 C2;
N=5;% 变量数
C1=rand(1,N);
C2=rand(1,N);
% 多目标参数
objnumber=2;% 目标数
varinumber=N;% 变量数
lb=0*ones(1,N);
ub=1*ones(1,N);
%% 果蝇算法参数设置
maxgen=50; % 迭代次数
popsize=50; % 果蝇数
value_foa_cell=cell(objnumber,1);
for i=1:objnumber
value20i_foa=zeros(maxgen,2);
value_foa_cell{i,1}=value20i_foa;
end
p0=lb+(ub-lb).*rand(1,N);% 随机果蝇群位置
vlb=-(ub-lb)/5;% 果蝇群体散布范围下限
vub=(ub-lb)/5;% 果蝇群体散布范围上限
foamat=genfoafun(popsize,N,p0,lb,ub,vlb,vub);% 初始化果蝇群
Value_foa=decodingfun(foamat,popsize);% 果蝇群解码
dominatedmat=determinedomination(Value_foa);% 找出非支配解
h_dominate= dominatedmat==0;
[mat2,index401]=matuniquefun(foamat(h_dominate,:));% 找出不重复的前沿集
index99= find(h_dominate);
indexselect=index99(index401);
non_DS_mat_foa=foamat(indexselect,:);% 非支配解集
non_DS_Value_foa=Value_foa(indexselect,:);% 非支配解结果
% 果蝇迭代寻优开始
tracemat_foa=zeros(maxgen,2);
tic;
wait_hand = waitbar(0,'running...', 'tag', 'TMWWaitbar');
for gen=1:maxgen
[frontmat42,foamat42,Value42,Fcell42]=nondominationsort2(non_DS_mat_foa,objnumber,non_DS_Value_foa);% 快速非支配排序
% 构造最优前沿面集中的最优果蝇
index201= find(frontmat42(:,1)==1);
K1=length(index201);
bestfoamat_foa=foamat42(index201(randi([1,K1],1,1)),:);
% 果蝇个体利用嗅觉搜寻食物之随机方向与距离
foamat=genfoafun(popsize,N,bestfoamat_foa,lb,ub,vlb,vub);% 初始化果蝇群
Value_foa=decodingfun(foamat,popsize);% 果蝇群解码
%% 找出不重复的非支配解
Value32=[Value_foa;
non_DS_Value_foa];
foamat32=[foamat;
non_DS_mat_foa];
dominatedmat=determinedomination(Value32);% 找出非支配解
h_dominate= dominatedmat==0;
[mat2,index401]=matuniquefun(foamat32(h_dominate,:));% 找出不重复的前沿集
index99= find(h_dominate);
indexselect=index99(index401);
non_DS_mat_foa=foamat32(indexselect,:);% 非支配解集
non_DS_Value_foa=Value32(indexselect,:);% 非支配解结果
%% 记录每代结果
for i=1:objnumber
value20i_foa=value_foa_cell{i,1};
if gen==1
value20i_foa(gen,1)=min(Value_foa(:,i));
value20i_foa(gen,2)=mean(Value_foa(:,i));
else
value20i_foa(gen,1)=min(value20i_foa(gen-1,1),min(Value_foa(:,i)));
value20i_foa(gen,2)=mean(Value_foa(:,i));
end
value_foa_cell{i,1}=value20i_foa;
end;
waitbar(gen/maxgen,wait_hand);
end
delete(wait_hand);
disp('多目标果蝇算法运行时间(s)');
runtime_mfoa=toc
%% 输出结果
% 迭代图
for i=1:objnumber
value20i_foa=value_foa_cell{i,1};
figure;
plot(value20i_foa(:,1),'r-','linewidth',1.5);
xlabel('迭代次数','fontname','宋体');
ylabel(['f',num2str(i)],'fontname','宋体');
title(['多目标果蝇算法的f',num2str(i),'的迭代曲线'],'fontname','宋体');
end
%% 绘制帕累托前沿
figure;
plot(non_DS_Value_foa(:,1),non_DS_Value_foa(:,2),'m.','markersize',25);
xlabel('f1');
ylabel('f2');
grid on;
title('多目标果蝇算法的帕累托前沿','fontname','宋体');
disp('多目标果蝇算法的非支配解集');
non_DS_mat_foa
程序结果如下:
多目标果蝇算法运行时间(s)
runtime_mfoa =
2.1129473
多目标果蝇算法的非支配解集
non_DS_mat_foa =
0.281529070568052 0.0590890372447153 0.109448971929703 0.508871222524378 0.398636967222503
0.312065234087438 0.0803893075698937 0.103092327203179 0.472743303828288 0.396707442028777
0.318987401164597 0.0212513733754174 0.171832320639786 0.585812992875377 0.358971256464241
0.332893493181511 0.140939901648527 0.176927006792709 0.612203165056279 0.298595100873427
0.348600603616145 0.130344976545472 0.108020799750472 0.505581406180552 0.306693676536294
0.359672158006426 0.209959614002127 0.191232533748836 0.645194716679489 0.387603232165614
0.377345684067041 0.0489121147659198 0.265912870814052 0.597619771770025 0.395504138802879
0.387993755558503 0.211049671755661 0.111833197396171 0.580548637444409 0.280949528180505
0.391414251081373 0.099317924631442 0.236359280644152 0.690429786262303 0.253417710519125
0.409999790140427 0.0664728901658547 0.20986501751927 0.601349446364376 0.28014504605911
0.417927804970149 0.11261813329189 0.172966236794817 0.443541810495566 0.407208998970319
0.432335192818351 0.257585698020452 0.242826629729395 0.587165861943348 0.296641681853981
0.44302515901766 0.323847609816048 0.306778178320629 0.620843034806123 0.308885986501764
0.462958187452964 0.138256521959909 0.277364580182994 0.666499135581078 0.450971711171312
0.471580517697884 0.253760948336572 0.360258692763866 0.66784928230003 0.342887616596598
0.488479452155754 0.274152381752689 0.27908011743756 0.69953377307371 0.464124049834965
0.499398478632513 0.190230376641373 0.201940211561481 0.609135713805042 0.343941921342137
0.5019695275938 0.201850268990663 0.297470926073133 0.593854511153816 0.312768962193639
0.502113299026207 0.21821673345669 0.168639206592291 0.519145196638603 0.27331990500601
0.507399559443537 0.264395567525362 0.296303398765765 0.571223056209843 0.345905718834095
0.517923026493715 0.332306279862442 0.471645648065789 0.748407041723098 0.277150240110769
0.533813756580378 0.207806846409946 0.409667611964824 0.683554292788522 0.296998896681237
0.542742646086376 0.275652773713531 0.296167803321112 0.692270417926959 0.388914098399186
0.544859868076099 0.259802754996253 0.304903222017411 0.708452446622985 0.360270392503715
0.546648118620109 0.314929648169749 0.422596788975688 0.784232314389307 0.392507941086082
0.547830923901792 0.194338017420069 0.439058779105106 0.660900419513183 0.353350758065167
0.549663868709311 0.200641397387088 0.379965884787946 0.686625631007657 0.316980345694805
0.558376225157816 0.229216227414653 0.237134158069796 0.513794679061414 0.347170985186086
0.565760794824809 0.341678620568327 0.392575891871274 0.623014680493164 0.407735048610594
0.575354325294194 0.380145219517939 0.500704438006833 0.739489580848948 0.401385328267415
0.586135032859694 0.371497272873063 0.35466517757376 0.657639482178558 0.346776975014609
0.594775662568759 0.394560793132782 0.383250182672986 0.685820184874265 0.379847181765291
0.598275261744054 0.412324132310377 0.531691740514567 0.743082828327586 0.393095701743428
0.599030831828262 0.311190537601332 0.37936546559416 0.595380241049165 0.355711313316464
0.599950412753946 0.166587155576137 0.43032376814183 0.65157115974071 0.356481762116999
0.601954449807273 0.248437910828943 0.495967302050426 0.722445561514444 0.342552373252135
0.60578848868878 0.287129392329198 0.383696876831212 0.593408902172655 0.423418815817413
0.614341175190332 0.432131423909278 0.632841643240695 0.769497946356189 0.351984408475451
0.626993089181833 0.272849879075238 0.387917617516554 0.731397600719425 0.399475291371101
0.629256714428429 0.317599398604408 0.493092344278978 0.803030296789031 0.330198133238684
0.630665438077723 0.394017772280619 0.456698720369813 0.735393255453274 0.354444403180128
0.632370644916022 0.25342910357445 0.382943775776282 0.736039471969027 0.41540538343387
0.636095985414505 0.465226861585503 0.467862667547475 0.767853470411083 0.313277199078946
0.637347605096804 0.301198861981369 0.449273320869204 0.736703848716199 0.381585373162099
0.637918476871177 0.295840773869232 0.395886420177243 0.663063918921661 0.315285316349606
0.638752354047612 0.310814478206828 0.458935222150262 0.724278679454829 0.351765597309808
0.64210848093131 0.31723901253065 0.436083602642679 0.657109615512709 0.241307922099395
0.649253224604415 0.398945926408724 0.484185151422483 0.699839957663715 0.410168454055753
0.65505921610401 0.380245060091952 0.578724965443241 0.83049420455028 0.316095876561522
0.657151947476506 0.352781237639851 0.594261012968729 0.744844965424782 0.409333894313003
0.66145565391586 0.285175363852258 0.342340264908197 0.712832312059045 0.372668776369034
0.662079321156293 0.367150673813722 0.501374787232547 0.806049017424718 0.26583585723575
0.663631124544717 0.448310223058011 0.549918935983404 0.687557073071393 0.371727110897995
0.664291216090457 0.342468832313301 0.473664689657122 0.682439067253116 0.353403323122022
0.670670883390434 0.36553714301695 0.582762685875764 0.692461513957224 0.400665079057526
0.674767304339804 0.41408403907627 0.510444754879197 0.844995254671665 0.435245266666284
0.68174607375718 0.506261636925052 0.53933179934478 0.749551587714605 0.31353471936357
0.685282402152793 0.341332981987546 0.583428264680984 0.678844493291734 0.33939875417361
0.690494596907168 0.342690218772129 0.594506903176432 0.877521686292031 0.306981510160017
0.698279611160178 0.38542476910419 0.434094334130219 0.823473726596438 0.322922906336804
0.699563687341085 0.36689314160817 0.573031008510399 0.732160034278482 0.413696118450582
0.709116800459622 0.526065324864381 0.579914995646065 0.831331823408293 0.393956023172455
0.722007844281387 0.385838837263099 0.593337880910066 0.829764455477597 0.45120321197957
0.726890723280092 0.452386168508039 0.654334114610373 0.793464256540623 0.35375967824541
0.730914530870931 0.480520347729568 0.705484290842658 0.87447619255375 0.321368250866126
0.731289461223078 0.381974776267062 0.450064720516123 0.837757714482843 0.393907313139135
0.737028891191366 0.444574253281846 0.55947490798285 0.894778467758921 0.341707624188488
0.738674747635925 0.506483516984844 0.668469964279081 0.774689638509736 0.408754433136785
0.74716649025081 0.427201645368338 0.578053705663445 0.748631085524629 0.442654412445917
0.758347054644649 0.338947412622602 0.489163948078251 0.778475351155957 0.435226878167701
0.758399705290049 0.423846809856522 0.593333258569861 0.752076783660835 0.354502987654183
0.766520952324626 0.517645719981587 0.671615730538785 0.845583165365077 0.316260290851938
0.773373536846309 0.489033775911217 0.590671739816047 0.819931088304115 0.38180112726139
0.779915294041818 0.436346960829732 0.683370665313383 0.81077192202712 0.443693509811393
0.783852505769512 0.609064468186844 0.74651681629313 0.908131438637214 0.365089175647632
0.795264453531832 0.409670509495619 0.732253092868371 0.777731838718863 0.339013347932609
0.797913212235045 0.527358034405558 0.706484254219795 0.880860672090604 0.425315826770531
0.830009822095749 0.37507996325152 0.568942368295483 0.814383942174904 0.35091613361189
0.834091374945869 0.57373871522664 0.626586814143968 0.844585317673434 0.345434137408358
0.842199802138936 0.452074549092015 0.616946589488977 0.821329541234919 0.285599535277858
0.854482159695813 0.481658007521954 0.626132421487073 0.807607933230516 0.466533805274653
0.865599992156774 0.53906817889729 0.718882726747022 0.861988437204616 0.439664097986959
0.87159964436274 0.575222804572071 0.769676442802733 0.95197418553381 0.430136266737309
0.877853881231441 0.490181856830689 0.686467753390999 0.86353124252685 0.369593148757514
0.879577031303 0.651165109524115 0.731995771793647 0.852936535818938 0.304357508897948
0.893354609372725 0.610919727390129 0.727858245897972 0.913538807217748 0.446732908695346
0.925954241550506 0.512937739357789 0.744585386358474 0.846588526874123 0.413371173391757
0.963752343023081 0.585629188914611 0.869778087860801 0.960322676487417 0.343223724022146
>>
完整代码见: https://download.csdn.net/download/corn1949/89217761