def test_init(self):
        """test init."""

        quadratic_program = QuadraticProgram()
        for _ in range(5):
            quadratic_program.continuous_var()

        coefficients_list = [[0 for _ in range(5)] for _ in range(5)]
        for i, v in enumerate(coefficients_list):
            for j, _ in enumerate(v):
                coefficients_list[min(i, j)][max(i, j)] += i * j
        coefficients_array = np.array(coefficients_list)
        coefficients_dok = dok_matrix(coefficients_list)
        coefficients_dict_int = {(i, j): v
                                 for (i, j), v in coefficients_dok.items()}
        coefficients_dict_str = {(f"x{i}", f"x{j}"): v
                                 for (i, j), v in coefficients_dok.items()}

        for coeffs in [
                coefficients_list,
                coefficients_array,
                coefficients_dok,
                coefficients_dict_int,
                coefficients_dict_str,
        ]:
            quadratic = QuadraticExpression(quadratic_program, coeffs)
            self.assertEqual((quadratic.coefficients != coefficients_dok).nnz,
                             0)
            self.assertTrue((quadratic.to_array() == coefficients_list).all())
            self.assertDictEqual(quadratic.to_dict(use_name=False),
                                 coefficients_dict_int)
            self.assertDictEqual(quadratic.to_dict(use_name=True),
                                 coefficients_dict_str)
Beispiel #2
0
    def test_setters(self):
        """ test setters. """

        quadratic_program = QuadraticProgram()
        for _ in range(5):
            quadratic_program.continuous_var()

        n = quadratic_program.get_num_vars()
        zeros = np.zeros((n, n))
        quadratic = QuadraticExpression(quadratic_program, zeros)

        coefficients_list = [[0 for _ in range(5)] for _ in range(5)]
        for i, v in enumerate(coefficients_list):
            for j, _ in enumerate(v):
                coefficients_list[min(i, j)][max(i, j)] += i * j
        coefficients_array = np.array(coefficients_list)
        coefficients_dok = dok_matrix(coefficients_list)
        coefficients_dict_int = {(i, j): v for (i, j), v in coefficients_dok.items()}
        coefficients_dict_str = {('x{}'.format(i), 'x{}'.format(j)): v for (i, j), v in
                                 coefficients_dok.items()}

        for coeffs in [coefficients_list,
                       coefficients_array,
                       coefficients_dok,
                       coefficients_dict_int,
                       coefficients_dict_str]:
            quadratic.coefficients = coeffs
            self.assertEqual((quadratic.coefficients != coefficients_dok).nnz, 0)
            self.assertTrue((quadratic.to_array() == coefficients_list).all())
            self.assertDictEqual(quadratic.to_dict(use_name=False), coefficients_dict_int)
            self.assertDictEqual(quadratic.to_dict(use_name=True), coefficients_dict_str)
Beispiel #3
0
 def test_symmetric_set(self):
     """ test symmetric set """
     q_p = QuadraticProgram()
     q_p.binary_var('x')
     q_p.binary_var('y')
     q_p.binary_var('z')
     quad = QuadraticExpression(q_p, {('x', 'y'): -1, ('y', 'x'): 2, ('z', 'x'): 3})
     self.assertDictEqual(quad.to_dict(use_name=True), {('x', 'y'): 1, ('x', 'z'): 3})
     self.assertDictEqual(quad.to_dict(symmetric=True, use_name=True),
                          {('x', 'y'): 0.5, ('y', 'x'): 0.5, ('x', 'z'): 1.5, ('z', 'x'): 1.5})
 def test_symmetric_set(self):
     """test symmetric set"""
     q_p = QuadraticProgram()
     q_p.binary_var("x")
     q_p.binary_var("y")
     q_p.binary_var("z")
     quad = QuadraticExpression(q_p, {("x", "y"): -1, ("y", "x"): 2, ("z", "x"): 3})
     self.assertDictEqual(quad.to_dict(use_name=True), {("x", "y"): 1, ("x", "z"): 3})
     self.assertDictEqual(
         quad.to_dict(symmetric=True, use_name=True),
         {("x", "y"): 0.5, ("y", "x"): 0.5, ("x", "z"): 1.5, ("z", "x"): 1.5},
     )