Exemplo n.º 1
0
 def test_conditional_sage_dual_1(self):
     n, m = 2, 6
     x = Variable(shape=(n, ), name='x')
     cons = [1 >= vector2norm(x)]
     gts = [lambda z: 1 - np.linalg.norm(z, 2)]
     eqs = []
     sigdom = SigDomain(n, coniclifts_cons=cons, gts=gts, eqs=eqs)
     np.random.seed(0)
     x0 = np.random.randn(n)
     x0 /= 2 * np.linalg.norm(x0)
     alpha = np.random.randn(m, n)
     c = np.array([1, 2, 3, 4, -0.5, -0.1])
     v0 = np.exp(alpha @ x0)
     v = Variable(shape=(m, ), name='projected_v0')
     t = Variable(shape=(1, ), name='epigraph_var')
     sage_constraint = sage_cones.DualSageCone(v,
                                               alpha,
                                               name='test',
                                               X=sigdom,
                                               c=c)
     epi_constraint = vector2norm(v - v0) <= t
     constraints = [sage_constraint, epi_constraint]
     prob = Problem(CL_MIN, t, constraints)
     prob.solve(solver='ECOS')
     v0 = sage_constraint.violation(norm_ord=1, rough=False)
     assert v0 < 1e-6
     v1 = sage_constraint.violation(norm_ord=np.inf, rough=True)
     assert v1 < 1e-6
     val = prob.value
     assert val < 1e-7
Exemplo n.º 2
0
 def test_ordinary_sage_dual_3(self):
     # provide a vector "c" in the dual SAGE cone constructor.
     # generate a point with zero distance from the dual SAGE cone
     n, m = 2, 6
     np.random.seed(0)
     alpha = 10 * np.random.randn(m, n)
     x0 = np.random.randn(n) / 10
     v0 = np.exp(alpha @ x0)
     dummy_vars = Variable(shape=(2, )).scalar_variables()
     c = np.array([1, 2, 3, 4, dummy_vars[0], dummy_vars[1]])
     c = Expression(c)
     v = Variable(shape=(m, ), name='projected_v0')
     t = Variable(shape=(1, ), name='epigraph_var')
     sage_constraint = sage_cones.DualSageCone(v,
                                               alpha,
                                               X=None,
                                               name='test_con',
                                               c=c)
     epi_constraint = vector2norm(v - v0) <= t
     constraints = [sage_constraint, epi_constraint]
     prob = Problem(CL_MIN, t, constraints)
     prob.solve(solver='ECOS')
     viol = sage_constraint.violation(norm_ord=1, rough=False)
     assert viol < 1e-6
     viol = sage_constraint.violation(norm_ord=np.inf, rough=True)
     assert viol < 1e-6
     val = prob.value
     assert val < 1e-7
Exemplo n.º 3
0
 def test_ordinary_sage_dual_2(self):
     # generate a point with zero distance from the dual SAGE cone
     n, m = 2, 6
     np.random.seed(0)
     alpha = 10 * np.random.randn(m, n)
     x0 = np.random.randn(n) / 10
     v0 = np.exp(alpha @ x0)
     v = Variable(shape=(m, ), name='projected_v0')
     t = Variable(shape=(1, ), name='epigraph_var')
     sage_constraint = sage_cones.DualSageCone(v,
                                               alpha,
                                               X=None,
                                               name='test_con')
     epi_constraint = vector2norm(v - v0) <= t
     constraints = [sage_constraint, epi_constraint]
     prob = Problem(CL_MIN, t, constraints)
     prob.solve(solver='ECOS')
     viol = sage_constraint.violation(norm_ord=1, rough=False)
     assert viol < 1e-6
     viol = sage_constraint.violation(norm_ord=np.inf, rough=True)
     assert viol < 1e-6
     val = prob.value
     assert val < 1e-7
Exemplo n.º 4
0
 def test_ordinary_sage_dual_1(self):
     # generate a point which has positive distance from the dual SAGE cone
     n, m = 2, 6
     np.random.seed(0)
     alpha = 10 * np.random.randn(m, n)
     v0 = 10 * np.abs(np.random.randn(m)) + 0.01
     v0[0] = -v0[0]
     v = Variable(shape=(m, ), name='projected_v0')
     t = Variable(shape=(1, ), name='epigraph_var')
     sage_constraint = sage_cones.DualSageCone(v,
                                               alpha,
                                               X=None,
                                               name='test_con')
     epi_constraint = vector2norm(v - v0) <= t
     constraints = [sage_constraint, epi_constraint]
     prob = Problem(CL_MIN, t, constraints)
     prob.solve(solver='ECOS')
     viol = sage_constraint.violation(norm_ord=1, rough=False)
     assert viol < 1e-6
     viol = sage_constraint.violation(norm_ord=np.inf, rough=True)
     assert viol < 1e-6
     val = prob.value
     assert val + 1e-6 >= abs(v0[0])