def test(self): problem = CyCoinMpsIO() problem.readMps(os.path.join(currentFilePath, '../input/hs268.qps')) self.assertEqual(problem.nVariables, 5) self.assertEqual(problem.nConstraints, 5) self.assertTrue( [chr(s) for s in problem.constraintSigns] == problem.nConstraints * ['G']) c = problem.matrixByRow self.assertTrue((abs(c.elements - np.array([ -1., -1., -1., -1., -1., 10., 10., -3., 5., 4., -8., 1., -2., -5., 3., 8., -1., 2., 5., -3., -4., -2., 3., -5., 1. ])) <= 10**-8).all()) self.assertTrue((abs(c.indices - np.array([ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 ])) <= 10**-8).all()) self.assertTrue((abs(c.vectorStarts - np.array([0, 5, 10, 15, 20, 25])) <= 10**-8).all()) self.assertTrue( (abs(problem.rightHandSide - np.array([-5., 20., -40., 11., -30.])) <= 10**-8).all()) H = problem.Hessian.todense() self.assertTrue( (abs(H - np.matrix([[20394., -24908., -2026., 3896., 658.], [-24908., 41818., -3466., -9828., -372.], [-2026., -3466., 3510., 2178., -348.], [3896., -9828., 2178., 3030., -44.], [658., -372., -348., -44., 54.]])) <= 10** -8).all())
def readQPS(inputFilename): problem = CyCoinMpsIO() problem.readMps(inputFilename) n = problem.nVariables m = problem.nConstraints signs = problem.constraintSigns iEq = [i for i in range(len(signs)) if chr(signs[i]) == 'E'] numberOfEqualities = len(iEq) iInEq = [i for i in range(len(signs)) if i not in iEq] numberOfInequalities = len(iInEq) c = problem.matrixByRow el = c.elements col = c.indices start = c.vectorStarts coefs = csr_matrixPlus((el, col, start), shape=(m, n)) # an invalid value for initialization # This wont make any problems because we always # check number of equalties and # inequalities first, before accessing them A = C = b = c_up = c_low = 0 rhs = problem.rightHandSide if numberOfEqualities: A = csr_matrixPlus(coefs[iEq]) b = rhs[iEq] if numberOfInequalities: C = csr_matrixPlus(coefs[iInEq]) c_up = problem.constraintUpper[iInEq] c_low = problem.constraintLower[iInEq] Hessian = problem.Hessian x_low = problem.variableLower x_up = problem.variableUpper c = problem.objCoefficients return (Hessian, c, A, b, C, c_low, c_up, x_low, x_up, n, len(iEq), len(iInEq), problem.objectiveOffset)
def test(self): problem = CyCoinMpsIO() problem.readMps(os.path.join(currentFilePath, '../input/hs268.qps')) self.assertEqual(problem.nVariables, 5) self.assertEqual(problem.nConstraints, 5) self.assertTrue([chr(s) for s in problem.constraintSigns] == problem.nConstraints * ['G']) c = problem.matrixByRow self.assertTrue((abs(c.elements - np.array([-1., -1., -1., -1., -1., 10., 10., -3., 5., 4., -8., 1., -2., -5., 3., 8., -1., 2., 5., -3., -4., -2., 3., -5., 1.])) <= 10 ** -8).all()) self.assertTrue((abs(c.indices - np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])) <= 10 ** -8).all()) self.assertTrue((abs(c.vectorStarts - np.array([0, 5, 10, 15, 20, 25])) <= 10 ** -8).all()) self.assertTrue((abs(problem.rightHandSide - np.array([-5., 20., -40., 11., -30.])) <= 10 ** -8).all()) H = problem.Hessian.todense() self.assertTrue((abs(H - np.matrix([[20394., -24908., -2026., 3896., 658.], [-24908., 41818., -3466., -9828., -372.], [-2026., -3466., 3510., 2178., -348.], [3896., -9828., 2178., 3030., -44.], [658., -372., -348., -44., 54.]])) <= 10 ** -8).all())