nsga2算法matlab代码
时间: 2023-08-01 14:06:32 浏览: 139
以下是NSGA-II算法的Matlab代码示例:
```matlab
function [pop,front]=nsga2(pop,np,pc,pm,max_gen)
%初始化
pop=initialize_pop(np);
pop=evaluate_pop(pop);
pop=fast_nondominated_sort(pop);
[pop,crowd]=crowding_distance(pop);
%迭代开始
for i=1:max_gen
%选择操作
mating_pool=tournament_selection(pop,np);
%交叉操作
offspring=crossover(mating_pool,pc);
%变异操作
offspring=mutate(offspring,pm);
%评估新一代个体
offspring=evaluate_pop(offspring);
%合并父代和子代种群
combined_pop=[pop,offspring];
%快速非支配排序
combined_pop=fast_nondominated_sort(combined_pop);
%计算拥挤度
[combined_pop,crowd]=crowding_distance(combined_pop);
%选择下一代个体
pop=selection(combined_pop,np,crowd);
end
%最终的非支配层和拥挤度计算
pop=fast_nondominated_sort(pop);
[pop,front]=crowding_distance(pop);
end
%快速非支配排序
function pop=fast_nondominated_sort(pop)
n=length(pop);
for i=1:n
pop(i).S=[];
pop(i).n=0;
for j=1:n
if i~=j
if dominates(pop(i),pop(j))
pop(i).S=[pop(i).S,j];
elseif dominates(pop(j),pop(i))
pop(i).n=pop(i).n+1;
end
end
end
if pop(i).n==0
pop(i).rank=1;
end
end
front=1;
while ~isempty(find([pop.rank]==front, 1))
Q=[];
for i=1:n
if pop(i).rank==front
for j=1:length(pop(i).S)
pop(pop(i).S(j)).n=pop(pop(i).S(j)).n-1;
if pop(pop(i).S(j)).n==0
pop(pop(i).S(j)).rank=front+1;
Q=[Q,pop(i).S(j)];
end
end
end
end
front=front+1;
end
end
%计算拥挤度
function [pop,crowd]=crowding_distance(pop)
n=length(pop);
for i=1:n
pop(i).crowd=0;
end
for m=1:length(pop(1).obj)
[pop,ord]=sort_population(pop,m);
pop(ord(1)).crowd=inf;
pop(ord(end)).crowd=inf;
fmax=pop(end).obj(m);
fmin=pop(1).obj(m);
for i=2:n-1
pop(i).crowd=pop(i).crowd+(pop(i+1).obj(m)-pop(i-1).obj(m))/(fmax-fmin);
end
end
crowd=[pop.crowd];
end
%选择操作
function mating_pool=tournament_selection(pop,np)
n=length(pop);
mating_pool=repmat(pop(1),1,np);
for i=1:np
p=randperm(n,2);
if pop(p(1)).rank<pop(p(2)).rank
mating_pool(i)=pop(p(1));
elseif pop(p(1)).rank>pop(p(2)).rank
mating_pool(i)=pop(p(2));
else
if pop(p(1)).crowd>pop(p(2)).crowd
mating_pool(i)=pop(p(1));
else
mating_pool(i)=pop(p(2));
end
end
end
end
%交叉操作
function offspring=crossover(mating_pool,pc)
n=length(mating_pool);
offspring=repmat(mating_pool(1),1,n);
for i=1:2:n
if rand<pc
p=randperm(n,2);
[offspring(i),offspring(i+1)]=cross(mating_pool(p(1)),mating_pool(p(2)));
else
offspring(i)=mating_pool(i);
offspring(i+1)=mating_pool(i+1);
end
end
end
%变异操作
function offspring=mutate(offspring,pm)
n=length(offspring);
for i=1:n
if rand<pm
offspring(i)=mut(offspring(i));
end
end
end
%选择下一代个体
function pop=selection(combined_pop,np,crowd)
[~,ord]=sort_population(combined_pop,1);
pop=[];
i=1;
while length(pop)<np
if combined_pop(ord(i)).rank==1
pop=[pop,combined_pop(ord(i))];
else
break
end
i=i+1;
end
if length(pop)<np
[~,ord]=sort(crowd(ord(i:end)),'descend');
pop=[pop,combined_pop(ord(i-1+ord(1:np-length(pop))))];
end
end
%排序操作
function [pop,ord]=sort_population(pop,m)
fvals=[pop.obj];
[~,ord]=sort(fvals(m,:));
pop=pop(ord);
end
%判断是否支配
function flag=dominates(a,b)
flag=all(a.obj<=b.obj) && any(a.obj<b.obj);
end
```
阅读全文