## Optimisation: generic function for optimisation
## Author: Oguz Emrah TURGUT
## Created: 2021-06-05
function sum = func (x, a)

if(a=="sphere")
    m=length(x);
    sum=0;
    for j=1:m
        sum=sum+x(j).*x(j);
    end
 elseif(a=="ackley")
     m=length(x);
     sum1=0;
     sum2=0;
     a=20;
     b=0.2;
     c=2*pi;
     for j=1:m
         sum1=sum1+x(j)*x(j);
     end
     for j=1:m
         sum2=sum2+cos(c*x(j));
     end
     sum=-a*exp(-b*sqrt(sum1/m))-exp(sum2/m)+a+exp(1);
 elseif(a=="rastrigin")
     m=length(x);
     B=10;
     sum3=0;
     for j=1:m
         sum3=sum3+(x(j)*x(j)-B*cos(2*pi*x(j)));
     end
     sum=B*m+sum3;
 elseif(a=="griewank")
     m=length(x);
     sum4=0;
     prod1=1;
     for j=1:m
         sum4=sum4+(x(j)*x(j)/4000);
     end
     for j=1:m
         prod1=prod1*cos(x(j)/sqrt(j));
     end
     sum=sum4-prod1+1;
 elseif(a=="zakharov")
     sum5=0;
     sum6=0;
     
     m=length(x);
     for j=1:m
         sum5=sum5+x(j)*x(j);
     end
     for j=1:m
         sum6=sum6+j*x(j);
     end
     sum6=sum6*0.5;
     sum=sum5+sum6*sum6+sum6*sum6*sum6*sum6;
 elseif(a=="salomon")
     m=length(x);
     sum7=0;
     for j=1:m
         sum7=sum7+x(j)*x(j);
     end
     sum=1.0-cos(2*pi*sqrt(sum7))+0.1*sqrt(sum7);
 elseif(a=="brown")
        m=length(x);
        sum8=0;
        for i=1:m-1
            sum8=sum8+(((x(i)*x(i))^(x(i)*x(i)+1))+((x(i+1)*x(i+1))^(x(i)*x(i)+1)));
        end
        sum=sum8;
 elseif(a=="sumofdiff")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+(abs(x(i))^(i+1));
        end
        sum=sum1;
 elseif(a=="bentcigar")
        m=length(x);
        sum1=0.0;
        for i=2:m
            sum1=sum1+x(i)*x(i);
        end
        sum=1e6*sum1+x(1)*x(1);
 elseif(a=="alpine")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+abs((x(i)*sin(x(i))+0.1*x(i)));
        end
        sum=sum1;
 elseif(a=="csendes")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+((x(i)^6)*(2+sin(1/x(i)))) ;
        end
        sum=sum1;
 elseif(a=="schaffer")
        m=length(x);
        sum1=0.0;
        for i=1:m-1
            sum1=sum1+(0.5+(sin(sqrt(x(i)*x(i)+x(i+1)*x(i+1)))*sin(sqrt(x(i)*x(i)+x(i+1)*x(i+1)))-0.5)/((1+0.001*(x(i)*x(i)+x(i+1)*x(i+1)))*(1+0.001*(x(i)*x(i)+x(i+1)*x(i+1)))))                 ;
        end
        sum=sum1;
 elseif(a=="yang2")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+abs(x(i)); 
        end
        sum2=0.0;
        for i=1:m
            sum2=sum2+sin(x(i)*x(i));
        end
        sum2=-sum2;
        sum=sum1*exp(sum2);
 elseif(a=="sumsquares")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+i*x(i)*x(i); 
        end
        sum=sum1;
 elseif(a=="schwefel2_22")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+abs(x(i)); 
        end
        prod1=1.0;
        for i=1:m
            prod1=prod1*abs(x(i)); 
        end
        sum=sum1+prod1;
 elseif(a=="deb1")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+(sin(5*pi*x(i))^6.0); 
        end
        sum=(sum1/m);
 elseif(a=="yaolui")
        m=length(x);
        sum1=0.0;
        for i=1:m
            sum1=sum1+(x(i)*x(i)-10*cos(2*pi*x(i))+10) ; 
        end
        sum=sum1;
 elseif(a=="cosinemixture")
        m=length(x);
        sum1=0.0;
        sum2=0.0;
        for i=1:m
            sum1=sum1+cos(5*pi*x(i)) ; 
        end
        for i=1:m
            sum2=sum2+x(i)*x(i);
        end
        sum=0.1*m-0.1*sum1+sum2;
 elseif(a=="wavy")
        m=length(x);
        sum1=0.0;
       
        for i=1:m
            sum1=sum1+(1-cos(10*x(i))*exp(-0.5*x(i)*x(i))) ; 
        end
        sum=sum1/m;
 end
endfunction