%Federica Tinti (ftinti@dm.unipd.it)
%Department of Mathematics of Ferrara (ITALY)
%
%Extragradient method with adaptive steplegth 
%   (first modified version)
%
clear all
%
stringa=input('test problem=', 's');
eval(stringa);
%parameters
P=eye(n);
eps=1e-4;
rx=0;
alpha=1.;
alphacap=1e-3;
beta=0.7;
csi=0.8;
gamma=0.9;
flag=0;
%
iter=0;      
nf=0;               
np=0;				
icontvisual=0;
tempo=cputime;
while norm(rx)>eps | iter==0 
        fx=feval(fs,x);
        nf=nf+1;
        f=-(x-alpha*fx);
        %first projection
        options=optimset('LargeScale','off','display','off');
        [xx]=quadprog(P,f,A,b,Aeq,beq,lb,ub,x,options);
        xx=abs(xx);
        np=np+1;        
        fxx=feval(fs,xx);
        nf=nf+1;
        diff=x-xx;
        fdiff=fx-fxx;
        num=fdiff'*fdiff;
        den=diff'*diff;
      while (alpha>beta*sqrt(den/num))   
          alpha=max(alphacap,min(alpha*csi,(beta*sqrt(den/num))));                          
          f=-(x-alpha*fx);   
          options=optimset('LargeScale','off','display','off');
          [xx]=quadprog(P,f,A,b,Aeq,beq,lb,ub,x,options);  
          xx=abs(xx);
          fxx=feval(fs,xx);
          np=np+1;
          nf=nf+1;
          diff=x-xx;
          fdiff=fx-fxx;
          num=fdiff'*fdiff;
          den=diff'*diff;
      end;    
          %second projection
          xold=x;
          f=-(x-alpha*fxx);
          options=optimset('LargeScale','off','display','off');
          [x]=quadprog(P,f,A,b,Aeq,beq,lb,ub,xx,options);  
          x=abs(x);
          np=np+1;
          rx=xold-x;
          iter=iter+1;
          alphab(iter)=alpha;
          %
          alpha=alpha+(beta*sqrt(den/num)-alpha)*gamma;      
      end;
  t=cputime-tempo
  fprintf('solution=%g \n',x);
  fprintf('number of projections=%g \n',np);          
  fprintf('number of function evaluations=%g \n',nf);         
  fprintf('number of iterations=%g \n',iter);           
  fprintf('input parameter beta=%g \n',beta); 
  fprintf('input parameter csi=%g \n',csi); 