function [Leader_score,xGbest,Convergence_curve,time_pso]=PSO_constricton_mod1(c1,c2,max_iters,SearchAgents_no,lb,ub,dim,fobj,Pos)
%Adapted by Pedro Bento November 2023
Positions=Pos;

% initialize position vector and score for the leader
%Leader_pos=zeros(1,dim);
Leader_score=inf; %change this to -inf for maximization problems

v=zeros(SearchAgents_no,dim);

All_fitness=zeros(1,SearchAgents_no)+inf;
xPbest=zeros(SearchAgents_no,dim)+inf;
xGbest=zeros(1,dim);

% initialize variables
% c1=2;
% c2=2;
phi=c1+c2;
k=1;
constrfactor=(2*k)/abs(2-phi-sqrt(phi^2-4*phi));

Convergence_curve=zeros(1,max_iters);

t=0;% Loop counter

tic
% Main loop
while (t<max_iters)
    
    
    for i=1:SearchAgents_no
        
        % Return back the search agents that go beyond the boundaries of the search space
        Flag4ub=Positions(i,:)>ub;
        Flag4lb=Positions(i,:)<lb;
        Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
        
        % Calculate objective function for each search agent
        fitness=feval(fobj,Positions(i,:));
        
        if fitness < All_fitness(i)
            
            All_fitness(i) = fitness;
            xPbest(i,:) = Positions(i,:);
            
        end
        
        if fitness < Leader_score
            
            Leader_score  = fitness;
            xGbest = Positions(i,:);
            
        end
        
    end
    
    for i=1:size(Positions,1)
        r1=rand(1,dim); % r1 is a random number in [0,1]
        r2=rand(1,dim); % r2 is a random number in [0,1]
        v(i,:)=constrfactor*(v(i,:)+c1*r1.*(xPbest(i,:)-Positions(i,:))+c2*r2.*(xGbest(1,:)-Positions(i,:)));
        Positions(i,:)=Positions(i,:)+v(i,:);
    end
    
    %     All_score=All_fitness;
    t=t+1;
    Convergence_curve(t)=Leader_score;
%     figure(1),plot(Positions(:,1),Positions(:,2),'*')
%     hold on
%     pause(0.15)
%     clf

end %while
time_pso=toc;
end
