Exemplo n.º 1
0
def test_graphpartitioning_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem.to_qubo())
    solution = problem.convert_solution(sol)

    assert solution in solutions
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, 1)

    e, sol = solve_qubo_bruteforce(problem.to_qubo(10))
    solution = problem.convert_solution(sol)

    assert solution in solutions
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, 1)

    e, sol = solve_qubo_bruteforce(problem_weighted.to_qubo())
    solution = problem_weighted.convert_solution(sol)
    assert solution == problem_weighted.convert_solution(
        [sol[i] for i in range(problem_weighted.num_binary_variables)])

    assert solution in solutions_weighted
    assert problem_weighted.is_solution_valid(solution)
    assert problem_weighted.is_solution_valid(sol)
    assert allclose(e, 1)
Exemplo n.º 2
0
def test_jobsequencing_qubo_logtrick_solve():

    e, sol = solve_qubo_bruteforce(Q_log)
    solution = problem_log.convert_solution(sol)
    assert problem_log.is_solution_valid(solution)
    assert problem_log.is_solution_valid(sol)
    assert solution in solutions
    assert allclose(e, obj_val)

    e, sol = solve_qubo_bruteforce(problem_log.to_qubo(3))
    solution = problem_log.convert_solution(sol)
    assert problem_log.is_solution_valid(solution)
    assert problem_log.is_solution_valid(sol)
    assert solution in solutions
    assert allclose(e, obj_val)
Exemplo n.º 3
0
def test_setcover_qubo_logtrick_solve():

    e, sol = solve_qubo_bruteforce(problem_log.to_qubo())
    solution = problem_log.convert_solution(sol)
    assert problem_log.is_solution_valid(solution)
    assert problem_log.is_solution_valid(sol)
    assert solution == {0, 2}
    assert allclose(e, len(solution))

    p = SetCover(U, V, M=1)
    e, sol = solve_qubo_bruteforce(p.to_qubo())
    solution = p.convert_solution(sol)
    assert p.is_solution_valid(solution)
    assert p.is_solution_valid(sol)
    assert solution == {0, 2}
    assert allclose(e, len(solution))
Exemplo n.º 4
0
def test_jobsequencing_qubo_solve():

    e, sol = solve_qubo_bruteforce(Q)
    solution = problem.convert_solution(sol)
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert solution in solutions
    assert allclose(e, obj_val)
Exemplo n.º 5
0
def test_bilp_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem.to_qubo())
    conv_solution = problem.convert_solution(sol)

    assert allclose(conv_solution, solution)
    assert problem.is_solution_valid(conv_solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, 2)

    e, sol = solve_qubo_bruteforce(problem.to_qubo(10, 1))
    conv_solution = problem.convert_solution(sol)

    assert allclose(conv_solution, solution)
    assert problem.is_solution_valid(conv_solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, 2)
Exemplo n.º 6
0
def test_setcover_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem.to_qubo())
    solution = problem.convert_solution(sol)
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert solution == {0, 2}
    assert allclose(e, len(solution))
Exemplo n.º 7
0
def test_weighted_setcover():

    e, sol = solve_qubo_bruteforce(problem_weighted.to_qubo())
    solution = problem_weighted.convert_solution(sol)
    assert problem_weighted.is_solution_valid(solution)
    assert problem_weighted.is_solution_valid(sol)
    assert solution == {0, 2}
    assert allclose(e, 1.1)
Exemplo n.º 8
0
def test_quso_qubo_solve():

    e, sols = solve_qubo_bruteforce(problem.to_qubo())
    sol = problem.convert_solution(sols, False)
    assert problem.is_solution_valid(sol)
    assert problem.is_solution_valid(sols)
    assert sol == solution
    assert allclose(e, -10)
Exemplo n.º 9
0
def test_vertexcover_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem.to_qubo())
    solution = problem.convert_solution(sol)

    assert solution == {"a", "c"}
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, 2)
Exemplo n.º 10
0
def test_qubo_qubo_solve():

    e, sols = solve_qubo_bruteforce(problem.to_qubo())
    sol = problem.convert_solution(sols)
    assert problem.is_solution_valid(sol)
    assert problem.is_solution_valid(sols)
    assert sol == solution
    assert allclose(e, obj)

    assert (problem.value(sol) == qubo_value(sol, problem) == e)
Exemplo n.º 11
0
def test_numberpartitioning_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem_withsoln.to_qubo())
    solution = problem_withsoln.convert_solution(sol)
    assert solution == problem_withsoln.convert_solution(
        [sol[i] for i in range(problem_withsoln.num_binary_variables)])

    assert solution in solutions_withsoln
    assert problem_withsoln.is_solution_valid(solution)
    assert problem_withsoln.is_solution_valid(sol)
    assert allclose(e, 0)

    e, sol = solve_qubo_bruteforce(problem_withoutsoln.to_qubo())
    solution = problem_withoutsoln.convert_solution(sol)

    assert solution in solutions_withoutsoln
    assert not problem_withoutsoln.is_solution_valid(solution)
    assert not problem_withoutsoln.is_solution_valid(sol)
    assert e != 0
def test_AlternatingSectorsChain_qubo_solve():

    e, sol = solve_qubo_bruteforce(problem.to_qubo(True))
    solution = problem.convert_solution(sol)
    assert solution == problem.convert_solution(solution, True)

    assert solution == (-1, ) * 12 or solution == (1, ) * 12
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, -66)

    # not pbc

    e, sol = solve_qubo_bruteforce(problem.to_qubo(False))
    solution = problem.convert_solution(sol)

    assert solution == (-1, ) * 12 or solution == (1, ) * 12
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, -65)
Exemplo n.º 13
0
    def solve_bruteforce(self, all_solutions=False):
        """solve_bruteforce.

        Solves the Set Cover problem exactly with a brute force method. THIS
        SHOULD NOT BE USED FOR LARGE PROBLEMS! The advantage over this method
        as opposed to using a brute force QUBO solver is that the QUBO
        formulation has many slack variables.

        Parameters
        ----------
        all_solutions : boolean (optional, defaults to False).
            If ``all_solutions`` is set to True, all the best solutions to the
            problem will be returned rather than just one of the best. If the
            problem is very big, then it is best if ``all_solutions == False``,
            otherwise this function will use a lot of memory.

        Returns
        -------
        res : set or list of sets.
            A set of which sets are included in the set cover. So if this
            function returns ``{0, 2, 3}``, then the set cover is the sets
            ``V[0]``, ``V[2]``, and ``V[3]``. If ``all_solutions == True``,
            then ``res`` will be a list of sets, where each element of the list
            is one of the optimal solutions.

        Examples
        --------
        >>> from qubovert import SetCover
        >>> U = {"a", "b", "c", "d"}
        >>> V = [{"a", "b"}, {"a", "c"}, {"c", "d"}]
        >>> problem = SetCover(U, V)
        >>> print(problem.solve_bruteforce())
        {0, 2}

        """
        # minimize the objective function
        Q = {(i, ): self._weights[i] for i in range(self._N)}
        # subject to the constraint
        valid = self.is_solution_valid

        obj, sol = solve_qubo_bruteforce(Q, all_solutions, valid)

        if obj is None:
            raise ValueError("Problem is not solvable. See "
                             "``SetCover.is_coverable()``")

        if all_solutions:
            return [self.convert_solution(s) for s in sol]
        return self.convert_solution(sol)
Exemplo n.º 14
0
    def runtests(self):

        assert self.problem.solve_bruteforce() == self.solution

        e, sol = solve_qubo_bruteforce(self.problem.to_qubo())
        assert self.is_valid(e, sol, False)

        e, sol = solve_quso_bruteforce(self.problem.to_quso())
        assert self.is_valid(e, sol, True)

        for deg in (None, ) + tuple(range(2, self.problem.degree + 1)):

            e, sol = solve_puso_bruteforce(self.problem.to_puso(deg))
            assert self.is_valid(e, sol, True)

            e, sol = solve_pubo_bruteforce(self.problem.to_pubo(deg))
            assert self.is_valid(e, sol, False)

        assert (self.problem.value(self.solution) == puso_value(
            self.solution, self.problem) == e)