📄 ps101010.m
字号:
% 多维数自变量的函数极小优化问题程序
function [xmin, fxmax, iter] = PSO()
num_success = 0; %success counter
sucrate = 0;
sumf = 0;
avestep=0;
sumstep=0;
%%%%%%%%%%%%%%%%%%%%%%
%不同的实验需要改变的参数:实验次数
trialnum = 100; %trial counter
%%%%%%%%%%%%%%%%%%%%%%
% 实验数次循环体开始执行
for num=1:1:trialnum
% Initializing variables
%%%%%%%%%%%%%%%%%%%%%%
%每更改一个函数需要改变的量:函数名称、自变量的上下界限和函数理论极小值
f = 'Rastrigrin'; % Objective Function
upbnd = 5.12; % Upper bound for init. of the swarm
lwbnd = -5.12; % Lower bound for init. of the swarm
foptimal = 0; % minimum fitness value
%%%%%%%%%%%%%%%%%%%%%%
%粒子数和最大迭代次数也需要改变
PopSize = 20; % Size of the swarm
MaxIt = 20000; % Maximum number of iterations
%%%%%%%%%%%%%%%%%
flag_suc = 0; % Success flag
iter = 0; % Iterations’counter
fevals = 0; % Function evaluations’ counter
maxw=0.9; % maximum inertia weight's value
minw=0.2; % minimum inertia weight's value
weveryit=floor(0.75*MaxIt); % Inertia decr.step, floor为取最小的整数,不四舍五入
c1 = 2; % PSO parameter C1
c2 = 2; % PSO parameter C2
inertdec=(maxw-minw)/weveryit; % inertia weight's decrement
%w = 0.6; % inertia weight
w=maxw; % initial inertia weight
dim = 10; % Dimension of the problem
GM = 0; % Global minimum (used in the stopping criterion)
ErrGoal = 1; % Desired accuracy
% Initializing swarm and velocities
popul = rand(dim, PopSize)*(upbnd-lwbnd) + lwbnd; %initialize location x,维数为D*m,popul代表位置x
vel = rand(dim, PopSize); %initialize velocity v,维数为D*m,代表速度v
%Evaluate initial population
for i = 1:PopSize,
fpopul(i) = feval(f, popul(:,i)); %一个粒子对应一个解,适应度个数为解的个数,所以循环体采用的是Popsize
fevals = fevals + 1;
end
%initializing best positions'matrix and the corresponding function values
bestpos = popul; %bestpos 为最好位置
fbestpos = fpopul; %fbestpos 为最好适应度函数
% Finding best particle in initial population
[fbestpart,g] = min(fpopul);
lastbpf = fbestpart;
%swarm evolution loop * start*
while (flag_suc == 0) & (iter < MaxIt),
iter = iter + 1;
%update the value of the inertia weight w
if (iter<=weveryit)
w=maxw-(iter-1)*inertdec; %w linearly decreasing
end
% velocity update
for i=1:PopSize,
A(:,i) = bestpos(:,g);
end
R1 = rand(dim, PopSize);
R2 = rand(dim, PopSize);
vel = w*vel + c1*R1.*(bestpos-popul) + c2*R2.*(A-popul);
% swarm update
popul = popul + vel;
% Evaluate the new swarm
for i = 1:PopSize,
fpopul(i) = feval(f,popul(:,i));
fevals = fevals + 1;
end
% Updating the best position for each particle
changeColumns = fpopul < fbestpos;
fbestpos = fbestpos.*( 1-changeColumns) + fpopul.*changeColumns;
bestpos(:, find(changeColumns)) = popul(:, find(changeColumns));
% Updating index g
[fbestpart, g] = min(fbestpos);
%currentTime = etime(clock,startTime);
% Checking stopping criterion if abs(fbestpart-GM)<=ErrGoal
%fopt(iter)=fbestpos(g);
if abs(fbestpart-GM) <= ErrGoal
flag_suc = 1;
else
lastbpf = fbestpart;
end
end
%Swarm evolution loop *end*
% Output arguments
xopt = popul(:,g);
fopt(num) = fbestpos(g);
sumf=sumf+fopt(num);
fstep(num)=iter;
sumstep=sumstep+fstep(num);
if (abs(fopt(num)-foptimal)<ErrGoal)
num_success = num_success+1;
end
end
fmean=sumf/trialnum;
success_rate = num_success*100/trialnum;
avestep=sumstep/trialnum;
fopt;
fprintf(' The success rate is : ');
fprintf('--- %g ',success_rate);
fprintf('%%');
fprintf('\n');
fprintf(' The mean fitness value is : ');
fprintf('--- %g ',fmean);
fprintf('\n');
fprintf(' The best fitness value is : ');
fprintf('--- %g ',foptimal);
fprintf('\n');
fprintf(' The mean iteration steps is : ');
fprintf('--- %g ',avestep);
fprintf('\n');
fprintf('\n');
%优化的函数定义
%==========================================
%%function1----DeJong函数,-10<x1,x2<10,fmin=0
%%1个全局最优点x=(0,0)
%寻优率100%
function DeJong = DeJong(x)
DeJong = sum(x.^2);
%==========================================
%%function2----Rastrigrin函数,-10<x1,x2<10,fmin=0
%%多个局部极值,1个全局最优点x=(0,0)
%寻优率
function Rastrigrin = Rastrigrin(x)
Rastrigrin = sum((x.^2-10*cos(2*pi*x)+10));
%==========================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -