Exemple #1
0
def test():
    timings = []
    repetitions = []
    are_the_same = []

    instances = tools.load_instances("../data/new_instances_25_30_35_40.json")

    limit = 50
    for i, instance in enumerate(instances['easy40'][:limit], start=1):
        puzzle, dataset_solution = instance['puzzle'], instance['solution']
        possibilities = _precompute_possibilities(puzzle)

        print(f'Starting instance #{i}')

        start = time.time()
        dfs_solution = _dfs(puzzle, possibilities)
        end = time.time()

        time_taken = end - start
        timings.append(time_taken)

        reps = _validate(dfs_solution)
        repetitions.append(reps)

        same = (dfs_solution == dataset_solution).all()
        are_the_same.append(same)

        print(
            f'Solution found by DFS after {time_taken} seconds, {reps} repetitions, are solutions the same: {same}'
        )

    print('     Timings:', timings)
    print(' Repetitions:', repetitions)
    print('Are the same:', are_the_same)
Exemple #2
0
def run(cfg: config.DFSConfig) -> None:
    sudoku = cfg.sudoku_instance
    possibilities = _precompute_possibilities(sudoku)

    start = time.time()
    solution = _dfs(sudoku, possibilities)
    end = time.time()

    print('solution found by DFS in {} seconds:'.format(end - start))
    print(solution)
    print('repetitions:', _validate(solution))
    print('solution from dataset:')
    print(cfg.sudoku_solution)
    print('are they the same?', solution == cfg.sudoku_solution)
Exemple #3
0
def final_tests():
    instances = tools.load_instances('../data/new_instances_25_30_35_40.json')
    instances['01_easy'] = instances['easy40']
    del instances['easy40']
    instances['02_medium'] = instances['medium35']
    del instances['medium35']
    instances['03_advanced'] = instances['advanced30']
    del instances['advanced30']
    instances['04_hard'] = instances['hard25']
    del instances['hard25']
    instances_to_test = {
        '01_easy': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
        '02_medium':
        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64],
        '03_advanced': [100, 101, 102, 103, 104, 105, 106, 107, 108],
        '04_hard': [150, 151, 152, 153, 154, 155, 156, 157, 158, 159],
    }
    sublist = lambda data, ix: [data[index] for index in ix]
    time_results = {
        '01_easy': {},
        '02_medium': {},
        '03_advanced': {},
        '04_hard': {},
    }

    for difficulty_level, instances_ids in instances_to_test.items():
        for instance_true_id in instances_ids:
            index = instance_true_id % len(instances[difficulty_level])
            instance = instances[difficulty_level][index]
            puzzle = instance['puzzle']

            print(f'Starting instance #{instance_true_id}')
            start = time.time()
            puzzle, possibilities = _preprocess_puzzle(puzzle)
            dfs_solution = _dfs(puzzle, possibilities)
            end = time.time()

            time_taken = end - start
            time_results[difficulty_level][instance_true_id] = time_taken

            reps = _validate(dfs_solution)
            if reps > 0:
                print(f'instance #{instance_true_id} was not solved properly!')
            print(dfs_solution)

    with open('dfs_report.yaml', 'w') as f:
        yaml.dump(time_results, f)
Exemple #4
0
def run(cfg: config.DFSConfig) -> None:
    sudoku = cfg.sudoku_instance
    print('not preprocessed sudoku:', np.count_nonzero(sudoku == 0),
          'slots to fill')
    preproccessed_sudoku, _ = _preprocess_puzzle(sudoku)
    print('preprocessed sudoku:', np.count_nonzero(preproccessed_sudoku == 0),
          'slots to fill')
    return

    start = time.time()
    sudoku, possibilities = _preprocess_puzzle(sudoku)
    solution = _dfs(sudoku, possibilities)
    end = time.time()

    print('solution found by DFS in {} seconds:'.format(end - start))
    print(solution)
    print('repetitions:', _validate(solution))
    print('solution from dataset:')
    print(cfg.sudoku_solution)
    print('are they the same?', solution == cfg.sudoku_solution)
Exemple #5
0
 def test_evaluation_correct(self):
     self.assertEqual(_validate(self.correct_sudoku), 0)
Exemple #6
0
 def test_evaluation_bad_columns(self):
     self.assertEqual(_validate(self.sudoku_with_bad_columns),
                      9 * self.row_error + 9 * self.square_error)