谁能在matlab中识别Hybrid GA PSO代码中的错误?

时间:2019-10-18 07:54:35

标签: matlab particles swarm genetic

索引超出矩阵尺寸。

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);

proposed2错误(第282行)                   [最佳位置,最佳适应度] = gapso_logic(6,x_corr,y_corr,energy)

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

1 个答案:

答案 0 :(得分:0)

我认为我发现了问题:

  • 第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);

我不确定这是否是正确的解决方案,因为您发布的代码无法按原样执行。
我不知道代码中是否还有其他问题...
您应使用调试器,以调试此类错误。