|
| 1 | +function [V,nr] = con2vert(A,b) |
| 2 | +% CON2VERT - convert a convex set of constraint inequalities into the set |
| 3 | +% of vertices at the intersections of those inequalities;i.e., |
| 4 | +% solve the "vertex enumeration" problem. Additionally, |
| 5 | +% identify redundant entries in the list of inequalities. |
| 6 | +% |
| 7 | +% V = con2vert(A,b) |
| 8 | +% [V,nr] = con2vert(A,b) |
| 9 | +% |
| 10 | +% Converts the polytope (convex polygon, polyhedron, etc.) defined by the |
| 11 | +% system of inequalities A*x <= b into a list of vertices V. Each ROW |
| 12 | +% of V is a vertex. For n variables: |
| 13 | +% A = m x n matrix, where m >= n (m constraints, n variables) |
| 14 | +% b = m x 1 vector (m constraints) |
| 15 | +% V = p x n matrix (p vertices, n variables) |
| 16 | +% nr = list of the rows in A which are NOT redundant constraints |
| 17 | +% |
| 18 | +% NOTES: (1) This program employs a primal-dual polytope method. |
| 19 | +% (2) In dimensions higher than 2, redundant vertices can |
| 20 | +% appear using this method. This program detects redundancies |
| 21 | +% at up to 6 digits of precision, then returns the |
| 22 | +% unique vertices. |
| 23 | +% (3) Non-bounding constraints give erroneous results; therefore, |
| 24 | +% the program detects non-bounding constraints and returns |
| 25 | +% an error. You may wish to implement large "box" constraints |
| 26 | +% on your variables if you need to induce bounding. For example, |
| 27 | +% if x is a person's height in feet, the box constraint |
| 28 | +% -1 <= x <= 1000 would be a reasonable choice to induce |
| 29 | +% boundedness, since no possible solution for x would be |
| 30 | +% prohibited by the bounding box. |
| 31 | +% (4) This program requires that the feasible region have some |
| 32 | +% finite extent in all dimensions. For example, the feasible |
| 33 | +% region cannot be a line segment in 2-D space, or a plane |
| 34 | +% in 3-D space. |
| 35 | +% (5) At least two dimensions are required. |
| 36 | +% (6) See companion function VERT2CON. |
| 37 | +% (7) ver 1.0: initial version, June 2005 |
| 38 | +% (8) ver 1.1: enhanced redundancy checks, July 2005 |
| 39 | +% (9) Written by Michael Kleder |
| 40 | +% |
| 41 | +% EXAMPLES: |
| 42 | +% |
| 43 | +% % FIXED CONSTRAINTS: |
| 44 | +% A=[ 0 2; 2 0; 0.5 -0.5; -0.5 -0.5; -1 0]; |
| 45 | +% b=[4 4 0.5 -0.5 0]'; |
| 46 | +% figure('renderer','zbuffer') |
| 47 | +% hold on |
| 48 | +% [x,y]=ndgrid(-3:.01:5); |
| 49 | +% p=[x(:) y(:)]'; |
| 50 | +% p=(A*p <= repmat(b,[1 length(p)])); |
| 51 | +% p = double(all(p)); |
| 52 | +% p=reshape(p,size(x)); |
| 53 | +% h=pcolor(x,y,p); |
| 54 | +% set(h,'edgecolor','none') |
| 55 | +% set(h,'zdata',get(h,'zdata')-1) % keep in back |
| 56 | +% axis equal |
| 57 | +% V=con2vert(A,b); |
| 58 | +% plot(V(:,1),V(:,2),'y.') |
| 59 | +% |
| 60 | +% % RANDOM CONSTRAINTS: |
| 61 | +% A=rand(30,2)*2-1; |
| 62 | +% b=ones(30,1); |
| 63 | +% figure('renderer','zbuffer') |
| 64 | +% hold on |
| 65 | +% [x,y]=ndgrid(-3:.01:3); |
| 66 | +% p=[x(:) y(:)]'; |
| 67 | +% p=(A*p <= repmat(b,[1 length(p)])); |
| 68 | +% p = double(all(p)); |
| 69 | +% p=reshape(p,size(x)); |
| 70 | +% h=pcolor(x,y,p); |
| 71 | +% set(h,'edgecolor','none') |
| 72 | +% set(h,'zdata',get(h,'zdata')-1) % keep in back |
| 73 | +% axis equal |
| 74 | +% set(gca,'color','none') |
| 75 | +% V=con2vert(A,b); |
| 76 | +% plot(V(:,1),V(:,2),'y.') |
| 77 | +% |
| 78 | +% % HIGHER DIMENSIONS: |
| 79 | +% A=rand(15,5)*1000-500; |
| 80 | +% b=rand(15,1)*100; |
| 81 | +% V=con2vert(A,b) |
| 82 | +% |
| 83 | +% % NON-BOUNDING CONSTRAINTS (ERROR): |
| 84 | +% A=[0 1;1 0;1 1]; |
| 85 | +% b=[1 1 1]'; |
| 86 | +% figure('renderer','zbuffer') |
| 87 | +% hold on |
| 88 | +% [x,y]=ndgrid(-3:.01:3); |
| 89 | +% p=[x(:) y(:)]'; |
| 90 | +% p=(A*p <= repmat(b,[1 length(p)])); |
| 91 | +% p = double(all(p)); |
| 92 | +% p=reshape(p,size(x)); |
| 93 | +% h=pcolor(x,y,p); |
| 94 | +% set(h,'edgecolor','none') |
| 95 | +% set(h,'zdata',get(h,'zdata')-1) % keep in back |
| 96 | +% axis equal |
| 97 | +% set(gca,'color','none') |
| 98 | +% V=con2vert(A,b); % should return error |
| 99 | +% |
| 100 | +% % NON-BOUNDING CONSTRAINTS WITH BOUNDING BOX ADDED: |
| 101 | +% A=[0 1;1 0;1 1]; |
| 102 | +% b=[1 1 1]'; |
| 103 | +% A=[A;0 -1;0 1;-1 0;1 0]; |
| 104 | +% b=[b;4;1000;4;1000]; % bound variables within (-1,1000) |
| 105 | +% figure('renderer','zbuffer') |
| 106 | +% hold on |
| 107 | +% [x,y]=ndgrid(-3:.01:3); |
| 108 | +% p=[x(:) y(:)]'; |
| 109 | +% p=(A*p <= repmat(b,[1 length(p)])); |
| 110 | +% p = double(all(p)); |
| 111 | +% p=reshape(p,size(x)); |
| 112 | +% h=pcolor(x,y,p); |
| 113 | +% set(h,'edgecolor','none') |
| 114 | +% set(h,'zdata',get(h,'zdata')-1) % keep in back |
| 115 | +% axis equal |
| 116 | +% set(gca,'color','none') |
| 117 | +% V=con2vert(A,b); |
| 118 | +% plot(V(:,1),V(:,2),'y.','markersize',20) |
| 119 | +% |
| 120 | +% % JUST FOR FUN: |
| 121 | +% A=rand(80,3)*2-1; |
| 122 | +% n=sqrt(sum(A.^2,2)); |
| 123 | +% A=A./repmat(n,[1 size(A,2)]); |
| 124 | +% b=ones(80,1); |
| 125 | +% V=con2vert(A,b); |
| 126 | +% k=convhulln(V); |
| 127 | +% figure |
| 128 | +% hold on |
| 129 | +% for i=1:length(k) |
| 130 | +% patch(V(k(i,:),1),V(k(i,:),2),V(k(i,:),3),'w','edgecolor','none') |
| 131 | +% end |
| 132 | +% axis equal |
| 133 | +% axis vis3d |
| 134 | +% axis off |
| 135 | +% h=camlight(0,90); |
| 136 | +% h(2)=camlight(0,-17); |
| 137 | +% h(3)=camlight(107,-17); |
| 138 | +% h(4)=camlight(214,-17); |
| 139 | +% set(h(1),'color',[1 0 0]); |
| 140 | +% set(h(2),'color',[0 1 0]); |
| 141 | +% set(h(3),'color',[0 0 1]); |
| 142 | +% set(h(4),'color',[1 1 0]); |
| 143 | +% material metal |
| 144 | +% for x=0:5:720 |
| 145 | +% view(x,0) |
| 146 | +% drawnow |
| 147 | +% end |
| 148 | + |
| 149 | +c = A\b; |
| 150 | +if ~all(A*c < b); |
| 151 | + [c,f,ef] = fminsearch(@obj,c,'params',{A,b}); |
| 152 | + if ef ~= 1 |
| 153 | + error('Unable to locate a point within the interior of a feasible region.') |
| 154 | + end |
| 155 | +end |
| 156 | +b = b - A*c; |
| 157 | +D = A ./ repmat(b,[1 size(A,2)]); |
| 158 | +[k,v2] = convhulln([D;zeros(1,size(D,2))]); |
| 159 | +[k,v1] = convhulln(D); |
| 160 | +if v2 > v1 |
| 161 | + error('Non-bounding constraints detected. (Consider box constraints on variables.)') |
| 162 | +end |
| 163 | +nr = unique(k(:)); |
| 164 | +G = zeros(size(k,1),size(D,2)); |
| 165 | +for ix = 1:size(k,1) |
| 166 | + F = D(k(ix,:),:); |
| 167 | + G(ix,:)=F\ones(size(F,1),1); |
| 168 | +end |
| 169 | +V = G + repmat(c',[size(G,1),1]); |
| 170 | +[null,I]=unique(num2str(V,6),'rows'); |
| 171 | +V=V(I,:); |
| 172 | +return |
| 173 | +function d = obj(c,params) |
| 174 | +A=params{1}; |
| 175 | +b=params{2}; |
| 176 | +d = A*c-b; |
| 177 | +k=(d>=-1e-15); |
| 178 | +d(k)=d(k)+1; |
| 179 | +d = max([0;d]); |
| 180 | +return |
| 181 | + |
| 182 | + |
| 183 | + |
0 commit comments