Exemplo n.º 1
0
def set_up(sets, propagate, smallest_first):
    """ Set up the solver and All_Different for the transversals problem. """
    Solver_FD.set_up()

    # Create a Var_FD for each set. Its initial range is the entire set.
    vars = {Var_FD(s.domain) for s in sets}
    All_Different(vars)

    trace = propagate and smallest_first
    solver_fd = Solver_FD(vars,
                          propagate=propagate,
                          smallest_first=smallest_first,
                          trace=trace)
    if solver_fd.trace:
        print(f'{"~" * 90}\n')
        print('Following is the trace of the final search.')
        print(Solver_FD.to_str(sets))
        print(f'propagate: {propagate}; smallest_first: {smallest_first};\n')
    return solver_fd
Exemplo n.º 2
0
The search is a standard depth-first search with two heuristics: propagate
and smallest_first.
           
The search is done four time with different settings of propagate and smallest-first.
           
When propagate is true, once an element is selected as the representative of one set,
it is removed from consideration as a posible representative of other sets.

When smallest-first is true, the search selectes an element for an unrepresented set
by chosing the smallest unrepresented set to find a representative for.
           
When both propagate and smallest_first are true, a trace of the search is shown.
""")

    print('The sets for which to find a traversal are:\n',
          Solver_FD.to_str(sets), '\n')
    print('The (alphabetized) traversals are:')

    for propagate in [False, True]:
        for smallest_first in [False, True]:
            solver_fd = set_up(sets,
                               propagate=propagate,
                               smallest_first=smallest_first)
            sol_str_set = set()

            if solver_fd.trace:
                print(
                    '*: Var was directly instantiated--and propagated if propagation is on.\n'
                    '-: Var was indirectly instantiated but not propagated.\n')
            assert solver_fd.propagate == propagate and solver_fd.smallest_first == smallest_first
            for _ in solver_fd.solve():