def test_empty_problem(self): """ Test empty problem """ op = QuadraticProgram() conv = InequalityToEquality() op = conv.convert(op) conv = IntegerToBinary() op = conv.convert(op) conv = LinearEqualityToPenalty() op = conv.convert(op) _, shift = op.to_ising() self.assertEqual(shift, 0.0)
def test_0var_range_inequality(self): """ Test InequalityToEquality converter when the var_rang of the slack variable is 0""" op = QuadraticProgram() op.binary_var('x') op.binary_var('y') op.linear_constraint(linear={'x': 1, 'y': 1}, sense='LE', rhs=0, name='xy_leq1') op.linear_constraint(linear={'x': 1, 'y': 1}, sense='GE', rhs=2, name='xy_geq1') op.quadratic_constraint(quadratic={('x', 'x'): 1}, sense='LE', rhs=0, name='xy_leq2') op.quadratic_constraint(quadratic={('x', 'y'): 1}, sense='GE', rhs=1, name='xy_geq2') ineq2eq = InequalityToEquality() new_op = ineq2eq.convert(op) self.assertEqual(new_op.get_num_vars(), 2) self.assertTrue(all(l_const.sense == Constraint.Sense.EQ for l_const in new_op.linear_constraints)) self.assertTrue(all(q_const.sense == Constraint.Sense.EQ for q_const in new_op.quadratic_constraints))
def test_inequality_mode_auto(self): """ Test auto mode of InequalityToEqualityConverter() """ op = QuadraticProgram() for i in range(3): op.binary_var(name='x{}'.format(i)) # Linear constraints linear_constraint = {'x0': 1, 'x1': 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, 'x0x1') linear_constraint = {'x1': 1, 'x2': -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, 'x1x2') linear_constraint = {'x0': 1.1, 'x2': 2.2} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 3.3, 'x0x2') conv = InequalityToEquality(mode='auto') op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual(lst, [Variable.Type.INTEGER, Variable.Type.CONTINUOUS])
def test_inequality_mode_continuous(self): """Test continuous mode of InequalityToEqualityConverter()""" op = QuadraticProgram() for i in range(3): op.binary_var(name="x{}".format(i)) # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") conv = InequalityToEquality(mode="continuous") op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual( lst, [Variable.Type.CONTINUOUS, Variable.Type.CONTINUOUS])
def test_inequality_mode_integer(self): """Test integer mode of InequalityToEqualityConverter()""" op = QuadraticProgram() for i in range(3): op.binary_var(name=f"x{i}") # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") conv = InequalityToEquality(mode="integer") op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual(lst, [Variable.Type.INTEGER, Variable.Type.INTEGER])
def test_0var_range_inequality(self): """Test InequalityToEquality converter when the var_rang of the slack variable is 0""" op = QuadraticProgram() op.binary_var("x") op.binary_var("y") op.linear_constraint(linear={ "x": 1, "y": 1 }, sense="LE", rhs=0, name="xy_leq1") op.linear_constraint(linear={ "x": 1, "y": 1 }, sense="GE", rhs=2, name="xy_geq1") op.quadratic_constraint(quadratic={("x", "x"): 1}, sense="LE", rhs=0, name="xy_leq2") op.quadratic_constraint(quadratic={("x", "y"): 1}, sense="GE", rhs=1, name="xy_geq2") ineq2eq = InequalityToEquality() new_op = ineq2eq.convert(op) self.assertEqual(new_op.get_num_vars(), 2) self.assertTrue( all(l_const.sense == Constraint.Sense.EQ for l_const in new_op.linear_constraints)) self.assertTrue( all(q_const.sense == Constraint.Sense.EQ for q_const in new_op.quadratic_constraints))
def test_inequality_binary(self): """Test InequalityToEqualityConverter with binary variables""" op = QuadraticProgram() for i in range(3): op.binary_var(name="x{}".format(i)) # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") # Quadratic constraints quadratic = {("x0", "x1"): 1, ("x1", "x2"): 2} op.quadratic_constraint({}, quadratic, Constraint.Sense.LE, 3, "x0x1_x1x2LE") quadratic = {("x0", "x1"): 3, ("x1", "x2"): 4} op.quadratic_constraint({}, quadratic, Constraint.Sense.GE, 3, "x0x1_x1x2GE") # Convert inequality constraints into equality constraints conv = InequalityToEquality() op2 = conv.convert(op) self.assertListEqual( [v.name for v in op2.variables], [ "x0", "x1", "x2", "x1x2@int_slack", "x0x2@int_slack", "x0x1_x1x2LE@int_slack", "x0x1_x1x2GE@int_slack", ], ) # Check names and objective senses self.assertEqual(op.name, op2.name) self.assertEqual(op.objective.sense, op2.objective.sense) # For linear constraints lst = [ op2.linear_constraints[0].linear.to_dict()[0], op2.linear_constraints[0].linear.to_dict()[1], ] self.assertListEqual(lst, [1, 1]) self.assertEqual(op2.linear_constraints[0].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[1].linear.to_dict()[1], op2.linear_constraints[1].linear.to_dict()[2], op2.linear_constraints[1].linear.to_dict()[3], ] self.assertListEqual(lst, [1, -1, 1]) lst = [op2.variables[3].lowerbound, op2.variables[3].upperbound] self.assertListEqual(lst, [0, 3]) self.assertEqual(op2.linear_constraints[1].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[2].linear.to_dict()[0], op2.linear_constraints[2].linear.to_dict()[2], op2.linear_constraints[2].linear.to_dict()[4], ] self.assertListEqual(lst, [1, 3, -1]) lst = [op2.variables[4].lowerbound, op2.variables[4].upperbound] self.assertListEqual(lst, [0, 2]) self.assertEqual(op2.linear_constraints[2].sense, Constraint.Sense.EQ) # For quadratic constraints lst = [ op2.quadratic_constraints[0].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[0].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[0].linear.to_dict()[5], ] self.assertListEqual(lst, [1, 2, 1]) lst = [op2.variables[5].lowerbound, op2.variables[5].upperbound] self.assertListEqual(lst, [0, 3]) lst = [ op2.quadratic_constraints[1].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[1].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[1].linear.to_dict()[6], ] self.assertListEqual(lst, [3, 4, -1]) lst = [op2.variables[6].lowerbound, op2.variables[6].upperbound] self.assertListEqual(lst, [0, 4]) new_x = conv.interpret(np.arange(7)) np.testing.assert_array_almost_equal(new_x, np.arange(3))
def test_inequality_integer(self): """ Test InequalityToEqualityConverter with integer variables """ op = QuadraticProgram() for i in range(3): op.integer_var(name='x{}'.format(i), lowerbound=-3, upperbound=3) # Linear constraints linear_constraint = {'x0': 1, 'x1': 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, 'x0x1') linear_constraint = {'x1': 1, 'x2': -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, 'x1x2') linear_constraint = {'x0': 1, 'x2': 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, 'x0x2') # Quadratic constraints quadratic = {('x0', 'x1'): 1, ('x1', 'x2'): 2} op.quadratic_constraint({}, quadratic, Constraint.Sense.LE, 3, 'x0x1_x1x2LE') quadratic = {('x0', 'x1'): 3, ('x1', 'x2'): 4} op.quadratic_constraint({}, quadratic, Constraint.Sense.GE, 3, 'x0x1_x1x2GE') conv = InequalityToEquality() op2 = conv.convert(op) self.assertListEqual([v.name for v in op2.variables], ['x0', 'x1', 'x2', 'x1x2@int_slack', 'x0x2@int_slack', 'x0x1_x1x2LE@int_slack', 'x0x1_x1x2GE@int_slack']) # For linear constraints lst = [ op2.linear_constraints[0].linear.to_dict()[0], op2.linear_constraints[0].linear.to_dict()[1], ] self.assertListEqual(lst, [1, 1]) self.assertEqual(op2.linear_constraints[0].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[1].linear.to_dict()[1], op2.linear_constraints[1].linear.to_dict()[2], op2.linear_constraints[1].linear.to_dict()[3], ] self.assertListEqual(lst, [1, -1, 1]) lst = [op2.variables[3].lowerbound, op2.variables[3].upperbound] self.assertListEqual(lst, [0, 8]) self.assertEqual(op2.linear_constraints[1].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[2].linear.to_dict()[0], op2.linear_constraints[2].linear.to_dict()[2], op2.linear_constraints[2].linear.to_dict()[4], ] self.assertListEqual(lst, [1, 3, -1]) lst = [op2.variables[4].lowerbound, op2.variables[4].upperbound] self.assertListEqual(lst, [0, 10]) self.assertEqual(op2.linear_constraints[2].sense, Constraint.Sense.EQ) # For quadratic constraints lst = [ op2.quadratic_constraints[0].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[0].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[0].linear.to_dict()[5], ] self.assertListEqual(lst, [1, 2, 1]) lst = [op2.variables[5].lowerbound, op2.variables[5].upperbound] self.assertListEqual(lst, [0, 30]) lst = [ op2.quadratic_constraints[1].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[1].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[1].linear.to_dict()[6], ] self.assertListEqual(lst, [3, 4, -1]) lst = [op2.variables[6].lowerbound, op2.variables[6].upperbound] self.assertListEqual(lst, [0, 60]) new_x = conv.interpret(np.arange(7)) np.testing.assert_array_almost_equal(new_x, np.arange(3))