Пример #1
0
def test_performance(filename: str) -> None:
    """
    Gets the solver to solve puzzles from a text file, and prints the statistics.
    """
    symbol_set, times = {str(i) for i in range(1, 10)}, []
    with open(filename) as data:
        for line in data:
            line = line.strip('\n').replace('.', '*').replace('0', '*')
            symbols = [[c for c in line[i:i + 9]]
                       for i in range(0, len(line), 9)]
            puzzle = SudokuPuzzle(9, symbols, symbol_set)
            start = time()
            solution = depth_first_solve(puzzle)
            end = time()
            times.append(end - start)
            print(f'Input puzzle:\n{puzzle}')
            print(f'\nSolved in {end - start:.4f} seconds.\n')
            print(f'{solution}\n')
    result = f'Puzzles solved: {len(times)}\n' \
        f'Total time taken: {sum(times)}\n' \
        f'Average time per puzzle: {sum(times) / len(times)}\n' \
        f'Max time: {max(times)}\n'

    with open('progress.txt', 'a') as log:
        log.write(f'{str(datetime.now())[:-7]} (File: {filename})')
        log.write('\n' + result + '\n')
    print(result)
Пример #2
0
 def test_puzzle1(self):
     puzzle = SudokuPuzzle(9,
                           [["*", "*", "*", "7", "*", "8", "*", "1", "*"],
                            ["*", "*", "7", "*", "9", "*", "*", "*", "6"],
                            ["9", "*", "3", "1", "*", "*", "*", "*", "*"],
                            ["3", "5", "*", "8", "*", "*", "6", "*", "1"],
                            ["*", "*", "*", "*", "*", "*", "*", "*", "*"],
                            ["1", "*", "6", "*", "*", "9", "*", "4", "8"],
                            ["*", "*", "*", "*", "*", "1", "2", "*", "7"],
                            ["8", "*", "*", "*", "7", "*", "4", "*", "*"],
                            ["*", "6", "*", "3", "*", "2", "*", "*", "*"]],
                           {"1", "2", "3", "4", "5", "6", "7", "8", "9"})
     solved = depth_first_solve(puzzle)
     if not solved:
         self.fail("No solution found when there should be a solution.")
     solution = [['6', '4', '5', '7', '3', '8', '9', '1', '2'],
                 ['2', '1', '7', '5', '9', '4', '8', '3', '6'],
                 ['9', '8', '3', '1', '2', '6', '5', '7', '4'],
                 ['3', '5', '2', '8', '4', '7', '6', '9', '1'],
                 ['4', '9', '8', '6', '1', '3', '7', '2', '5'],
                 ['1', '7', '6', '2', '5', '9', '3', '4', '8'],
                 ['5', '3', '9', '4', '6', '1', '2', '8', '7'],
                 ['8', '2', '1', '9', '7', '5', '4', '6', '3'],
                 ['7', '6', '4', '3', '8', '2', '1', '5', '9']]
     self.assertEqual(solved.get_symbols(), solution, 'Solution is wrong.')
Пример #3
0
def test_performance(filename: str) -> None:
    """
    Gets the solver to solve puzzles from a text file, and prints the statistics.
    """
    symbol_set, times = {str(i) for i in range(1, 10)}, []
    speed_begin = measure_cpu_speed(5)
    with open(filename) as data:
        for line in data:
            line = line.strip('\n').replace('.', '*').replace('0', '*')
            symbols = [[c for c in line[i:i + 9]]
                       for i in range(0, len(line), 9)]
            puzzle = SudokuPuzzle(9, symbols, symbol_set)
            start = time()
            solution = depth_first_solve(puzzle)
            end = time()
            assert solution is not None, 'valid solution exists but not found'
            times.append(end - start)
            print(f'Input puzzle:\n{puzzle}')
            print(f'\nSolved in {end - start:.4f} seconds.\n')
            print(f'{solution}\n')
    speed_end = measure_cpu_speed(5)
    unstable = max(speed_begin, speed_end) / min(speed_begin, speed_end) > 1.10
    average_solve_time = sum(times) / len(times)
    average_cpu_speed = (speed_begin + speed_end) / 2
    result = f'Puzzles solved: {len(times)}\n' \
             f'Total time taken: {sum(times)}\n' \
             f'Average time per puzzle: {average_solve_time}\n' \
             f'Max time: {max(times)}\n' \
             f'Processor speeds: {speed_begin, speed_end} ({"unstable" if unstable else "stable"})\n' \
             f'Relative performance: {1_000_000 / (average_cpu_speed * average_solve_time)}\n'

    with open('progress.txt', 'a') as log:
        log.write(f'{str(datetime.now())[:-7]} (File: {filename})')
        log.write('\n' + result + '\n')
    print(result)
Пример #4
0
 def test_puzzle_empty(self):
     puzzle = SudokuPuzzle(9, [['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*'],
                               ['*', '*', '*', '*', '*', '*', '*', '*', '*']],
                           {"1", "2", "3", "4", "5", "6", "7", "8", "9"})
     solved = depth_first_solve(puzzle)
     if not solved:
         self.fail("No solution found when there should be a solution.")
     self.assertTrue(solved.is_solved(), 'Solve empty puzzle failed.')