-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlcon2vert.m
626 lines (397 loc) · 14.2 KB
/
lcon2vert.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
function [V,nr,nre]=lcon2vert(A,b,Aeq,beq,TOL,checkbounds)
%An extension of Michael Kleder's con2vert function, used for finding the
%vertices of a bounded polyhedron in R^n, given its representation as a set
%of linear constraints. This wrapper extends the capabilities of con2vert to
%also handle cases where the polyhedron is not solid in R^n, i.e., where the
%polyhedron is defined by both equality and inequality constraints.
%
%SYNTAX:
%
% [V,nr,nre]=lcon2vert(A,b,Aeq,beq,TOL)
%
%The rows of the N x n matrix V are a series of N vertices of the polyhedron
%in R^n, defined by the linear constraints
%
% A*x <= b
% Aeq*x = beq
%
%By default, Aeq=beq=[], implying no equality constraints. The output "nr"
%lists non-redundant inequality constraints, and "nre" lists non-redundant
%equality constraints.
%
%The optional TOL argument is a tolerance used for both rank-estimation and
%for testing feasibility of the equality constraints. Default=1e-10.
%The default can also be obtained by passing TOL=[];
%
%
%EXAMPLE:
%
%The 3D region defined by x+y+z=1, x>=0, y>=0, z>=0
%is described by the following constraint data.
%
%
% A =
%
% 0.4082 -0.8165 0.4082
% 0.4082 0.4082 -0.8165
% -0.8165 0.4082 0.4082
%
%
% b =
%
% 0.4082
% 0.4082
% 0.4082
%
%
% Aeq =
%
% 0.5774 0.5774 0.5774
%
%
% beq =
%
% 0.5774
%
%
% >> V=lcon2vert(A,b,Aeq,beq)
%
% V =
%
% 1.0000 0.0000 0.0000
% 0.0000 0.0000 1.0000
% -0.0000 1.0000 0.0000
%
%
%%initial argument parsing
nre=[];
nr=[];
if nargin<5 || isempty(TOL), TOL=1e-10; end
if nargin<6, checkbounds=true; end
switch nargin
case 0
error 'At least 1 input argument required'
case 1
b=[]; Aeq=[]; beq=[];
case 2
Aeq=[]; beq=[];
case 3
beq=[];
error 'Since argument Aeq specified, beq must also be specified'
end
b=b(:); beq=beq(:);
if xor(isempty(A), isempty(b))
error 'Since argument A specified, b must also be specified'
end
if xor(isempty(Aeq), isempty(beq))
error 'Since argument Aeq specified, beq must also be specified'
end
nn=max(size(A,2)*~isempty(A),size(Aeq,2)*~isempty(Aeq));
if ~isempty(A) && ~isempty(Aeq) && ( size(A,2)~=nn || size(Aeq,2)~=nn)
error 'A and Aeq must have the same number of columns if both non-empty'
end
inequalityConstrained=~isempty(A);
equalityConstrained=~isempty(Aeq);
[A,b]=rownormalize(A,b);
[Aeq,beq]=rownormalize(Aeq,beq);
if equalityConstrained && nargout>2
[discard,nre]=lindep([Aeq,beq].',TOL);
if ~isempty(nre) %reduce the equality constraints
Aeq=Aeq(nre,:);
beq=beq(nre);
else
equalityConstrained=false;
end
end
%%Find 1 solution to equality constraints within tolerance
if equalityConstrained
Neq=null(Aeq);
x0=pinv(Aeq)*beq;
if norm(Aeq*x0-beq)>TOL*norm(beq), %infeasible
nre=[]; nr=[]; %All constraints redundant for empty polytopes
V=[];
return;
elseif isempty(Neq)
V=x0(:).';
nre=(1:nn).'; %Equality constraints determine everything.
nr=[];%All inequality constraints are therefore redundant.
return
end
rkAeq= nn - size(Neq,2);
end
%%
if inequalityConstrained && equalityConstrained
AAA=A*Neq;
bbb=b-A*x0;
elseif inequalityConstrained
AAA=A;
bbb=b;
elseif equalityConstrained && ~inequalityConstrained
error('Non-bounding constraints detected. (Consider box constraints on variables.)')
end
nnn=size(AAA,2);
if nnn==1 %Special case
idxu=sign(AAA)==1;
idxl=sign(AAA)==-1;
idx0=sign(AAA)==0;
Q=bbb./AAA;
U=Q;
U(~idxu)=inf;
L=Q;
L(~idxl)=-inf;
[ub,uloc]=min(U);
[lb,lloc]=max(L);
if ~all(bbb(idx0)>=0) || ub<lb %infeasible
V=[]; nr=[]; nre=[];
return
elseif ~isfinite(ub) || ~isfinite(lb)
error('Non-bounding constraints detected. (Consider box constraints on variables.)')
end
Zt=[lb;ub];
if nargout>1
nr=unique([lloc,uloc]); nr=nr(:);
end
else
if nargout>1
[Zt,nr]=con2vert(AAA,bbb,TOL,checkbounds);
else
Zt=con2vert(AAA,bbb,TOL,checkbounds);
end
end
if equalityConstrained && ~isempty(Zt)
V=bsxfun(@plus,Zt*Neq.',x0(:).');
else
V=Zt;
end
if isempty(V),
nr=[]; nre=[];
end
function [V,nr] = con2vert(A,b,TOL,checkbounds)
% CON2VERT - convert a convex set of constraint inequalities into the set
% of vertices at the intersections of those inequalities;i.e.,
% solve the "vertex enumeration" problem. Additionally,
% identify redundant entries in the list of inequalities.
%
% V = con2vert(A,b)
% [V,nr] = con2vert(A,b)
%
% Converts the polytope (convex polygon, polyhedron, etc.) defined by the
% system of inequalities A*x <= b into a list of vertices V. Each ROW
% of V is a vertex. For n variables:
% A = m x n matrix, where m >= n (m constraints, n variables)
% b = m x 1 vector (m constraints)
% V = p x n matrix (p vertices, n variables)
% nr = list of the rows in A which are NOT redundant constraints
%
% NOTES: (1) This program employs a primal-dual polytope method.
% (2) In dimensions higher than 2, redundant vertices can
% appear using this method. This program detects redundancies
% at up to 6 digits of precision, then returns the
% unique vertices.
% (3) Non-bounding constraints give erroneous results; therefore,
% the program detects non-bounding constraints and returns
% an error. You may wish to implement large "box" constraints
% on your variables if you need to induce bounding. For example,
% if x is a person's height in feet, the box constraint
% -1 <= x <= 1000 would be a reasonable choice to induce
% boundedness, since no possible solution for x would be
% prohibited by the bounding box.
% (4) This program requires that the feasible region have some
% finite extent in all dimensions. For example, the feasible
% region cannot be a line segment in 2-D space, or a plane
% in 3-D space.
% (5) At least two dimensions are required.
% (6) See companion function VERT2CON.
% (7) ver 1.0: initial version, June 2005
% (8) ver 1.1: enhanced redundancy checks, July 2005
% (9) Written by Michael Kleder
%
%Modified by Matt Jacobson - March 30, 2011
%
%%%3/4/2012 Improved boundedness test - unfortunately slower than Michael Kleder's
if checkbounds
[aa,bb,aaeq,bbeq]=vert2lcon(A,TOL);
if any(bb<=0) || ~isempty(bbeq)
error('Non-bounding constraints detected. (Consider box constraints on variables.)')
end
clear aa bb aaeq bbeq
end
dim=size(A,2);
%%%Matt J initialization
if strictinpoly(b,TOL)
c=zeros(dim,1);
else
slackfun=@(c)b-A*c;
%Initializer0
c = pinv(A)*b; %02/17/2012 -replaced with pinv()
s=slackfun(c);
if ~approxinpoly(s,TOL) %Initializer1
c=Initializer1(TOL,A,b,c);
s=slackfun(c);
end
if ~approxinpoly(s,TOL) %Attempt refinement
%disp 'It is unusually difficult to find an interior point of your polytope. This may take some time... '
%disp ' '
c=Initializer2(TOL,A,b,c);
%[c,fval]=Initializer1(TOL,A,b,c,10000);
s=slackfun(c);
end
if ~approxinpoly(s,TOL)
%error('Unable to locate a point near the interior of the feasible region.')
V=[];
nr=[];
return
end
if ~strictinpoly(s,TOL) %Added 02/17/2012 to handle initializers too close to polytope surface
%disp 'Recursing...'
idx=( abs(s)<=max(s)*TOL );
Amod=A; bmod=b;
Amod(idx,:)=[];
bmod(idx)=[];
Aeq=A(idx,:); %pick the nearest face to c
beq=b(idx);
faceVertices=lcon2vert(Amod,bmod,Aeq,beq,TOL,1);
if isempty(faceVertices)
disp 'Something''s wrong. Couldn''t find face vertices. Possibly polyhedron is unbounded.'
keyboard
end
c=faceVertices(1,:).'; %Take any vertex - find local recession cone vector
s=slackfun(c);
idx=( abs(s)<=max(s)*TOL );
Asub=A(idx,:); bsub=b(idx,:);
[aa,bb,aaeq,bbeq]=vert2lcon(Asub);
aa=[aa;aaeq;-aaeq];
bb=[bb;bbeq;-bbeq];
clear aaeq bbeq
[bmin,idx]=min(bb);
if bmin>=-TOL
disp 'Something''s wrong. We should have found a recession vector (bb<0).'
keyboard
end
Aeq2=null(aa(idx,:)).';
beq2=Aeq2*c; %find intersection of polytope with line through facet centroid.
linetips = lcon2vert(A,b,Aeq2,beq2,TOL,1);
if size(linetips,1)<2
disp 'Failed to identify line segment through interior.'
disp 'Possibly {x: Aeq*x=beq} has weak intersection with interior({x: Ax<=b}).'
keyboard
end
lineCentroid=mean(linetips);%Relies on boundedness
clear aa bb
c=lineCentroid(:);
s=slackfun(c);
end
b = s;
end
%%%end Matt J initialization
D=bsxfun(@rdivide,A,b);
k = convhulln(D);
nr = unique(k(:));
G = zeros(size(k,1),dim);
ee=ones(size(k,2),1);
discard=false( 1, size(k,1) );
for ix = 1:size(k,1) %02/17/2012 - modified
F = D(k(ix,:),:);
if lindep(F,TOL)<dim;
discard(ix)=1;
continue;
end
G(ix,:)=F\ee;
end
G(discard,:)=[];
V = bsxfun(@plus, G, c.');
[discard,I]=unique( round(V*1e6),'rows');
V=V(I,:);
return
function [c,fval]=Initializer1(TOL, A,b,c,maxIter)
thresh=-10*max(eps(b));
if nargin>4
[c,fval]=fminsearch(@(x) max([thresh;A*x-b]), c,optimset('MaxIter',maxIter));
else
[c,fval]=fminsearch(@(x) max([thresh;A*x-b]), c);
end
return
function c=Initializer2(TOL,A,b,c)
%norm( (I-A*pinv(A))*(s-b) ) subj. to s>=0
maxIter=100000;
[mm,nn]=size(A);
Ap=pinv(A);
Aaug=speye(mm)-A*Ap;
Aaugt=Aaug.';
M=Aaugt*Aaug;
C=sum(abs(M),2);
C(C<=0)=min(C(C>0));
slack=b-A*c;
slack(slack<0)=0;
% relto=norm(b);
% relto =relto + (relto==0);
%
% relres=norm(A*c-b)/relto;
IterThresh=maxIter;
s=slack;
ii=0;
%for ii=1:maxIter
while ii<=2*maxIter %HARDCODE
ii=ii+1;
if ii>IterThresh,
%warning 'This is taking a lot of iterations'
IterThresh=IterThresh+maxIter;
end
s=s-Aaugt*(Aaug*(s-b))./C;
s(s<0)=0;
c=Ap*(b-s);
%slack=b-A*c;
%relres=norm(slack)/relto;
%if all(0<slack,1)||relres<1e-6||ii==maxIter, break; end
end
return
function [r,idx,Xsub]=lindep(X,tol)
%Extract a linearly independent set of columns of a given matrix X
%
% [r,idx,Xsub]=lindep(X)
%
%in:
%
% X: The given input matrix
% tol: A rank estimation tolerance. Default=1e-10
%
%out:
%
% r: rank estimate
% idx: Indices (into X) of linearly independent columns
% Xsub: Extracted linearly independent columns of X
if ~nnz(X) %X has no non-zeros and hence no independent columns
Xsub=[]; idx=[];
return
end
if nargin<2, tol=1e-10; end
[Q, R, E] = qr(X,0);
diagr = abs(diag(R));
%Rank estimation
r = find(diagr >= tol*diagr(1), 1, 'last'); %rank estimation
if nargout>1
idx=sort(E(1:r));
idx=idx(:);
end
if nargout>2
Xsub=X(:,idx);
end
function [A,b]=rownormalize(A,b)
%Modifies A,b data pair so that norm of rows of A is either 0 or 1
if isempty(A), return; end
normsA=sqrt(sum(A.^2,2));
idx=normsA>0;
A(idx,:)=bsxfun(@rdivide,A(idx,:),normsA(idx));
b(idx)=b(idx)./normsA(idx);
function tf=approxinpoly(s,TOL)
smax=max(s);
if smax<=0
tf=false; return
end
tf=all(s>=-smax*TOL);
function tf=strictinpoly(s,TOL)
smax=max(s);
if smax<=0
tf=false; return
end
tf=all(s>=smax*TOL);