介绍
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站
目前的多目标优化算法有很多, Kalyanmoy Deb的带精英策略的快速非支配排序遗传算法( nondominated sorting genetic algorithm Il,NSGA-I)无疑是其中应用最为广泛也是最为成功的一种。
clc;clear;
tic;
%% 初始化
PopSize=200;%种群大小
MaxIteration =300;%最大迭代次数
R=50;
location1=load('location1_100.txt');%优化100个城市
location2=load('A_location2_100.txt');
% location1=load('location1.txt');
% location2=load('location2.txt');
CityNum =size(location1,2);%城市数
V=CityNum;
M=2;
pc=0.8;pm=0.9;
for i=1:PopSize
chromosome(i,1:CityNum)=randperm(CityNum);
chromosome(i,CityNum+1:CityNum+2)=costfunction(chromosome(i,1:CityNum),location1,location2);
end
chromosome= non_domination_sort_mod(chromosome);%将解分 最后一列为拥挤度 倒数第二列为分级数
index=find(chromosome(:,103)==1);
costrep=chromosome(index,101:102);%第一级即非劣解
%% 主循环
pool = round(PopSize/2); %突变池规模
for Iteration=1:MaxIteration
if ~mod(Iteration,10)
fprintf('current iter:%d\n',Iteration)
disp([' Number of Repository Particles = ' num2str(size(costrep,1))]);
end
parent_chromosome = selection_individuals(chromosome,pool,2);
parent_var=parent_chromosome(:,1:CityNum);%分离出解向量
%% 交叉
offspring_var=[];offspring_cost=[];
for ic=1:pool/2
m1=randi(pool);%选出交叉向量
m2=randi(pool);
while m1==m2
m1=randi(pool);
end
scro(1,:)=parent_var(m1,:);
scro(2,:)=parent_var(m2,:);
if rand<pc
c1=randi(CityNum);%选出交叉位置
c2=randi(CityNum);
while c1==c2
c1=randi(CityNum);
end
chb1=min(c1,c2);
chb2=max(c1,c2);
middle=scro(1,chb1+1:chb2);
scro(1,chb1+1:chb2)=scro(2,chb1+1:chb2);
scro(2,chb1+1:chb2)=middle;
for i=1:chb1
while find(scro(1,chb1+1:chb2)==scro(1,i))
zhi=find(scro(1,chb1+1:chb2)==scro(1,i));
y=scro(2,chb1+zhi);
scro(1,i)=y;
end
while find(scro(2,chb1+1:chb2)==scro(2,i))
zhi=find(scro(2,chb1+1:chb2)==scro(2,i));
y=scro(1,chb1+zhi);
scro(2,i)=y;
end
end
for i=chb2+1:CityNum
while find(scro(1,1:chb2)==scro(1,i))
zhi=find(scro(1,1:chb2)==scro(1,i));
y=scro(2,zhi);
scro(1,i)=y;
end
while find(scro(2,1:chb2)==scro(2,i))
zhi=find(scro(2,1:chb2)==scro(2,i));
y=scro(1,zhi);
scro(2,i)=y;
end
end
end
if rand<pm%逆序变异
m1=randi(CityNum);
m2=randi(CityNum);
while m1==m2
m1=randi(CityNum);
end
loc1=min(m1,m2);loc2=max(m1,m2);
scro(1,loc1:loc2)=fliplr(scro(1,loc1:loc2));
% tt=scro(1,m2);
% scro(1,m2)=scro(1,m1);
% scro(1,m1)=tt;
end
if rand<pm%对换变异
m1=randi(CityNum);
m2=randi(CityNum);
while m1==m2
m1=randi(CityNum);
end
tt=scro(2,m2);
scro(2,m2)=scro(2,m1);
scro(2,m1)=tt;
end
scro_cost(1,:)=costfunction(scro(1,:),location1,location2);
scro_cost(2,:)=costfunction(scro(2,:),location1,location2);
offspring_var=[offspring_var;scro];%解
offspring_cost=[offspring_cost;scro_cost];%适应度
end
offspring_chromosome(:,1:V)=offspring_var;
offspring_chromosome(:,V+1:V+M)=offspring_cost;
main_pop = size(chromosome,1);
offspring_pop = size(offspring_chromosome,1);
intermediate_chromosome(1:main_pop,:) = chromosome;
intermediate_chromosome(main_pop + 1 :main_pop + offspring_pop,1 : M+V) = ...
offspring_chromosome;
intermediate_chromosome = ...
non_domination_sort_mod(intermediate_chromosome);
%% 选择
chromosome = replace_chromosome(intermediate_chromosome,PopSize);
index=find(intermediate_chromosome(:,103)==1);
costrep=intermediate_chromosome(index,101:102);
cost=intermediate_chromosome(:,101:102);
if ~mod(Iteration,1)
figure (1)
plot(costrep(:,1),costrep(:,2),'r*',cost(:,1),cost(:,2),'kx');
xlabel('F1');ylabel('F2');
title(strcat('Interaction ',num2str(Iteration), ' Pareto non-dominated solutions'));
% hold on
end
if ~mod(Iteration,MaxIteration)
% if ~mod(Iteration,1)
fun_pf=costrep;
[fun_pf,~]=sortrows(fun_pf,1);
plot(fun_pf(:,1),fun_pf(:,2),'k*-');
title(strcat('Interaction ',num2str(Iteration), ' Pareto non-dominated solutions'));
hold on;
grid on;
end
end
运行过程
运行结果
如需帮助VX:zhangshu2274
代码下载链接