-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathwritesdpa.m
392 lines (387 loc) · 7.32 KB
/
writesdpa.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
% This function takes a problem in SeDuMi MATLAB format and writes it out
% in SDPA sparse format.
%
% Usage:
%
% ret=writesdpa(fname,A,b,c,K,pars)
%
% fname Name of SDPA file, in quotes
% A,b,c,K Problem in SeDuMi form
% pars Optional parameters.
% pars.printlevel=0 No printed output
% pars.printlevel=1 (default) Some printed output.
%
% ret ret=0 on success, ret=1 on failure.
%
% Notes:
%
% Problems with complex data are not allowed.
%
% Quadratic cone and rotated cone constraints are not supported.
%
% Nonsymmetric A.s and C.s matrices are symmetrized with A=(A+A')/2
% a warning is given when this happens.
%
% Free variables are not supported.
%
% Floating point numbers are written out with 18 decimal digits for
% accuracy.
%
% Please contact the author (Brian Borchers, [email protected]) with any
% questions or bug reports.
%
% Third Version: 3/3/06. Corrected a bug in the handling of
% nonsymmetric constraint matrices.
%
% Second Version: 7/14/04. Modified to vastly speed up operations on sparse
% matrices. On some problems, this version is 100 times
% faster!
%
% First Version: 7/14/03. Modified from old writesdp.m which wrote problems
% in SDPPack format.
%
function ret=writesdpa(fname,A,b,c,K,pars)
%
% First, check to see whether or not we should be quiet.
%
if (nargin > 5)
if (isfield(pars,'printlevel'))
if (pars.printlevel == 0)
quiet=1;
else
quiet=0;
end
else
quiet=0;
end
else
pars.printlevel=1;
quiet=0;
end
%
% First, check for complex numbers in A, b, or c.
%
if (isreal(A) ~= 1)
if (quiet == 0)
fprintf('A is not real!\n');
end
ret=1;
return
end
if (isreal(b) ~= 1)
if (quiet == 0)
fprintf('b is not real!\n');
end
ret=1;
return
end
if (isreal(c) ~= 1)
if (quiet == 0)
fprintf('c is not real!\n');
end
ret=1;
return
end
%
% Check for any quadratic cone constraints.
%
if (isfield(K,'q'))
if ((~isempty(K.q)) && (K.q ~= 0))
if (quiet == 0)
fprintf('quadratic cone constraints are not supported.\n');
end
ret=1;
return
end
end
%
% Check for any rotated cone constraints.
%
if (isfield(K,'r'))
if ((~isempty(K.r)) && (K.r ~= 0))
if (quiet == 0)
fprintf('rotated cone constraints are not supported.\n');
end
ret=1;
return
end
end
%
% Check for any free variables.
%
if (isfield(K,'f'))
if ((~isempty(K.f)) && (K.f ~= 0))
if (quiet == 0)
fprintf('Free variables are not supported.\n');
end
ret=1;
return
end
end
%
% Find the number of constraints.
%
m=length(b);
%
% Deal with the following special case. If A is transposed, transpose
% it again so that it is of the right size.
%
[Am,An]=size(A);
if (Am ~= m)
if (An == m)
if (quiet==0)
fprintf('Transposing A to match b \n');
end
AT=A;
A=A';
%
% Also swap Am and An so that they're correct.
%
temp=Am;
Am=An;
An=temp;
else
%
% In this case, A is just plain the wrong size.
%
if (quiet==0)
fprintf('A is not of the correct size to match b \n');
end
ret=1;
return
end
else
%
% No need to transpose A, but we'll need AT.
%
AT=A';
end
%
% Deal with the following special case: if c==0, then c should really
% be a zero vector of the appropriate size.
%
if (c == 0)
if (quiet==0)
fprintf('Expanding c to the appropriate size\n');
end
c=sparse(An,1);
end
%
% If c is empty, then act as if it was zero.
%
if (isempty(c))
if (quiet==0)
fprintf('Expanding empty c to zeros of the appropriate size\n');
end
c=sparse(An,1);
end
%
% If c is a row vector, make it a column vector.
%
[cm,cn]=size(c);
if (cn > cm)
c=c';
end
%
% Get the size data.
%
%
% First, the size of the LP block.
%
if (isfield(K,'l'))
nlin=K.l;
sizelin=nlin;
if (isempty(sizelin))
sizelin=0;
nlin=0;
end
if (K.l == 0)
nlin=0;
sizelin=0;
end
else
nlin=0;
sizelin=0;
end
%
% Get the sizes of the SDP blocks.
%
if (isfield(K,'s'))
nsdpblocks=length(K.s);
sizesdp=sum((K.s).^2);
if (isempty(sizesdp))
sizesdp=0;
nsdpblocks=0;
end
if (K.s == 0)
nsdpblocks=0;
sizesdp=0;
end
else
sizesdp=0;
nsdpblocks=0;
end
%
% Figure out the number of blocks.
%
nblocks=nsdpblocks;
if (nlin>0)
nblocks=nblocks+1;
end
%
% print out some size information
%
if (quiet==0)
fprintf('Number of constraints: %d \n',m);
fprintf('Number of SDP blocks: %d \n',nsdpblocks);
fprintf('Number of LP vars: %d \n',nlin);
end
%
% Open up the file for writing.
%
fid=fopen(fname,'w');
if (fid==-1)
if (quiet==0)
fprintf('Could not open file for output!');
end
ret=1;
return
end
%
% Print out m, the number of constraints.
%
fprintf(fid,'%d \n',m);
%
% Next, the number of blocks.
%
fprintf(fid,'%d \n',nblocks);
%
% Print out the block structure.
%
if (K.s > 0)
fprintf(fid,'%d ',K.s);
end
if (nlin > 0)
fprintf(fid,'%d ',-nlin);
end
fprintf(fid,'\n');
%
% Next, b, with all on one line.
%
fprintf(fid,'%.18e ',full(b));
fprintf(fid,'\n');
%
% First, the C matrix.
%
%
% First, calculate where in c things start.
%
base=sizelin+1;
%
% Next, work through the SDP blocks.
%
for i=1:nsdpblocks
%
% Get the current block of C.
%
I=find(c);
II=find(I>=base);
I=I(II);
II=find(I<=base+K.s(i)^2-1);
I=I(II);
II=I-(base-1)*ones(size(I));
work=sparse(II,ones(size(II)),c(I),K.s(i)^2,1);
work=reshape(work,K.s(i),K.s(i));
%
% Check this block for symmetry.
%
if (norm(work-work','fro') ~= 0)
if (quiet==0)
fprintf('Non symmetric C.s matrix!\n');
end
work=(work+work')/2;
end
%
% Write out the C.s matrix.
%
work=triu(work);
[II,JJ,V]=find(work);
cnt=length(II);
if (cnt ~= 0)
fprintf(fid,'%d %d %d %d %.18e\n',full([zeros(size(II)) i*ones(size(II)) II JJ -V]'));
end
%
% Next, update to the next base.
%
base=base+K.s(i)^2;
end
%
% Print out the coefficients for the linear block of C.
%
for i=1:nlin
if (c(i) ~= 0.0)
fprintf(fid,'%d %d %d %d %.18e\n',full([0 nsdpblocks+1 i i -c(i)]));
end
end
%
% Now, loop through the constraints, one at a time.
%
for cn=1:m
%
% Print out the SDP part of constraint cn.
%
base=sizelin+1;
rowcn=AT(:,cn);
for i=1:nsdpblocks
I=find(rowcn);
II=find(I>=base);
I=I(II);
II=find(I<=(base+K.s(i)^2-1));
I=I(II);
II=I-(base-1)*ones(size(I));
work=sparse(II,ones(size(II)),rowcn(I),K.s(i)^2,1);
work=reshape(work,K.s(i),K.s(i));
if (norm(work-work','fro') ~= 0)
if (quiet==0)
fprintf('Non symmetric A.s matrix! \n');
end
work=(work+work')/2;
end
%
% Ignore the lower left triangle.
%
work=triu(work);
%
% Get the nonzero entries.
%
[II,JJ,V]=find(work);
cnt=length(II);
if (cnt ~= 0)
fprintf(fid,'%d %d %d %d %.18e\n',full([cn*ones(size(II)) i*ones(size(II)) II JJ V]'));
end
%
% Next, update to the next base.
%
base=base+K.s(i)^2;
end
%
% Finally, the linear part.
%
I=find(rowcn);
II=find(I<=nlin);
I=I(II);
workrow=sparse(I,ones(size(I)),rowcn(I),nlin,1);
[II,JJ,V]=find(workrow);
if (length(II) > 0)
fprintf(fid,'%d %d %d %d %.18e\n',full([cn*ones(length(II),1) (nsdpblocks+1)*ones(length(II),1) II II V]'));
end
end
%
% Close the file.
%
fclose(fid);
%
% Return success
%
ret=0;
return