function [fit] = func(x, fun)
%benchmark functions
% Developed by: Dr. Mahamed G.H. Omran (omran.m@gust.edu.kw) 12-May-2011
% Modified and improved by: Maurice Clerc (15-May-2011)
% x is a D-dimensional vector representing the solution we want to evalaute
% fun is the indix of the function of interest
% fit is the fitness of x (i.e. fit = f(x))
D = length(x);
switch(fun)
% case {1,2,3,4,5,6,7,8,9,10,11,12,13,14}, % CEC'05 functions
% NOTE:
% For these functions to work, you need to include the following
% lines in your file:
% global initial_flag
% initial_flag = 0;
% In addition, vector x should be x(1,D) where D is the problem's
% dimension
% fit = benchmark_func(x, fun);
case {1}, % Sphere function
fit = sum(x.^2);
case {2}, % Rastrigin
fit = 10*D + sum(x.^2 - 10*cos(2.*pi.*x));
case {3}, %Step
fit = sum(floor(x + 0.5).^2);
case {4}, %Rosenbrock
f = 0;
for i = 1:1:D-1
f = f + 100*((x(i+1) - x(i)^2)^2) + (1 - x(i))^2;
end
fit = f;
case {5}, %Ackley
fit = -20*exp(-0.2*sqrt(sum(x.^2)/D)) - exp(sum(cos(2*pi .*x))/D) + 20 + exp(1);
case {6}, %Griewank
sum1 = 0;
prod1 = 1;
for i = 1:1:D
sum1 = sum1 + (x(i)^2/4000);
prod1 = prod1*cos(x(i)/sqrt(i));
end
fit = sum1 - prod1 + 1;
case {7}, %Salomon
fit = -cos(2*pi*sqrt(sum(x.^2))) + 0.1*sqrt(sum(x.^2)) + 1;
case {8}, %Quartic function
sum1 = 0;
for i = 1:1:D
sum1 = sum1 + i*x(i)^4;
end
fit = sum1 + rand();
case {9}, %Alpine
sum1 = 0;
for i=1:1:D
sum1 = sum1 + abs(x(i)*sin(x(i)) + 0.1*x(i));
end
fit = sum1;
case {10}, %Six Hump Camel bsck
fit = 4*x(1).^2 - 2.1*x(1).^4 + (1/3)*x(1).^6 + x(1)*x(2) - 4*x(2)^2 + 4*x(2).^4;
case {11}, % Branin
fit = (x(2)-(5.1/(4*pi^2))*x(1)^2+5*x(1)/pi-6)^2+10*(1-1/(8*pi))*cos(x(1))+10;
case {12}, % Shubert
sum0 = 0;
sum1 = 0;
for j=1:1:5
sum0 = sum0 + j*cos((j+1)*x(1) + j);
sum1 = sum1 + j*cos((j+1)*x(2) + j);
end
fit = sum0*sum1;
case {13}, % Easom
fit = -cos(x(1))*cos(x(2))*exp(-((x(1) - pi)^2 + (x(2) - pi)^2));
case {14}, % Shekel function
% Matlab Code by A. Hedar (Nov. 23, 2005).
% The number of variables n = 4
% The parameter m should be adjusted m = 5,7,10.
% The default value of m = 10.
%
m = 10;
a = ones(10,4);
a(1,:) = 4.0*a(1,:);
a(2,:) = 1.0*a(2,:);
a(3,:) = 8.0*a(3,:);
a(4,:) = 6.0*a(4,:);
for j = 1:2;
a(5,2*j-1) = 3.0; a(5,2*j) = 7.0;
a(6,2*j-1) = 2.0; a(6,2*j) = 9.0;
a(7,j) = 5.0; a(7,j+2) = 3.0;
a(8,2*j-1) = 8.0; a(8,2*j) = 1.0;
a(9,2*j-1) = 6.0; a(9,2*j) = 2.0;
a(10,2*j-1)= 7.0; a(10,2*j)= 3.6;
end
c(1) = 0.1; c(2) = 0.2; c(3) = 0.2; c(4) = 0.4; c(5) = 0.4;
c(6) = 0.6; c(7) = 0.3; c(8) = 0.7; c(9) = 0.5; c(10)= 0.5;
s = 0;
for j = 1:m;
p = 0;
for i = 1:4
p = p+(x(i)-a(j,i))^2;
end
s = s+1/(p+c(j));
end
fit = -s;
otherwise,
error('No such function');
fit = -1;
end
% fit=abs(fit-opt_f);
end