我想转换我的GAPSO代码(gapso_logic),类似于我的PSO代码(pso_logic)。我做了很多更改,但仍然无法正常工作。如果变量,函数输入,输出等存在错误,请告诉我。谢谢。
function [best_position,best_fitness] = pso_logic(CN,x,y,E)
model=CreateModel(CN,x,y,E);
disp('default Sensors parameters')
CostFunction=@(tour) TourLength(tour,model);
No_of_Sensors = CN; %input('Enter the number of Sensors :');
nVars =No_of_Sensors;
% parameters
nPopSize = 100;
nIters = 10; %input('Enter the number of Iterations (apprx 400):');
%% PSO Logic
CreatePopFcn = @CreatePop;
FitnessFcn = CostFunction;
UpdatePosition = @UpdatePop;
%% Initialization
c2 = 1.0; %1.2; % PSO parameter C1
c1 = 1.0; %1.0;%0.4-0.7;%0.12; % PSO parameter C2
w = 0.9; %0.9; % pso momentum or inertia
fitness = inf * ones(nPopSize, nIters);
%-----------------------------%
% initialize the parameter %
%-----------------------------%
% R1 = rand(nPopSize, nVars);
% R2 = rand(nPopSize, nVars);
current_fitness = inf*ones(nPopSize, 1);
%------------------------------------------------%
% Initializing swarm and veloSensors and position %
%------------------------------------------------%
current_position = CreatePopFcn(nPopSize, nVars);
velocity = zeros(nPopSize, 1);
local_best_position = current_position;
% local_best_position
%-------------------------------------------%
% Evaluate initial population %
%-------------------------------------------%
for i = 1:nPopSize
current_fitness(i) = FitnessFcn(current_position(i));
end
% current_fitness
local_best_fitness = current_fitness ;
[global_best_fitness,g] = min(local_best_fitness) ;
% global_best_fitness
% g
globl_best_position=zeros(nPopSize,1);
for i = 1:nPopSize
globl_best_position(i) = local_best_position(g);
end
% globl_best_position
% globl_best_position
%-------------------%
% VELOCITY UPDATE %
%-------------------%
for i=1:1:nPopSize
velocity = w *velocity + c1*rand()*(local_best_position(i)-current_position(i)) + c2*rand()*(globl_best_position(i)-current_position(i));
end
%------------------%
% SWARMUPDATE %
%------------------%
current_position = UpdatePosition(current_position + velocity);
%------------------------%
% evaluate a new swarm %
%------------------------%
%% Main Loop
iter = 0 ; % Iterations Counter
while ( iter < nIters )
iter = iter + 1;
for i = 1:nPopSize
current_fitness(i) = FitnessFcn(current_position(i, :));
end
for i = 1 : nPopSize
if current_fitness(i) < local_best_fitness(i)
local_best_fitness(i) = current_fitness(i);
local_best_position(i, :) = current_position(i, :);
end
end
[current_global_best_fitness,g] = min(local_best_fitness);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
for i=1:nPopSize
globl_best_position(i, :) = local_best_position(g, :);
end
end
for i=1:1:nPopSize
velocity = w *velocity + c1*rand()*(local_best_position(i)-current_position(i)) + c2*rand()*(globl_best_position(i)-current_position(i));
end
current_position = UpdatePosition(current_position + velocity);
fitness(:, iter) = current_fitness;
sprintf('The value of interation iter %3.0f ', iter );
% draw current fitness
% figure(1);
% plot(iter,min(current_fitness),'-*r','linewidth',2)
% title('Fitness Value')
% hold on
end % end of while loop its mean the end of all step that the birds move it
xx=fitness(:, nIters);
[best_fit, I] = min(xx);
best_position = current_position(I, :);
%
% save rtr xx best_fit I best_position E
best_fitness = best_fit/sum(E);
% best_fitness1=best_fit;
all_scores =fitness;
% fitness
%% Save Results
%
% % save
% saveResults(1, best_position, best_fitness ,all_scores);
%% output the best Set
% best_position
% best_fitness
% draw current fitness
% figure(1);
% plot(iter,best_fitness,'-*r','linewidth',2)
% title('Fitness Value')
% hold on
function [best_position,best_fitness] = gapso_logic(CN,x,y,E)
model=CreateModel(CN,x,y,E);
disp('default Sensors parameters')
% model
CostFunction=@(tour) TourLength(tour,model);
No_of_Sensors = CN; %input('Enter the number of Sensors :');
nVars =No_of_Sensors;
% parameters
nPopSize = 100; %input('Enter the Value of Population Size (apprx 100):');
nIters = 10; %input('Enter the number of Iterations (apprx 400):');
%% PSO Logic
CreatePopFcn = @CreatePop;
FitnessFcn = CostFunction;
UpdatePosition = @UpdatePop;
% Set algorithm parameters
constant = 0.95;
c1 = 1.5; %1.4944; %2;
c2 = 1.5; %1.4944; %2;
w = 0.792 * constant;
% Allocate memory and initialize
gBestScore = inf;
fitness = inf * ones(nPopSize, nIters);
current_position = CreatePopFcn(nPopSize, nIters);
velocity = zeros(nPopSize, 1);
local_best_position = current_position; %local_best_position = x;
% update lbest
cost_p = inf * ones(1, nPopSize); %feval(FUN, local_best_position');
for i=1:nPopSize
current_fitness(i) = FitnessFcn(current_position(i));
% cost_p(i) = FitnessFcn(local_best_position(i, 1:nPlant));
end
lbest = update_lbest(current_fitness(i), local_best_position, nPopSize);
for iter = 1 : nIters
if mod(iter,1000) == 0
parents = randperm(nPopSize);
for i = 1:nPopSize
x(i,:) = (local_best_position(i,:) + local_best_position(parents(i),:))/2;
% v(i,:) = local_best_position(parents(i),:) - x(i,:);
% v(i,:) = (v(i,:) + v(parents(i),:))/2;
end
else
% Update velocity
v = w*v + c1*rand(nPopSize,nCity).*(local_best_position-x) + c2*rand(nPopSize,nCity).*(lbest-x);
% Update position
x = x + v;
x = UpdatePosition(x);
end
% Update local_best_position
cost_x = inf * ones(1, nPopSize);
for i=1:nPopSize
cost_x(i) = FitnessFcn(x(i, 1:nPlant));
end
s = cost_x<cost_p;
cost_p = (1-s).*cost_p + s.*cost_x;
s = repmat(s',1,nCity);
local_best_position = (1-s).*local_best_position + s.*x;
% update lbest
lbest = update_lbest(cost_p, local_best_position, nPopSize);
% update global best
all_scores(:, iter) = cost_x;
[cost,index] = min(cost_p);
if (cost < gBestScore)
gbest = local_best_position(index, :);
gBestScore = cost;
end
% draw current fitness
figure(1);
plot(iter,min(cost_x),'cp','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',8)
hold on
str=strcat('Best fitness: ', num2str(min(cost_x)));
disp(str);
end
end
% Function to update lbest
function lbest = update_lbest(cost_p, x, nPopSize)
sm(1, 1)= cost_p(1, nPopSize);
sm(1, 2:3)= cost_p(1, 1:2);
[cost, index] = min(sm);
if index==1
lbest(1, :) = x(nPopSize, :);
else
lbest(1, :) = x(index-1, :);
end
for i = 2:nPopSize-1
sm(1, 1:3)= cost_p(1, i-1:i+1);
[cost, index] = min(sm);
lbest(i, :) = x(i+index-2, :);
end
sm(1, 1:2)= cost_p(1, nPopSize-1:nPopSize);
sm(1, 3)= cost_p(1, 1);
[cost, index] = min(sm);
if index==3
lbest(nPopSize, :) = x(1, :);
else
lbest(nPopSize, :) = x(nPopSize-2+index, :);
end
end