Esempio n. 1
0
    def setUp(self):

        row = np.array([0, 1, 4, 1, 2, 7, 2, 3, 5, 3, 4, 5, 4, 7, 5, 6, 6, 7])
        col = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7])
        data = np.array([
            27, 5, 12, 56, 66, 34, 94, 31, 41, 7, 98, 72, 24, 33, 78, 47, 98,
            41
        ])
        m = COOSymMatrix((data, (row, col)), shape=(8, 8))

        self.block00 = m

        row = np.array([0, 3, 1, 0])
        col = np.array([0, 3, 1, 2])
        data = np.array([4, 5, 7, 9])
        m = COOMatrix((data, (row, col)), shape=(4, 8))

        self.block10 = m

        row = np.array([0, 1, 2, 3])
        col = np.array([0, 1, 2, 3])
        data = np.array([1, 1, 1, 1])
        m = COOSymMatrix((data, (row, col)), shape=(4, 4))

        self.block11 = m

        bm = BlockSymMatrix(2)
        bm.name = 'basic_matrix'
        bm[0, 0] = self.block00
        bm[1, 0] = self.block10
        bm[1, 1] = self.block11
        self.basic_m = bm
Esempio n. 2
0
    def setUp(self):
        row = np.array([0, 3, 1, 2, 3, 0])
        col = np.array([0, 0, 1, 2, 3, 3])
        data = np.array([2., 1., 3., 4., 5., 1.])
        m = COOMatrix((data, (row, col)), shape=(4, 4))
        m.name = 'basic_matrix'
        self.full_m = m

        row = np.array([0, 3, 1, 2, 3])
        col = np.array([0, 0, 1, 2, 3])
        data = np.array([2., 1., 3., 4., 5.])
        m = CSCSymMatrix((data, (row, col)), shape=(4, 4))
        m.name = 'basic_sym_matrix'
        self.basic_m = m

        row = np.array([0, 0, 1, 2, 3])
        col = np.array([0, 3, 1, 2, 3])
        data = np.array([2., 1., 3., 4., 5.])
        m = COOSymMatrix((data, (col, row)), shape=(4, 4))
        m.name = 'basic_sym_matrix'
        self.transposed = m

        row = np.array(
            [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5])
        col = np.array(
            [0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5])
        data = np.array([
            36., 17., 33., 19., 18., 43., 12., 11., 13., 18., 8., 7., 8., 6.,
            9., 15., 14., 16., 11., 8., 29.
        ])
        m = CSCSymMatrix((data, (row, col)), shape=(6, 6))
        m.name = 'G'
        self.g_matrix = m
Esempio n. 3
0
    def test_hessian(self):

        nz = len(self.complicated_vars_ids)
        Hi = BlockSymMatrix(2)
        Hi[0, 0] = COOSymMatrix(self.G)
        Hi[1, 1] = EmptyMatrix(
            nz, nz)  # this is because of the way the test problem was setup

        Hi = Hi.todense()
        x = self.nlp.create_vector_x()
        y = self.nlp.create_vector_y()
        H = self.nlp.hessian_lag(x, y)
        for i in range(self.n_scenarios):
            self.assertTrue(np.allclose(H[i, i].todense(), Hi))
        self.assertTrue(
            np.allclose(H[self.n_scenarios, self.n_scenarios].todense(),
                        EmptyMatrix(nz, nz).todense()))
Esempio n. 4
0
    def test_sub_sparse(self):
        m = self.basic_m
        mm = m - m
        mm2 = np.zeros(m.shape, dtype=np.double)
        self.assertIsInstance(mm, CSRSymMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             mm2.flatten().tolist())

        row = np.array([0, 3, 2])
        col = np.array([0, 0, 2])
        data = np.array([2., 1., 4.])
        m2 = COOSymMatrix((data, (row, col)), shape=(4, 4))

        test_m = np.array([[0., 0., 0., 0.], [0., 3., 0., 0.],
                           [0., 0., 0., 0.], [0., 0., 0., 5.]])

        mm = m - m2
        self.assertIsInstance(mm, CSRSymMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())

        row = np.array([0, 3, 1, 0, 2])
        col = np.array([0, 3, 1, 2, 1])
        data = np.array([4., 5., 7., 9., 6.])
        m2 = COOMatrix((data, (row, col)), shape=(4, 4))

        mm = m - m2
        test_m = np.array([[-2., 0., -9., 1.], [0., -4., 0., 0.],
                           [0., -6., 4., 0.], [1., 0., 0., 0]])
        self.assertIsInstance(mm, CSCMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())

        test_m = np.array([[2., 0., 9., -1.], [0., 4., 0., 0.],
                           [0., 6., -4., 0.], [-1., 0., 0., 0]])

        mm = m2 - m
        self.assertIsInstance(mm, CSRMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())
Esempio n. 5
0
    def test_add_sparse(self):
        m = self.basic_m
        mm = m + m
        test_m = np.array([[2., 0., 0., 1.], [0., 3., 0., 0.],
                           [0., 0., 4., 0.], [1., 0., 0., 5.]])
        mm2 = test_m * 2
        self.assertIsInstance(mm, CSRSymMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             mm2.flatten().tolist())

        row = np.array([0, 3, 2])
        col = np.array([0, 0, 2])
        data = np.array([2., 1., 4.])
        m2 = COOSymMatrix((data, (row, col)), shape=(4, 4))

        test_m = np.array([[4., 0., 0., 2.], [0., 3., 0., 0.],
                           [0., 0., 8., 0.], [2., 0., 0., 5.]])

        mm = m + m2
        self.assertIsInstance(mm, CSRSymMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())

        row = np.array([0, 3, 1, 0, 2])
        col = np.array([0, 3, 1, 2, 1])
        data = np.array([4., 5., 7., 9., 6.])
        m2 = COOMatrix((data, (row, col)), shape=(4, 4))

        mm = m + m2

        test_m = np.array([[6., 0., 9., 1.], [0., 10., 0., 0.],
                           [0., 6., 4., 0.], [1., 0., 0., 10.]])
        self.assertIsInstance(mm, CSCMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())

        mm = m2 + m
        self.assertIsInstance(mm, CSRMatrix)
        self.assertListEqual(mm.toarray().flatten().tolist(),
                             test_m.flatten().tolist())
Esempio n. 6
0
def _convert_matrix_to_symmetric(mat, check_symmetry=True):

    if not isinstance(mat, SparseBase):
        raise RuntimeError("Operation only supported for pynumero matrices")

    if mat.is_symmetric:
        return mat

    if check_symmetry and not _is_symmetric_numerically(mat):
        err_msg = "Cannot convert matrix because it has no symmetry"
        raise RuntimeError(err_msg)

    from pyomo.contrib.pynumero.sparse import (COOSymMatrix, CSCMatrix,
                                               CSCSymMatrix, CSRMatrix,
                                               CSRSymMatrix)

    l = tril(mat)
    if isinstance(mat, CSCMatrix):
        return CSCSymMatrix(l)
    elif isinstance(mat, CSRMatrix):
        return CSRSymMatrix(l)
    else:
        return COOSymMatrix(l)
Esempio n. 7
0
def compute_init_lam(nlp, x=None, lam_max=1e3):

    if x is None:
        x = nlp.x_init
    else:
        assert x.size == nlp.nx

    assert nlp.nd == 0, "only supported for equality constrained nlps for now"

    nx = nlp.nx
    nc = nlp.nc

    # create Jacobian
    jac_c = nlp.jacobian_g(x)

    # create gradient of objective
    df = nlp.grad_objective(x)

    diag_ones = np.ones(nx)
    irows = np.arange(nx)
    jcols = np.arange(nx)
    eye = COOSymMatrix((diag_ones, (irows, jcols)), shape=(nx, nx))

    # create KKT system
    kkt = BlockSymMatrix(2)
    kkt[0, 0] = eye
    kkt[1, 0] = jac_c

    zeros = np.zeros(nc)
    rhs = BlockVector([-df, zeros])


    flat_kkt = kkt.tofullmatrix().tocsc()
    flat_rhs = rhs.flatten()

    sol = spsolve(flat_kkt, flat_rhs)
    return sol[nlp.nx: nlp.nx + nlp.ng]