📄 thlizi.m
字号:
function THlizi
for iii=1:10
xmax=10;
wstart=0.95; %初始惯性权重
wend=0.4; %终止惯性权重
c1start=3; %c1初始值
c1end=1; %c1终止值
c2start=1; %c2初始值
c2end=3; %c2终止值
MaxDT=10000; %跌代次数
p3=0.3; %繁殖概率
n1=100; %种群数
n2=10; %变量维数
A=rand(n1,n2);
pop=-10+(10+10)*A ; %随机初始化位置
B=rand(n1,n2);
V=-10+(10+10)*B ; %随机初始化速度
objvalue=calobjvalue(pop);
Pbest=pop;
[ggBest,xindex]=max(-objvalue);
xtemp=pop(xindex,1:n2);
Gbest=xtemp;
for n=1:MaxDT
c1=(c1start-c1end)*(n/MaxDT)^2+(c1end-c1start)*(2*n/MaxDT)+c1start;
c2=(c2end-c2start)*(n/MaxDT)^2+(c2start-c2end)*(2*n/MaxDT)+c2start;
w=(wstart-wend)*(n/MaxDT)^2+(wend-wstart)*(2*n/MaxDT)+wstart;
for i=1:n1
for j=1:n2
sub1=Pbest(i,j)-pop(i,j);
sub2=Gbest(1,j)-pop(i,j);
tempV=w*V(i,j)+c1*rand*sub1+c2*rand*sub2;
if tempV>xmax
V(i,j)=xmax;
elseif tempV<(-xmax)
V(i,j)=-xmax;
else
V(i,j)=tempV;
end
tempposition=pop(i,j)+V(i,j);
if tempposition>xmax
pop(i,j)=xmax;
elseif tempposition<-xmax
pop(i,j)=-xmax;
else
pop(i,j)=tempposition;
end
end
end
objvalue1=calobjvalue(Pbest);
objvalue2=calobjvalue(pop);
for i=1:n1
if objvalue1(i,:)<objvalue2(i,:)
Pbest(i,:)=Pbest(i,:);
elseif objvalue1(i,:)>objvalue2(i,:)
Pbest(i,:)=pop(i,:);
end
end
objvalue=calobjvalue(pop);
[ggBest1,xindex]=max(-objvalue);
if ggBest1<ggBest
Gbest=Gbest;
elseif ggBest1>ggBest
xtemp=pop(xindex,1:n2);
Gbest=xtemp;
ggBest=ggBest1;
end
d=fix(p3*n1);
for q=1:d
b=fix(1+(n1-1)*rand);
c=fix(1+(n1-1)*rand);
p=rand(1,n2);
pop(b,:)=p.*pop(b,:)+(1-p).*pop(c,:);
pop(c,:)=p.*pop(c,:)+(1-p).*pop(b,:);
V(b,:)=(V(b,:)+V(c,:))/(abs(V(b,:)+V(c,:)))*abs(V(b,:));
V(b,:)=(V(b,:)+V(c,:))/(abs(V(b,:)+V(c,:)))*abs(V(c,:));
end
for i=1:n1
for j=1:n2
tempV=V(i,j);
if tempV>xmax
V(i,j)=xmax;
elseif tempV<(-xmax)
V(i,j)=-xmax;
else
V(i,j)=tempV;
end
tempposition=pop(i,j);
if tempposition>xmax
pop(i,j)=xmax;
elseif tempposition<-xmax
pop(i,j)=-xmax;
else
pop(i,j)=tempposition;
end
end
end
end
ggBest2(iii,:)=-ggBest ;
Gbest2(iii,:)=Gbest ;
end
ggBest2
Gbest2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -