1 简介
NSGA(非支配排序遗传算法)、NSGAII(带精英策略的非支配排序的遗传算法),都是基于遗传算法的多目标优化算法,都是基于pareto最优解讨论的多目标优化.
2 部分代码
clear all
clc
global V M xl xu etac etam p pop_size pm
%% 程序开始运行
M=2;
p=1;%input('输入测试问题编号:');
tic
pop_size=300; % 种群数量,这个变量可以自己选择大小,50、100、200、300、500等
no_runs=1; % 运行次数
gen_max=150; % 最大迭代次数,可修改100
fname='test_case'; % 目标函数和约束条件
if p==13 % OSY
pop_size=100;
no_runs=10;
end
if (p==2 | p==5 | p==7), gen_max=1000; end;
if p<=9 % p<=9是非约束测试函数Unconstrained test functions
tV=[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=120
v_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是否救援需求点i
txu_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; %100
else % 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:NN
etac = 20;
etam = 100;
end
pm=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
% end
end
%% 计算目标函数
for i =1:pop_size
[ff(i,:) err(i,:)] =feval(fname, x(i,:)); % 计算目标函数
end
error_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 mutation
for ii = 1:pop_size
[fff(ii,:) err(ii,:)]=feval(fname, child_offspring(ii,:)); % 子代种群目标函数计算
end
error_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); % 非支配排序
%% 替代操作 - N
new_pop=replacement(population_inter_sorted, front);
population=new_pop;
end
new_pop=sortrows(new_pop,V+1);
paretoset(run).trial=new_pop(:,1:V+M+1);
Q = [Q; paretoset(run).trial];
end
toc
%% 保存结果到excel表格中
xlswrite('new_pop.xls', new_pop);
xlswrite('f.xls', [new_pop(:,V+1) new_pop(:,V+2)]);
%% 结果和绘制pareto前沿
if run==1
plot(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前沿序列1
pareto_rank1=pareto_filter(rank1_index,1:V+M);
plot(pareto_rank1(:,V+1),pareto_rank1(:,V+2),'*') % 绘制最终pareto结果
end
xlabel('objective function 1')
ylabel('objective function 2')
if p==1
title(' 1 - Test case 1')
elseif p==2
title(' 2 - ZDT1')
elseif p==3
title(' 3 - KUR')
elseif p==4
title(' 4 - SCH')
elseif p==5
title(' 5 - ZDT2')
elseif p==6
title(' 6 - Test case 3')
elseif p==7
title(' 7 - ZDT3')
elseif p==8
title(' 8 - ZDT4')
elseif p==9
title(' 9 - ZDT6')
elseif p==10
title(' 10 - BNH')
elseif p==11
title(' 11 - SRN')
elseif p==12
title(' 12 - TNK')
elseif p==13
title(' 13 - OSY')
elseif p==14
title(' 14 - CONSTR')
elseif p==15
title(' 15 - EMERGE')
end
3 仿真结果
4 参考文献
[1]徐慧英等. "改进NSGA Ⅱ算法在车辆路径多目标优化问题中的应用." 计算机工程与科学 32.10(2010):117-121.
5 MATLAB代码与数据下载地址
见博客主页