def test_variables_handling(self):
        """ test add variables """
        quadratic_program = QuadraticProgram()

        self.assertEqual(quadratic_program.get_num_vars(), 0)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 0)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 0)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 0)

        x_0 = quadratic_program.continuous_var()
        self.assertEqual(x_0.name, 'x0')
        self.assertEqual(x_0.lowerbound, 0)
        self.assertEqual(x_0.upperbound, INFINITY)
        self.assertEqual(x_0.vartype, Variable.Type.CONTINUOUS)

        self.assertEqual(quadratic_program.get_num_vars(), 1)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 1)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 0)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 0)

        x_1 = quadratic_program.continuous_var(name='x1',
                                               lowerbound=5,
                                               upperbound=10)
        self.assertEqual(x_1.name, 'x1')
        self.assertEqual(x_1.lowerbound, 5)
        self.assertEqual(x_1.upperbound, 10)
        self.assertEqual(x_1.vartype, Variable.Type.CONTINUOUS)

        self.assertEqual(quadratic_program.get_num_vars(), 2)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 2)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 0)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 0)

        x_2 = quadratic_program.binary_var()
        self.assertEqual(x_2.name, 'x2')
        self.assertEqual(x_2.lowerbound, 0)
        self.assertEqual(x_2.upperbound, 1)
        self.assertEqual(x_2.vartype, Variable.Type.BINARY)

        self.assertEqual(quadratic_program.get_num_vars(), 3)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 2)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 1)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 0)

        x_3 = quadratic_program.binary_var(name='x3')
        self.assertEqual(x_3.name, 'x3')
        self.assertEqual(x_3.lowerbound, 0)
        self.assertEqual(x_3.upperbound, 1)
        self.assertEqual(x_3.vartype, Variable.Type.BINARY)

        self.assertEqual(quadratic_program.get_num_vars(), 4)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 2)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 2)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 0)

        x_4 = quadratic_program.integer_var()
        self.assertEqual(x_4.name, 'x4')
        self.assertEqual(x_4.lowerbound, 0)
        self.assertEqual(x_4.upperbound, INFINITY)
        self.assertEqual(x_4.vartype, Variable.Type.INTEGER)

        self.assertEqual(quadratic_program.get_num_vars(), 5)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 2)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 2)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 1)

        x_5 = quadratic_program.integer_var(name='x5',
                                            lowerbound=5,
                                            upperbound=10)
        self.assertEqual(x_5.name, 'x5')
        self.assertEqual(x_5.lowerbound, 5)
        self.assertEqual(x_5.upperbound, 10)
        self.assertEqual(x_5.vartype, Variable.Type.INTEGER)

        self.assertEqual(quadratic_program.get_num_vars(), 6)
        self.assertEqual(quadratic_program.get_num_continuous_vars(), 2)
        self.assertEqual(quadratic_program.get_num_binary_vars(), 2)
        self.assertEqual(quadratic_program.get_num_integer_vars(), 2)

        with self.assertRaises(QiskitOptimizationError):
            quadratic_program.continuous_var(name='x0')

        with self.assertRaises(QiskitOptimizationError):
            quadratic_program.binary_var(name='x0')

        with self.assertRaises(QiskitOptimizationError):
            quadratic_program.integer_var(name='x0')

        variables = [x_0, x_1, x_2, x_3, x_4, x_5]
        for i, x in enumerate(variables):
            y = quadratic_program.get_variable(i)
            z = quadratic_program.get_variable(x.name)
            self.assertEqual(x.name, y.name)
            self.assertEqual(x.name, z.name)
        self.assertDictEqual(quadratic_program.variables_index,
                             {'x' + str(i): i
                              for i in range(6)})
    def test_read_from_lp_file(self):
        """test read lp file"""
        try:
            q_p = QuadraticProgram()
            with self.assertRaises(FileNotFoundError):
                q_p.read_from_lp_file('')
            with self.assertRaises(FileNotFoundError):
                q_p.read_from_lp_file('no_file.txt')
            lp_file = self.get_resource_path(
                path.join('resources', 'test_quadratic_program.lp'))
            q_p.read_from_lp_file(lp_file)
            self.assertEqual(q_p.name, 'my problem')
            self.assertEqual(q_p.get_num_vars(), 3)
            self.assertEqual(q_p.get_num_binary_vars(), 1)
            self.assertEqual(q_p.get_num_integer_vars(), 1)
            self.assertEqual(q_p.get_num_continuous_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 3)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 3)

            self.assertEqual(q_p.variables[0].name, 'x')
            self.assertEqual(q_p.variables[0].vartype, Variable.Type.BINARY)
            self.assertEqual(q_p.variables[0].lowerbound, 0)
            self.assertEqual(q_p.variables[0].upperbound, 1)
            self.assertEqual(q_p.variables[1].name, 'y')
            self.assertEqual(q_p.variables[1].vartype, Variable.Type.INTEGER)
            self.assertEqual(q_p.variables[1].lowerbound, -1)
            self.assertEqual(q_p.variables[1].upperbound, 5)
            self.assertEqual(q_p.variables[2].name, 'z')
            self.assertEqual(q_p.variables[2].vartype,
                             Variable.Type.CONTINUOUS)
            self.assertEqual(q_p.variables[2].lowerbound, -1)
            self.assertEqual(q_p.variables[2].upperbound, 5)

            self.assertEqual(q_p.objective.sense,
                             QuadraticObjective.Sense.MINIMIZE)
            self.assertEqual(q_p.objective.constant, 1)
            self.assertDictEqual(q_p.objective.linear.to_dict(use_name=True), {
                'x': 1,
                'y': -1,
                'z': 10
            })
            self.assertDictEqual(
                q_p.objective.quadratic.to_dict(use_name=True), {
                    ('x', 'x'): 0.5,
                    ('y', 'z'): -1
                })

            cst = q_p.linear_constraints
            self.assertEqual(cst[0].name, 'lin_eq')
            self.assertDictEqual(cst[0].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 2
            })
            self.assertEqual(cst[0].sense, Constraint.Sense.EQ)
            self.assertEqual(cst[0].rhs, 1)
            self.assertEqual(cst[1].name, 'lin_leq')
            self.assertDictEqual(cst[1].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 2
            })
            self.assertEqual(cst[1].sense, Constraint.Sense.LE)
            self.assertEqual(cst[1].rhs, 1)
            self.assertEqual(cst[2].name, 'lin_geq')
            self.assertDictEqual(cst[2].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 2
            })
            self.assertEqual(cst[2].sense, Constraint.Sense.GE)
            self.assertEqual(cst[2].rhs, 1)

            cst = q_p.quadratic_constraints
            self.assertEqual(cst[0].name, 'quad_eq')
            self.assertDictEqual(cst[0].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 1
            })
            self.assertDictEqual(cst[0].quadratic.to_dict(use_name=True), {
                ('x', 'x'): 1,
                ('y', 'z'): -1,
                ('z', 'z'): 2
            })
            self.assertEqual(cst[0].sense, Constraint.Sense.EQ)
            self.assertEqual(cst[0].rhs, 1)
            self.assertEqual(cst[1].name, 'quad_leq')
            self.assertDictEqual(cst[1].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 1
            })
            self.assertDictEqual(cst[1].quadratic.to_dict(use_name=True), {
                ('x', 'x'): 1,
                ('y', 'z'): -1,
                ('z', 'z'): 2
            })
            self.assertEqual(cst[1].sense, Constraint.Sense.LE)
            self.assertEqual(cst[1].rhs, 1)
            self.assertEqual(cst[2].name, 'quad_geq')
            self.assertDictEqual(cst[2].linear.to_dict(use_name=True), {
                'x': 1,
                'y': 1
            })
            self.assertDictEqual(cst[2].quadratic.to_dict(use_name=True), {
                ('x', 'x'): 1,
                ('y', 'z'): -1,
                ('z', 'z'): 2
            })
            self.assertEqual(cst[2].sense, Constraint.Sense.GE)
            self.assertEqual(cst[2].rhs, 1)
        except MissingOptionalLibraryError as ex:
            self.skipTest(str(ex))
        except RuntimeError as ex:
            self.fail(str(ex))
Exemple #3
0
    def test_read_from_lp_file(self):
        """test read lp file"""
        try:
            q_p = QuadraticProgram()
            with self.assertRaises(FileNotFoundError):
                q_p.read_from_lp_file("")
            with self.assertRaises(FileNotFoundError):
                q_p.read_from_lp_file("no_file.txt")
            lp_file = self.get_resource_path("test_quadratic_program.lp",
                                             "problems/resources")
            q_p.read_from_lp_file(lp_file)
            self.assertEqual(q_p.name, "my problem")
            self.assertEqual(q_p.get_num_vars(), 3)
            self.assertEqual(q_p.get_num_binary_vars(), 1)
            self.assertEqual(q_p.get_num_integer_vars(), 1)
            self.assertEqual(q_p.get_num_continuous_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 3)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 3)

            self.assertEqual(q_p.variables[0].name, "x")
            self.assertEqual(q_p.variables[0].vartype, Variable.Type.BINARY)
            self.assertEqual(q_p.variables[0].lowerbound, 0)
            self.assertEqual(q_p.variables[0].upperbound, 1)
            self.assertEqual(q_p.variables[1].name, "y")
            self.assertEqual(q_p.variables[1].vartype, Variable.Type.INTEGER)
            self.assertEqual(q_p.variables[1].lowerbound, -1)
            self.assertEqual(q_p.variables[1].upperbound, 5)
            self.assertEqual(q_p.variables[2].name, "z")
            self.assertEqual(q_p.variables[2].vartype,
                             Variable.Type.CONTINUOUS)
            self.assertEqual(q_p.variables[2].lowerbound, -1)
            self.assertEqual(q_p.variables[2].upperbound, 5)

            self.assertEqual(q_p.objective.sense,
                             QuadraticObjective.Sense.MINIMIZE)
            self.assertEqual(q_p.objective.constant, 1)
            self.assertDictEqual(q_p.objective.linear.to_dict(use_name=True), {
                "x": 1,
                "y": -1,
                "z": 10
            })
            self.assertDictEqual(
                q_p.objective.quadratic.to_dict(use_name=True),
                {
                    ("x", "x"): 0.5,
                    ("y", "z"): -1
                },
            )

            cst = q_p.linear_constraints
            self.assertEqual(cst[0].name, "lin_eq")
            self.assertDictEqual(cst[0].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 2
            })
            self.assertEqual(cst[0].sense, Constraint.Sense.EQ)
            self.assertEqual(cst[0].rhs, 1)
            self.assertEqual(cst[1].name, "lin_leq")
            self.assertDictEqual(cst[1].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 2
            })
            self.assertEqual(cst[1].sense, Constraint.Sense.LE)
            self.assertEqual(cst[1].rhs, 1)
            self.assertEqual(cst[2].name, "lin_geq")
            self.assertDictEqual(cst[2].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 2
            })
            self.assertEqual(cst[2].sense, Constraint.Sense.GE)
            self.assertEqual(cst[2].rhs, 1)

            cst = q_p.quadratic_constraints
            self.assertEqual(cst[0].name, "quad_eq")
            self.assertDictEqual(cst[0].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 1
            })
            self.assertDictEqual(
                cst[0].quadratic.to_dict(use_name=True),
                {
                    ("x", "x"): 1,
                    ("y", "z"): -1,
                    ("z", "z"): 2
                },
            )
            self.assertEqual(cst[0].sense, Constraint.Sense.EQ)
            self.assertEqual(cst[0].rhs, 1)
            self.assertEqual(cst[1].name, "quad_leq")
            self.assertDictEqual(cst[1].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 1
            })
            self.assertDictEqual(
                cst[1].quadratic.to_dict(use_name=True),
                {
                    ("x", "x"): 1,
                    ("y", "z"): -1,
                    ("z", "z"): 2
                },
            )
            self.assertEqual(cst[1].sense, Constraint.Sense.LE)
            self.assertEqual(cst[1].rhs, 1)
            self.assertEqual(cst[2].name, "quad_geq")
            self.assertDictEqual(cst[2].linear.to_dict(use_name=True), {
                "x": 1,
                "y": 1
            })
            self.assertDictEqual(
                cst[2].quadratic.to_dict(use_name=True),
                {
                    ("x", "x"): 1,
                    ("y", "z"): -1,
                    ("z", "z"): 2
                },
            )
            self.assertEqual(cst[2].sense, Constraint.Sense.GE)
            self.assertEqual(cst[2].rhs, 1)
        except RuntimeError as ex:
            self.fail(str(ex))