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 objfn2(Ab, *args): """Volume objective function for SLSQP.""" initcs = args[0] A, b = splitAb(Ab, initcs.nd) cl = con2vert(A, b)[1] if cl: closed = 1 else: closed = -1 vol = tryvol(A, b, initcs)[0] return closed * -vol
def objfn(Ab, *args): """Volume objective function for SLSQP.""" initcs = args[0] A = r_[eye(initcs.nd), -eye(initcs.nd)] b = array([Ab]).T cl = con2vert(A, b)[1] if cl: closed = 1 else: closed = -1 vol = tryvol(A, b, initcs)[0] return closed * -vol
def intersect(self, conset2): """Determine intersection between current constraint set and another""" def remredcons(A, b, verts): """Reduce a constraint set by removing unnecessary constraints.""" eps = 10e-9 #1 Co-planar constraints; # Remove as not to affect 3rd check Ab = c_[A, b] Abnorms = ones((Ab.shape[0], 1)) for i in range(Ab.shape[0]): Abnorms[i] = linalg.norm(Ab[i, :]) Abn = Ab/Abnorms Abkeep = ones((0, Ab.shape[1])) Abtest = ones((0, Ab.shape[1])) for r1 in range(Abn.shape[0]): noocc = ones((1, 0)) for r2 in range(Abn.shape[0]): #print abs(Abn[r1, :] - Abn[r2, :]) if numpy.all(abs(Abn[r1, :] - Abn[r2, :]) < eps): noocc = c_[noocc, r2] if noocc.size == 1: Abtest = vstack([Abtest, Ab[r1, :]]) else: Abkeep = vstack([Abkeep, Ab[r1, :]]) if Abkeep.shape[0] > 1: Abkeep = uniqm(Abkeep, eps) #2 Vert subset satisfying; no action needed (redundancy uncertain) #3 All vert satisfying constraints; A, b = splitAb(array(Abtest).ravel(), verts.shape[1]) keepA = ones((0, A.shape[1])) keepb = ones((0, 1)) bt = tile(b, (1, verts.shape[0])) k = mat(A)*mat(verts.T) - bt kk = sum(k > eps, axis=1) for i in range(len(kk)): if kk[i] != 0: keepA = vstack([keepA, A[i, :]]) keepb = vstack([keepb, b[i, :]]) outAb = vstack([c_[keepA, keepb], Abkeep]) return splitAb(outAb.ravel(), verts.shape[1]) #Combine constraints and vertices combA = vstack((self.A, conset2.A)) combb = vstack((self.b, conset2.b)) combv = vstack((self.vert, conset2.vert)) #Remove redundant constraints ncombA, ncombb = remredcons(combA, combb, combv) #Calc and return intersection intcombvert = con2vert(combA, combb)[0] return intcombvert
def __init__(self, *inargs): if len(inargs) == 1: self.vert = inargs[0] self.A, self.s, self.b = vert2con(self.vert) self.closed = True elif len(inargs) == 3: if all(sign(inargs[1]) == -1): # ensure an all -1 sign vector self.A, self.s, self.b = inargs else: self.A, self.s, self.b = mat2ab(c_[inargs]) self.vert, self.closed = con2vert(self.A, self.b) else: exit(1) # TODO: Raise exception self.nd = self.A.shape[1] self.cscent = sum(self.vert, axis=0)/len(self.vert)
def objfn2(Ab, *args): """Volume objective function for fmin (Simplex).""" initcs = args[0] A = r_[eye(initcs.nd), -eye(initcs.nd)] b = array([Ab]).T vol, V = tryvol(A, b, initcs) Pv = 200. #Penalties # points outside of init space iterset = ConSet(V) outnorm = linalg.norm(iterset.allinside(initcs)[1]) # open shape cl = con2vert(A, b)[1] if cl: closed = 1 else: closed = -1 vol = tryvol(A, b, initcs)[0] return (-vol*closed) + Pv*(outnorm**3)
def objfn(Ab, *args): """Volume objective function for fmin (Simplex).""" initcs = args[0] A, b = splitAb(Ab, initcs.nd) vol, V = tryvol(A, b, initcs) Pv = 200. Pn = 100. #Penalties # large b norm bnorm = abs(linalg.norm(b) - 1) # points outside of init space iterset = ConSet(V) outnorm = linalg.norm(iterset.allinside(initcs)[1]) #outnorm = iterset.allinside(initcs)[2] # open shape cl = con2vert(A, b)[1] if cl: closed = 1 else: closed = -1 vol = tryvol(A, b, initcs)[0] return (-vol*closed) + Pn*(bnorm**initcs.nd) + Pv*(outnorm**(initcs.nd+3))