def QPModel(self, addW=False): A = self.A c = self.c s = CyClpSimplex() x = s.addVariable('x', self.nCols) if addW: w = s.addVariable('w', self.nCols) s += A * x >= 1 n = self.nCols if not addW: s += 0 <= x <= 1 else: s += x + w == 1 s += 0 <= w <= 1 ## s += -1 <= x <= 1 s.objective = c * x if addW: G = sparse.lil_matrix((2*n, 2*n)) for i in xrange(n/2, n): #xrange(n-1): G[i, i] = 1 G[2*n-1, 2*n-1] = 10**-10 else: G = sparse.lil_matrix((n, n)) for i in xrange(n/2, n): #xrange(n-1): G[i, i] = 1 s.Hessian = G return s
def generateQP(self): m = self.m n = self.n s = CyClpSimplex() iNonZero = set(random.randint(n, size=self.nnzPerCol)) iZero = [i for i in range(n) if i not in iNonZero] x_star = np.matrix(np.zeros((n, 1))) z_star = np.matrix(np.zeros((n, 1))) for i in iNonZero: x_star[i, 0] = 1 for i in iZero: z_star[i, 0] = 0 if random.randint(2) else random.random() G = getG(n) A = getA(m, n, self.nnzPerCol) y_star = np.matrix(random.random((m, 1))) c = -(G * x_star - A.T * y_star - z_star) obj = 0.5 * ((x_star.T * G) * x_star) + c.T * x_star print(obj) c = CyLPArray((c.T)[0]) b = CyLPArray(((A * x_star).T)[0]) b = np.squeeze(np.asarray(b)) x = s.addVariable('x', n) s += A * x == b s += x >= 0 c = CyLPArray(c) s.objective = c * x s.Hessian = G self.model = s return s