Example #1
0
 def test_bad_alpha_type(self):
     c = primal_power(
         r1=variable(lb=0),
         r2=variable(lb=0),
         x=[variable(),
            variable()],
         alpha=parameter())
     c = primal_power(
         r1=variable(lb=0),
         r2=variable(lb=0),
         x=[variable(),
            variable()],
         alpha=data_expression())
     with self.assertRaises(TypeError):
         c = primal_power(
             r1=variable(lb=0),
             r2=variable(lb=0),
             x=[variable(),
                variable()],
             alpha=variable())
     with self.assertRaises(TypeError):
         c = primal_power(
             r1=variable(lb=0),
             r2=variable(lb=0),
             x=[variable(),
                variable()],
             alpha=expression())
Example #2
0
 def test_bad_alpha_type(self):
     c = primal_power(
         r1=variable(lb=0),
         r2=variable(lb=0),
         x=[variable(),
            variable()],
         alpha=parameter())
     c = primal_power(
         r1=variable(lb=0),
         r2=variable(lb=0),
         x=[variable(),
            variable()],
         alpha=data_expression())
     with self.assertRaises(TypeError):
         c = primal_power(
             r1=variable(lb=0),
             r2=variable(lb=0),
             x=[variable(),
                variable()],
             alpha=variable())
     with self.assertRaises(TypeError):
         c = primal_power(
             r1=variable(lb=0),
             r2=variable(lb=0),
             x=[variable(),
                variable()],
             alpha=expression())
Example #3
0
class Test_primal_power(_conic_tester_base, unittest.TestCase):

    _object_factory = lambda self: primal_power(x=[variable(),
                                                   variable()],
                                                r1=variable(lb=0),
                                                r2=variable(lb=0),
                                                alpha=parameter(value=0.4))

    def test_bad_alpha_type(self):
        c = primal_power(x=[variable(), variable()],
                         r1=variable(lb=0),
                         r2=variable(lb=0),
                         alpha=parameter())
        c = primal_power(x=[variable(), variable()],
                         r1=variable(lb=0),
                         r2=variable(lb=0),
                         alpha=data_expression())
        with self.assertRaises(TypeError):
            c = primal_power(x=[variable(), variable()],
                             r1=variable(lb=0),
                             r2=variable(lb=0),
                             alpha=variable())
        with self.assertRaises(TypeError):
            c = primal_power(x=[variable(), variable()],
                             r1=variable(lb=0),
                             r2=variable(lb=0),
                             alpha=expression())

    def test_expression(self):
        c = self._object_factory()
        self.assertIs(c._body, None)
        with self.assertRaises(ValueError):
            self.assertIs(c(), None)
        with self.assertRaises(ValueError):
            self.assertIs(c(exception=True), None)
        self.assertIs(c(exception=False), None)
        self.assertIs(c._body, None)
        self.assertIs(c.slack, None)
        self.assertIs(c.lslack, None)
        self.assertIs(c.uslack, None)

        c.x[0].value = 1.1
        c.x[1].value = -2.3
        c.r1.value = 5.9
        c.r2.value = 3.4
        val = round((1.1**2 + (-2.3)**2)**0.5 - \
                    (5.9**0.4)*(3.4**0.6), 9)
        self.assertEqual(round(c(), 9), val)
        self.assertEqual(round(c.slack, 9), -val)
        self.assertEqual(c.lslack, float('inf'))
        self.assertEqual(round(c.uslack, 9), -val)
        self.assertIs(c._body, None)
        # check body
        self.assertEqual(round(c.body(), 9), val)
        self.assertEqual(round(c(), 9), val)
        self.assertEqual(round(c.slack, 9), -val)
        self.assertEqual(c.lslack, float('inf'))
        self.assertEqual(round(c.uslack, 9), -val)
        self.assertIsNot(c._body, None)

    def test_check_convexity_conditions(self):
        c = self._object_factory()
        self.assertEqual(c.check_convexity_conditions(), True)

        c = self._object_factory()
        c.x[0].domain_type = IntegerSet
        self.assertEqual(c.check_convexity_conditions(), False)
        self.assertEqual(c.check_convexity_conditions(relax=True), True)

        c = self._object_factory()
        c.r1.domain_type = IntegerSet
        self.assertEqual(c.check_convexity_conditions(), False)
        self.assertEqual(c.check_convexity_conditions(relax=True), True)
        c = self._object_factory()
        c.r1.lb = None
        self.assertEqual(c.check_convexity_conditions(), False)
        c = self._object_factory()
        c.r1.lb = -1
        self.assertEqual(c.check_convexity_conditions(), False)

        c = self._object_factory()
        c.r2.domain_type = IntegerSet
        self.assertEqual(c.check_convexity_conditions(), False)
        self.assertEqual(c.check_convexity_conditions(relax=True), True)
        c = self._object_factory()
        c.r2.lb = None
        self.assertEqual(c.check_convexity_conditions(), False)
        c = self._object_factory()
        c.r2.lb = -1
        self.assertEqual(c.check_convexity_conditions(), False)

        c = self._object_factory()
        c.alpha.value = 0
        self.assertEqual(c.check_convexity_conditions(), False)
        c = self._object_factory()
        c.alpha.value = 1
        self.assertEqual(c.check_convexity_conditions(), False)

    def test_as_domain(self):
        ret = primal_power.as_domain(x=[1, 2], r1=3, r2=4, alpha=0.5)
        self.assertIs(type(ret), block)
        q, c, x, r1, r2 = ret.q, ret.c, ret.x, ret.r1, ret.r2
        self.assertEqual(q.check_convexity_conditions(), True)
        self.assertIs(type(q), primal_power)
        self.assertIs(type(x), variable_tuple)
        self.assertEqual(len(x), 2)
        self.assertIs(type(r1), variable)
        self.assertIs(type(r2), variable)
        self.assertIs(type(c), constraint_tuple)
        self.assertEqual(len(c), 4)
        self.assertEqual(c[0].rhs, 1)
        x[0].value = 1
        self.assertEqual(c[0].slack, 0)
        x[0].value = None
        self.assertEqual(c[1].rhs, 2)
        x[1].value = 2
        self.assertEqual(c[1].slack, 0)
        x[1].value = None
        self.assertEqual(c[2].rhs, 3)
        r1.value = 3
        self.assertEqual(c[2].slack, 0)
        r1.value = None
        r2.value = 4
        self.assertEqual(c[3].slack, 0)
        r2.value = None