def con2vert(A, b): """ Convert sets of constraints to a list of vertices (of the feasible region). If the shape is open, con2vert returns False for the closed property. """ # Python implementation of con2vert.m by Michael Kleder (July 2005), # available: http://www.mathworks.com/matlabcentral/fileexchange/7894 # -con2vert-constraints-to-vertices # Author: Michael Kelder (Original) # Andre Campher (Python implementation) c = linalg.lstsq(mat(A), mat(b))[0] btmp = mat(b)-mat(A)*c D = mat(A)/matlib.repmat(btmp, 1, A.shape[1]) fmatv = qhull(D, "Ft") #vertices on facets G = zeros((fmatv.shape[0], D.shape[1])) for ix in range(0, fmatv.shape[0]): F = D[fmatv[ix, :], :].squeeze() G[ix, :] = linalg.lstsq(F, ones((F.shape[0], 1)))[0].transpose() V = G + matlib.repmat(c.transpose(), G.shape[0], 1) ux = uniqm(V) eps = 1e-13 Av = dot(A, ux.T) bv = tile(b, (1, ux.shape[0])) closed = sciall(Av - bv <= eps) return ux, closed
def vert2con(V): """ Convert sets of vertices to a list of constraints (of the feasible region). Return A, b and s of the set; Ax < b (s is the sign-vector [to be used later]) vert2con always closes the shape and generates inequalities accordingly. """ # Dependencies: * qhull (libqhull5, qhull-bin) # * scipy k = qhull(V,"n") #convert to martrix with vertices # k is a (n+1)x(p) matrix in the form [A b] (from qhull doc: Ax < -b is # satisfied), thus; A = k[:, :-1] b = array([-k[:, -1]]).T s = -ones(b.shape) return A, s, b
def tryvol(A, b, cs): """ Try to determine volume of feasible region, otherwise impose box constraint. """ try: V = con2vert(A, b)[0] # get vertices for constraint set iteration except NameError: #catch unbound shapes #create large box around original shape fixA = r_[eye(cs.nd), -eye(cs.nd)] fixb = r_[[numpy.max(cs.vert[:, x]) for x in range(cs.vert.shape[1])], [-numpy.min(cs.vert[:, x]) for x in range(cs.vert.shape[1])]] fixb = array([fixb]).T fA = r_[fixA, A] fb = 2. * r_[fixb, b] # double the box size V = con2vert(fA, fb)[0] #return vol (normal or fixed) and vertices (normal or fixed) return qhull(V, "FS"), V
def vol(self): """Return 'volume' of feasible region.""" return qhull(self.vert,"FS")