Пример #1
0
 def test_triu(self):
     A = np.random.randn(5, 5).round(decimals=3)
     A_cl = Variable(shape=A.shape)
     A_cl.value = A
     temp = aff.triu(A)
     expr0 = aff.sum(temp)
     expr1 = aff.sum(np.triu(A_cl))
     assert Expression.are_equivalent(expr0, expr1.value)
Пример #2
0
 def test_trace_and_diag(self):
     x = Variable(shape=(5, ))
     A = np.random.randn(5, 5).round(decimals=3)
     for i in range(5):
         A[i, i] = 0
     temp = A + aff.diag(x)
     expr0 = aff.trace(temp)
     expr1 = aff.sum(x)
     assert Expression.are_equivalent(expr0, expr1)
Пример #3
0
 def test_tile(self):
     x = np.array([0, 1, 2])
     x_cl = Variable(shape=x.shape)
     x_cl.value = x
     A = aff.tile(x_cl, 2)
     assert np.allclose(np.tile(x, 2), A.value)
     expr0 = aff.sum(A)
     expr1 = aff.sum(x) * 2
     assert Expression.are_equivalent(expr0.value, expr1)
Пример #4
0
    def test_stack(self):
        array = np.random.randn(3, 4)
        arrays = [array for _ in range(10)]
        a_cl = Variable(shape=array.shape)
        a_cl.value = array
        arrays_cl = [a_cl for _ in range(10)]
        expected = np.stack(arrays, axis=0)
        actual = aff.stack(arrays_cl, axis=0)
        assert np.allclose(expected, actual.value)
        assert Expression.are_equivalent(expected.shape, actual.shape)

        expected1 = np.stack(arrays, axis=1)
        actual1 = aff.stack(arrays_cl, axis=1)
        assert np.allclose(expected1, actual1.value)
        assert Expression.are_equivalent(expected.shape, actual.shape)

        a_cl.value = 1 + array
        assert not np.allclose(expected, actual.value)
        assert not np.allclose(expected1, actual1.value)
Пример #5
0
 def __init__(self, arg):
     self.id = PSD._CONSTRAINT_ID_
     PSD._CONSTRAINT_ID_ += 1
     self.arg = arg
     self.expr = None
     if PSD._SYMMETRY_CHECK_:
         from sageopt.coniclifts.base import Expression
         expr_sym = (arg + arg.T) / 2
         if not Expression.are_equivalent(arg, expr_sym):
             raise RuntimeError('Argument to LMI was not symmetric.')
     pass
Пример #6
0
    def test_repeat(self):
        x = np.array([3])
        x_cl = Variable(shape=x.shape)
        x_cl.value = x
        A = aff.repeat(x_cl, 4)
        assert np.allclose(np.repeat(x, 4), A.value)
        expr0 = aff.sum(A.value)
        expr1 = aff.sum(x_cl) * 4
        assert Expression.are_equivalent(expr0, expr1.value)
        # x_cl.value = x + 1
        # assert not np.allclose(np.repeat(x, 4), A)

        x1 = np.array([[1, 2], [3, 4]])
        x1_cl = Variable(shape=x1.shape)
        x1_cl.value = x1
        A1 = aff.repeat(x1_cl, 2)
        assert np.allclose(np.repeat(x1, 2), A1.value)
        expr2 = aff.sum(A1.value)
        expr3 = aff.sum(x1_cl) * 2
        assert Expression.are_equivalent(expr2, expr3.value)
Пример #7
0
 def test_variables(self):
     # random problem data
     G = np.random.randn(3, 6)
     h = G @ np.random.rand(6)
     c = np.random.rand(6)
     # input to coniclift's Problem constructor
     x = cl.Variable(shape=(6,))
     constrs = [0 <= x, G @ x == h]
     objective_expression = c @ x
     prob = cl.Problem(cl.MIN, objective_expression, constrs)
     x = Variable(shape=(3,), name='my_name')
     shallow_copy = [v for v in prob.all_variables]
     assert Expression.are_equivalent(shallow_copy, prob.variables())
Пример #8
0
 def test_dot(self):
     x = Variable(shape=(4, ))
     a = np.array([1, 2, 3, 4])
     expr0 = aff.dot(x, a)
     expr1 = aff.dot(a, x)
     x0 = np.random.rand(4).round(decimals=4)
     expect = np.dot(a, x0)
     x.value = x0
     actual0 = expr0.value
     actual1 = expr1.value
     assert actual0 == expect
     assert actual1 == expect
     assert Expression.are_equivalent(expr0, expr1)
Пример #9
0
 def test_inner(self):
     # test with scalar inputs
     x = Variable()
     a = 2.0
     expr0 = aff.inner(a, x)
     expr1 = aff.inner(x, a)
     assert Expression.are_equivalent(expr0, expr1)
     # test with multidimensional arrays
     a = np.arange(24).reshape((2, 3, 4))
     x = Variable(shape=(4, ))
     x.value = np.arange(4)
     expr = aff.inner(a, x)
     expect = np.inner(a, np.arange(4))
     actual = expr.value
     assert np.allclose(expect, actual)
Пример #10
0
    def test_parse_integer_constraints(self):
        x = Variable(shape=(3,), name='my_name_x')
        y = Variable(shape=(3,), name='my_name_y')
        z = Variable(shape=(3,), name='my_name_z')
        invalid_lst = [2, 4, 6]
        self.assertRaises(ValueError, Problem._parse_integer_constraints, x, invalid_lst)
        valid_lst = [x, y, z]

        prob = cl.Problem(cl.MIN, cl.sum(x), [x==1,  y == 0, z == -1])
        prob.variable_map = {'my_name_x': np.array([0, 1]),
            'my_name_y': np.array([1, 2]),
            'my_name_z': np.array([2, 3])
            }
        prob._parse_integer_constraints(valid_lst)
        int_indices_expected = [0, 1, 1, 2, 2, 3]
        assert Expression.are_equivalent(int_indices_expected, prob._integer_indices)
        prob1 = cl.Problem(cl.MIN, cl.sum(x), [x==1,  y == 0, z[:-1] == -1])
        self.assertRaises(ValueError, Problem._parse_integer_constraints, prob1, valid_lst)
        z_part = z[:-1]
        self.assertRaises(ValueError, Problem._parse_integer_constraints, prob1, [x, y, z_part])