Beispiel #1
0
 def test_ordinary_constrained_2(self):
     x = standard_poly_monomials(1)[0]
     f = -x**2
     gts = [1 - x, x - (-1)]
     eqs = []
     res, dual = primal_dual_constrained(f, gts, eqs, 0, 2, 0, None)
     expect = -1
     assert abs(res[0] - expect) < 1e-6
     assert abs(res[1] - expect) < 1e-6
     sols = poly_solution_recovery.poly_solrec(dual, ineq_tol=0, eq_tol=0)
     assert len(sols) > 0
     x0 = sols[0]
     assert f(x0) >= expect
Beispiel #2
0
 def test_conditional_sage_1(self):
     x = standard_poly_monomials(1)[0]
     f = -x**2
     gts = [1 - x**2]
     opt = -1
     X = infer_domain(f, gts, [])
     res_uncon00 = primal_dual_unconstrained(f, 0, 0, X)
     assert abs(res_uncon00[0] - opt) < 1e-6
     assert abs(res_uncon00[1] - opt) < 1e-6
     res_con010, dual = primal_dual_constrained(f, [], [], 0, 1, 0, X)
     assert abs(res_con010[0] - opt) < 1e-6
     assert abs(res_con010[1] - opt) < 1e-6
     solns = poly_solution_recovery.poly_solrec(dual)
     x_star = solns[0]
     gap = abs(f(x_star) - opt)
     assert gap < 1e-6
Beispiel #3
0
 def test_conditional_sage_4(self):
     n = 4
     x = standard_poly_monomials(n)
     f0 = -x[0] * x[2] ** 3 + 4 * x[1] * x[2] ** 2 * x[3] + 4 * x[0] * x[2] * x[3] ** 2
     f1 = 2 * x[1] * x[3] ** 3 + 4 * x[0] * x[2] + 4 * x[2] ** 2 - 10 * x[1] * x[3] - 10 * x[3] ** 2 + 2
     f = f0 + f1
     sign_sym = [0.25 - x[i] ** 2 for i in range(n)]
     X = infer_domain(f, sign_sym, [])
     gts = [x[i] + 0.5 for i in range(n)] + [0.5 - x[i] for i in range(n)]
     dual = poly_constrained_relaxation(f, gts, [], X, p=1, q=2)
     dual.solve()
     expect = -3.180096
     self.assertAlmostEqual(dual.value, expect, places=5)
     solns = poly_solution_recovery.poly_solrec(dual)
     self.assertGreaterEqual(len(solns), 1)
     x_star = solns[0]
     gap = f(x_star) - dual.value
     self.assertLessEqual(gap, 1e-5)
Beispiel #4
0
 def test_conditional_sage_3(self):
     n = 5
     x = standard_poly_monomials(n)
     f = 0
     for i in range(n):
         sel = np.ones(n, dtype=bool)
         sel[i] = False
         f += 2 ** (n - 1) * np.prod(x[sel])
     gts = [0.25 - x[i] ** 2 for i in range(n)]  # -0.5 <= x[i] <= 0.5 for all i.
     opt = -3
     expect = -5
     X = infer_domain(f, gts, [])
     res_con010, dual = primal_dual_constrained(f, [], [], 0, 1, 0, X)
     assert abs(res_con010[0] - expect) < 1e-4
     assert abs(res_con010[1] - expect) < 1e-4
     solns = poly_solution_recovery.poly_solrec(dual)
     assert len(solns) > 0
     x_star = solns[0]
     gap = abs(f(x_star) - opt)
     assert gap < 1e-4
     pass