位置1处的索引超出数组边界,不能超出1,这个怎么解决,


function [fMin,bestX,Convergence_curve] = BWO(pop,Max_it,lb,ub,dim,fobj)

% disp('Beluga Whale Optimization is optimizing your problem');


fit = inf*ones(pop,1);
newfit = fit;
Convergence_curve = inf*ones(1,Max_it);
kk = zeros(1,Max_it);
Counts_run = 0;

if size(ub,2)==1
    lb = lb*ones(1,dim); ub = ub*ones(1,dim);
end
pos = rand(pop,dim).*(ub-lb)+lb;

for i = 1:pop
    fit(i,1) = fobj(pos(i,:));
    Counts_run = Counts_run+1;
end
[bestX,index]=min(fit);
fMin = pos(index,:);

T = 1;
while T <= Max_it
    newpos = pos;
    WF = 0.1-0.05*(T/Max_it);  % The probability of whale fall
    kk = (1-0.5*T/Max_it)*rand(pop,1); % The probability in exploration or exploitation
    for i = 1:pop
        if kk(i) > 0.5 % exploration phase
            r1 = rand(); r2 = rand();
            RJ = ceil(pop*rand);   % Roulette Wheel Selection
            while RJ == i
                RJ = ceil(pop*rand);
            end
            if dim <= pop/5
                params = randperm(dim,2);
                newpos(i,params(1)) = pos(i,params(1))+(pos(RJ,params(1))-pos(i,params(2)))*(r1+1)*sin(r2*360);
                newpos(i,params(2)) = pos(i,params(2))+(pos(RJ,params(1))-pos(i,params(2)))*(r1+1)*cos(r2*360);
            else
                params=randperm(dim);
                for j = 1:floor(dim/2)
                    newpos(i,2*j-1) = pos(i,params(2*j-1))+(pos(RJ,params(1))-pos(i,params(2*j-1)))*(r1+1)*sin(r2*360);
                    newpos(i,2*j) = pos(i,params(2*j))+(pos(RJ,params(1))-pos(i,params(2*j)))*(r1+1)*cos(r2*360);
                end
            end
        else  % exploitation phase
            r3 = rand(); r4 = rand(); C1 = 2*r4*(1-T/Max_it);
            RJ = ceil(pop*rand);   % Roulette Wheel Selection
            while RJ == i
                RJ = ceil(pop*rand);
            end
            alpha=3/2;
            sigma=(gamma(1+alpha)*sin(pi*alpha/2)/(gamma((1+alpha)/2)*alpha*2^((alpha-1)/2)))^(1/alpha); % Levy flight
            u=randn(1,dim).*sigma;
            v=randn(1,dim);
            S=u./abs(v).^(1/alpha);
            KD = 0.05;
            LevyFlight=KD.*S;
            newpos(i,:) = r3*fMin - r4*pos(i,:) + C1*LevyFlight.*(pos(RJ,:)-pos(i,:));
        end
        % boundary
        Flag4ub = newpos(i,:)>ub;
        Flag4lb = newpos(i,:)i,:)=(newpos(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
        newfit(i,1) = fobj(newpos(i,:));    % fitness calculation
        Counts_run = Counts_run+1;
        if newfit(i,1) < fit(i,1)
            pos(i,:) = newpos(i,:);
            fit(i,1) = newfit(i,1);
        end
    end

    for i = 1:pop
        % whale falls
        if kk(i) <= WF
            RJ = ceil(pop*rand); r5 = rand(); r6 = rand(); r7 = rand();
            C2 = 2*pop*WF;
            stepsize2 = r7*(ub-lb)*exp(-C2*T/Max_it);
            newpos(i,:) = (r5*pos(i,:) - r6*pos(RJ,:)) + stepsize2;
            % boundary
            Flag4ub = newpos(i,:)>ub;
            Flag4lb = newpos(i,:)i,:)=(newpos(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
            newfit(i,1) = fobj(newpos(i,:));    % fitness calculation
            Counts_run = Counts_run+1;
            if newfit(i,1) < fit(i,1)
                pos(i,:) = newpos(i,:);
                fit(i,1) = newfit(i,1);
            end
        end
    end
    [fval,index]=min(fit);
    if fvalend
    
    kk_Record(T) = kk(1);
    Convergence_curve(T) = bestX;
    T = T+1;
end
% display(['The function call is ', num2str(Counts_run)]);

img

这个是你把数据发给我运行一下看看

img