主题:PSO求解函数最优程序运行时有问题,请高手帮忙
PSO求解DE JONG函数问题
function [xmin, fxmin, iter] = PSO()
% Initializing variables
success = 0; % Success flag
PopSize = 20; % Size of the swarm
MaxIt = 5000; % Maximum number of iterations
iter = 0; % Iterations’counter
fevals = 0; % Function evaluations’ counter
maxw = 1.2; % Maximum inertia weight’s value
minw = 0.1; % Minimum inertia weight’s value
weveryit = floor(0.75*MaxIt); % Inertiadecr. Step floor函数返回不大于参数的整数
c1 = 0.5; % PSO parameter C1
c2 = 0.5; % PSO parameter C2
inertdec = (maxw-minw)/weveryit; % Inertia weight’s decrement
w = maxw; % initial inertia weight
f = 'DeJong'; % Objective Function
dim = 2; % Dimension of the problem
upbnd = 5; % Upper bound for init. of the swarm
lwbnd = -5; % Lower bound for init. of the swarm
GM = 0; % Global minimum (used in the stopping criterion)
ErrGoal = 1e-3; % Desired accuracy
% Initializing swarm and velocities
popul = rand(dim, PopSize)*(upbnd-lwbnd) + lwbnd;
vel = rand(dim, PopSize);
% Evaluate initial population
for i = 1:PopSize,
fpopul(i) = feval(f, popul(:,i));
fevals = fevals + 1;
end
% Initializing Best positions’ matrix and
% the corresponding function values
bestpos = popul;
fbestpos = fpopul;
% Finding best particle in initial population
[fbestpart,g] = min(fpopul);
lastbpf = fbestpart;
% SWARM EVOLUTION LOOP ∗ START ∗
while (success == 0) && (iter < MaxIt),
iter = iter + 1;
% Update the value of the inertia weight w
if (iter<=weveryit)
w = maxw -(iter-1)*inertdec;
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);
% SWARMUPDATE
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);
% Checking stopping criterion
%if abs(fbestpart-lastbpf) <= ErrGoal
if abs(fbestpart-GM) <= ErrGoal
success = 1;
else
lastbpf = fbestpart;
end
end
% SWARM EVOLUTION LOOP ∗ END ∗
% Output arguments
xmin = popul(:,g);
fxmin = fbestpos(g);
function DeJong=DeJong(x)
DeJong = sum(x.^2);
[color=FF0000]??? Undefined function or method 'FIND' for input arguments of type 'logical'.
Error in ==> PSO at 67
bestpos(:, FIND(changeColumns)) = popul(:, FIND(changeColumns));[/color]
小弟不知该如何修改,请大哥们指点小弟,在此先谢谢了
function [xmin, fxmin, iter] = PSO()
% Initializing variables
success = 0; % Success flag
PopSize = 20; % Size of the swarm
MaxIt = 5000; % Maximum number of iterations
iter = 0; % Iterations’counter
fevals = 0; % Function evaluations’ counter
maxw = 1.2; % Maximum inertia weight’s value
minw = 0.1; % Minimum inertia weight’s value
weveryit = floor(0.75*MaxIt); % Inertiadecr. Step floor函数返回不大于参数的整数
c1 = 0.5; % PSO parameter C1
c2 = 0.5; % PSO parameter C2
inertdec = (maxw-minw)/weveryit; % Inertia weight’s decrement
w = maxw; % initial inertia weight
f = 'DeJong'; % Objective Function
dim = 2; % Dimension of the problem
upbnd = 5; % Upper bound for init. of the swarm
lwbnd = -5; % Lower bound for init. of the swarm
GM = 0; % Global minimum (used in the stopping criterion)
ErrGoal = 1e-3; % Desired accuracy
% Initializing swarm and velocities
popul = rand(dim, PopSize)*(upbnd-lwbnd) + lwbnd;
vel = rand(dim, PopSize);
% Evaluate initial population
for i = 1:PopSize,
fpopul(i) = feval(f, popul(:,i));
fevals = fevals + 1;
end
% Initializing Best positions’ matrix and
% the corresponding function values
bestpos = popul;
fbestpos = fpopul;
% Finding best particle in initial population
[fbestpart,g] = min(fpopul);
lastbpf = fbestpart;
% SWARM EVOLUTION LOOP ∗ START ∗
while (success == 0) && (iter < MaxIt),
iter = iter + 1;
% Update the value of the inertia weight w
if (iter<=weveryit)
w = maxw -(iter-1)*inertdec;
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);
% SWARMUPDATE
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);
% Checking stopping criterion
%if abs(fbestpart-lastbpf) <= ErrGoal
if abs(fbestpart-GM) <= ErrGoal
success = 1;
else
lastbpf = fbestpart;
end
end
% SWARM EVOLUTION LOOP ∗ END ∗
% Output arguments
xmin = popul(:,g);
fxmin = fbestpos(g);
function DeJong=DeJong(x)
DeJong = sum(x.^2);
[color=FF0000]??? Undefined function or method 'FIND' for input arguments of type 'logical'.
Error in ==> PSO at 67
bestpos(:, FIND(changeColumns)) = popul(:, FIND(changeColumns));[/color]
小弟不知该如何修改,请大哥们指点小弟,在此先谢谢了