• 大小: 4KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: Matlab
  • 标签: PSO  

资源简介

matlab写的粒子群优化算法(Particle Swarm Optimization),测试过,很好用!

资源截图

代码片段和文件信息

%
% Copyright (c) 2015 Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the “license.txt“ for license terms.
%
% Project Code: YPEA102
% Project title: Implementation of Particle Swarm Optimization in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)

% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)

% Contact Info: sm.kalami@gmail.com info@yarpiz.com
%

clc;
clear;
close all;

%% Problem Definition

% CostFunction=@(x) Sphere(x);        % Cost Function
CostFunction=@(x) Ackley(x);        % Cost Function

nVar=30;            % Number of Decision Variables

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

VarMin=-32;         % Lower Bound of Variables
VarMax= 32;         % Upper Bound of Variables


%% PSO Parameters

MaxIt=1000;      % Maximum Number of Iterations

nPop=50;        % Population Size (Swarm Size)

% PSO Parameters
w=1;            % Inertia Weight
wdamp=0.99;     % Inertia Weight Damping Ratio
c1=1.5;         % Personal Learning Coefficient
c2=2.0;         % Global Learning Coefficient

% If you would like to use Constriction Coefficients for PSO
% uncomment the following block and comment the above set of parameters.

% % Constriction Coefficients
% phi1=2.05;
% phi2=2.05;
% phi=phi1+phi2;
% chi=2/(phi-2+sqrt(phi^2-4*phi));
% w=chi;          % Inertia Weight
% wdamp=1;        % Inertia Weight Damping Ratio
% c1=chi*phi1;    % Personal Learning Coefficient
% c2=chi*phi2;    % Global Learning Coefficient

% Velocity Limits
VelMax=0.1*(VarMax-VarMin);
VelMin=-VelMax;

%% Initialization

empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];

particle=repmat(empty_particlenPop1);

GlobalBest.Cost=inf;

for i=1:nPop
    
    % Initialize Position
    particle(i).Position=unifrnd(VarMinVarMaxVarSize);
    
    % Initialize Velocity
    particle(i).Velocity=zeros(VarSize);
    
    % Evaluation
    particle(i).Cost=CostFunction(particle(i).Position);
    
    % Update Personal Best
    particle(i

评论

共有 条评论