Codes in MATLAB For Particle Swarm Optimization: Function
Codes in MATLAB For Particle Swarm Optimization: Function
Particle swarm optimization (PSO) codes in MATLAB suitable for solving constrained optimization problem
Save the following codes in MATLAB script file (*.m) and save as ofun.m.
----------------------------------------------------------------------------------------------------------------------------------start
function f=ofun(x)
c0=[];
c0(1)=x(1)+x(2)+x(3)-5; % <=0 type constraints
c0(2)=x(1)^2+2*x(2)-x(3); % <=0 type constraints
Save the following main program codes in MATLAB script file (*.m) as run_pso.m (any name can be used) and
run.
----------------------------------------------------------------------------------------------------------------------------------start
tic
clc
clear all
close all
rng default
1
ResearchGate, March 2016
% pso algorithm---------------------------------------------------start
ite=1;
tolerance=1;
while ite<=maxite && tolerance>10^-12
% evaluating fitness
for i=1:n
f(i,1)=ofun(x(i,:));
end
2
ResearchGate, March 2016
pbest(i,:)=x(i,:);
f0(i,1)=f(i,1);
end
end
% calculating tolerance
if ite>100;
tolerance=abs(ffmin(ite-100,run)-fmin0);
end