def test_parse_non_expanded_quadratic_expression(self):
        x, y, z = self.vars[:3]

        offset = 5
        expr = (x + y)**2 - (z - 2)**2 + offset
        target = {
            frozenset([x]): 1,
            frozenset([y]): 1,
            frozenset([x, y]): 2,
            frozenset([z]): -1
        }
        linear_target = {z: 4}

        constraint = Constraint(expr, lb=0)
        offset_const, linear_terms_const, quad_terms_const = parse_optimization_expression(
            constraint, quadratic=True)
        offset_obj, linear_terms_obj, quad_terms_obj = parse_optimization_expression(
            Objective(expr), expression=expr, linear=False)

        self.assertEqual(offset_const - constraint.lb, -4 + offset)
        self.assertEqual(offset_obj, -4 + offset)
        _compare_term_dicts(self, linear_terms_const, linear_target)
        _compare_term_dicts(self, linear_terms_obj, linear_target)
        _compare_term_dicts(self, quad_terms_const, target)
        _compare_term_dicts(self, quad_terms_obj, target)
Example #2
0
 def setUp(self):
     self.model = Model()
     x = Variable('x', lb=0, ub=10)
     y = Variable('y', lb=0, ub=10)
     constr = Constraint(1. * x + y, lb=3, name="constr1")
     obj = Objective(2 * x + y)
     self.model.add(x)
     self.model.add(y)
     self.model.add(constr)
     self.model.objective = obj
Example #3
0
    def test_parse_linear_expression(self):
        x, y, z = self.vars[:3]
        expr = 1 * x + 2 * y - 3 * z
        target = {x: 1, y: 2, z: -3}

        linear_terms_const, quad_terms_const = parse_optimization_expression(
            Constraint(expr, lb=0))
        linear_terms_obj, quad_terms_obj = parse_optimization_expression(
            Objective(expr), linear=False)

        self.assertEqual(linear_terms_const, target)
        self.assertEqual(linear_terms_obj, target)
        self.assertEqual(quad_terms_const, {})
        self.assertEqual(quad_terms_obj, {})
Example #4
0
    def test_parse_quadratic_expression(self):
        x, y, z = self.vars[:3]

        expr = 2 * x**2 + 3 * x * y - 4 * z**2
        target = {frozenset([x]): 2, frozenset([z]): -4, frozenset([x, y]): 3}

        linear_terms_const, quad_terms_const = parse_optimization_expression(
            Constraint(expr, lb=0), linear=False)
        linear_terms_obj, quad_terms_obj = parse_optimization_expression(
            Objective(expr), quadratic=True)

        self.assertEqual(linear_terms_const, {})
        self.assertEqual(linear_terms_obj, {})
        self.assertEqual(quad_terms_const, target)
        self.assertEqual(quad_terms_obj, target)
Example #5
0
    def test_parse_linear_expression(self):
        x, y, z = self.vars[:3]
        offset = 3
        expr = 1 * x + 2 * y - 3 * z + offset
        target = {x: 1, y: 2, z: -3}

        offset_const, linear_terms_const, quad_terms_const = parse_optimization_expression(Constraint(expr, lb=0))
        offset_obj, linear_terms_obj, quad_terms_obj = parse_optimization_expression(Objective(expr), linear=False)

        self.assertEqual(offset_const, 0)
        self.assertEqual(offset_obj, offset)
        _compare_term_dicts(self, linear_terms_const, target)
        _compare_term_dicts(self, linear_terms_obj, target)
        self.assertEqual(quad_terms_const, {})
        self.assertEqual(quad_terms_obj, {})
Example #6
0
    def test_parse_quadratic_expression(self):
        x, y, z = self.vars[:3]

        offset = 4
        expr = 2 * x**2 + 3 * x * y - 4 * z**2 + offset
        target = {frozenset([x]): 2, frozenset([z]): -4, frozenset([x, y]): 3}

        offset_const, linear_terms_const, quad_terms_const = parse_optimization_expression(Constraint(expr, lb=0), linear=False)
        offset_obj, linear_terms_obj, quad_terms_obj = parse_optimization_expression(Objective(expr), quadratic=True)

        self.assertEqual(offset_const, 0)
        self.assertEqual(offset_obj, offset)
        self.assertEqual(linear_terms_const, {})
        self.assertEqual(linear_terms_obj, {})
        _compare_term_dicts(self, quad_terms_const, target)
        _compare_term_dicts(self, quad_terms_obj, target)
        self.assertEqual((_quad_terms_to_expression(quad_terms_obj) - (expr - offset)).expand(), 0)
        self.assertEqual((_quad_terms_to_expression(quad_terms_const) - (expr - offset)).expand(), 0)
Example #7
0
    def test_parse_non_expanded_quadratic_expression(self):
        x, y, z = self.vars[:3]

        expr = (x + y)**2 - (z - 2)**2
        target = {
            frozenset([x]): 1,
            frozenset([y]): 1,
            frozenset([x, y]): 2,
            frozenset([z]): -1
        }
        linear_target = {z: 4}

        linear_terms_const, quad_terms_const = parse_optimization_expression(
            Constraint(expr, lb=0), quadratic=True)
        linear_terms_obj, quad_terms_obj = parse_optimization_expression(
            Objective(expr), linear=False)

        self.assertEqual(linear_terms_const, linear_target)
        self.assertEqual(linear_terms_obj, linear_target)
        self.assertEqual(quad_terms_const, target)
        self.assertEqual(quad_terms_obj, target)
Example #8
0
 def test_number_objective(self):
     self.model.objective = Objective(0.)
     self.assertEqual(self.model.objective.__str__(), 'Maximize\n0')
Example #9
0
 def test_number_objective(self):
     self.model.objective = Objective(0.)
     self.assertIn('Maximize\n0', self.model.objective.__str__())