[37] | 1 | function [C,A,b,blk] = sdpt3data(F,h) |
---|
| 2 | %SDPT3DATA Internal function to convert data to SDPT3 format |
---|
| 3 | |
---|
| 4 | % Author Johan Löfberg |
---|
| 5 | % $Id: sdpt3data.m,v 1.3 2004/11/24 09:13:05 johanl Exp $ |
---|
| 6 | |
---|
| 7 | if ~(isempty(F) | isa(F,'lmi')) |
---|
| 8 | help lmi |
---|
| 9 | error('First argument (F) should be an lmi object. See help text above'); |
---|
| 10 | end |
---|
| 11 | |
---|
| 12 | if ~(isempty(h) | isa(h,'sdpvar')) |
---|
| 13 | help solvesdp |
---|
| 14 | error('Third argument (the objective function h) should be an sdpvar object (or empty). See help text above'); |
---|
| 15 | end |
---|
| 16 | |
---|
| 17 | [ProblemString,real_data] = catsdp(F); |
---|
| 18 | |
---|
| 19 | % This one is used a lot |
---|
| 20 | nvars = sdpvar('nvars'); |
---|
| 21 | |
---|
| 22 | % Convert the objective |
---|
| 23 | onlyfeasible = 0; |
---|
| 24 | if isempty(h) |
---|
| 25 | c=zeros(nvars,1); |
---|
| 26 | else |
---|
| 27 | [n,m]=size(h); |
---|
| 28 | if ~((n==1) & (m==1)) |
---|
| 29 | error('Scalar expression to minimize please.'); |
---|
| 30 | else |
---|
| 31 | lmi_variables = getvariables(h); |
---|
| 32 | c = zeros(nvars,1); |
---|
| 33 | for i=1:length(lmi_variables) |
---|
| 34 | c(lmi_variables(i))=getbasematrix(h,lmi_variables(i)); |
---|
| 35 | end; |
---|
| 36 | end |
---|
| 37 | end |
---|
| 38 | |
---|
| 39 | [F_struc,K] = lmi2sedumistruct(F); |
---|
| 40 | |
---|
| 41 | % Which sdpvar variables are actually in the problem |
---|
| 42 | used_variables_LMI = find(any(F_struc(:,2:end),1)); |
---|
| 43 | used_variables_obj = find(any(c',1)); |
---|
| 44 | used_variables = uniquestripped([used_variables_LMI used_variables_obj]); |
---|
| 45 | |
---|
| 46 | % Check for unbounded variables |
---|
| 47 | unbounded_variables = setdiff(used_variables_obj,used_variables_LMI); |
---|
| 48 | if ~isempty(unbounded_variables) |
---|
| 49 | % Remove unbounded variable from problem |
---|
| 50 | used_variables = setdiff(used_variables,unbounded_variables); |
---|
| 51 | end |
---|
| 52 | |
---|
| 53 | % Pick out the necessary rows |
---|
| 54 | if length(used_variables)<nvars |
---|
| 55 | c = c(used_variables); |
---|
| 56 | F_struc = sparse(F_struc(:,[1 1+[used_variables]])); |
---|
| 57 | end |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | if (K.f>0) |
---|
| 61 | % Extract the inequalities |
---|
| 62 | A_equ = F_struc(1:K.f,2:end); |
---|
| 63 | b_equ = -F_struc(1:K.f,1); |
---|
| 64 | |
---|
| 65 | % Find feasible (turn off annoying warning on PC) |
---|
| 66 | % Using method from Nocedal-Wright book |
---|
| 67 | showprogress('Solving equalities',options.ShowProgress); |
---|
| 68 | [Q,R] = qr(A_equ'); |
---|
| 69 | n = size(R,2); |
---|
| 70 | Q1 = Q(:,1:n); |
---|
| 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 | % We dont need the rows for equalities anymore |
---|
| 79 | F_struc = F_struc(K.f+1:end,:); |
---|
| 80 | K.f = 0; |
---|
| 81 | |
---|
| 82 | % We found a new basis |
---|
| 83 | H = Q(:,n+1:end); % New basis |
---|
| 84 | |
---|
| 85 | % objective in new basis |
---|
| 86 | c = H'*c; |
---|
| 87 | % LMI in new basis |
---|
| 88 | F_struc = [F_struc*[1;x_equ] F_struc(:,2:end)*H]; |
---|
| 89 | else |
---|
| 90 | % For simpliciy we introduce a dummy coordinate change |
---|
| 91 | x_equ = 0; |
---|
| 92 | H = 1; |
---|
| 93 | end |
---|
| 94 | |
---|
| 95 | [C,A,b,blk] = sdpt3struct2sdpt3block(F_struc,c,K); |
---|
| 96 | A = svec(blk,A,ones(size(blk,1),1)); |
---|