失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【优化求解】基于NSGAII算法求解含约束多目标优化问题matlab代码

【优化求解】基于NSGAII算法求解含约束多目标优化问题matlab代码

时间:2023-07-18 03:07:34

相关推荐

【优化求解】基于NSGAII算法求解含约束多目标优化问题matlab代码

1 简介

NSGA(非支配排序遗传算法)、NSGAII(带精英策略的非支配排序的遗传算法),都是基于遗传算法的多目标优化算法,都是基于pareto最优解讨论的多目标优化​.

2 部分代码

clear allclcglobal V M xl xu etac etam p pop_size pm%% 程序开始运行M=2;p=1;%input('输入测试问题编号:');ticpop_size=300;% 种群数量,这个变量可以自己选择大小,50、100、200、300、500等no_runs=1; % 运行次数gen_max=150;% 最大迭代次数,可修改100fname='test_case'; % 目标函数和约束条件if p==13 % OSYpop_size=100;no_runs=10;end if (p==2 | p==5 | p==7), gen_max=1000; end;if p<=9 % p<=9是非约束测试函数Unconstrained test functionstV=[2;30;3;1;30;4;30;10;10];V=tV(p);txl=[-5*ones(1,V);zeros(1,V);-5*ones(1,V);-1000*ones(1,V);zeros(1,V);-1/sqrt(V)*ones(1,V);zeros(1,V); 0 -5*ones(1,V-1);zeros(1,V)];txu=[10*ones(1,V); ones(1,V);5*ones(1,V);1000*ones(1,V);ones(1,V);1/sqrt(V) *ones(1,V);ones(1,V);1 5*ones(1,V-1);ones(1,V)];xl=(txl(p,1:V));% 自变量下限xu=(txu(p,1:V));% 自变量上限etac = 20; % 交叉etam = 20; % 变异elseif p==15 %应急物资问题V=120;%上个版本x_j(4) x_ij(36) y_ij(36),因此V=76;这个版本q_j(6) x_j(6) x_ij(9*6) y_ij(9*6),因此V=120v_I=9;v_J=6;v_x_ij=v_I*v_J;%v_x_ij决策变量v_y_ij=v_I*v_J;%v_y_ij决策变量v_x_j=v_J;txl_q_j=zeros(1,v_x_j);%q_j下限,表示储备库j的库存储备量,最高300吨,最低0吨txl_x_j=zeros(1,v_x_j);%x_j下限,为0,1变量;当为1时,表示j备选点设置储备库;否则,为不设置txl_x_ij=zeros(1,54);%x_ij下限,表示从应急物资储备库j运输到应急需求点i的物资量txl_y_ij=zeros(1,54);%y_ij下限,应急资源点j是否救援需求点itxu_q_j=ones(1,6)*300;%q_j上限txu_x_j=ones(1,6);%x_j上限txu_x_ij=ones(1,54)*80;%x_ij上限,注意此处x_ij上限可修改txu_y_ij=ones(1,54);%y_ij上限txl=[txl_q_j txl_x_j txl_x_ij txl_y_ij];%q_j(6) x_j(6) x_ij(9*6) y_ij(9*6)txu=[txu_q_j txu_x_j txu_x_ij txu_y_ij];%q_j(6) x_j(6) x_ij(9*6) y_ij(9*6)xl=(txl(1,1:V));% 自变量下限xu=(txu(1,1:V));% 自变量上限etac = 20; etam = 100; %100else% p>9为约束测试函数p1=p-9;tV=[2;2;2;6;2];V=tV(p1);txl=[0 0 0 0 0 0;-20 -20 0 0 0 0;0 0 0 0 0 0;0 0 1 0 1 0;0.1 0 0 0 0 0]; %自变量下限txu=[5 3 0 0 0 0;20 20 0 0 0 0;pi pi 0 0 0 0;10 10 5 6 5 10;1 5 0 0 0 0];%自变量上限xl=(txl(p1,1:V));% 自变量下限xu=(txu(p1,1:V));% 自变量下限 i=1:NNetac = 20; etam = 100; endpm=1/V; % 变异率Q=[];for run = 1:no_runs%% 初始种群xl_temp=repmat(xl, pop_size,1);xu_temp=repmat(xu, pop_size,1);x = xl_temp+((xu_temp-xl_temp).*rand(pop_size,V));%进行离散化处理与关联处理if(p==15)x(:,7:12)=round(x(:,7:12));x(:,67:120)=round(x(:,67:120));x(:,13:66)=x(:,13:66).*x(:,67:120);% % 如果应急储备库设置点x_j=0,则x_ij,应急物资储备库j运输到应急需求点i的物资量为0% for i = 1:6% if(x(6+i)==0)%x(13+()*9)=0;% end% endend%% 计算目标函数for i =1:pop_size[ff(i,:) err(i,:)] =feval(fname, x(i,:));% 计算目标函数enderror_norm=normalisation(err); % 约束条件规一化population_init=[x ff error_norm];[population front]=NDS_CD_cons(population_init); % 初始种群非支配排序%% 开始迭代for gen_count=1:gen_max% 选择父种群parent_selected=tour_selection(population); % 10 锦标赛选择法%% 产生子代child_offspring = genetic_operator(parent_selected(:,1:V)); % SBX crossover and polynomial mutationfor ii = 1:pop_size[fff(ii,:) err(ii,:)]=feval(fname, child_offspring(ii,:)); % 子代种群目标函数计算enderror_norm=normalisation(err); child_offspring=[child_offspring fff error_norm];%% 中间种群 (Rt= Pt U Qt of 2N size)population_inter=[population(:,1:V+M+1) ; child_offspring(:,1:V+M+1)];[population_inter_sorted front]=NDS_CD_cons(population_inter); % 非支配排序%% 替代操作 - Nnew_pop=replacement(population_inter_sorted, front);population=new_pop;endnew_pop=sortrows(new_pop,V+1);paretoset(run).trial=new_pop(:,1:V+M+1);Q = [Q; paretoset(run).trial]; endtoc%% 保存结果到excel表格中xlswrite('new_pop.xls', new_pop);xlswrite('f.xls', [new_pop(:,V+1) new_pop(:,V+2)]);%% 结果和绘制pareto前沿if run==1plot(new_pop(:,V+1),new_pop(:,V+2),'*')else[pareto_filter front]=NDS_CD_cons(Q); rank1_index=find(pareto_filter(:,V+M+2)==1); % pareto前沿序列1pareto_rank1=pareto_filter(rank1_index,1:V+M);plot(pareto_rank1(:,V+1),pareto_rank1(:,V+2),'*') % 绘制最终pareto结果endxlabel('objective function 1')ylabel('objective function 2')if p==1title(' 1 - Test case 1')elseif p==2title(' 2 - ZDT1')elseif p==3title(' 3 - KUR')elseif p==4title(' 4 - SCH')elseif p==5title(' 5 - ZDT2')elseif p==6title(' 6 - Test case 3')elseif p==7title(' 7 - ZDT3')elseif p==8title(' 8 - ZDT4')elseif p==9title(' 9 - ZDT6')elseif p==10title(' 10 - BNH')elseif p==11title(' 11 - SRN')elseif p==12title(' 12 - TNK')elseif p==13title(' 13 - OSY')elseif p==14title(' 14 - CONSTR')elseif p==15title(' 15 - EMERGE')end

3 仿真结果

4 参考文献

[1]徐慧英等. "改进NSGA Ⅱ算法在车辆路径多目标优化问题中的应用." 计算机工程与科学 32.10():117-121.

5 MATLAB代码与数据下载地址

见博客主页

如果觉得《【优化求解】基于NSGAII算法求解含约束多目标优化问题matlab代码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。