matlabparticlesswarmgenetic

Can anyone identify the errors in Hybrid GA PSO code in matlab?


Index exceeds matrix dimensions.

Error in gapso_logic>update_lbest (line 103) sm(1, 1)= cost_p(1, nPopSize);

Error in gapso_logic (line 46) local_best_position = update_lbest(current_fitness(i), local_best_position, nPopSize);

Error in proposed2 (line 282) [best_position,best_fitness] = 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

Solution

  • I think I found the problem:

    Scalar in MATLAB is considered to be a matrix in size 1x1.
    That's the reason you are getting "Index exceeds matrix dimensions." error.
    Error message is confusing because variable is scalar and not a matrix.

    You can correct line 46 to:

    lbest = update_lbest(current_fitness, local_best_position, nPopSize);
    

    I am not sure if this is the right solution, because the code you have posted is not executable as is.
    I don't know if there are any other problems in the code...
    You should use the debugger, for debugging that kind of errors.