%  as of/starting Nov30/15 ... polishing dec5/15
%  solves  x'Ax+2a'x  s.t. x'x<=delta   b'x<=beta
%  This code calls Algoritm3 to solve large sacale instances from third class of test problems.
% 
%%======================================================================
%  Initialization below:
%%  notests
%%  n  for dimension
%%  density  for density of A
clear all         
clc
profile clear
profile on
format long e
global A a b beta n    
%===================================================================
n=input('Enter n:');
density=input('Enter density:');
rng('shuffle')
rngsave=rng;
rng=rngsave;
%===================================================================
notests=10;   
KKT1=zeros(notests,1);   
KKT2=zeros(notests,1);   
STRONG=zeros(notests,1);
nLNGM=zeros(notests,1);
TIME=zeros(notests,1);
TIMESTRONG=zeros(notests,1);

for ii=1:notests,
    
    fprintf('\n\n Start new problem #%i in thirdclass.m with n=%i density=%g \n',ii,n,density)
    %% generate test problem 
    [A,a,b,beta,delta]=thirdclass(n,density);
    fprintf('solve min x''Ax +2a''x s.t. ||x||^2<=delta=%g, b''x<=beta = %g \n',delta,beta)
    [kkt1,kkt2,lambda_1opt,strong,LNGM,time,timestrong,fval,xopt]=Algorithm3(A,a,b,beta,delta,n);
    KKT1(ii)=kkt1;
    KKT2(ii)=kkt2;
    STRONG(ii)=strong; % if strong=0, we dont have strong duality. if strong=1, we have strong duality for eTRS
    nLNGM(ii)=LNGM;  % LNGM=0, solution of eTRS does not happen at LNGM of TRS. if LNGM=1,solution of eTRS is LNGM of TRS.         
    TIME(ii)=time;   % Time taken for Algorithm 3 (Main Algorith)
    TIMESTRONG(ii)=timestrong; % Time taken for verifyimg strong duality property
    fprintf('test # %i and: Time Timestrong KKT1 KKT2 values are:  %g %g  %g %g \n',...
        ii,TIME(ii),TIMESTRONG(ii),KKT1(ii),KKT2(ii))
   
end
%====================================================================
% =========    averaged results  ====================================
TIME=sum(TIME)/notests
TIMESTRONG=sum(TIMESTRONG)/notests
KKT1=sum(KKT1)/notests
KKT2=sum(KKT2)/notests
nLNGM=sum(nLNGM)    % number of test problems for which the optimal solution is LNGM
fprintf('\n \n #tests %i and average Time, Timestrong, KKT1, KKT2 values are:  %g %g  %g %g %g %g\n',...
    notests,TIME,TIMESTRONG,KKT1,KKT2)
fprintf('number of tests %i and number of NLGM optimal %i \n',notests,nLNGM)

profile report
