• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

粒子群优化算法—Matlab

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

PSO算法

clc;
clear ;
close ;
%%  Problem Definition

CostFunction = @(x) sphere(x);  % Cost Function

nVar = 5;                       % Dimension of Decision Variables

VarSize = [1,nVar];             % Matrix Size of Decision Variables

VarMin = -10;                   % Lower Bound of Decision Variables
VarMax =  10;                   % Upper Bound of Decision Variables

 
%%  Parameters of PSO

MaxIt = 1000;            % Maximum Number of Iterations

nPop = 50;               % Population Size 

w  = 1;                  % Inertia Coefficient
wdamp = 0.81;            % Damping Ratio of Inertia Coefficient
c1 = 2;                  % Personal Acceleration Coefficient
c2 = 2;                  % Social Acceleration Coefficient

%%  Initialization

% The Patticle Template
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];

% Create Population Array
particle = repmat(empty_particle,nPop,1);

% Initialize Global Best
GlobalBest.Cost = inf;

% Iniitialize Population Members
for i=1:nPop
    
    % Generate Random Solution
    particle(i).Position = unifrnd(VarMin,VarMax,VarSize);
    
    % Initialize Velocity
    particle(i).Velocity = zeros(VarSize);
    
    % Evaluation
    particle(i).Cost = CostFunction(particle(i).Position);
    
    % Update the Personal Best 
    particle(i).Best.Position = particle(i).Position;
    particle(i).Best.Cost = particle(i).Cost;
    
    % Update Global Best
    if particle(i).Best.Cost < GlobalBest.Cost
        GlobalBest = particle(i).Best;
    end
        
end

% Array to Hold Best Cost Value
BestCosts = zeros(MaxIt,1);

%%  Main Loop of PSO

for it=1:MaxIt
    
    for i=1:nPop
        
        % Update Velocity
        particle(i).Velocity =  w*particle(i).Velocity ...
          + c1*rand(VarSize).*(particle(i).Best.Position - particle(i).Position)...
          + c2*rand(VarSize).*(GlobalBest.Position - particle(i).Position);
        % Update Position
        particle(i).Position = particle(i).Position + particle(i).Velocity;
        
        % Evaluation
        particle(i).Cost = CostFunction( particle(i).Position);
        
        % Update Personal Best
        if particle(i).Cost < particle(i).Best.Cost
            
            particle(i).Best.Position = particle(i).Position;
            particle(i).Best.Cost = particle(i).Cost;
            
            % Update Global Best
            if particle(i).Best.Cost < GlobalBest.Cost
                GlobalBest = particle(i).Best;
            end
            
        end
        
    end
    
    % Store the Best Cost Value
    BestCosts(it) = GlobalBest.Cost;
    
    % Display Iteration Information
    disp([\'Iteration \' num2str(it) \': Best Cost = \' num2str(BestCosts(it))]);
    
    % Damping Inertia Coefficient
    w = w * wdamp;
    
end

%%  Results

figure;
plot(BestCosts,\'LineWidth\',2);
semilogy(BestCosts,\'LineWidth\',2);
xlabel(\'Iterations\');
ylabel(\'Best Cost\');
grid on;

测试函数

function z = sphere(x)
%%  目标函数
    z = sum(x.^2);
end

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphiidhttp实战用法(TIdhttpEx)发布时间:2022-07-18
下一篇:
Delphi2007下cxComboBox乱码.发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap