标准粒子群优化算法(PSO)

global dimension
global popsize
dimension=1;
popsize=1000;  
run_times=10;
max_length=5000;
sample_point=250;
 
w=1.0;
c1=2.0;
c2=2.0;
vmax=600;
xmin=-600;
xmax=600;




current_v=zeros(dimension,popsize);
pbest=zeros(dimension,popsize);








current_position=zeros(dimension,popsize);  %数组预定义
gbest=zeros(dimension,1);
gbest_fitness=zeros(1,popsize);
current_fitness=zeros(1,popsize);
gbest1=zeros(dimension,popsize);
gfitness_sampoint=zeros(run_times,fix(max_length/sample_point));
total_best=zeros(1,run_times);
gfitness_ave=zeros(1,fix(max_length/sample_point));


test_data=zeros(run_times,popsize);


for i=1:run_times  %主程序,运行次数为run_times 
    current_position=(xmax-xmin)*rand(dimension,popsize)+xmin; %微粒参数的初始化
    current_v=vmax*rand(dimension,popsize);
    pbest=current_position;
    pbest_fitness=F11_FITNESS(current_position);  %函数适应值计算
    gbest_fitness=min(pbest_fitness);
    locate=minfitness_locate(pbest_fitness,gbest_fitness);  %群体最优适应值定位
    gbest=pbest(:,locate);
    gbest1=repmat(gbest,1,popsize);
    w=0.9;
    for j=1:max_length  %搜索最优值过程,循环代数为max_length
        w=0.9-(j-1)*0.5/(max_length-1);
        current_v=w*current_v+c1*rand(dimension,popsize).*(pbest-current_position)+c2*rand(dimension,popsize).*(gbest1-current_position);   
        current_v=modify1_v(current_v,vmax); %修改速度参数
        current_position=current_v+current_position;
        current_position=modify_position(current_position,xmax,xmin);   %修改位置参数
        current_fitness=F11_FITNESS(current_position);  %计算个体当前适应值
        [pbest_fitness,pbest]=modify_fitness(pbest_fitness,current_fitness,pbest,current_position);  %修改个体历史最优值和最优位置
        gbest_fitness=min(pbest_fitness);  %修改群体最优值
        
        locate=minfitness_locate(pbest_fitness,gbest_fitness);  %群体最优适应值定位
        gbest=pbest(:,locate); %修改群体最优位置
        gbest1=repmat(gbest,1,popsize);
        if mod(j,sample_point)==0  %进化过程最优值统计
            a=j/sample_point;
            gfitness_sampoint(i,a)=gbest_fitness;
        end
        
         temp_fitness=min(current_fitness);
        locate=minfitness_locate(current_fitness,temp_fitness);
        % test_data(i,locate)=test_data(i,locate)+1;
        aaa(j)=temp_fitness;
    end
    total_best(1,i)=gbest_fitness;  %最优值统计
    gbest_fitness
    plot(1:max_length,aaa);
end
best_fitness=min(total_best); %最优值计算
worst_fitness=max(total_best); %最次值计算
mean_fitness=mean(total_best); %均值计算
std_fitness=std(total_best); %本点均值计算
gfitness_ave=sum(gfitness_sampoint)/run_times; 
ave_data=sum(test_data)/run_times;


format short e %方差计算


mean_fitness %输出所需值
std_fitness
best_fitness  
worst_fitness
gfitness_ave
total_best
plot();

你可能感兴趣的:(优化算法,Matlab)