def test_maximize_to_minimize(self): """Test maximization to minimization conversion""" op_max = QuadraticProgram() op_min = QuadraticProgram() for i in range(2): op_max.binary_var(name="x{}".format(i)) op_min.binary_var(name="x{}".format(i)) op_max.integer_var(name="x{}".format(2), lowerbound=-3, upperbound=3) op_min.integer_var(name="x{}".format(2), lowerbound=-3, upperbound=3) op_max.maximize(constant=3, linear={"x0": 1}, quadratic={("x1", "x2"): 2}) op_min.minimize(constant=3, linear={"x0": 1}, quadratic={("x1", "x2"): 2}) # check conversion of maximization problem conv = MaximizeToMinimize() op_conv = conv.convert(op_max) self.assertEqual(op_conv.objective.sense, op_conv.objective.Sense.MINIMIZE) x = [0, 1, 2] fval_min = op_conv.objective.evaluate(conv.interpret(x)) self.assertAlmostEqual(fval_min, -7) self.assertAlmostEqual(op_max.objective.evaluate(x), -fval_min) # check conversion of minimization problem op_conv = conv.convert(op_min) self.assertEqual(op_conv.objective.sense, op_min.objective.sense) fval_min = op_conv.objective.evaluate(conv.interpret(x)) self.assertAlmostEqual(op_min.objective.evaluate(x), fval_min)
def test_minimization_problem(self): """Tests the optimizer with a minimization problem""" optimizer = GoemansWilliamsonOptimizer(num_cuts=10, seed=0) problem = Maxcut(self.graph).to_quadratic_program() # artificially convert to minimization max2min = MaximizeToMinimize() problem = max2min.convert(problem) results = optimizer.solve(problem) np.testing.assert_almost_equal([0, 1, 1, 0], results.x, 3) np.testing.assert_almost_equal(4, results.fval, 3)