def test_feasibility(self):
        """Tests feasibility methods."""
        q_p = QuadraticProgram("test")
        _ = q_p.continuous_var(-1, 1, "x")
        _ = q_p.continuous_var(-10, 10, "y")
        q_p.minimize(linear={"x": 1, "y": 1})
        q_p.linear_constraint({"x": 1, "y": 1}, "<=", 10, "c0")
        q_p.linear_constraint({"x": 1, "y": 1}, ">=", -10, "c1")
        q_p.linear_constraint({"x": 1, "y": 1}, "==", 5, "c2")
        q_p.quadratic_constraint({"y": 1}, {("x", "x"): 1}, "<=", 10, "c3")
        q_p.quadratic_constraint({"y": 1}, {("x", "x"): 1}, ">=", 5, "c4")
        q_p.quadratic_constraint(None, {
            ("x", "x"): 1,
            ("y", "y"): 1
        }, "==", 25, "c5")

        self.assertTrue(q_p.is_feasible([0, 5]))
        self.assertFalse(q_p.is_feasible([1, 10]))
        self.assertFalse(q_p.is_feasible([1, -12]))
        self.assertFalse(q_p.is_feasible([1, 5]))
        self.assertFalse(q_p.is_feasible([5, 0]))
        self.assertFalse(q_p.is_feasible([1, 1]))
        self.assertFalse(q_p.is_feasible([0, 0]))

        feasible, variables, constraints = q_p.get_feasibility_info([10, 0])
        self.assertFalse(feasible)
        self.assertIsNotNone(variables)
        self.assertEqual(1, len(variables))
        self.assertEqual("x", variables[0].name)

        self.assertIsNotNone(constraints)
        self.assertEqual(3, len(constraints))
        self.assertEqual("c2", constraints[0].name)
        self.assertEqual("c3", constraints[1].name)
        self.assertEqual("c5", constraints[2].name)
    def test_feasibility(self):
        """Tests feasibility methods."""
        mod = Model('test')
        # 0, 5
        x = mod.continuous_var(
            -1,
            1,
            'x',
        )
        y = mod.continuous_var(-10, 10, 'y')
        mod.minimize(x + y)
        mod.add(x + y <= 10, 'c0')
        mod.add(x + y >= -10, 'c1')
        mod.add(x + y == 5, 'c2')
        mod.add(x * x + y <= 10, 'c3')
        mod.add(x * x + y >= 5, 'c4')
        mod.add(x * x + y * y == 25, 'c5')
        q_p = QuadraticProgram()
        q_p.from_docplex(mod)

        self.assertTrue(q_p.is_feasible([0, 5]))
        self.assertFalse(q_p.is_feasible([1, 10]))
        self.assertFalse(q_p.is_feasible([1, -12]))
        self.assertFalse(q_p.is_feasible([1, 5]))
        self.assertFalse(q_p.is_feasible([5, 0]))
        self.assertFalse(q_p.is_feasible([1, 1]))
        self.assertFalse(q_p.is_feasible([0, 0]))

        feasible, variables, constraints = q_p.get_feasibility_info([10, 0])
        self.assertFalse(feasible)
        self.assertIsNotNone(variables)
        self.assertEqual(1, len(variables))
        self.assertEqual('x', variables[0].name)

        self.assertIsNotNone(constraints)
        self.assertEqual(3, len(constraints))
        self.assertEqual('c2', constraints[0].name)
        self.assertEqual('c3', constraints[1].name)
        self.assertEqual('c5', constraints[2].name)
Example #3
0
    def test_feasibility(self):
        """Tests feasibility methods."""
        mod = Model("test")
        # 0, 5
        x = mod.continuous_var(
            -1,
            1,
            "x",
        )
        y = mod.continuous_var(-10, 10, "y")
        mod.minimize(x + y)
        mod.add(x + y <= 10, "c0")
        mod.add(x + y >= -10, "c1")
        mod.add(x + y == 5, "c2")
        mod.add(x * x + y <= 10, "c3")
        mod.add(x * x + y >= 5, "c4")
        mod.add(x * x + y * y == 25, "c5")
        q_p = QuadraticProgram()
        q_p.from_docplex(mod)

        self.assertTrue(q_p.is_feasible([0, 5]))
        self.assertFalse(q_p.is_feasible([1, 10]))
        self.assertFalse(q_p.is_feasible([1, -12]))
        self.assertFalse(q_p.is_feasible([1, 5]))
        self.assertFalse(q_p.is_feasible([5, 0]))
        self.assertFalse(q_p.is_feasible([1, 1]))
        self.assertFalse(q_p.is_feasible([0, 0]))

        feasible, variables, constraints = q_p.get_feasibility_info([10, 0])
        self.assertFalse(feasible)
        self.assertIsNotNone(variables)
        self.assertEqual(1, len(variables))
        self.assertEqual("x", variables[0].name)

        self.assertIsNotNone(constraints)
        self.assertEqual(3, len(constraints))
        self.assertEqual("c2", constraints[0].name)
        self.assertEqual("c3", constraints[1].name)
        self.assertEqual("c5", constraints[2].name)