🌞欢迎来到智能优化算法的世界
🌈博客主页:卿云阁💌欢迎关注🎉点赞👍收藏⭐️留言📝
🌟本文由卿云阁原创!
🌠本阶段属于筑基阶段之一,希望各位仙友顺利完成突破
📆首发时间:🌹2021年1月7日🌹
✉️希望可以和大家一起完成进阶之路!
🙏作者水平很有限,如果发现错误,请留言轰炸哦!万分感谢!
目录
0️⃣✨✨✨基本介绍✨✨✨
现实生活中的很多决策问题都要考虑同时优化若干个目标,而这些目标之间有时是彼此约束,甚至相互冲突,这样就需要从所有可能的方案中找到最合理、最可靠的解决方案。而遗传算法是模拟达尔文的遗传选择和自然淘汰的生物进化过程的一种新的迭代的全局优化搜索算法,它能够使群体进化并行搜寻多个目标,并逐渐找到问题的最优解
本程序所使用的是:
目标函数
min f1=300/x+500/y+300/(100-x-y)
min f2=30(300/x-3)+12(500/y-5)+(300/(100-x-y)-3)
约束
x、y为正整数,且x+y<100
对于该问题,利用权重系数变换法很容易求出最优解
1️⃣✨✨✨代码部分✨✨✨
(1)biobjective.m % 主函数,用遗传算法解决双目标优化问题 % =====================目标函数====================== % min f1=300/x+500/y+300/(100-x-y) % min f2=30(300/x-3)+12(500/y-5)+(300/(100-x-y)-3) % ======================约束========================= % x、y为正整数,且x+y<100 % ======================调用函数说明================= % inipop.m 生成初始种群 % Pareto.m 把种群中的pareto解保存到popbest中 % crossover.m 交叉操作 % mutation.m 变异操作 % decoding.m 解码操作 % elimination.m 将popbest中的非pareto解去掉 % selection.m 选择操作 % =======================变量说明==================== %=================================================== clear all global popsize L popsize=100;%种群规模 Maxgen=100;%最大迭代次数 pm=0.15;%变异概率 pc=0.8;%交叉概率 L=14;% 100的二进制为1100100,所以由x、y构成的染色体长度为14。 [pop,objective]=inipop(); popbest=[];%记忆库,记录已搜到的pareto解个体 objectivebest=[];%记录相应的目标值 [popbest,objectivebest]=Pareto(pop,objective,popbest,objectivebest); for generation=1:Maxgen oldpop=pop; pop=crossover(pop,pc); pop=mutation(pop,pm); [pop,objective]=decoding(pop,oldpop); [popbest,objectivebest]=Pareto(pop,objective,popbest,objectivebest); [popbest,objectivebest]=elimination(popbest,objectivebest); [pop]=selection(pop,objective); end [objectivebest,index]=sortrows(objectivebest);%排序,方便画图 x=bin2dec(popbest(index,1:L/2)); y=bin2dec(popbest(index,L/2+1:end)); figure(1); plot(objectivebest(:,1),objectivebest(:,2),'r-*'); xlabel('f1'); ylabel('f2'); title('最优帕累托值'); figure(2); plot(x,y,'r-*'); xlabel('x'); ylabel('y'); title('相应x、y坐标点'); clear global (2)crossover.m function pop=crossover(pop,pc) % 两点交叉操作 global popsize L index=find(rand(popsize,1)<pc); for i=1:fix(length(index)/2) sit=randperm(L); sit=sort(sit(1:2)); string=pop(2*i-1,sit(1):sit(2)); pop(2*i-1,sit(1):sit(2))=pop(2*i,sit(1):sit(2)); pop(2*i,sit(1):sit(2))=string; end (3)decoding.m function [pop,objective]=decoding(pop,oldpop) % 解码操作,先修复不可行解,再计算目标值。 global popsize L x=zeros(popsize,1); y=zeros(popsize,1); for i=1:popsize x(i)=bin2dec(pop(i,1:L/2)); y(i)=bin2dec(pop(i,L/2+1:end)); % x、y和小于100, 且均不为零 if x(i)+y(i)>99 switch 1 case x(i)>98&y(i)>98 pop(i,:)=oldpop(i,:); x(i)=bin2dec(pop(i,1:L/2)); y(i)=bin2dec(pop(i,L/2+1:end)); case x(i)>98&y(i)<=98 x(i)=ceil(rand*(99-y(i))); a=dec2bin([x(i),100]); pop(i,1:L/2)=a(1,:); case x(i)<=98&y(i)>98 y(i)=ceil(rand*(99-x(i))); a=dec2bin([y(i),100]); pop(i,L/2+1:end)=a(1,:); otherwise if rand<0.5 x(i)=ceil(rand*(99-y(i))); a=dec2bin([x(i),100]); pop(i,1:L/2)=a(1,:); else y(i)=ceil(rand*(99-x(i))); a=dec2bin([y(i),100]); pop(i,L/2+1:end)=a(1,:); end end end if x(i)*y(i)==0 switch 1 case x(i)==0&y(i)~=0&y(i)~=99 x(i)=ceil(rand*(99-y(i))); a=dec2bin([x(i),100]); pop(i,1:L/2)=a(1,:); case y(i)==0&x(i)~=0&x(i)~=99 y(i)=ceil(rand*(99-x(i))); a=dec2bin([y(i),100]); pop(i,L/2+1:end)=a(1,:); otherwise pop(i,:)=oldpop(i,:); x(i)=bin2dec(pop(i,1:L/2)); y(i)=bin2dec(pop(i,L/2+1:end)); end end end objective=[300./x+500./y+300./(100-x-y),30*(300./x-3)+12*(500./y-5)+(300./(100-x-y)-3)]; (4)elimination.m function [popbest,objectivebest]=elimination(popbest,objectivebest) % 用于消除记忆库popbest中的受只配解。 global popsize L c=1; group=[]; for i=1:size(popbest,1) index1=find(objectivebest(:,1)<objectivebest(i,1)); index2=find(objectivebest(:,2)<=objectivebest(i,2)); index3=find(objectivebest(:,1)<=objectivebest(i,1)); index4=find(objectivebest(:,2)<objectivebest(i,2)); if ~isempty(intersect(index1,index2))|~isempty(intersect(index3,index4)) %受支配 group(c)=i; c=c+1; end end if ~isempty(group) %去掉非pareto解 popbest(group,:)=[]; objectivebest(group,:)=[]; end (5)inipop.m function [pop,objective]=inipop() % 生成初始种群 % 生成方法是先生成x基因串,再生成y的基因串。 global popsize L pop=zeros(popsize,L); x=ceil(rand(popsize,1).*98); y=ceil(rand(popsize,1).*(99-x)); a=dec2bin([x;100]);%保证x占7个基因位 b=dec2bin([y;100]); pop=[a(1:popsize,:),b(1:popsize,:)]; objective=[300./x+500./y+300./(100-x-y),30*(300./x-3)+12*(500./y-5)+(300./(100-x-y)-3)]; (6)mutation.m function pop=mutation(pop,pm) % 逆序交叉 global popsize L index=find(rand(popsize,1)<pm); for i=1:length(index) sit=randperm(L); sit=sort(sit(1:2)); pop(index(i),sit(1):sit(2))=pop(index(i),sit(2):-1:sit(1)); end (7)Pareto.m function [popbest,objectivebest]=Pareto(pop,objective,popbest,objectivebest) % 过滤Pareto解 global popsize c=1; for i=1:popsize % for j=1:popsize % if objective(i,1)>objective(j,1)&objective(i,2)>=objective(j,2)|objective(i,1)>=objective(j,1)&objective(i,2)>objective(j,2) % j=j-1; % break; % end % end % if j==popsize % popbest(c,:)=pop(i,:); % objectivebest(c,:)=objective(i,:); % c=c+1; % end index1=find(objective(:,1)<objective(i,1)); index2=find(objective(:,2)<=objective(i,2)); index3=find(objective(:,1)<=objective(i,1)); index4=find(objective(:,2)<objective(i,2)); if isempty(intersect(index1,index2))&isempty(intersect(index3,index4)) %不受支配 group(c)=i; c=c+1; end end popbest=[popbest;pop(group,:)]; objectivebest=[objectivebest;objective(group,:)]; (8)selection.m function [newpop]=selection(pop,objective) % 选择操作,采用锦标赛方法。 % 先根据解的质量对群体分级。 global popsize L index=1:popsize; group=[]; class=zeros(50,50); r=1; while ~isempty(index) index=setdiff(index,group); group=[]; c=1; for i=index index1=find(objective(index,1)<objective(i,1)); index2=find(objective(index,2)<=objective(i,2)); index3=find(objective(index,1)<=objective(i,1)); index4=find(objective(index,2)<objective(i,2)); if isempty(intersect(index1,index2))&isempty(intersect(index3,index4)) group(c)=i; c=c+1; end end class(r,1:length(group))=group; r=r+1; end for i=1:popsize index=randperm(popsize); index1=find(class==index(1)); index2=find(class==index(2)); [x1,y1]=ind2sub(size(class),index1); [x2,y2]=ind2sub(size(class),index2); while x1==x2 index=randperm(popsize); index1=find(class==index(1)); index2=find(class==index(2)); [x1,y1]=ind2sub(size(class),index1); [x2,y2]=ind2sub(size(class),index2); end if x1<x2 newpop(i,:)=pop(index(1),:); else newpop(i,:)=pop(index(2),:); end end
2️⃣✨✨✨结果✨✨✨