Esempio n. 1
0
def solve_problem_2_4(puzzles):
    print('\n\nSolving puzzles for problem 2.4 with AC-3 only...\n')
    for p in puzzles:
        puzzle = SudokuIO(p)
        sudoku = CSP(puzzle.variables, puzzle.domains, puzzle.neighbors,
                     equal_constraint)
        result = backtracking_search(puzzle.assignment, sudoku, mrv, mac,
                                     False)
        print('Puzzle {} result: {}'.format(p, result))
        puzzle.output_puzzle(result)
    print(
        '\n\nSolving puzzles for problem 2.4 with all inference methods...\n')
    for p in puzzles:
        puzzle = SudokuIO(p)
        sudoku = CSP(puzzle.variables, puzzle.domains, puzzle.neighbors,
                     equal_constraint)
        result = backtracking_search(puzzle.assignment, sudoku, mrv, mac, True)
        print('Puzzle {} result: {}'.format(p, result))
        puzzle.output_puzzle(result)
Esempio n. 2
0
def solve(variables, domains, neighbors, assignment, heuristic,
          with_inferences, instrumented):
    sudoku = CSP(variables, domains, neighbors, equal_constraint)
    if with_inferences:
        # problem 2.4
        return backtracking_search(assignment, sudoku, heuristic, mac)
    elif instrumented:
        # problem 2.3
        return instrumented_recursive_backtracking(assignment, sudoku,
                                                   heuristic)
    else:
        # problem 2.2
        return recursive_backtracking_search(assignment, sudoku, heuristic)
Esempio n. 3
0
def search(sudoku, useAc3=False):
    if useAc3 is False:
        return backtracking_search(sudoku, var_selector_no_ac3)
    else:
        variables = set()
        for i in range(len(sudoku)):
            for j in range(len(sudoku[i])):
                if sudoku[i][j] == 0:
                    variables.add((i, j))
        domainSetMat = calculateDomainSetMat(sudoku)
        check = ac3(sudoku, variables, domainSetMat)
        if check is False:
            return None
        return backtracking_search_memory(sudoku, var_selector_ac3, domainSetMat, variables)
def search(sudoku):
    return backtracking_search(sudoku, var_selector, True)
Esempio n. 5
0
tests_dir = 'data/'
tests_patterns = r'level_32_test_03_[0-9]+.*\.txt', r'level_32_test_04_[0-9]+.*\.txt'
files = [
    f for f in os.listdir(tests_dir)
    if any([re.match(pat, f) for pat in tests_patterns])
]
print(files)

times = 1
for filename in files:
    for implementation in (
            'csp_tuple', ):  # 'csp_list', 'csp_bitstring', 'csp_number'):
        csp_data = load_csp(tests_dir + filename)
        zero = time.process_time()
        for _ in range(times):
            solution = backtracking.backtracking_search(
                csp_data, implementation)
        elapsed = time.process_time() - zero
        print('{} backtracking_search({}): {}'.format(filename, implementation,
                                                      elapsed / times))
        if solution:
            for line in solution:
                # print(('{:0' + str(w) + 'b}').format(line).replace('1', '8').replace('0', ' '))
                # print(line.replace('1', '8').replace('0', ' '))
                print(line)
                pass
        else:
            print('NOT FOUND')
            # zero = time.process_time()
            # for _ in range(times):
            #     solution = iterimprov.iterative_improvement(csp_data, implementation)
            # elapsed = time.process_time() - zero