Home > utilities > readsdpa_old.m

# readsdpa_old

## PURPOSE

read the file & separate the linear constraint matrix, return

## SYNOPSIS

function sdpdata=readsdpa(filename);

## DESCRIPTION

     read the file & separate the linear constraint matrix, return
     the problem in the following structure:

    function [mDIM,nBLOCK,bLOCKsTRUCT,c,F]=readsdpa(filename);

     Read a problem in SDPA sparse format.

     [mDIM,nBLOCK,bLOCKsTRUCT,c,F] = read_data(fname)

     <INPUT>
     - filename: string; filename of the SDP data with SDPA foramt.

     <OUTPUT>
     - mDIM       : integer; number of primal variables
     - nBLOCK     : integer; number of blocks of F
     - bLOCKsTRUCT: vector; represetns the block structure of F
     - c          : vector; coefficient vector
     - F          : cell array; coefficient matrices

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function sdpdata=readsdpa(filename);
    0002 % read the file & separate the linear constraint matrix, return
    0003 % the problem in the following structure:
    0004 %
    0005 %function [mDIM,nBLOCK,bLOCKsTRUCT,c,F]=readsdpa(filename);
    0006 %
    0007 % Read a problem in SDPA sparse format.
    0008 %
    0009 % [mDIM,nBLOCK,bLOCKsTRUCT,c,F] = read_data(fname)
    0010 %
    0011 % <INPUT>
    0012 % - filename: string; filename of the SDP data with SDPA foramt.
    0013 %
    0014 % <OUTPUT>
    0015 % - mDIM       : integer; number of primal variables
    0016 % - nBLOCK     : integer; number of blocks of F
    0017 % - bLOCKsTRUCT: vector; represetns the block structure of F
    0018 % - c          : vector; coefficient vector
    0019 % - F          : cell array; coefficient matrices
    0020 %
    0021
    0022 % This file is a component of SDPA
    0023 % edited by JF 2011
    0024 %
    0025 % Copyright (C) 2004 SDPA Project
    0026 %
    0027 % This program is free software; you can redistribute it and/or modify
    0028 % it under the terms of the GNU General Public License as published by
    0029 % the Free Software Foundation; either version 2 of the License, or
    0030 % (at your option) any later version.
    0031 %
    0032 % This program is distributed in the hope that it will be useful,
    0033 % but WITHOUT ANY WARRANTY; without even the implied warranty of
    0034 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0035 % GNU General Public License for more details.
    0036 %
    0037 % You should have received a copy of the GNU General Public License
    0038 % along with this program; if not, write to the Free Software
    0039 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    0040 %
    0041 % SDPA-M: $Revision: 6.2 $
    0042 % $Id: read_data.m,v 6.2 2005/05/28 02:36:40 drophead Exp $
    0043
    0044 % check the validity of the arguments
    0045 if ( nargin ~= 1 | ( nargin == 1 & ~isstr(filename) ) )
    0046   error('input argument must be a filename');
    0047 end
    0048
    0049 sdpdata=[];
    0050
    0051 % identify whether a file is sparse format or not.
    0052 bsparse=0;
    0053 len=length(filename);
    0054 if len >= 2
    0055   str=filename(end-1:end);
    0056   if strncmp(str,'-s',2)
    0057     bsparse=1;
    0058   end
    0059 end
    0060
    0061 fid=fopen(filename,'r');
    0062 if fid == -1
    0063   error(sprintf('Cannot open %s',filename));
    0064 end
    0065
    0066 % skip comment and after it, read a number of decision variables (mDIM)
    0067 while 1
    0068   str=fgetl(fid);
    0069   if( str(1)~='*' & str(1) ~='"' )
    0070     mDIM=sscanf(str,'%d',1);
    0071     break;
    0072   end
    0073 end
    0074 %disp(sprintf('mDIM=%d',mDIM));
    0075
    0076 % read a number of blocks (nBLOCK)
    0077 nBLOCK=fscanf(fid,'%d',1);
    0078 %disp(sprintf('nBLOCK=%d',nBLOCK));
    0079
    0080 % read each size of blocks (bLOCKsTRUCT)
    0081 bLOCKsTRUCT=zeros(nBLOCK,1);
    0082 for idx=1:nBLOCK
    0083   bLOCKsTRUCT(idx)=fscanf(fid,'%*[^0-9+-]%d',1);
    0084   %if bLOCKsTRUCT(idx) == 1
    0085   %  bLOCKsTRUCT(idx) = -1;
    0086   %end
    0087 end
    0088
    0089 % read cost vector (c)
    0090 c=zeros(mDIM,1);
    0091 for idx=1:mDIM
    0092   c(idx)=fscanf(fid,'%*[^0-9+-]%lg',1);
    0093 end
    0094
    0095 % read coefficient matrices (F)
    0096 F=cell(nBLOCK,mDIM+1);
    0097
    0098 if bsparse
    0099   % sparse format case
    0100   while 1
    0101     [k,cnt]=fscanf(fid,'%*[^0-9+-]%d',1);
    0102     [l,cnt]=fscanf(fid,'%*[^0-9+-]%d',1);
    0103     [i,cnt]=fscanf(fid,'%*[^0-9+-]%d',1);
    0104     [j,cnt]=fscanf(fid,'%*[^0-9+-]%d',1);
    0105     [value,cnt]=fscanf(fid,'%*[^0-9+-]%lg',1);
    0106     if cnt
    0107       if isempty(F{l,k+1})
    0108         bsize=abs(bLOCKsTRUCT(l));
    0109         if bLOCKsTRUCT(l) < 0
    0110           F{l,k+1}=sparse(zeros(bsize,1));
    0111         else
    0112           F{l,k+1}=sparse(zeros(bsize));
    0113         end
    0114       end
    0115       if bLOCKsTRUCT(l) < 0
    0116         F{l,k+1}(i)=value;
    0117       else
    0118         if i < j
    0119           F{l,k+1}(i,j)=value;
    0120           F{l,k+1}(j,i)=value;
    0121         elseif i == j
    0122           F{l,k+1}(i,j)=value;
    0123         end
    0124       end
    0125     else
    0126       break;
    0127     end
    0128   end
    0129 else
    0130   % dense format case
    0131   for k=1:mDIM+1
    0132     for l=1:nBLOCK
    0133       bsize=abs(bLOCKsTRUCT(l));
    0134       if bLOCKsTRUCT(l) > 0
    0135         F{l,k}=zeros(bsize);
    0136         for i=1:bsize
    0137           for j=1:bsize
    0138             [value,cnt]=fscanf(fid,'%*[^0-9+-]%lg',1);
    0139             if cnt
    0140               F{l,k}(i,j)=value;
    0141             else
    0142               error(sprintf('Failed to read an element at %d %d %d %d'),...
    0143             k-1,l,i,j);
    0144             end
    0145           end
    0146         end
    0147       else
    0148         F{l,k}=zeros(bsize,1);
    0149         for i=1:bsize
    0150           [value,cnt]=fscanf(fid,'%*[^0-9+-]%lg',1);
    0151           if cnt
    0152             F{l,k}(i)=value;
    0153           else
    0154             error(sprintf('Failed to read an element at %d %d %d %d'),...
    0155               k-1,l,i,i);
    0156           end
    0157         end
    0158       end
    0159     end
    0160   end
    0161 end
    0162 fclose(fid);
    0163
    0164 % How to treat 1-dim blocks - exclude into the linear inequality block?
    0165 %linblk=find(bLOCKsTRUCT<0 | bLOCKsTRUCT==1);
    0166 linblk=find(bLOCKsTRUCT<0);
    0167 matr=setdiff(1:nBLOCK,linblk);
    0168 sdpdata.Ng=sum(abs(bLOCKsTRUCT(linblk)));
    0169
    0170 %linblk
    0171 %matr
    0172
    0173 % this might not work if linblk has more than 1 element
    0174 % do blocks one by one + perhaps use vertcat (instead of horzcat [])
    0175 %B=[F{linblk,2:end}];
    0176 %d=[F{linblk,1}];
    0177
    0178 B=[]; d=[];
    0179 % still doesn't work (see truss1) - some of the matrices (columns in B) are
    0180 % empty instead of zeros -> won't concatenate the columns correctly
    0181 % ==> modify F and substitute empty matrices by zero matrices
    0182 if (~isempty(linblk))
    0183   for k=linblk
    0184     sz=abs(bLOCKsTRUCT(k));
    0185     for i=1:mDIM+1
    0186       if (isempty(F{k,i}))
    0187         F{k,i} = sparse(sz, 1); % in linblk ==> it is just a vector, not a matrix
    0188       end
    0189     end
    0190   end
    0191   for k=linblk
    0192     Bk=[F{k,2:end}];   % should be a matrix dim_of_kth_block x mDIM
    0193     dk=[F{k,1}];       % should be a vector dim_of_kth_block x 1
    0194     % check dimensions if concatenated properly
    0195     if (size(Bk,1)~=abs(bLOCKsTRUCT(k)) || size(Bk,2)~=mDIM)
    0196       disp(sprintf('ERR: wrong dimensions of Bk, k=%i: %i %i',k,size(Bk)))
    0197     end
    0198     if (size(dk,1)~=abs(bLOCKsTRUCT(k)) || size(dk,2)~=1)
    0199       disp(sprintf('ERR: wrong dimensions of dk, k=%i: %i %i',k,size(dk)))
    0200     end
    0201     % vertical concatenation
    0202     B=[B;Bk];
    0203     d=[d;dk];
    0204   end
    0205 end
    0206
    0207 %size(B)
    0208 %size(d)
    0209
    0210 % apply the same fix for the first blocks ("absolute term"), the others are covered by Adep
    0211 for k=matr
    0212   sz=bLOCKsTRUCT(k);
    0213   if (isempty(F{k,1}))
    0214     F{k,1}=sparse(sz,sz);
    0215   end
    0216 end
    0217
    0218 % probably won't work as F{k,i}... if F{k,i} is empty it won't add a column :(
    0219 % better [F{k,:}]
    0220 %B=[];
    0221 %for k=linblk
    0222 %  D=[];
    0223 %  for i=2,mDIM+1
    0224 %    D=[D, F{k,i}]
    0225 %  end
    0226 %  B=[B;D]
    0227 %end
    0228
    0229 sdpdata.name=filename;
    0230 sdpdata.Nx=mDIM;
    0231 sdpdata.Na=length(matr);  %nBLOCK;
    0232 sdpdata.B=B;
    0233 sdpdata.d=d;
    0234 sdpdata.c=c;
    0235 sdpdata.NaDims=bLOCKsTRUCT(matr);
    0236 sdpdata.A=F(matr,:);
    0237
    0238 % create dependency table, Adep(k) = vector of all x indices which Ak depends on
    0239 sdpdata.Adep=cell(sdpdata.Na,1);
    0240 for k=1:sdpdata.Na
    0241   list=[];
    0242   for i=2:mDIM+1
    0243     if (~isempty(sdpdata.A{k,i}) && nnz(sdpdata.A{k,i})>0)
    0244       list=[list,i-1];
    0245     end
    0246   end
    0247   sdpdata.Adep{k}=list;
    0248 end
    0249
    0250 sdpdata.origF=F;
    0251 sdpdata.origNaDims=bLOCKsTRUCT;
    0252
    0253 % End of File