Esempio n. 1
0
    def test_power_cone_system(self):
        n = 4
        rng = np.random.default_rng(12345)

        # Create w and z in one array, where the last one will be z
        wz = Variable(shape=(n+1,), name='wz')

        # Make the last element negative to indicate that that element is z in the wz variable
        lamb = rng.random((n+1,))
        lamb[-1] = -1*np.sum(lamb[:-1])

        # Create simple constraints
        constraints1 = [PowCone(wz, lamb)]
        A, b, K, _, _, _ = compile_constrained_system(constraints1)
        A = A.toarray()
        assert np.allclose(A, np.identity(n+1))
        assert np.allclose(b, np.zeros((n+1,)))
        actual_annotations = {'weights': np.array(lamb[:-1]/np.sum(lamb[:-1])).tolist()}
        assert K[0] == Cone('pow', n+1, annotations=actual_annotations)

        # Increment z
        offset = np.zeros((n+1,))
        offset[-1] = 1
        wz1 = wz + offset
        constraints2 = [PowCone(wz1, lamb)]
        A, b, K, _, _, _ = compile_constrained_system(constraints2)
        A = A.toarray()
        assert np.allclose(A, np.identity(n + 1))
        expected_b = np.zeros((n + 1,))
        expected_b[-1] = 1
        assert np.allclose(b, expected_b)
        actual_annotations = {'weights': np.array(lamb[:-1] / np.sum(lamb[:-1])).tolist()}
        assert K[0] == Cone('pow', n + 1, annotations=actual_annotations)

        #Increment w
        r = rng.random((n+1,))
        r[-1] = 0
        wz2 = wz + r
        constraints3 = [PowCone(wz2, lamb)]
        A, b, K, _, _, _ = compile_constrained_system(constraints3)
        A = A.toarray()
        assert np.allclose(A, np.identity(n + 1))
        expected_b = r
        assert np.allclose(b[:-1], expected_b[:-1])
        assert K[0] == Cone('pow', n+1, annotations=actual_annotations)

        # Last test reconstructed from matrix creation
        x = Variable(shape=(n+1,), name='x')
        M = np.random.rand(n+1, n+1)
        y = M @ x
        constraints4 = [PowCone(y, lamb)]
        A, b, K, _, _, _ = compile_constrained_system(constraints4)
        A = A.toarray()
        assert np.allclose(A, M)
        assert K[0] == Cone('pow', n+1, annotations=actual_annotations)
Esempio n. 2
0
 def test_vector2norm_2(self):
     x = Variable(shape=(3, ), name='x')
     y = Variable(shape=(1, ), name='y')
     nrm = vector2norm(x - np.array([0.1, 0.2, 0.3]))
     con = [nrm <= y]
     A_expect = np.array([
         [0, 0, 0, 1,
          -1],  # linear inequality constraint in terms of epigraph variable
         [0, 0, 0, 0,
          1],  # epigraph component in second order cone constraint
         [1, 0, 0, 0,
          0],  # start of main block in second order cone constraint
         [0, 1, 0, 0, 0],
         [0, 0, 1, 0, 0]
     ])  # end of main block in second order cone constraint
     b_expect = np.zeros(shape=(5, ))
     b_expect[0] = 0
     b_expect[2] = -0.1
     b_expect[3] = -0.2
     b_expect[4] = -0.3
     A, b, K, _, _, _ = compile_constrained_system(con)
     A = np.round(A.toarray(), decimals=1)
     assert np.all(A == A_expect)
     assert np.all(b == b_expect)
     assert K == [Cone('+', 1), Cone('S', 4)]
Esempio n. 3
0
 def test_pos_1(self):
     x = Variable(shape=(3, ), name='x')
     con = [cl_pos(affine.sum(x) - 7) <= 5]
     A_expect = np.array([[0, 0, 0, -1], [0, 0, 0, 1], [-1, -1, -1, 1]])
     A, b, K, _1, _2, _3 = compile_constrained_system(con)
     A = np.round(A.toarray(), decimals=1)
     assert np.all(A == A_expect)
     assert np.all(b == np.array([5, 0, 7]))
     assert K == [Cone('+', 1), Cone('+', 2)]
     # value propagation
     x.value = np.array([4, 4, 4])
     viol = con[0].violation()
     assert viol == 0
     x.value = np.array([4.2, 3.9, 4.0])
     viol = con[0].violation()
     assert abs(viol - 0.1) < 1e-7
Esempio n. 4
0
 def test_abs_1(self):
     x = Variable(shape=(2, ), name='x')
     one_norm = affine.sum(cl_abs(x))
     con = [one_norm <= 5]
     A_expect = np.array([[0, 0, -1, -1], [1, 0, 1, 0], [-1, 0, 1, 0],
                          [0, 1, 0, 1], [0, -1, 0, 1]])
     A, b, K, _1, _2, _3 = compile_constrained_system(con)
     A = np.round(A.toarray(), decimals=1)
     assert np.all(A == A_expect)
     assert np.all(b == np.array([5, 0, 0, 0, 0]))
     assert K == [Cone('+', 1), Cone('+', 2), Cone('+', 2)]
     # value propagation
     x.value = np.array([1, -2])
     viol = con[0].violation()
     assert viol == 0
     x.value = np.array([-3, 3])
     viol = con[0].violation()
     assert viol == 1
Esempio n. 5
0
 def test_relent_1(self):
     # compilation and evaluation
     x = Variable(shape=(2, ), name='x')
     y = Variable(shape=(2, ), name='y')
     re = relent(2 * x, np.exp(1) * y)
     con = [re <= 10, 3 <= x, x <= 5]
     # compilation
     A, b, K, _, _, _ = compile_constrained_system(con)
     A_expect = np.array([
         [0., 0., 0., 0., -1.,
          -1.],  # linear inequality on epigraph for relent constr
         [1., 0., 0., 0., 0., 0.],  # bound constraints on x
         [0., 1., 0., 0., 0., 0.],  #
         [-1., 0., 0., 0., 0., 0.],  # more bound constraints on x
         [0., -1., 0., 0., 0., 0.],  #
         [0., 0., 0., 0., -1., 0.],  # first exponential cone
         [0., 0., 2.72, 0., 0., 0.],  #
         [2., 0., 0., 0., 0., 0.],  #
         [0., 0., 0., 0., 0., -1.],  # second exponential cone
         [0., 0., 0., 2.72, 0., 0.],  #
         [0., 2., 0., 0., 0., 0.]
     ])  #
     A = np.round(A.toarray(), decimals=2)
     assert np.all(A == A_expect)
     assert np.all(
         b == np.array([10., -3., -3., 5., 5., 0., 0., 0., 0., 0., 0.]))
     assert K == [
         Cone('+', 1),
         Cone('+', 2),
         Cone('+', 2),
         Cone('e', 3),
         Cone('e', 3)
     ]
     # value propagation
     x0 = np.array([1, 2])
     x.value = x0
     y0 = np.array([3, 4])
     y.value = y0
     actual = re.value
     expect = np.sum(rel_entr(2 * x0, np.exp(1) * y0))
     assert abs(actual - expect) < 1e-7
Esempio n. 6
0
    def test_LP_systems(self):
        n = 4
        m = 5
        x = Variable(shape=(n, 1), name='x')

        G = np.random.randn(m, n)
        h = G @ np.abs(np.random.randn(n, 1))
        constraints = [G @ x == h,
                       x >= 0]
        # Reference case : the constraints are over x, and we are interested in no variables other than x.
        A0, b0, K0, var_mapping0, _, _ = compile_constrained_system(constraints)
        A0 = A0.toarray()
        #   A0 should be the (m+n)-by-n matrix formed by stacking -G on top of the identity.
        #   b0 should be the (m+n)-length vector formed by concatenating h with the zero vector.
        #   Should see K0 == [('0',m), ('+',n)]
        #   var_mapping0 should be a length-1 dictionary with var_mapping0['x'] == np.arange(n).reshape((n,1)).
        assert np.all(b0 == np.hstack([h.ravel(), np.zeros(n)]))
        assert K0 == [Cone('0', m), Cone('+', n)]
        assert var_maps_equal(var_mapping0, {'x': np.arange(0, n).reshape((n, 1))})
        expected_A0 = np.vstack((-G, np.eye(n)))
        assert np.all(A0 == expected_A0)
Esempio n. 7
0
 def test_relent_2(self):
     # compilation with the elementwise option
     x = Variable(shape=(2, ), name='x')
     y = Variable(shape=(2, ), name='y')
     re = affine.sum(relent(2 * x, np.exp(1) * y, elementwise=True))
     con = [re <= 1]
     A, b, K, _, _, _ = compile_constrained_system(con)
     A_expect = np.array([
         [0., 0., 0., 0., -1.,
          -1.],  # linear inequality on epigraph for relent constr
         [0., 0., 0., 0., -1., 0.],  # first exponential cone
         [0., 0., 2.72, 0., 0., 0.],  #
         [2., 0., 0., 0., 0., 0.],  #
         [0., 0., 0., 0., 0., -1.],  # second exponential cone
         [0., 0., 0., 2.72, 0., 0.],  #
         [0., 2., 0., 0., 0., 0.]
     ])  #
     A = np.round(A.toarray(), decimals=2)
     assert np.all(A == A_expect)
     assert np.all(b == np.array([1., 0., 0., 0., 0., 0., 0.]))
     assert K == [Cone('+', 1), Cone('e', 3), Cone('e', 3)]
Esempio n. 8
0
 def test_vector2norm_1(self):
     x = Variable(shape=(3, ), name='x')
     nrm = vector2norm(x)
     con = [nrm <= 1]
     A_expect = np.array([
         [0, 0, 0,
          -1],  # linear inequality constraint in terms of epigraph variable
         [0, 0, 0, 1],  # epigraph component in second order cone constraint
         [1, 0, 0,
          0],  # start of main block in second order cone constraint
         [0, 1, 0, 0],
         [0, 0, 1, 0]
     ])  # end of main block in second order cone constraint
     A, b, K, _1, _2, _3 = compile_constrained_system(con)
     A = np.round(A.toarray(), decimals=1)
     assert np.all(A == A_expect)
     assert np.all(b == np.array([1, 0, 0, 0, 0]))
     assert K == [Cone('+', 1), Cone('S', 4)]
     # value propagation
     x.value = np.zeros(3)
     viol = con[0].violation()
     assert viol == 0
Esempio n. 9
0
    def test_SDP_system_1(self):
        x = Variable(shape=(2, 2), name='x', var_properties=['symmetric'])

        D = np.random.randn(2, 2)
        D += D.T
        D /= 2.0
        B = np.random.rand(1, 2)
        C = np.diag([3, 0.5])
        constraints = [affine.trace(D @ x) == 5,
                       B @ x @ B.T >= 1,
                       B @ x @ B.T >> 1,  # a 1-by-1 LMI
                       C @ x @ C.T >> -2]
        A, b, K, _, _, _ = compile_constrained_system(constraints)
        A = A.toarray()
        assert K == [Cone('0', 1), Cone('+', 1), Cone('P', 1), Cone('P', 3)]
        expect_row_0 = -np.array([D[0, 0], 2 * D[1, 0], D[1, 1]])
        assert np.allclose(expect_row_0, A[0, :])
        temp = B.T @ B
        expect_row_1and2 = np.array([temp[0, 0], 2 * temp[0, 1], temp[1, 1]])
        assert np.allclose(expect_row_1and2, A[1, :])
        assert np.allclose(expect_row_1and2, A[2, :])
        expect_rows_3to6 = np.diag([C[0, 0] ** 2, C[0, 0] * C[1, 1], C[1, 1] ** 2])
        assert np.allclose(expect_rows_3to6, A[3:, :])
        assert np.all(b == np.array([5, -1, -1, 2, 2, 2]))