Ejemplo n.º 1
0
    def test_simple_sage_1(self):
        """
        Solve a simple SAGE relaxation for a signomial minimization problem.

        Do this without resorting to "Signomial" objects.
        """
        alpha = np.array([[0, 0],
                          [1, 0],
                          [0, 1],
                          [1, 1],
                          [0.5, 0],
                          [0, 0.5]])
        gamma = cl.Variable(shape=(), name='gamma')
        c = cl.Expression([0 - gamma, 3, 2, 1, -4, -2])
        expected_val = -1.8333331773244161

        # with presolve
        cl.presolve_trivial_age_cones(True)
        con = cl.PrimalSageCone(c, alpha, None, 'test_con_name')
        obj = gamma
        prob = Problem(cl.MAX, obj, [con])
        status, val = prob.solve(solver='ECOS', verbose=False)
        assert abs(val - expected_val) < 1e-6
        v = con.violation()
        assert v < 1e-6

        # without presolve
        cl.presolve_trivial_age_cones(False)
        con = cl.PrimalSageCone(c, alpha, None, 'test_con_name')
        obj = gamma
        prob = Problem(cl.MAX, obj, [con])
        status, val = prob.solve(solver='ECOS', verbose=False)
        assert abs(val - expected_val) < 1e-6
        v = con.violation()
        assert v < 1e-6
Ejemplo n.º 2
0
 def test_unconstrained_sage_1(self, presolve=False, compactdual=True, kernel_basis=False):
     # Background
     #
     #       This is Example 1 from a 2018 paper by Murray, Chandrasekaran, and Wierman
     #       (https://arxiv.org/pdf/1810.01614.pdf).
     #
     # Tests
     #
     #       (1) Check that primal / dual objectives are close to a reference values, for ell \in {0, 1}.
     #
     #       (2) Recover a globally optimal solution at ell == 1.
     #
     initial_presolve = sage_cones.SETTINGS['presolve_trivial_age_cones']
     initial_compactdual = sage_cones.SETTINGS['compact_dual']
     initial_kb = sage_cones.SETTINGS['kernel_basis']
     cl.presolve_trivial_age_cones(presolve)
     cl.compact_sage_duals(compactdual)
     cl.kernel_basis_age_witnesses(kernel_basis)
     alpha = np.array([[0, 0],
                       [1, 0],
                       [0, 1],
                       [1, 1],
                       [0.5, 0],
                       [0, 0.5]])
     c = np.array([0, 3, 2, 1, -4, -2])
     s = Signomial(alpha, c)
     expected = [-1.83333, -1.746505595]
     pd0, _ = primal_dual_vals(s, 0)
     self.assertAlmostEqual(pd0[0], expected[0], 4)
     self.assertAlmostEqual(pd0[1], expected[0], 4)
     pd1, dual = primal_dual_vals(s, 1)
     self.assertAlmostEqual(pd1[0], expected[1], 4)
     self.assertAlmostEqual(pd1[1], expected[1], 4)
     optsols = sig_solrec(dual)
     assert (s(optsols[0]) - dual.value) < 1e-6
     cl.presolve_trivial_age_cones(initial_presolve)
     cl.compact_sage_duals(initial_compactdual)
     cl.kernel_basis_age_witnesses(initial_kb)
Ejemplo n.º 3
0
 def test_unconstrained_sage_4(self, presolve=False, compactdual=False, kernel_basis=False):
     # Background
     #
     #       This example was constructed soley as a test case for sageopt.
     #
     #       Minimize s(x) = exp(3*x) - 4*exp(2*x) + 7*exp(x) + exp(-x), over x \in R.
     #
     # Tests
     #
     #       (1) Check that primal / dual objectives are close to reference values, for ell \in {0, 1, 2}.
     #
     #       (2) Recover a globally optimal solution from the dual relaxation, when ell == 3.
     #
     # Notes
     #
     #       It may not be obvious, but the signomial "s" is actually convex!
     #
     initial_presolve = sage_cones.SETTINGS['presolve_trivial_age_cones']
     initial_compactdual = sage_cones.SETTINGS['compact_dual']
     initial_kb = sage_cones.SETTINGS['kernel_basis']
     cl.presolve_trivial_age_cones(presolve)
     cl.compact_sage_duals(compactdual)
     cl.kernel_basis_age_witnesses(kernel_basis)
     s = Signomial.from_dict({(3,): 1, (2,): -4, (1,): 7, (-1,): 1})
     expected = [3.464102, 4.60250026, 4.6217973]
     pds = [primal_dual_vals(s, ell) for ell in range(3)]
     for ell in range(3):
         assert abs(pds[ell][0][0] - expected[ell]) < 1e-5
         assert abs(pds[ell][0][1] - expected[ell]) < 1e-5
     dual = sig_relaxation(s, form='dual', ell=3)
     dual.solve(solver='ECOS', verbose=False)
     optsols = sig_solrec(dual)
     assert s(optsols[0]) < 1e-6 + dual.value
     cl.presolve_trivial_age_cones(initial_presolve)
     cl.compact_sage_duals(initial_compactdual)
     cl.kernel_basis_age_witnesses(initial_kb)