def test_variable_independence(self): model = Model() x = Variable('x', lb=0, ub=20) self.assertNotEqual(id(x), id(self.model.variables['x'])) y = Variable('y', lb=0, ub=10) constr = Constraint(1. * x + y, lb=3, name="constr1") model.add(constr) self.assertNotEqual(id(self.model.variables['x']), id(model.variables['x'])) self.assertNotEqual(id(self.model.variables['y']), id(model.variables['y'])) self.assertNotEqual(self.model.variables['y'].problem, model) self.assertNotEqual(self.model.variables['x'].problem, model) x.lb = -10 self.assertNotEqual(self.model.variables['x'].lb, model.variables['x'].lb)
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, {})
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, {}) self.assertEqual(quad_terms_const, target) self.assertEqual(quad_terms_obj, target)
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)
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} offset_const, linear_terms_const, quad_terms_const = parse_optimization_expression(Constraint(expr, lb=0), quadratic=True) offset_obj, linear_terms_obj, quad_terms_obj = parse_optimization_expression(Objective(expr), linear=False) self.assertEqual(offset_const, -4) 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)
def test_canonicalization_with_lb_and_ub(self): constraint = Constraint(-20 + self.x + 3, ub=666, lb=-666) self.assertEqual(constraint.lb, -649) self.assertEqual(constraint.ub, 683) self.assertEqual(constraint.expression, self.x)
def test_canonicalization_with_lb(self): constraint = Constraint(-20 + self.x + 3, lb=-666) self.assertEqual(constraint.lb, -649) self.assertEqual(constraint.ub, None) self.assertEqual(constraint.expression, self.x)