%Federica Tinti (ftinti@dm.unipd.it)
%Department of Mathematics of Ferrara (ITALY)
%
%Projection-Contraction method (Solodov-Tseng)
%
clear all
flag1=0;
%
stringa=input('test problem=', 's');
eval(stringa);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameters
P=eye(n);
eps=1e-4;                       
alpha=1;
theta=1.9;          %theta=1;   %theta=1.5;
rho=0.5;            %rho=0.1
beta=0.3;   
iteresterna=0;                  
nf=1;                      
rxx=0;                          
fx=feval(fs,x);
%fprintf('f(x)=%g \n',fx);
%pause;
np=0;                           
%
z=zeros(n,1);
num=0;
fz=zeros(n,1);
diff=x-z;
%
tempo=cputime;
while norm(diff)>eps | iteresterna==0
    iter=0;
%diff=x-z;
%                   
    while alpha*diff'*(fx-fz)>(1-rho)*num | iter==0 
        if iter ~=0
            alpha=alpha*beta;
        end;
        f=-(x-alpha*fx);
        options=optimset('LargeScale','off','display','off');
        [z]=quadprog(P,f,[],[],Aeq,beq,lb,ub,x,options);
        z=abs(z);
        np=np+1;
        nf=nf+1;
        fz=feval(fs,z);
        diff=x-z;
        num=diff'*diff;
        iter=iter+1;
    end;
    aux=diff-alpha*(fx-fz);
    den=aux'*aux;
    gamma=theta*rho*num/den; 
    x=x-gamma*aux;
    %fprintf('x=%g \n',x);
    iteresterna=iteresterna+1;
    fx=feval(fs,x);
    nf=nf+1;
    diff=x-z;
end;
t=cputime-tempo
fprintf('iteresterna=%g \n',iteresterna);
fprintf('np=%g \n',np);
fprintf('nf=%g \n',nf);
fprintf('theta=%g \n',theta);
fprintf('rho=%g \n',rho);