php 数组大于1,位置 1 的索引超出数组范围(不能超过 1)。

%清空运行环境

clc

clear

%速度更新参数

c1 = 1.49445;

c2 = 1.49445;

maxgen = 300; %最大迭代次数

sizepop = 20; %种群规模数

%个体和速度最大最小值,即为位置速度边界

popmax = 2;

popmin = -2;

Vmax = 0.5;

Vmin = -0.5;

%种群初始化

for i = 1:sizepop

%产生随机种群

pop(i,:)=2*rands(1,2); %初始化粒子,随机生成1行2列的数组

V(i,:)=0.5*rands(1,2); %初始化速度

%计算粒子适应度值

fitness(i)= fun(pop(i,:));

end

%寻找最初极值

[bestfitness bestindex] = min(fitness);%记录极值及其序号

zbest = pop(bestindex,:);%群体极值位置

gbest = pop;  %个体极值位置

w=0.8;%惯性权重

fitnessgbest = fitness; %个体极值适应度值

fitnesszbest = bestfitness; %群体极值适应度值

%迭代寻优

for i = 1:maxgen

%粒子位置速度更新

for j = 1:sizepop

%速度更新

V(j,:)=V(j,:)+c1*rand*(gbest(j,:)-pop(j,:))+c2*rand*(zbest-pop(j,:));

V(j,find(V(j,:)>Vmax)) = Vmax;

V(j,find(V(j,:)

%位置更新判断

pop(j,:) = pop(j,:)+0.5*V(j,:);

pop(j,find(pop(j,:)

pop(j,find(pop(j,:)>popmax)) = popmax;

%适应度更新

fitness(j) = fun(pop(j,:));

end

%个体极值和群体极值更新

for j = 1:sizepop

%个体极值更新

if fitness(j) > fitnessgbest(j)

gbest=pop(j,:);

fitnessgbest(j) = fitness(j);

end

%群体极值更新

if fitness(j) > fitnesszbest

zbest=pop(j,:);

fitnesszbest = fitness(j);

end

end

%每代最优值记录到yy数组中

result(i) = fitnesszbest;

end

%画图

plot(result);

title('最有个体适应度','fontsize',12);

xlabel('进化代数','fontsize',12);

ylabel('适应度值','fontsize',12);

%适应度函数

function y = fun(x)

%x 为粒子的位置

%y 为粒子的适应度值

y=(sqrt(x(1).^2+x(2).^2)./sqrt(x(1).^2+x(2).^2)+...

exp((cos(2*pi*x(1))+cos(2*pi*x(2))))/2)-2.71289;

end

报错:位置 1 的索引超出数组范围(不能超过 1)。

出错 jobshop (line 204)

V(j,:)=V(j,:)+c1*rand*(gbest(j,:)-pop(j,:))+c2*rand*(zbest-pop(j,:));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值