粒子群求极值 matlab,MATLAB粒子群优化算法实现(PSO)

%------初始格式化--------------------------------------------------

clear all;

clc;

format long;

%------给定初始化条件----------------------------------------------

c1=2; %学习因子1

c2=2; %学习因子2

w=0.7298; %惯性权重

MaxDT=200; %最大迭代次数

% D=2; %搜索空间维数(未知数个数)

N=20; %初始化群体个体数目

%eps=10^(-6); %设置精度(在已知最小值时候用)

Vmax=1;

Vmin=-1;

popmax=5;

popmin=-5;

%------初始化种群的个体(可以在这里限定位置和速度的范围)------------

for i=1:N

pop(i,:)=popmin+(popmax-popmin)*rand(1,2); %随机初始化位置

V(i,:)=rand(1,2); %随机初始化速度

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

end

%------先计算各个粒子的适应度,并初始化Pi和Pg----------------------

[fitnessgbest bestindex]=min(fitness);

gbest=pop(bestindex,:);

pbest=pop;

fitnesspbest=fitness;

for i=1:MaxDT

for j=1:N

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

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

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

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

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

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

% if rand>0.8

% k=ceil(2*rand);

% pop(j,k)=rand;

% end

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

if fitness(j)

pbest(j,:)=pop(j,:);

fitnesspbest(j)=fitness(j);

end

if fitness(j)

gbest=pop(j,:);

fitnessgbest=fitness(j);

end

end

yy(i)=fitnessgbest;

end

%------最后给出计算结果

plot(yy)

title(['适应度曲线 ' '终止次数=' num2str(MaxDT)]);

xlabel('进化代数');

ylabel('适应度')

%------算法结束---DreamSun GL & HF-----------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值