clear all

fid = fopen('output1s.txt','w');
fclose(fid);

fid = fopen('output2s.txt','w');
fclose(fid);

fid = fopen('times.txt','w');
fclose(fid);

for k = [4 5] % set the number of sets
  for imax = [10 10] % set the maximum set sizes 
      
    for choice = [1 2]
      
      if choice == 1 % structured graph
        m = randi(imax,k,1);   % this might yield sets with 'same size'
        if sum(m == 1) > 0
          m = m + 1;
        end
        p = 20; % can play around with this p also
        
        [L,edgenumber] = graph_gen(m,p);
        n = sum(m);       % number of nodes
        A = diag(diag(L)) - L; % adjacency matrix
        totaledge = sum(sum(A))/2;
      else % random graph % QP gives better lower bound, sometimes better upper bound
        m = randi(imax,k,1);    % this might yield sets with 'same size'
        if sum(m == 1) > 0
          m = m + 1;
        end
        n = sum(m);       % number of nodes
        %%%%%method 1: find a random adjacency matrix 
        A = round(rand(n));
        A = round((A + A')/2);
        A = A - diag(diag(A));    % random adjacency matrix
        totaledge = sum(sum(A))/2;
        
        d = sum(A,2);
        L = -A; L(1:n+1:end) = d;
      end
      
      [Lbnd,Abndeq,~,~,upperL,upperA,e1,e2] = myprojeigbd(L,m);
      [qpbd,~,upperQ,fval,q1,q2] = myqpbd(L,m);
      [optvalt3A,~,upperSDP,s1,s2,s0] = mysdpbd(L,m);
      [optvaldnng,~,upperDNN,d1,d2,d0] = myDNNbd(L,m);
      
      upperL2 = floor(upperL); upperA2 = floor(upperA); upperQ2 = floor(upperQ); upperSDP2 = floor(upperSDP); upperDNN2 = floor(upperDNN);
      Lbnd2 = ceil(Lbnd); Abndeq2 = ceil(Abndeq); qpbd2 = ceil(qpbd); optvalt3A2 = ceil(optvalt3A(2)); optvaldnng2 = ceil(optvaldnng(2));
      
      bestupper = min([upperL2,upperA2,upperQ2,upperSDP2,upperDNN2]);
      bestlower = max([Lbnd2,Abndeq2,qpbd2,optvalt3A2,optvaldnng2]);
      relgap = (bestupper - bestlower)/(bestupper + bestlower);
      
      if choice == 1 % nodes, partition number, edges, natural bound, lower bounds: eigL, eigA, QP, SDP, upper bounds:..., reference value
        fid = fopen('output1s.txt','a');
        fprintf(fid,' %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %5.4f &&& %g\n', ...
          size(L,1), size(m(:),1), full(totaledge), edgenumber, Lbnd2, Abndeq2, qpbd2, optvalt3A2, optvaldnng2, upperL2, upperA2, upperQ2, upperSDP2, upperDNN2, relgap, fval)
        fclose(fid);
      else % nodes, partition number, edges, lower bounds: eigL, eigA, QP, SDP, upper bounds:..., reference value
        fid = fopen('output2s.txt','a');
        fprintf(fid,' %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %g & %5.4f &&& %g\n', ...
          size(L,1), size(m(:),1), full(totaledge), Lbnd2, Abndeq2, qpbd2, optvalt3A2, optvaldnng2, upperL2, upperA2, upperQ2, upperSDP2, upperDNN2, relgap, fval)
        fclose(fid);
      end
      fid = fopen('times.txt','a');
      fprintf(fid,' %g & %g & %g & %g & %g & %g & %g & %g & %g(%g) & %g & %g(%g) & %g\n', choice, size(L,1), size(m(:),1), full(totaledge),e1,e2,q1,q2,s1,s0,s2,d1,d0,d2)
      fclose(fid);
      
    end
  end
end