function [fobj, bound] = Optimizer(select)
%% 目标函数
switch select
case 1
fobj = @ Sphere; % 效果很好 [-100, 100]
bound = [-100, 100];
case 2
fobj = @ Ackley; % 效果比较好 [-32, 32]
bound = [-32.768, 32.768];
case 3
fobj = @ Rastrigin; % 效果好 [-5.12, 5.12]
bound = [-5.12, 5.12];
case 4
fobj = @ Alpine; % 效果好 [-10,10]
bound = [-10, 10];
case 5
fobj = @ Squares; % 效果好 [-10,10]
bound = [-10, 10];
case 6
fobj = @ Griewank; % 效果好 [-600, 600]
bound = [-600, 600];
case 7
fobj = @ Powell; % 效果好 [-4, 5]
bound = [-4, 5];
case 8
fobj = @ Zakharov; % 效果好 [-5, 10]
bound = [-5, 10];
case 9
fobj = @ Sumpow; % 效果好 [-1, 1]
bound = [-1, 1];
case 10
fobj = @ Rothyp; % 效果好 [-65, 65]
bound = [-65, 65];
case 11
fobj = @ Schaffer; % [-100, 100]
bound = [-100, 100];
case 12
fobj = @ schw; % [-100, 100]
bound = [-500, 500];
case 13
fobj = @ shekel; % [-100, 100]
bound = [0, 10];
case 14
fobj = @ trid; % [-100, 100]
bound = [-100, 100];
end
function [y] = Sphere(xx)
d = length(xx);
sum = 0;
for ii = 1:d
xi = xx(ii);
sum = sum + xi^2;
end
y = sum;
end
function [y] = Ackley(xx)
d = length(xx);
sum1 = 0;
sum2 = 0;
for ii = 1:d
xi = xx(ii);
sum1 = sum1 + xi^2;
sum2 = sum2 + cos(2*pi*xi);
end
term1 = -20*exp(-0.2*sqrt(sum1/d));
term2 = -exp(sum2/d);
y = term1 + term2 + 20 + exp(1);
end
function [y] = Rastrigin(xx)
d = length(xx);
sum = 0;
for ii = 1:d
xi = xx(ii);
sum = sum + (xi^2 - 10*cos(2*pi*xi));
end
y = 10*d + sum;
end
function [y] = Alpine(xx)
d = length(xx);
sum = 0;
for i=1:d
sum = sum + abs(xx(i)*sin(xx(i))+0.1*xx(i));
end
y = sum;
end
function [y] = Squares(xx)
d = length(xx);
sum = 0;
for i = 1:d
xi = xx(i);
sum = sum + i*xi^2;
end
y = sum;
end
function [y] = Griewank(xx)
d = length(xx);
sum = 0;
prod = 1;
for ii = 1:d
xi = xx(ii);
sum = sum + xi^2/4000;
prod = prod * cos(xi/sqrt(ii));
end
y = sum - prod + 1;
end
function [y] = Powell(xx)
d = length(xx);
sum = 0;
for ii = 1:(d/4)
term1 = (xx(4*ii-3) + 10*xx(4*ii-2))^2;
term2 = 5 * (xx(4*ii-1) - xx(4*ii))^2;
term3 = (xx(4*ii-2) - 2*xx(4*ii-1))^4;
term4 = 10 * (xx(4*ii-3) - xx(4*ii))^4;
sum = sum + term1 + term2 + term3 + term4;
end
y = sum;
end
function [y] = Zakharov(xx)
d = length(xx);
sum1 = 0;
sum2 = 0;
for ii = 1:d
xi = xx(ii);
sum1 = sum1 + xi^2;
sum2 = sum2 + 0.5*ii*xi;
end
y = sum1 + sum2^2 + sum2^4;
end
function [y] = Sumpow(xx)
d = length(xx);
sum = 0;
for ii = 1:d
xi = xx(ii);
new = (abs(xi))^(ii+1);
sum = sum + new;
end
y = sum;
end
function [y] = Rothyp(xx)
d = length(xx);
outer = 0;
for ii = 1:d
inner = 0;
for jj = 1:ii
xj = xx(jj);
inner = inner + xj^2;
end
outer = outer + inner;
end
y = outer;
end
function [y] = Schaffer(xx)
x1 = xx(1);
x2 = xx(2);
fact1 = (sin(x1^2-x2^2))^2 - 0.5;
fact2 = (1 + 0.001*(x1^2+x2^2))^2;
y = 0.5 + fact1/fact2;
end
function y = schw(x)
n = 2;
s = sum(-x.*sin(sqrt(abs(x))));
y = 418.9829*n+s;
end
function y = shekel(x) % 4变量
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
y = -s;
end
function y = trid(x)
n = 10;
s1 = 0;
s2 = 0;
for j = 1:n
s1 = s1+(x(j)-1)^2;
end
for j = 2:n
s2 = s2+x(j)*x(j-1);
end
y = s1-s2;
end
end