任何人都可以识别 matlab 中混合 GA PSO 代码中的错误吗?



索引超过矩阵维度。

gapso_logic>update_lbest错误(第 103 行( sm(1, 1(= cost_p(1, nPopSize(;

gapso_logic错误(第 46 行( local_best_position = update_lbest(current_fitness(i(, local_best_position, nPopSize(;

建议中的错误2(第 282 行( [best_position,best_fitness] = gapso_logic(6,x_corr,y_corr,能量(

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

我想我发现了问题:

  • 第 46 行:
    lbest = update_lbest(current_fitness(i), local_best_position, nPopSize);第一个
    参数为:current_fitness(i)
    current_fitness(i)是标量(标量大小为 1x1(。

  • 第 103 行:
    sm(1, 1)= cost_p(1, nPopSize);
    nPopSize> 1(假设nPopSize = 2(时,您正在尝试访问超过矩阵维度的cost_p(1, 2)cost_p因为它是标量。

MATLAB 中的标量被认为是大小为 1x1 的矩阵。
这就是您收到"索引超过矩阵维度"错误的原因。
错误消息令人困惑,因为变量是标量而不是矩阵。

您可以将第 46 行更正为:

lbest = update_lbest(current_fitness, local_best_position, nPopSize);

我不确定这是否是正确的解决方案,因为您发布的代码不是可执行的。
我不知道代码中是否有任何其他问题...您应该使用调试器来调试此类错误。

最新更新