Пример #1
0
def test_jobsequencing_puso_solve():

    e, sol = solve_puso_bruteforce(H)
    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)
Пример #2
0
def test_setcover_puso_solve():

    e, sol = solve_puso_bruteforce(problem.to_puso())
    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))
Пример #3
0
def test_numberpartitioning_puso_solve():

    e, sol = solve_puso_bruteforce(problem_withsoln.to_puso())
    solution = problem_withsoln.convert_solution(sol)

    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_puso_bruteforce(problem_withoutsoln.to_puso())
    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
Пример #4
0
def test_bilp_puso_solve():

    e, sol = solve_puso_bruteforce(problem.to_puso())
    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)
Пример #5
0
def test_vertexcover_puso_solve():

    e, sol = solve_puso_bruteforce(problem.to_puso())
    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)
def test_AlternatingSectorsChain_puso_solve():

    e, sol = solve_puso_bruteforce(problem.to_puso(True))
    solution = problem.convert_solution(sol, 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_puso_bruteforce(problem.to_puso(False))
    solution = problem.convert_solution(sol, True)

    assert solution == (-1, ) * 12 or solution == (1, ) * 12
    assert problem.is_solution_valid(solution)
    assert problem.is_solution_valid(sol)
    assert allclose(e, -65)
Пример #7
0
def test_quso_puso_solve():

    e, sols = solve_puso_bruteforce(problem.to_puso())
    sol = problem.convert_solution(sols)
    assert problem.is_solution_valid(sol)
    assert problem.is_solution_valid(sols)
    assert sol == solution
    assert allclose(e, -10)

    assert (
        problem.value(sol) ==
        quso_value(sol, problem) ==
        e
    )
Пример #8
0
def test_pcso_ne_constraint():

    for i in range(1 << 2):
        P = pubo_to_puso(integer_var('a', 2)) - i
        H = PCSO().add_constraint_ne_zero(P, log_trick=False)
        for sol in H.solve_bruteforce(True):
            assert P.value(sol)

    for i in range(1 << 2):
        P = pubo_to_puso(integer_var('a', 2)) - i
        H = PCSO(P).add_constraint_ne_zero(P, lam=0, log_trick=False)
        for sol in H.solve_bruteforce(True):
            assert P.value(sol)

    for i in range(1 << 2):
        P = pubo_to_puso(integer_var('a', 2)) - i
        H = PCSO().add_constraint_ne_zero(P, log_trick=False)
        for sol in solve_puso_bruteforce(H, True)[1]:
            assert P.value(sol)
Пример #9
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)