% Acknowledgement:
% This function is provided by Dr. P. N. Suganthan, and we have done minor
% revisions.
function f = benchmark_func(x, func_num, o, A, M, a, alpha, b)
persistent fhd f_bias
% benchmark_func.m is the main function for 25 test functions , all
% minimize
% problems
% e.g. f = benchmark_func(x , func_num)
% x is the variable , f is the function value
% func_num is the function num ,
% 25 TEST FUCNTIONS
% Unimodal Functions (5):
% 1. Shifted Sphere Function Bounds[-100 , 100] f_bias = -450
% 2. Shifted Schwefel's Problem 1.2 Bounds[-100 , 100] f_bias = -450
% 3. Shifted Rotated High Conditioned Elliptic Function Bounds[-100 , 100] f_bias = -450
% 4. Shifted Schwefel's Problem 1.2 with Noise in Fitness Bounds[-100 , 100] f_bias = -450
% 5. Schwefel's Problem 2.6 with Global Optimum on Bounds Bounds[-100 , 100] f_bias = -310
%
% Multimodal Functions (20):
% Basic Functions (7):
% 6. Shifted Rosenbrock's Function Bounds[-100 , 100] f_bias = 390
% 7. Shifted Rotated Griewank's Function without Bounds Initilization Range [0 , 600] f_bias = -180
% 8. Shifted Rotated Ackley's Function with Global Optimum on Bounds Bounds[-32 , 32] f_bias = -140
% 9. Shifted Rastrigin's Function Bounds[-5 , 5] f_bias = -330
% 10. Shifted Rotated Rastrigin's Function Bounds[-5 , 5] f_bias = -330
% 11. Shifted Rotated Weierstrass Function Bounds[-0.5 , 0.5] f_bias = 90
% 12. Schwefel's Problem 2.13 Bounds[-100 , 100] f_bias = -460
%
% Expanded Functions (2):
% 13. Expanded Extended Griewank's plus Rosenbrock's Function (F8F2) Bounds[-3 , 1] f_bias = -130
% 14. Expanded Rotated Extended Scaffe's F6 Bounds[-100 , 100] f_bias = -300
%
% Hybrid Composition Functions (11):
% 15. Hybrid Composition Function 1 Bounds[-5 , 5] f_bias = 120
% 16. Rotated Hybrid Composition Function 1 Bounds[-5 , 5] f_bias = 120
% 17. Rotated Hybrid Composition Function 1 with Noise in Fitness Bounds[-5 , 5] f_bias = 120
% 18. Rotated Hybrid Composition Function 2 Bounds[-5 , 5] f_bias = 10
% 19. Rotated Hybrid Composition Function 2 with a Narrow Basin for the Global Optimum Bounds[-5 , 5]] f_bias = 10
% 20. Rotated Hybrid Composition Function 2 with the Global Optimum on the Bounds Bounds[-5 , 5] f_bias = 10
% 21. Rotated Hybrid Composition Function 3 Bounds[-5 , 5] f_bias = 360
% 22. Rotated Hybrid Composition Function 3 with High Condition Number Matrix Bounds[-5 , 5] f_bias = 360
% 23. Non-Continuous Rotated Hybrid Composition Function 3 Bounds[-5 , 5] f_bias = 360
% 24. Rotated Hybrid Composition Function 4 Bounds[-5 , 5] f_bias = 260
% 25. Rotated Hybrid Composition Function 4 without Bounds Intilization Range[-2 , 5] f_bias = 260
%
%J. J. Liang & P. N. Suganthan 2005.Feb 18
if func_num == 1 fhd = str2func('sphere_func'); %[-100, 100]
elseif func_num == 2 fhd = str2func('schwefel_102'); %[-100, 100]
elseif func_num == 3 fhd = str2func('high_cond_elliptic_rot_func'); %[-100, 100]
elseif func_num == 4 fhd = str2func('schwefel_102_noise_func'); %[-100, 100]
elseif func_num == 5 fhd = str2func('schwefel_206'); %[no bound], initial[-100, 100];
elseif func_num == 6 fhd = str2func('rosenbrock_func'); %[-100, 100]
elseif func_num == 7 fhd = str2func('griewank_rot_func'); %[-600, 600]
elseif func_num == 8 fhd = str2func('ackley_rot_func'); %[-32, 32]
elseif func_num == 9 fhd = str2func('rastrigin_func'); %[-5, 5]
elseif func_num == 10 fhd = str2func('rastrigin_rot_func'); %[-5, 5]
elseif func_num == 11 fhd = str2func('weierstrass_rot'); %[-0.5, 0.5]
elseif func_num == 12 fhd = str2func('schwefel_213'); %[-pi, pi]
elseif func_num == 13 fhd = str2func('EF8F2_func'); %[-3, 1]
elseif func_num == 14 fhd = str2func('E_ScafferF6_func'); %[-100, 100]
elseif func_num == 15 fhd = str2func('hybrid_func1'); %[-5, 5]
elseif func_num == 16 fhd = str2func('hybrid_rot_func1'); %[-5, 5]
elseif func_num == 17 fhd = str2func('hybrid_rot_func1_noise'); %[-5, 5]
elseif func_num == 18 fhd = str2func('hybrid_rot_func2'); %[-5, 5]
elseif func_num == 19 fhd = str2func('hybrid_rot_func2_narrow'); %[-5, 5]
elseif func_num == 20 fhd = str2func('hybrid_rot_func2_onbound'); %[-5, 5]
elseif func_num == 21 fhd = str2func('hybrid_rot_func3'); %[-5, 5]
elseif func_num == 22 fhd = str2func('hybrid_rot_func3_highcond'); %[-5, 5]
elseif func_num == 23 fhd = str2func('hybrid_rot_func3_noncont'); %[-5, 5]
elseif func_num == 24 fhd = str2func('hybrid_rot_func4'); %[-5, 5]
elseif func_num == 25 fhd = str2func('hybrid_rot_func4'); %[-5, 5]
end
f = feval(fhd, x, o, A, M, a, alpha, b);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Unimodal %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.Shifted Sphere Function
function fit = sphere_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
fit = sum(x .^ 2, 2);
% 2.Shifted Schwefel's Problem 1.2
function f = schwefel_102(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
f = 0;
for i = 1 : D
f = f + sum(x(:, 1 : i), 2).^2;
end
% 3.Shifted Rotated High Conditioned Elliptic Function
function fit = high_cond_elliptic_rot_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
x = x * M;
a = 1e+6;
fit = 0;
for i = 1 : D
fit = fit + a .^ ((i - 1) / (D - 1)) .* x(:, i).^2;
end
% 4.Shifted Schwefel's Problem 1.2 with Noise in Fitness
function f = schwefel_102_noise_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
f = 0;
for i = 1 : D
f = f + sum(x(:, 1 : i), 2).^2;
end
f = f .* (1 + 0.4 .* abs(normrnd(0, 1, ps, 1)));
% 5.Schwefel's Problem 2.6
function f = schwefel_206(x, o, A, M, a, alpha, b)%after Fletcher and Powell
[ps, D] = size(x);
if length(o) >= D
A = A(1 : D, 1 : D); o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
A = round(-100 + 2 * 100 .* rand(D, D));
while det(A) == 0
A = round(-100 + 2 * 100 .* rand(D, D));
end
end
o(1 : ceil(D / 4)) = -100; o(max(floor(0.75 * D), 1) : D) = 100;
B = A * o';
for i = 1 : ps
f(i, 1) = max(abs(A * (x(i, :)') - B));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Multimodal %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 6.Shifted Rosenbrock's Function
function f = rosenbrock_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -90 + 180 * rand(1, D);
end
x = x - repmat(o, ps, 1) + 1;
f = sum(100 .* (x(:, 1 : D - 1) .^ 2 - x(:, 2 : D)) .^ 2 + (x(:, 1 : D - 1) - 1) .^ 2, 2);
% 7.Shifted Rotated Griewank's Function
function f = griewank_rot_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -600 + 0 * rand(1, D);
end
o = o(1 : D);
x = x - repmat(o, ps, 1);
x = x * M;
f = 1;
for i = 1 : D
f = f .* cos(x(:, i) ./ sqrt(i));
end
f = sum(x .^ 2, 2) ./ 4000 - f + 1;
% 8.Shifted Rotated Ackley's Function with Global Optimum on Bounds
function f = ackley_rot_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -30 + 60 * rand(1, D);
end
o(2 .* [1 : floor(D / 2)] - 1) = -32;
x = x - repmat(o, ps, 1);
x = x * M;
f = sum(x .^ 2, 2);
f = 20 - 20.* exp(-0.2 .* sqrt(f ./ D)) - exp(sum(cos(2 .* pi .* x), 2) ./ D) + exp(1);
% 9.Shifted Rastrign's Function
function f = rastrigin_func(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D)