www.pudn.com > PSO-algorithm.rar > main.asv, change:2011-12-08,size:1796b

```clear;
clc;
%jsp_pso
PAR_NUM=50;
GEN_NUM=1000;
JOB_NUM=6;
MAC_NUM=6;

%初始化
n=JOB_NUM*MAC_NUM;
particle=rand(PAR_NUM,n+1);  %相当于Chrome
vel=rand(PAR_NUM,n);

%evaluate
for i=1:PAR_NUM

[A B]=sort(particle(i,1:n));
array=ceil(B/MAC_NUM);% 相当于S，一个种群的的基因大小
makespan=jp_makespan(array);
particle(i,n+1)=makespan;

end

pbest_p=zeros(PAR_NUM,n);
gbest_p=zeros(1,n);              %pbest_p和gbest_p分别代表单个粒子最优序列和种群最优序列；

pbest_v=zeros(PAR_NUM,1);
gbest_v=zeros(1,1);                %pbest_v和gbest_v分别代表单个粒子和种群的最小makespan;

for i=1:PAR_NUM

pbest_p(i,1:n)=particle(i,1:n);
pbest_v(i)=particle(i,n+1);

end

[I J]=sort(particle(:,n+1));

gbest_p=particle(J(1),1:n);
gbest_v=particle(J(1),n+1);

e=0.729;
k1=2;
k2=2.1;
iter=0;

while iter<GEN_NUM

for i=1:PAR_NUM

%update
A=rand(1);
B=rand(1);
vel(i,:)=e*vel(i,:)+k1*A*(pbest_p(i,:)-particle(i,1:n))+k2*B*(gbest_p-particle(i,1:n));
particle(i,1:n)=particle(i,1:n)+vel(i,:);

%evaluate
[G H]=sort(particle(i,1:n));
array=ceil(H/MAC_NUM);
makespan=jp_makespan(array);
particle(i,n+1)=makespan;

if particle(i,n+1)<pbest_v(i)

pbest_p(i,:)=particle(i,1:n);
pbest_v(i)=particle(i,1+n);

end

if pbest_v(i)<gbest_v

gbest_p=pbest_p(i,:);
gbest_v=pbest_v(i)

end

end

iter=iter+1;

end

[zz qq]=sort(gbest_p);
array=ceil(qq/MAC_NUM);
[jobname t_start t_end]=best(array);
gant6c6(jobname,t_start,t_end,gbest_v)
gbest_v```