Beispiel #1
0
    def test_variable_tracking(self):
        x = Variable((5,), name='x')
        y = Variable((3, 4), name='y')

        # Should have a single integer.
        val1 = np.unique([id(v.parent) for v in x.scalar_variables()])
        assert val1.size == 1
        # Should have a single integer (distinct from above).
        val2 = np.unique([id(v.parent) for v in y.scalar_variables()])
        assert val2.size == 1 and val2[0] != val1[0]
        # Should have single integer (same as immediately above).
        val3 = np.unique([id(v.parent) for v in y[:5].scalar_variables()])
        assert val3.size == 1 and val3[0] == val2[0]
        assert x.is_proper()
        assert y.is_proper()
        assert not y[:3].is_proper()

        # This variable does nothing for the first test.
        z = Variable((2, 2, 2), name='z')

        # First statement should be "True", second should be ['x','y']
        constrs1 = [affine.sum(x) == 1, affine.sum(x) - affine.sum(y) <= 0]
        vars1 = find_variables_from_constraints(constrs1)
        assert all(v.is_proper() for v in vars1)
        assert [v.name for v in vars1] == ['x', 'y']

        # First statement should be "False", second should be "True", third should be ['y', 'z']
        zz = z[:, :, 1]
        assert not zz.is_proper()
        constrs2 = [y == 0, affine.trace(zz) >= 10]
        vars2 = find_variables_from_constraints(constrs2)
        assert all(v.is_proper() for v in vars2)
        assert [v.name for v in vars2] == ['y', 'z']
 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)
 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)
 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)
    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)
Beispiel #6
0
 def age_witnesses(self):
     if self._age_witnesses is None:
         self._age_witnesses = dict()
         if self._m > 1:
             for i in self.ech.U_I:
                 wi_expr = Expression(np.zeros(self._m,))
                 num_cover = self.ech.expcover_counts[i]
                 if num_cover > 0:
                     x_i = self._nus[i]
                     wi_expr[self.ech.expcovers[i]] = pos_operator(x_i, eval_only=True)
                     wi_expr[i] = -aff.sum(wi_expr[self.ech.expcovers[i]])
                 self._age_witnesses[i] = wi_expr
         else:
             self.age_vectors[0] = self.c
             self._age_witnesses[0] = Expression([0])
     return self._age_witnesses
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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)]