% Test suite for optimization toolbox

errcmd = ['err, disp(''Error evaluating: '');', ...
	'disp(routines(item,:)); ',...
	'error(''Error in optimization routine'')']; 
%errcmd = 'err, disp(''Error in optimization routine''), pause';
opts=foptions; 

if ~exist('item'), item = 1; end
item = max(item, 1);
if ~exist('verbosity'), verbosity=0, disp('verbosity variable is off'),  end
if ~exist('test_long'), 
	test_long=0, 
	disp('test_long = 0 - so testing short version'), 
	disp('test_long = 2 - test everything')            
end
f='toptimf';
g='toptimg';
gol=[-5,-3,-1];
w=abs(gol); 
vlb=-4*ones(2); 
vub=-vlb;
z2=zeros(2,1); 
z22=zeros(2);

routines = [...
	'[x, opts] = fminu(f, x0, o, [], 1);                       %1'
	'[x, opts] = fminu(f, x0, o, g, 1);                          '
	'o(7)=1;[x, opts] = fminu(f, x0, o, [], 1);                  '
	'o(7)=1;[x, opts] = fminu(f, x0, o, g, 1);                   '
	'[x, opts] = constr(f, x0, o,[],[],[],2);                  %5'
	'[x, opts] = constr(f, x0, o,[],[],g ,2);                    '
	'o(9)=1;[x, opts] = constr(f, x0, o,[],[],g ,2);             '
	'[x, opts] = attgoal(f,x0, gol,w,o,vlb,vub,[],3);            '
	'o(15)=3;[x,opts]=attgoal(f,x0,gol,w,o,-2*ones(2),vub,[],3); '
	'[x, opts] = minimax(f,x0,o,[],[],[],4);                  %10'
	'[x, opts] = minimax(f,x0,o,[],[],g, 4);                     '
	'o(15)=5;[x, opts] = minimax(f,x0,o,[],[],g, 4);             '
	'[x,opts] = leastsq(f,x0,o,[],5);                            '
	'[x,opts] = leastsq(f,x0,o,g,5);                             '
	'o(7)=1; [x,opts] = leastsq(f,x0,o,g,5);                  %15'
	'[x,opts] = fsolve(''(x*x*x-[1,2;3,4])'',x0,o);                '
	'o(5)=1;[x,opts] = fsolve(''(x*x*x-[1,2;3,4])'',x0,o);         '
	'o(7)=1;[x,opts]=fsolve(''(x*x*x-[1,2;3,4])'',x0,o);           '
	'[x,opts] = fsolve(f,x0,o,[],6);                             '
	'[x,l]=lp(-[5 4 6],[1 -1 1;3 2 4;3 2 0],[20;42;30],x0);   %20'
	'[x,l]=qp([1 -1;-1 2]/2,-[2;6],[1 1;-1 2;2 1],[2;2;3],x0);   '
	'[x,l]=lp(f1,A,-b,x0,[],[],1);                               '
	'[x, opts] = fmins(f, x0, o, [], 1);                         '
	'[x, opts] = constr(f, x0, o, [], [],[],1);                  '
	'[x, opts] = seminf(f, 2, x0, o, [], [],7);               %25'
	'[x, opts] = seminf(f, 1, x0, o, [], [],8);                  '
	'[x, opts] = fmin(f, x0, 3*pi-x0, o, 9);                     '
	'o(9)=1; [x,opts] = leastsq(f,x0,o,g,5);                     '
	'o(9)=1; [x, opts] = fminu(f, x0, o, g, 1);                  '
];


% Variables for lp solution Pg 86 Schittkowski
A = -[1 1 1 -2 
	-1 -1 -1 -1 
	-0.2 -0.5 -1 -2
	-2 -1 -0.5 -0.2]; 
b=[6,-10,-10,-10]';
f1=[-0.5,-1,-0.5,-1]';

% Solutions
X1=ones(1,2); 
X01= [-1.2, 1];

X2=ones(1,2); 
X02 = [-1.2, 1];

X3=ones(1,2);  
X03 = [-1.2, 1];

X4=ones(1,2);  
X04 = [-1.2, 1];

X5=[24, 12, 12];
X05 = [10, 10, 10];

X6=[24, 12, 12];
X06 = [10, 10, 10];

X7=[24, 12, 12];
X07 = [10, 10, 10];

X8=[-4, -0.2564; -4, -4];
X08 = zeros(2);

X9=[ -2 0.9179; -1.8290, -2];
X09 = zeros(2);

X10=[4, 4];
X010 = [0.1,0.1];

X11=[4, 4];
X011 = [0.1,0.1];

X12=[8.7769, 0.6613];
X012 = [0.1,0.1];

X13=[0.25783 0.25783];
X013 = [0.3,0.4];

X14=[0.25783 0.25783];
X014 = [0.3,0.4];

X15=[0.25783 0.25783];
X015 = [0.3,0.4];

X16=[-0.12915 0.86022; 1.2903 1.1612];
X016 = ones(2);

X17=[-0.12915 0.86022; 1.2903 1.1612]; 
X017 = ones(2);

X18=[-0.12915 0.86022; 1.2903 1.1612]; 
X018 = ones(2);

X19=[-0.12915 0.86022; 1.2903 1.1612]; 
X019 = ones(2);

X20=[0; 15; 3];
X020 = zeros(3,1);

X21=[2/3; 1+1/3];
X021 = [0; 0];

X22=[0;26/3;0;4/3];
X022 =  zeros(4,1);

X23=ones(1,2); 
X023 = [-1.2, 1];

X24=ones(1,2); 
X024 = [-1.2, 1];

% Semi-infinite:
X25=[6.9559e-01   3.0521e-01   4.2615e-01];
X025 =  [0.5,0.2,0.3];

X26=[ 0.2011   0.2011   0.1978];  
X026 =  [0.2,0.2,0.2];

X27=asin(-1) + 2*pi;  
X027 =  [-0.1];

X28=X14;
X028 = X014;

X29=X1;
X029 = X01;

FBEST = [ 1.0617e-11  7.6506e-11 3.9279e-09  6.4309e-15  -3.4560e+03  -3.4560e+03 -3.4560e+03  -3.8626e-01  5.5556e-02 -5.0657e-12 -1.5151e-12 1.0761e+01 1.2436e+02];
FBEST = [ FBEST, 1.2436e+02 1.2436e+02  1.3011e-12  9.4929e-14  7.2413e-10 1.3011e-12  1.3011e-12  1.3011e-12  1.3011e-12 0 2.4745e-09 8.1719e-02 7.24e-6 -1 1.2436e+2 7.6506e-11];
bestiters=[149    86   160    52    54    18    21   118    80    25     7     9 41    25    12    45    51    40    45     0     0     0   175   156 37 37 6 27  88];
iters = bestiters; 

OLDTIME = [4.7224  3.6716 4.3786 2.8475 2.2460 1.2616 1.4257  9.7611 7.3355 2.2928 1.7030 2.1639 2.1210 3.5076 1.9999 1.6874 1.6444 1.6093 1.2460 0.0977 0.0976 0.2461 2.8318 4.8942 47.9852 35.1345 0 0 0];




format compact
if verbosity, routines, end 

sumiters =  0; 
for item = item:min(size(routines))
	o=(verbosity>0); o(9)=0; 
	if test_long
		eval(['x0 = X0',int2str(item),';'])
	else
		eval(['x0 = X',int2str(item),'-1e-9;'])
	end
	disp(routines(item,:))
	t0 = clock;
	eval(routines(item, :))
	TIME(item) = etime(clock, t0);
	TIME(item) - OLDTIME(item);
	iters(item) = opts(10);
	FV(item) = opts(8);
	if (FV(item) > FBEST(item)+1e-4*(1e-3+abs(FBEST(item)))) disp('Worse function value');  eval(errcmd), end
	if (FV(item) < FBEST(item)-1e-3) disp('Better function value!');  end
	eval(['err = x - X', int2str(item), ';'])	
	disp(routines(item,:))
	if norm(err)>1e-3, eval(errcmd), end 
	if (verbosity)
		item
		iteration_count = opts(10)
	end
	opts(10) = 0; 
	sumiters = iters(item)+sumiters;
end

if test_long  % Iteration check 
findworse = find(bestiters<iters);
if length(findworse)
	difference = bestiters(findworse) - iters(findworse)
	disp('------------Slower routines---------------')
	disp(routines(findworse,:))
end
findbetter = find(bestiters>iters);
if length(findbetter)
	difference = bestiters(findbetter) - iters(findbetter)
	disp('-----------------Faster routines---------------')
	disp(routines(findbetter,:))
end
end

% sumiters = 1426 DecStation 8/9 /90
if verbosity
	sumiters
	best_previous = 1426
end
if test_long == 2
	pause(-2)
	tutdemo
	clear method
	datdemo
	clear method
	bandemo
	clear method
	dfildemo
	pause(-2)
end
disp('Optimization Toolbox Passed !')
