谁能帮我看看这个 pso程序错在哪里了,我是按照正确代码一点一点编的呀,求大师指导啊
本帖最后由 瞿培华 于 2012-2-18 17:26 编辑global pop; %种群
global c1; %个体最优导向系数
global c2; %全局最优导向系数
global gbest_x; %全局最优解x轴坐标
global gbest_y; %全局最优解y轴坐标
global best_fitness; %最优解
global best_in_history; %最优解变化轨迹
global x_min; %x的下限
global x_max; %x的上限
global y_min; %y的下限
global y_max; %y的上限
global gen; %迭代次数
global exetime; %当前迭代次数
global max_velocity; %最大速度
innitial; %初始化
for exetime=1:gen
outputdata; %实时输出结果
adapting; %计算适应值
errorcompute(); %计算当前种群适值标准差
updatepop; %更新粒子位置
pause(0.01);
end
clear i;
clear exetime;
clear x_max;
clear x_min;
clear y_min;
clear y_max;
for i=1:popsize
pop(i,8)=100*(pop(i,1)^2-pop(i,2)^2+(1-pop(i,1))^2);
if pop(i,7)>pop(i,8)
pop(i,7)=pop(i,8);
pop(i,5:6)=pop(i,1:2);
end
end
if best_fitness>min(pop(:,7))
best_fitness=min(pop(:,7));
gbest_x=pop(find(pop(:,7)==min(pop(:,7))),1);
gbest_y=pop(find(pop(:,7)==min(pop(:,7))),2);
end
best_in_history(exetime)=best_fitness;
gen=100;
popsize=30;
best_in_history(gen)=inf;
best_in_history(: )=inf;
max_velocity=0.3;
best_fitness=inf;
pop(popsize,8)=0;
for i=1:popsize
pop(i,1)=4*rand()-2;
pop(i,2)=4*rand()-2;
pop(i,5)=pop(i,1);
pop(i,6)=pop(i,2);
pop(i,3)=rand()*0.02-0.01;
pop(i,4)=rand()*0.02-0.01;
pop(i,7)=inf;
pop(i,8)=inf;
end
c1=2;
c2=2;
x_min=-2;
y_min=-2;
x_max=2;
y_max=2;
gbest_x=pop(1,1);
gbest_y=pop(1,2);
subplot(1,2,1);
for i=1:popsize
plot(pop(i,1),pop(i,2),'b*');
hold on;
end
plot(gbest_x,gbest_y,'r.','markersize',20);axis([-2,2,-2,2])
hold off;
subplot(1,2,2);
axis();
if exetime-1>0
line(,);
hold on;
end
for i=1:popsize
pop(i,3)=rand()*pop(i,3)+c1*rand()*(pop(i,5)-pop(i,1))+c2*rand()*(gbest_x-pop(i,1));
pop(i,4)=rand()*pop(i,4)+c1*rand()*(pop(i,6)-pop(i,2))+c2*rand()*(gbest_x-pop(i,2));
if abs(pop(i,3))>max_velocity
if pop(i,3)>0
pop(i,3)=max_velocity;
else
pop(i,3)=-max_velocity;
end
end
if abs(pop(i,4))>max_velocity
if pop(i,4)>0
pop(i,4)=max_velocity;
else
pop(i,4)=-max_velocity;
end
end
end
for i=1:popsize
pop(i,1)=pop(i,1)+pop(i,3);
pop(i,2)=pop(i,2)+pop(i,4);
end
这是我的程序,但是运行结果老是出现:如下图
{:3_60:}{:3_60:}{:3_60:}
这么多全局变量 变量太多我没有仔细看,应该是后面用到的变量在前面没有定义或者是定义、拼写时出现错误 新生泪 发表于 2012-2-19 00:09 static/image/common/back.gif
变量太多我没有仔细看,应该是后面用到的变量在前面没有定义或者是定义、拼写时出现错误
matlab要学的很多啊,你说的对,我的这个程序写的比较乱,前面有几个变量没有定义,谢谢了! 这么多变量啊 变量啊 如此之多……
页:
[1]