1 | function solution = savesdpafile(varargin) |
---|
2 | %SAVESDPAFILE Saves a problem definition in the SDPA format |
---|
3 | % |
---|
4 | % SAVESDPAFILE(F,h,'filename') Saves the problem min(h(x)), F(x)>0 to the file filename |
---|
5 | % SAVESDPAFILE(F,h) A "Save As" - box will be opened |
---|
6 | |
---|
7 | % Author Johan Löfberg |
---|
8 | % $Id: savesdpafile.m,v 1.5 2005/09/27 10:21:32 joloef Exp $ |
---|
9 | |
---|
10 | F = varargin{1}; |
---|
11 | h = varargin{2}; |
---|
12 | nvars = yalmip('nvars'); |
---|
13 | |
---|
14 | % Expand nonlinear operators |
---|
15 | [F,failure,cause] = expandmodel(F,h,sdpsettings); |
---|
16 | if failure % Convexity propgation failed |
---|
17 | interfacedata = []; |
---|
18 | recoverdata = []; |
---|
19 | solver = ''; |
---|
20 | diagnostic.solvertime = 0; |
---|
21 | diagnostic.problem = 14; |
---|
22 | diagnostic.info = yalmiperror(14,cause); |
---|
23 | return |
---|
24 | end |
---|
25 | |
---|
26 | % Get the SP format |
---|
27 | [F_struc,K] = lmi2sedumistruct(F,[]); |
---|
28 | |
---|
29 | % Convert the objective |
---|
30 | if isempty(h) |
---|
31 | c=zeros(nvars,1); |
---|
32 | else |
---|
33 | [n,m]=size(h); |
---|
34 | if ~((n==1) & (m==1)) |
---|
35 | error('Scalar expression to minimize please.'); |
---|
36 | else |
---|
37 | lmi_variables = getvariables(h); |
---|
38 | c = zeros(nvars,1); |
---|
39 | for i=1:length(lmi_variables) |
---|
40 | c(lmi_variables(i))=getbasematrix(h,lmi_variables(i)); |
---|
41 | end; |
---|
42 | end |
---|
43 | end |
---|
44 | |
---|
45 | % Which sdpvar variables are actually in the problem |
---|
46 | used_variables_LMI = find(any(F_struc(:,2:end),1)); |
---|
47 | used_variables_obj = find(any(c',1)); |
---|
48 | used_variables = uniquestripped([used_variables_LMI used_variables_obj]); |
---|
49 | |
---|
50 | % Check for unbounded variables |
---|
51 | unbounded_variables = setdiff(used_variables_obj,used_variables_LMI); |
---|
52 | if ~isempty(unbounded_variables) |
---|
53 | % Remove unbounded variable from problem |
---|
54 | used_variables = setdiff(used_variables,unbounded_variables); |
---|
55 | end |
---|
56 | |
---|
57 | % Pick out the necessary rows |
---|
58 | if length(used_variables)<nvars |
---|
59 | c = c(used_variables); |
---|
60 | F_struc = sparse(F_struc(:,[1 1+[used_variables]])); |
---|
61 | end |
---|
62 | |
---|
63 | if K.f>0 |
---|
64 | % Extract the inequalities |
---|
65 | A_equ = F_struc(1:K.f,2:end); |
---|
66 | b_equ = -F_struc(1:K.f,1); |
---|
67 | [Q,R] = qr(A_equ'); |
---|
68 | n = max(find(any(R'))); |
---|
69 | Q1 = Q(:,1:n); |
---|
70 | Q2 = Q(:,n+1:end); |
---|
71 | R = R(1:n,:); |
---|
72 | x_equ = Q1*(R'\b_equ); |
---|
73 | % Exit if no consistent solution exist |
---|
74 | if (norm(A_equ*x_equ-b_equ)>sqrt(eps)) |
---|
75 | error('Linear constraints inconsistent.'); |
---|
76 | return |
---|
77 | end |
---|
78 | % So we dont need these rows anymore |
---|
79 | F_struc = F_struc(K.f+1:end,:); |
---|
80 | K.f = 0; |
---|
81 | % OK, we found a new basis |
---|
82 | H = Q2; |
---|
83 | % objective in new basis |
---|
84 | c = H'*c; |
---|
85 | % LMI in new basis |
---|
86 | F_struc = [F_struc*[1;x_equ] F_struc(:,2:end)*H]; |
---|
87 | end |
---|
88 | |
---|
89 | % Is a filename supplied |
---|
90 | if nargin<3 |
---|
91 | [filename, pathname] = uiputfile('*.dat-s', 'Save SDPA sparse format file'); |
---|
92 | if isa(filename,'double') |
---|
93 | return % User canceled |
---|
94 | else |
---|
95 | % Did the user change the extension |
---|
96 | if isempty(findstr(filename,'.')) |
---|
97 | filename = [pathname filename '.dat-s']; |
---|
98 | else |
---|
99 | filename = [pathname filename]; |
---|
100 | end |
---|
101 | end |
---|
102 | else |
---|
103 | filename = varargin{3}; |
---|
104 | end |
---|
105 | |
---|
106 | % Save to file |
---|
107 | createsdplibfile(F_struc, K, c, filename); |
---|