% command to run min search
% tic; [fmin, xmin] = DummyClass.findOptimumGrad(2000, 1e4, 6); toc;
classdef DummyClass
properties
end;
properties ( Constant )
M2 = 1600;
USE_PROD_COS = 1;
end;
methods ( Static )
function [f, g] = optfunc(x)
x2 = x*x';
par_part = 1 - (x2/(numel(x) * DummyClass.M2));
cos_part = 0.6*cos(1*pi*x2) + 0.1*cos(2*pi*x2) + 0.3*cos(3*pi*x2);
pro_part = 1;
m = (1 : numel(x)) / (0.5*numel(x));
if (DummyClass.USE_PROD_COS > 0)
x_cos = cos(m.*x);
pro_part = prod(x_cos);
end;
f = -(par_part*cos_part*pro_part);
if (nargout == 2)
d_par_part = -2*x/(numel(x) * DummyClass.M2);
d_cos_part = -2*pi*x*(0.6*sin(1*pi*x2) + 0.2*sin(2*pi*x2) + 0.9*sin(3*pi*x2));
d_pro_part = 0;
if (DummyClass.USE_PROD_COS > 0)
d_pro_part = m .* sin(m .* x) .* pro_part;
for i = 1 : numel(x)
t_cos = x_cos(i);
if (abs(t_cos) > 0.05)
d_pro_part(i) = -d_pro_part(i)/t_cos;
else
d_pro_part(i) = -prod(x_cos(setdiff(1 : numel(x), i)));
end;
end;
end;
g = -(d_par_part*cos_part*pro_part + par_part*d_cos_part*pro_part + par_part*cos_part*d_pro_part);
end;
end;
function [fmin, xmin] = findOptimumGrad(MaxIter, N, len_x)
objFun = @(x) DummyClass.optfunc(x);
nonlcon = [];
lb = -2 * ones(1, len_x);
ub = +sqrt(DummyClass.M2) * ones(1, len_x);
options = optimset('Algorithm', 'sqp', 'GradObj', 'on', 'Hessian', 'off', 'MaxIter', MaxIter, 'MaxFunEvals', 2000, 'TolFun', 1e-12, 'TolCon', 1e-12, 'FinDiffRelStep', 1e-9, 'Display', 'off');
xmin = lb;
fmin = 1e10;
for iter = 1 : N
% try to choose bad start point
xstart = 30 + rand(size(lb));
[x0,fval,exitflag,output] = fmincon(objFun, xstart, [], [], [], [], lb, ub, nonlcon, options);
if (fval < fmin)
fmin = fval;
xmin = x0;
end;
end;
end;
end;
end