Weighted Superposition Attraction (WSA): A swarm intelligence algorithm for optimization problems

%

by  Adil Baykasoglu and Sener Akpinar

%

% Matlab code of WSA for Schaffer1 (SF1) function

 

clc;

clear all;

close all;

%-------PARAMETERS-------

indiv=100;

Maxiter=150;

step=0.035;

Fi=0.0001;

lamda=-0.8;

%------FUNCTION DATA-----

dim=2;

UL=100;

LL=-100;

 

%------OTHER VARIABLES----

iteration=1;

TT=3;

BF=zeros(1,Maxiter);

I=zeros(1,Maxiter);

best_solution=zeros(1,dim);

best_fitness=100000000;

 

file=fopen('individuals.dat','w');

file1=fopen('best_individual.dat','w');

 

%**********************3-D PLOT AND CONTOUR PLOT OF SF1********************

[x,y] = meshgrid(-100:200/100:100,-100:200/100:100);

a=sin(sqrt(x.^2+y.^2)).^2-0.5;

b=(1+0.001*(x.^2+y.^2)).^2;

sum = a./b;

sum = sum +0.5;

z=sum;

figure(1)

surf(x,y,z);

xlabel('x1','fontSize',14);

ylabel('x2','fontSize',14);

zlabel('f(x1,x2)','fontSize',14)

title('Schaffer 1','fontSize',14);

shading interp;

figure(2)

contour(x,y,z);

xlabel('x1','fontSize',14);

ylabel('x2','fontSize',14);

title('Initial Individuals','fontSize',14);

%**************************************************************************

 

tic %STARTS EXECUTION TIME

 

initial=LL+rand(indiv,dim)*(UL-LL);%GENERATES INITIAL SOLUTIONS

 

%**********************FITNESS EVALUATION**********************************

fitness=zeros(1,indiv);

for i=1:indiv

    fact1 = (sin(sqrt(initial(i,1)^2-initial(i,2))^2))^2 - 0.5;

    fact2 = (1 + 0.001*(initial(i,1)^2+initial(i,2)^2))^2;

    fitness(1,i) = 0.5 + fact1/fact2;

    if (best_fitness>fitness(1,i))

        best_fitness=fitness(1,i);

        best_solution(1,:)=initial(i,:);

    end

end

%**************************************************************************

 

%*****************SCATTERING INITIAL INDIVIDUALS ON CONTOUR PLOT***********

for i=1:indiv

    text(initial(i,1),initial(i,2),fitness(1,i),'*','FontSize',20, 'Color', [0 0 0]);

end

%**************************************************************************

 

%*********RANKING OF INITIAL SOLUTIONS WITH REGARD TO FITNESS VALUES*******

for k=1:indiv

    ek = fitness(1,k);

    i = k;

    for j=k+1:indiv

        if (fitness(1,j)<ek)

            ek = fitness(1,j);

            i = j;

        end

    end

    temp=zeros(1,dim);

    for j=1:dim

   

        temp(1,j)=initial(k,j);

        initial(k,j)=initial(i,j);

        initial(i,j)=temp(1,j);

    end

    fitness(1,i)=fitness(1,k);

    fitness(1,k)=ek;

end

%**************************************************************************

 

%****************WRITING INITIAL INDIVIDUALS TO A FILE*********************

fprintf(file,'Initial Individuals\n');

fprintf(file,'------------------\n');

 

for i=1:indiv

    for j=1:dim

        fprintf(file,'%g\t',initial(i,j));

    end

    fprintf(file,'\tFitness=%g\n',fitness(1,i));

end

%**************************************************************************

 

while iteration<=Maxiter

 

%********************DETERMINATION OF AGGREGATE VECTOR*********************

    weight=zeros(1,indiv);

    aggre=zeros(1,dim);

    for i=1:indiv

        weight(1,i)=i^(lamda);

            for j=1:dim

                aggre(1,j)=(aggre(1,j)+initial(i,j))*weight(1,i);

            end

    end

%**************************************************************************

   

%***************FITNESS EVALUATION OF AGGREGATE VECTOR*********************

    fact1 = (sin(sqrt(aggre(1,1)^2-aggre(1,2)^2)))^2 - 0.5;

    fact2 = (1 + 0.001*(aggre(1,1)^2+aggre(1,2)^2))^2;

    f_aggre = 0.5 + fact1/fact2;

%**************************************************************************

 

%*********************DIRECTION DETERMINATION******************************

    product_1=zeros(indiv,dim);

    direction=zeros(indiv,dim);

    for i=1:indiv

        if fitness(i)>=f_aggre

            for j=1:dim

                product_1(i,j)=aggre(1,j)-initial(i,j);

            end

            for d=1:dim

                direction(i,d)=sign(product_1(i,d));

            end

        elseif fitness(i)<f_aggre

            if rand()<exp(fitness(i)-f_aggre)

                for j=1:dim

                    product_1(i,j)=aggre(1,j)-initial(i,j);

                end

                for d=1:dim

                    direction(i,d)=sign(product_1(i,d));

                end

            else

                for d=1:dim

                direction(i,d)= sign(-1 + (1+1)*rand());

                end

            end

        end

    end

%**************************************************************************

 

%***********************MOVEMENT OF INDIVIDUALS****************************

    A=iteration/(iteration+1);

    B=exp(-A);

    r=rand();   

    if r<=0.95

        step=step-B*Fi*step;

    else

        step=step+B*Fi*step;

    end

    for i=1:indiv

        for d=1:dim

            initial(i,d)=initial(i,d)+step*direction(i,d)*abs(initial(i,d));

            if initial (i,d)<LL

                initial(i,d)=LL;

            elseif initial(i,d)>UL

                initial(i,d)=UL;

            end

        end

    end

%**************************************************************************

   

%**********************FITNESS EVALUATION**********************************

    fitness=zeros(1,indiv);

    for i=1:indiv

        fact1 = (sin(sqrt(initial(i,1)^2-initial(i,2))^2))^2 - 0.5;

        fact2 = (1 + 0.001*(initial(i,1)^2+initial(i,2)^2))^2;

        fitness(1,i) = 0.5 + fact1/fact2;

        if (best_fitness>fitness(1,i))

            best_fitness=fitness(1,i);

            best_solution(1,:)=initial(i,:);

        end

    end

%**************************************************************************

 

%**************RANKING SOLUTIONS WITH REGARD TO FITNESS VALUES*************

    for k=1:indiv

        ek = fitness(1,k);

        i = k;

        for j=k+1:indiv

            if (fitness(1,j)<ek)

                ek = fitness(1,j);

                i = j;

            end

        end

        temp=zeros(1,dim);

        for j=1:dim

            temp(1,j)=initial(k,j);

            initial(k,j)=initial(i,j);

            initial(i,j)=temp(1,j);

        end

        fitness(1,i)=fitness(1,k);

        fitness(1,k)=ek;

    end

%**************************************************************************

 

%WRITING INDIVIDUALS OF A DESIRED ITERATION TO A FILE AND DRAW CONTOUR PLOT

    if(mod(iteration,15)==0)

        fprintf(file,'\nIndividuals of %d.Iteration\n'iteration);

        fprintf(file,'------------------\n');

        for i=1:indiv

            for j=1:dim

                fprintf(file,'%e\t',initial(i,j));

            end

            fprintf(file,'\tFitness=%g\n',fitness(1,i));

        end

        TT=TT+1;

        figure(TT)

        contour(x,y,z);

        xlabel('x1','fontSize',14);

        ylabel('x2','fontSize',14);

        tit=strcat({'Individuals of '},{num2str(iteration)},{'.Iteration'});

        title(tit,'FontSize',14)

        for i=1:indiv

            text(initial(i,1),initial(i,2),fitness(1,i),'*','FontSize',20, 'Color', [0 0 0]);

        end

    end

%**************************************************************************

 

%************WRITING BEST FITNESS AND BEST INDIVIDUAL TO A FILE************

    fprintf(file1,'%d. ITERATION\n\nBEST FITNESS=%e\n\n',iterationbest_fitness);

    fprintf(file1,'BEST SOLUTION:\n\n');

    for j=1:dim

        fprintf(file1,'%g\t',best_solution(1,j));

    end

    fprintf(file1,'\n\n-----------------------------\n\n');

%**************************************************************************

    BF(1,iteration)=best_fitness;

    I(1,iteration)=iteration;

    iteration=iteration+1;

end

fclose(file);

fclose(file1);

toc%END OF EXECUTION TIME

 

%******************PLOTTING BEST FITNESS VERSUS ITERATION******************

figure (3)

plot(I,BF);

xlabel('Iteration Number','fontSize',14);

ylabel('Fitness Value','fontSize',14);

%**************************************************************************