source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/solvers/callpenbmim.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 8.3 KB
Line 
1function output = callpenbmi(interfacedata);
2
3% Author Johan Löfberg
4% $Id: callpenbmim.m,v 1.21 2006/08/24 17:12:27 joloef Exp $
5
6if any(interfacedata.variabletype > 2)
7    % Polynomial problem, YALMIP has to bilienarize
8    output = callpenbmi_with_bilinearization(interfacedata);
9else
10    % Old standard code
11    output = callpenbmi_without_bilinearization(interfacedata);
12end
13
14function output = callpenbmi_without_bilinearization(interfacedata);
15
16% Retrieve needed data
17clear penbmim
18
19options = interfacedata.options;
20F_struc = interfacedata.F_struc;
21c       = interfacedata.c;
22Q       = interfacedata.Q;
23K       = interfacedata.K;
24x0      = interfacedata.x0;
25monomtable = interfacedata.monomtable;
26ub      = interfacedata.ub;
27lb      = interfacedata.lb;
28
29% Linear before bilinearize
30temp = sum(monomtable,2)>1;
31tempnonlinearindicies = find(temp);
32templinearindicies = find(~temp);
33
34% Any stupid constant>0 constraints
35% FIX : Recover duals afterwards
36% Better fix : Do this outside
37zrow = [];
38if K.l >0
39    zrow = find(any(F_struc(1:K.l+K.f,:),2)==0);
40    if ~isempty(zrow)
41        K.l = K.l - nnz(zrow>K.f);
42        K.f = K.f - nnz(zrow<=K.f);       
43        F_struc(zrow,:) = [];
44    end
45end
46
47% Bounded variables converted to constraints
48if ~isempty(ub)
49    [F_struc,K] = addbounds(F_struc,K,ub,lb);
50end
51
52% This one only occurs if called from bmibnb
53if K.f>0
54    F_struc = [-F_struc(1:K.f,:);F_struc];
55    F_struc(1:K.f,1) = F_struc(1:K.f,1)+sqrt(eps);
56    K.l = K.l + 2*K.f;
57    K.f = 0;
58end
59
60if isempty(monomtable)
61    monomtable = eye(length(c));
62end
63
64temp = sum(monomtable,2)>1;
65nonlinearindicies = find(temp);
66linearindicies = find(~temp);
67c0 = c;
68c  = c(linearindicies);
69Q = Q(linearindicies,linearindicies);
70nonlinear_scalars = [];
71
72% Any non-linear scalar inequalities?
73% Move these to the BMI part
74if K.l>0
75    nonlinear_scalars = find(any(full(F_struc(1:K.l,[nonlinearindicies(:)'+1])),2));
76    if ~isempty(nonlinear_scalars)
77        Kold = K;
78        % SETDIFF DONE FASTER
79         aa = 1:K.l;
80         bb = nonlinear_scalars;
81         tf = ~(ismembc(aa,bb));
82         cc = aa(tf);
83         cc = unique(cc);
84         linear_scalars = cc;
85%        linear_scalars = setdiff1(1:K.l,nonlinear_scalars);
86        F_struc = [F_struc(linear_scalars,:);F_struc(nonlinear_scalars,:);F_struc(K.l+1:end,:)];     
87        K.l = K.l-length(nonlinear_scalars);
88        if (length(K.s)==1) & (K.s==0)
89            K.s    = [repmat(1,1,length(nonlinear_scalars))];
90            K.rank = repmat(1,1,length(nonlinear_scalars));
91        else
92            K.s    = [repmat(1,1,length(nonlinear_scalars)) K.s];
93            K.rank = [repmat(1,1,length(nonlinear_scalars)) K.rank];
94        end
95    end
96end
97
98if ~isempty(F_struc)
99    pen = sedumi2pen(F_struc(:,[1 linearindicies(:)'+1]),K,c,x0);
100else
101    pen = sedumi2pen([],K,c,x0);
102end
103
104if ~isempty(nonlinearindicies)
105    bmi = sedumi2pen(F_struc(:,[nonlinearindicies(:)'+1]),K,[],[]);
106    pen.ki_dim = bmi.ai_dim;
107    % Nonlinear index
108    pen.ki_dim = bmi.ai_dim;
109    pen.ki_row = bmi.ai_row;
110    pen.ki_col = bmi.ai_col;
111    pen.ki_nzs = bmi.ai_nzs;
112    pen.ki_val = bmi.ai_val;
113    if 0
114        for i = 1:length(bmi.ai_idx)
115            nl = nonlinearindicies(1+bmi.ai_idx(i));
116            v = find(monomtable(nl,:));
117            if length(v)==1
118                v(2)=v(1);
119            end
120            pen.ki_idx(i)=find(linearindicies == v(1));
121            pen.kj_idx(i)=find(linearindicies == v(2));
122        end
123    else
124        top = 1;
125        [ii,jj,kk] = find(monomtable(nonlinearindicies(1+bmi.ai_idx),:)');
126        pen.ki_idx = zeros(1,length(bmi.ai_idx));
127        pen.kj_idx = zeros(1,length(bmi.ai_idx));
128        for i = 1:length(bmi.ai_idx)
129            if kk(top)==2
130                v(1) = ii(top);
131                v(2) = ii(top);
132                top = top + 1;
133            else
134                v(1) = ii(top);
135                v(2) = ii(top+1);
136                top = top + 2;
137            end
138            % FIX : precompute this map
139            pen.ki_idx(i)=find(linearindicies == v(1));
140            pen.kj_idx(i)=find(linearindicies == v(2));
141        end
142    end
143
144else
145    pen.ki_dim = 0*pen.ai_dim;
146    pen.ki_row = 0;
147    pen.ki_col = 0;
148    pen.ki_nzs = 0;
149    pen.ki_idx = 0;
150    pen.kj_idx = 0;
151    pen.kj_val = 0;   
152end
153
154if nnz(Q)>0
155    [row,col,vals] = find(triu(Q));
156    pen.q_nzs = length(row);
157    pen.q_val = vals';
158    pen.q_col = col'-1;
159    pen.q_row = row'-1;
160else
161    pen.q_nzs = 0;
162    pen.q_val = 0;
163    pen.q_col = 0;
164    pen.q_row = 0;   
165end
166
167ops = struct2cell(options.penbmi);ops = [ops{1:end}];
168pen.ioptions = ops(1:12);
169pen.foptions = ops(13:end);
170pen.ioptions(4) = max(0,min(3,options.verbose+1));
171if pen.ioptions(4)==1
172    pen.ioptions(4)=0;
173end
174
175% ****************************************
176% UNCOMMENT THIS FOR PENBMI version 1
177% ****************************************
178%pen.ioptions = pen.ioptions(1:8);
179%pen.foptions = pen.foptions(1:8);
180
181if ~isempty(x0)   
182    pen.x0(isnan(pen.x0)) = 0;
183    pen.x0 = x0(linearindicies);   
184    pen.x0 = pen.x0(:)';
185end
186
187if options.savedebug
188    save penbmimdebug pen
189end
190
191if options.showprogress;showprogress(['Calling ' interfacedata.solver.tag],options.showprogress);end
192
193solvertime = clock;
194[f,xout,u,iflag,niter,feas] = penbmim(pen);
195solvertime = etime(clock,solvertime);
196
197if options.saveduals & isempty(zrow)
198   
199    % Get dual variable
200    % First, get the nonlinear scalars treated as BMIs
201    if ~isempty(nonlinear_scalars)
202        n_orig_scalars = length(nonlinear_scalars)+K.l;
203        linear_scalars = setdiff(1:n_orig_scalars,nonlinear_scalars);
204        u_nonlinear=u(K.l+1:K.l+length(nonlinear_scalars));
205        u(K.l+1:K.l+length(nonlinear_scalars))=[];
206        u_linear = u(1:K.l);
207        u_scalar = zeros(1,n_orig_scalars);
208        u_scalar(linear_scalars)=u_linear;
209        u_scalar(nonlinear_scalars)=u_nonlinear;
210        u = [u_scalar u(1+K.l:end)];
211        K = Kold;
212    end         
213   
214    u = u(:);
215    D_struc = u(1:1:K.l);
216    if length(K.s)>0
217        if K.s(1)>0
218            pos = K.l+1;
219            for i = 1:length(K.s)
220                temp = zeros(K.s(i),K.s(i));
221                vecZ = u(pos:pos+0.5*K.s(i)*(K.s(i)+1)-1);
222                top = 1;
223                for j = 1:K.s(i)
224                    len = K.s(i)-j+1;
225                    temp(j:end,j)=vecZ(top:top+len-1);top=top+len;
226                end
227                temp = (temp+temp');j = find(speye(K.s(i)));temp(j)=temp(j)/2;
228                D_struc = [D_struc;temp(:)];
229                pos = pos + (K.s(i)+1)*K.s(i)/2;
230            end
231        end
232    end
233else
234    D_struc = [];
235end
236
237%Recover solution
238if isempty(nonlinearindicies)
239    x = xout(:);
240else
241    x = zeros(length(interfacedata.c),1);   
242    for i = 1:length(templinearindicies)
243        x(templinearindicies(i)) = xout(i);
244    end
245end
246
247problem = 0;
248switch iflag
249    case 0
250        problem = 0; % OK
251    case {1,3}
252        problem = 4;
253    case 2
254        problem = 1; % INFEASIBLE
255    case 4
256        problem = 3; % Numerics
257    case 5
258        problem = 7;
259    case {6,7}
260        problem = 11;
261    otherwise
262        problem = -1;
263end
264infostr = yalmiperror(problem,interfacedata.solver.tag);       
265
266if options.savesolveroutput   
267    solveroutput.f = f;
268    solveroutput.xout = xout;
269    solveroutput.u = u;
270    solveroutput.iflag = iflag;
271    solveroutput.niter = niter;
272    solveroutput.feas = feas;
273else
274    solveroutput = [];
275end
276
277if options.savesolverinput
278    solverinput.pen = pen;
279else
280    solverinput = [];
281end
282
283% Standard interface
284output.Primal      = x(:);
285output.Dual        = D_struc;
286output.Slack       = [];
287output.problem     = problem;
288output.infostr     = infostr;
289output.solverinput = solverinput;
290output.solveroutput= solveroutput;
291output.solvertime  = solvertime;
292
293function output = callpenbmi_with_bilinearization(interfacedata);
294
295% Bilinearize
296[p,changed] = bilinearize(interfacedata);
297
298% Convert bilinearizing equalities to inequalities
299if p.K.f>0
300    p.F_struc = [-p.F_struc(1:p.K.f,:);p.F_struc];
301    p.F_struc(1:p.K.f,1) = p.F_struc(1:p.K.f,1)+sqrt(eps);
302    p.K.l = p.K.l + 2*p.K.f;
303    p.K.f = 0;
304end
305
306% Solve bilinearized problem
307 output = callpenbmi_without_bilinearization(p);
308 
309 % Get our original variables & duals
310 output.Primal = output.Primal(1:length(interfacedata.c));
311 if ~isempty(output.Dual)
312     n_equ = p.K.f - interfacedata.K.f;
313     % First 2*n_eq are the duals for the new inequalities
314     output.Dual = output.Dual(1+2*n_equ:end);
315 end
316 
317 
318 
319
320
Note: See TracBrowser for help on using the repository browser.