遗传算法物流中心选址优化代码,在MATLAB里一直运行没有反应,各位大神能帮忙看下吗?我这方面真的不太懂

clear
clc
global gen;
G=300;
NP=100;
chromsome=24;%染色体的长度
Pc=0.5;%交叉概率
Pm=0.2;
gen=0;
max=600;
min=145;
l=5;
n=4;
M=[200 400 600 200];
D=[174 232 439 145 221];
f=[9;11;11;7];
a=[4;5;3;3;4;4;2;4;3;2;3;3;2;3;2;3;3;3;3;4];
c=[16;58;30;25;51;45;27;19;32;21;74;5;76;8;48;60;67;47;66;41];
%%目标函数
%function minF
for i=1:NP
while 0 for j=1:4
chrom1(i,j)=round( rand(1));
end
if sum(chrom1(i,:),2)>=1
break
end
end
end
sumx=zeros(NP,4);
for i=1:NP
for j=1:4
if chrom1(i,j)==0
chrom2(i,(5*(j-1)+1):(5*j))=0;
else
while chrom1(i,j)==1
chrom2(i,(5*(j-1)+1):(5*j))=rand(1,5).*(rep([M(j)],[1 l]));
sumx(i,j)=sum(chrom2(i,(5*(j-1)+1):(5*j)));
end
end
end
end
chrom=[chrom1 chrom2];%产生初始种群
%种群初始化
%initPop=initializega(100,[145 600],'fitness');
% [x endPop bpop trace]=ga([145 600],'calfitvalue',[],chrom,[1e-6 2 1],'maxGenTerm',800,...
% 'arithXover',0.5,'nonUnifMutation',0.2);
% x
% hold on
% plot(endPop(:,1),endPop(:,2),'ro')
% figure(2)
% plot(trace(:,1),trace(:,3),'b:')
% hold on
% plot(trace(:,1),trace(:,2),'r-:')
% xlabel('迭代次数');ylabel('适应度');
% legend('Mean Fitness','Best Fitness')

%f=rand(chromsome,NP)*(max-min)+min;
%fitness=1/minF;

%%按适应度升序排列
% for np= 1:NP
% MSLL(np)=func2;
%%
[objvalue]=calobjvalue(chrom,n,l,a,c);
[fitvalue,restriction]=calfitvalue(objvalue,chrom,max,n,1,M,D);
[bestindividual,bestfit,bestrestriction,nopos]=best(chrom,fitvalue,restriction);
gen=0;
while gen [objvalue]=calobjvalue(chrom,n,l,a,c,f);
[fitvalue,restriction]=calfitvalue(objvalue,chrom,max,m,n,1,M,D);
[bestindividuall ,bestfitl ,bestrestrictionl ,nopos1 ]=best(chrom,fitvalue,restriction);
if bestrestriction>bestrestrictionl
bestindividual=bestindividual1 ;
bestfit=bestfit1;
bestrestriction=bestrestriction1;
end
if (bestrestriction==bestrestrictionl)&(bestfit bestindividual=bestindividual1;
bestfit=bestfit1;
bestrestriction=bestrestriction1;
end
chrom(nopos1,: )=bestindividual;
[newchrom]=selection(chrom,fitvalue);
[newchrom]=crossover(newchromn,l);
[newchrom]=mutation(newchrom,Pm,n,l);
[bestindividual2,bestfit2,bestrestriction2,nopos2]=best(newchrom,fitvalue,restriction);
if bestrestriction>bestrestriction2
bestindividual=bestindividual2;
bestfit=bestfit2;
bestrestriction=bestrestriction2;
end
if (bestrestriction==bestrestriction2)&(bestfit<bestfit2)
bestindividual=bestindividual2;
bestfit=bestfit2;
bestrestriction=bestrestriction2;
end
chrom=newchrom;
gen=gen+1;
end
bestindividual,bestfit,bestrestriction

figure(1)
plot(sumx(:, 1), sumx(:, 2),'y*')
figure(2)
plot(gen(:, 1), gen(:, 2), 'r-')
xlabel('Generation');
ylabel('Fittness');

%目标函数
function [objvalue]=calobjvalue(chrom,n,l,a,c,f,D)
chrom1=chrom(:,1:n);
chrom2=chrom(:,(n+l):(n+l*n));
%%[NP,chromsome]=size(chrom);
for i=1:NP
for j=l :n
u(ij)=120*sum(chrom2(i,(2*(j-1)+1):(2*j)),2);
end
end
objvalue=chrom2*a*c*D+chrom1*f+chrom2*300*(D/15);
%适应度计算和约束判断
function [fitvalue, restriction]=calfitvalue(objvalue,chrom,max,n,l,M,D)
global gen;
%%[NP,chromsome]=size(chrom);
chrom1=chrom(:,1:n);
chrom2=chrom(:,(n+1):(n+l*n));
restriction=zeros(NP,1);
s=zeros(NP,n);
t=zeros(NP,1);
u=zeros(NP,2);
p=zeros(NP,n);
for i=1:NP
for j=1:1
t(i, j)=sum((chrom2(i,j:l:n*1)),2)-D(j);
if t(ij) restriction(i,1)=restriction(i,1)+1;
end
end
for j=l :n
s(i,j)=chrom1(i,j)*M(j)-sum(chrom2(i,(m*(j-1)+1):(m*j)),2);
p(i,j)=abs(sum(chrorn3(i,(1*(j-1)+1):(1*j)),2)-sum(chrom2(i,(m*(j-1)+1):(m*J)),2));
if s(ij) restriction(i,1)=restriction(i,1)+1;
end
if p(ij)>=1e-3
restriction(i,1)=restriction(i,1)+1;
end
end
u(i, l )=P-sum(chrom1(i,:),2);
if u(i, l ) restriction(i,1)=restriction(i,1)+1;
end
u(i,2)=sum(chrom1(i,:),2)-1;
if u(i,2) restriction(i, l)=restriction(i,1)+ 1;
end
if (objvalue(i, l ) fitvalue(i,1)=max-objvalue(i,1);
else
fitvalue(i, l )=0.0;
end
end
%找出最优个体和最差个体
function [bestindividual,bestfit,bestrestriction,nopos]=best(chrom,fitvalue,restriction)
%%[NP,chromsome]=size(chrom);
pos=1;
for i=1:NP
if restriction(pos, l )>restriction(i, l )
pos=i;
end
if (restriction(pos,1)==restriction(i,1))&&(fitvalue(pos,1) pos=i;
end
end
bestindividual=chrom(pos,:);
bestfit=fitvalue(pos);
bestrestriction=restriction(pos,:);
nopos=1;
for i=1 :NP
if restriction(nopos, l ) nopos=i;
end
if (restriction(nopos,1)==restriction(i,1))&&(fitvalue(nopos,1)>fitvalue(i,1))
nopos=i;
end
end

%选择
function [newchrom]=selection(chrom,fitvalue)
totalfit=sum(fitvalue);
fitvalue=fitvalue/totalfit;
fitvalue=cumsum(fitvalue);
%%[NP,chromsome]=size(chrom);
ms=sort(rand(NP, l ));
fitin=1;newin=1;
while newin<=NP
if(ms(newin)) temp(newin,:)=chrom(fitin,:);
newin=newin+ 1;
else
fitin=fitin+l;
end
if fitin>=NP
fitin=NP;
end
end
newchrom=temp;

%交叉
function [newchrom]=crossover(chrom,n,l)
global gen;
%%[NP,chromsone]=size(chrom);
chrom1=chrom(:, l :n);
chrom2=chrom(:,(n+1):(n+l*n));
newchrom=zeros(NP,chromsome);
for i=1:2:NP-1
if (rand<Pc)
point=ceil(rand*(n-1));
if point<5
newchrom(i,:)=[chroml(i,l:point) chroml(i+l,point+l:n)...
chrom2(i, l :n*point) chrom2(i+l,n*point+l :l*n)];
newchrom(i+1,:)=[chrom1(i+l,l:point) chroml(i,point+l:n)...
chrom2(i+1,1:1*point) chrorn3(i,l*point+l:n*1)];
else
newchrom(i,:)=chrom(i,:);
newchrom(i+1,:)=chrom(i+1,:);
end
else
newchrom(i,:)=chrom(i,:);
newchrom(i+1,:)=chrom(i+1,:);
end
end

%变异
function [newchrom]=mutation(chrom,Pm,n,l)
global gen;
Fie1dDR=[0 0 0 0 0 0 0 0 ;200 200 400 400 600 600 200 200];
RANGE=[0 0 0 0 0;174 232 439 145 221];
[NP,chromsome]=size(chrom);
chroml =chrom(:, l :n);
chrom2=chrom(:,(n+l ):(n+l*n));
newchrom=zeros(NP,chromsome);
newchrom1=zeros(NP,n);
newchrom2=zeros(NP,l*n);
for i=1:NP
for j=l :n
if chrom1(ij)==0
newchrom2(i,(l*(j-1)+1):(l*j))=0;
else
if round(rand)==0
newchrom2(i,(1 * (j-1)+1): (1 * j))=chrorn2(i,(1 * (j-1)+1):(1*j))+...,
(D-chrom2(i,(1*(j-1)+1):(1*j)))*(1-rand^((1-gen/800)^10));
elseif round(rand)==1
newchrom2(i,(1*(j-1)+1):(1*j))=chrom2(i,(1*(j-1)+1):(1*j))-...,
(chrom3(i,(1*(j-1)+1):(1*j))-[0 0 0 0 ] ) *(1-rand^((1-gen/800)^ 10));
end
end
end
end
newchrom1=chroml;
newchrom=[newchrom1 newchrom2];
figure(1)
plot(1:iter, fitness_ave, 'r', 1:iter, fitness_best, 'b')
grid on
legend('平均适应度', '最优适应度')
e = PlotModel(chrom_best)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问