-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdpa_to_vsdp.m
127 lines (112 loc) · 3.18 KB
/
sdpa_to_vsdp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function [blk,A,C,b] = sdpa_to_vsdp(fname)
% SDPA_TO_VSDP Import problem in SDPA sparse format to VSDP's format.
%
% [blk,A,C,b] = SDPA_TO_VSDP(fname) The input 'fname' name of the file
% containing SDP data in SDPA format. The problem data (blk,A,C,b) of
% VSDP's block-diagonal problem is described in 'mysdps.m'.
%
% Example:
%
% fname = fullfile('sdplib_vsdp', 'arch4.dat-s');
% [blk,A,C,b] = sdpa_to_vsdp(fname);
%
% See also mysdps.
% Copyright 2004-2006 Christian Jansson ([email protected])
%Open the file for input
compressed = 0;
if exist(fname,'file')
fid = fopen(fname,'r');
elseif exist([fname,'.Z'],'file')
compressed = 1;
unix(['uncompress ',fname,'.Z']);
fid = fopen(fname,'r');
elseif exist([fname,'.gz'],'file')
compressed = 2;
unix(['gunzip ',fname,'.gz']);
fid = fopen(fname,'r');
else
fprintf('** Problem not found, please specify the correct path. \n');
return;
end
%Clean up special characters and comments from the file
datavec = fscanf(fid,'%c');
linefeeds = findstr(datavec,char(10));
comment_chars = '*"=';
cumidx = [];
for i=1:length(comment_chars)
idx = findstr(datavec,comment_chars(i));
cumidx = [cumidx,idx];
end
for j=length(cumidx):-1:1
if (cumidx(j)==1) || (strcmp(datavec(cumidx(j)-1),char(10)))
datavec(cumidx(j):linefeeds(min(find(cumidx(j)<linefeeds))))='';
else
datavec(cumidx(j):linefeeds(min(find(cumidx(j)<linefeeds)))-1)='';
end
end
special_chars = ',{}()';
cumidx=[];
for i=1:length(special_chars)
idx = findstr(datavec,special_chars(i));
cumidx = [cumidx,idx];
end
datavec(cumidx) = blanks(length(cumidx));
clear linefeeds;
%Close the file
fclose('all');
if compressed==1; unix(['compress ',fname]); end
if compressed==2; unix(['gzip ',fname]); end
%Next, read in basic problem size parameters.
datavec = sscanf(datavec,'%f');
if size(datavec,1) < size(datavec,2); datavec = datavec'; end
m = datavec(1); %the number of dual variables
numblk = datavec(2); %the number of blocks in a matrix
blksize = abs(datavec(2+(1:numblk))); %the block structure vector
if size(blksize,1) > size(blksize,2); blksize = blksize'; end
A=cell(numblk,m);
C=cell(numblk,1);
%Get input b.
idxstrt = 2+numblk;
b = datavec(idxstrt+(1:m));
idxstrt = idxstrt+m;
b = -b;
%Construct blk
%
denumblk = length(blksize);
for p = 1:denumblk
sj = blksize(p);
blk{p,1} = 's'; blk{p,2} = sj;
end
%blk{1,1} = 's'; blk{1,2} = blksize;
%Construct A and C
len = length(datavec);
Y = reshape(datavec(idxstrt+1:len),5,(len-idxstrt)/5)';
clear datavec;
Y = sortrows(Y,[1 2]);
matidx = [0; find(diff(Y(:,1)) ~= 0); size(Y,1)];
for k = 1:length(matidx)-1
idx = (matidx(k)+1 : matidx(k+1));
Ytmp = Y(idx,1:5);
matno = Ytmp(1,1);
Ytmp2 = Ytmp(:,2);
for p = 1:numblk
n = blksize(p);
idx = find(Ytmp2 == p);
ii = Ytmp(idx,3); jj = Ytmp(idx,4); vv =Ytmp(idx,5);
idxtmp = find(ii > jj);
if ~isempty(idxtmp)
tmp = jj(idxtmp);
jj(idxtmp) = ii(idxtmp); ii(idxtmp) = tmp;
end
tmp = -sparse(ii,jj,vv,n,n);
tmp = tmp + triu(tmp,1)';
if (matno == 0)
C{p,1} = tmp;
else
% A{p,1}(:,matno) = tmp;
A{p,matno} = tmp;
end
end
end
A = A';
end