clear all
%profile off
%profile on
%% june23/14   Starting
%% dec28/14 rerunning tests for adding final norm(residual) values.
%%          One instance is rerun with the rank decreasing at each iteration.


%% comment out next lines appropriately to start at 'default'
rng('shuffle')
%rng('default')
saverng=rng;
%rng(saverng)
fprintf('\n\nStarting a *****a SINGLE new problem***** \n generating and solving TPCP problem; using **constr. rank** DR adaptation \n')
fprintf('We assume that both the TP and unital constraint are included so order of A_i m = order of B_i n \n')
m=input('USER: enter desired values of order of A_i and B_i , m=n values [n=m=12]');
if isempty(m),
	 m=12;
end
n=m;
k=input('enter desired value for number of constraints, k value [k=8]');
if isempty(k),
	 k=8;
end
%Pmax=input('enter 1,0 whether maxrank P is desired or not [=1]');
%if isempty(Pmax),
	 Pmax=0;   % leave as 0; no max rank desired
%end
r=input('enter desired original rank of feasible TPCP Ptr, r value [r=15]');
if isempty(r),
      r=15;
end
fprintf('code modified to find feasible solutions with rank constraints\n')
fprintf('rankconstr=rstart:-1:2   so beware of using rstart too large \n')
rstart=input('enter desired start for rank constraints, rstart value [rstart=20]');
if isempty(rstart),
      rstart=20;
end

totaltimes=[];
finaliters=[];
maxcos=[];
rankconstr=rstart;  % first rank constraint value

mn=m*n;
toler=1e-14;   % tolerance for residual in alt proj
iterlimit=3500;
fprintf('current tolerance for desired-accuracy; and iterlimit are  %g , %i \n',...
          toler,iterlimit)
%%%%test without knowledge of original rank 
P0=[];
%i=sqrt(-1);   % better to use    1i
  % generate Fi's using unitary Fi and prob distrib. p to get Ptr
	fprintf('generating cell F with unitaries for generating initial feasible Ptr \n')
	F=cell(r,1);
	p=rand(r,1)+1;
	p=p/sum(p);             % prob distrib.
	for rr=1:r,
		[temp,~]=qr(randn(m,n)+1i*randn(m,n));
		F{rr}=temp;
	end
	Ptr=FtoP(F,p,toler);
rankPs{1}=rank(Ptr);
% add I for last cell for unital constraint
k=k+1;
A=cell(k,1);   
B=cell(k,1);
for ii=1:k,
    if ii==k,
      A{k}=eye(n)/n;
      B{k}=eye(m)/m;
      fprintf('last constraint k= %i is the unital constraint \n',k);
    else
	[q,~]=qr(randn(n)+1i*randn(n));
	d=10*rand(n,1);
	%d=[1;zeros(n-1,1)];
	d=d/sum(d);
	A{ii}=q*diag(d)*q';
	A{ii}=(A{ii}+A{ii}')/2;
	%[q,~]=qr(randn(m)+1i*randn(m));
	%d=10*rand(m,1);
	%d=d/sum(d);
	%B{ii}=q*diag(d)*q';
	%B{ii}=(B{ii}+B{ii}')/2;
	B{ii}=TP(m,n,Ptr,A{ii});  % use image to try and get feasible problem
%testAB
     end
end
Ps=cell(1);
ranks=[];
maxcos=[];
normresiduals=[];
finaliters=[];
while rankconstr>1,  % while successful and > 1 (set to 0 otherwise)
  fprintf('input values [m n k r rankconstr] are  %i %i %i %i  %i \n',...
            m,n,k,r,rankconstr);
  P0=[];
  [Pcurrs,rankend,totaliters,maxcosvalue,normresidual]=...
                 maxrankTPCP(n,m,rankconstr,A,B,Pmax,toler,iterlimit,P0);
  if rankend > 0,
  	fprintf('from runrankconstrTPCPDR: constr. rank problem finished successfully\n')
	normresiduals=[normresiduals normresidual];
  	ranks=[ranks rankend];
        maxcos=[maxcos maxcosvalue]; % save max of the iterations
        finaliters=[finaliters totaliters]; % save min of the iterations
          rankconstr=rankconstr-1;
  	Ps{length(ranks)}=Pcurrs{end};
  else
          rankconstr=0;
  end
  fprintf('feasible Ps are in cell Pcurrs \n')
  fprintf('%i rank values are in variable ranks \n ',length(ranks))
  fprintf('\n')
  %fprintf('%i iteration values are in variable totaliters ',length(totaliters))
  %for rr=1:length(totaliters),
  %    fprintf(' %i ',totaliters(rr))
  %end
  fprintf('\n')
  
  
save tempdatasave  maxcos finaliters normresiduals ranks
end   % while
%%% no latex table produced for single output
fprintf('at end of runrankconstrTPCPDR.m\n')
fprintf('file tempdatasave.mat has last values for variables: \n')
fprintf('  maxcos finaliters normresiduals ranks \n')
figure(1)
plot(ranks,normresiduals)
title('ranks versus norm(residuals)')
xlabel('ranks')
ylabel('norm(residuals')

%profile report
