一个简单的粒子群算法,在第32行标注的地方一直报错
我检查了这些数组的大小都是一样的啊,为啥会报错 ,看了我一个小时555
clc
clear
c1 = 1.4955;
c2 = 1.4955;
w = 1;
maxgen = 100;
sizepop = 50;
nvar = 2;
vmax = 5 ;
vmin = -5;
popmax = 100;
popmin = -100;
pbest = [];
for i = 1 : sizepop
pop(i,:) = (popmax-popmin) * rand(1,nvar) + popmin;
V(i,:) = (vmax-vmin) * rands(1,nvar) + vmin;
funtion(i) = fun(pop(i,:));
fitness(i) = 1/funtion(i);
end
[bestfitness,bestindex] = max(fitness);
bestfuntion = funtion(bestindex);
bestpop = pop(bestindex,:);
pbest = pop;
pbestfuntion = funtion;
pbestfitness = fitness;
for i = 1 : maxgen
for j = 1 : sizepop
V(j,:) = w.V(j,:) + c1rand*(pbest(j,:)-pop(j,:))+c2rand(bestpop-pop(j,:));
V(j, find(V(j,:)) > vmax) = vmax;
V(j, find(V(j,:)) < vmin) = vmin;
pop(j,:) = pop(j,:) + V(j,:);
pop(j,find(pop(j,:) > popmax)) = popmax;
pop(j,find(pop(j,:) < popmin)) = popmin;
funtion(j) = fun(pop(j,:));
fitness(j) = 1/funtion(j);
end
for j = 1 : sizepop
if fitness(j) > pbestfitness(j)
pbestfitness(j) = fitness(j);
pbestfuntion(j) = funtion(j);
pbest = pop(j,:);
end
if fitness(j) > bestfitness
bestfitness = fitness(j);
bestfuntion = funtion(j);
bestpop = pop(j,:);
end
end
end