예제 #1
0
def sudoku_search_benchmarks(initial_state, runs=3):
    puzzle = Sudoku(initial_state)
    print("Start:")
    puzzle.display(puzzle.infer_assignment())

    options = [
        backtracking_search, AC3, min_conflicts, depth_first_graph_search
    ]
    exec_times = {
        "backtracking_search": [],
        'AC3': [],
        'min_conflicts': [],
        'depth_first_graph_search': []
    }

    for run in range(runs):
        for i in range(3):
            i = 0
            start = time()
            options[i](puzzle)
            exec_time = time() - start
            search_name = options[i].__name__
            exec_times[search_name].append(exec_time)
            # print_result(puzzle, options[i].__name__, exec_time, False)
    for key in exec_times:
        num_runs = len(exec_times[key])
        if num_runs:
            print(time_str(sum(exec_times[key]) / num_runs),
                  "average for",
                  key,
                  "(",
                  end='')
            for ms_time in exec_times[key]:
                print(time_str(ms_time), end=', ')
            print(")")
예제 #2
0
from csp import Sudoku, backtracking_search
import time

easy = '003020600900305001001806400008102900700000008006708200002609500800203009005010300'
medium = '000012300000400000401005600705000010600020007030000806003600109000001000006590000'
hard = '417369805030000000000700000020000060000080400000010000000603070500200000104000000'

for grid in [easy, medium, hard]:
    prob = Sudoku(grid)
    start = time.time()
    Sudoku.display(backtracking_search(prob))
    print(f"Blank cell = {grid.count('0')}")
    print(f"Time to execute: {(time.time()-start)*1000:.2f}ms")
    print()
예제 #3
0
These calls are mostly copied/adapted from AIMA Python.

@author: kvlinden
@version 14feb2013
"""
import sys
sys.path.append('/home/neg6/cs344/cs344-code/tools/aima')
sys.path.append('/home/neg6/cs344/cs344-code/tools/paip')
from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \
    forward_checking, min_conflicts, mac
from search import depth_first_graph_search

# 1. Set up the puzzle.
# Examples from the textbook
solved_fig64b = Sudoku(
    '483921657967345821251876493548132976729564138136798245372689514814253769695417382'
)
easy_fig64a = Sudoku(
    '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..'
)
# Example from the AIMA csp.py library
harder_aima_csp = Sudoku(
    '4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......'
)
# Example from http://zonkedyak.blogspot.com/2006/11/worlds-hardest-sudoku-puzzle-al.html
hardest_escargot = Sudoku(
    '1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..'
)
puzzle = harder_aima_csp

print('\nStart:')
예제 #4
0
Run the various CSP solvers on selected Sudoku puzzles.
These calls are mostly copied/adapted from AIMA Python.

@author: kvlinden
@version 14feb2013
"""
from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \
    forward_checking, min_conflicts
from search import depth_first_graph_search

# 1. Set up the puzzle.
#puzzle = Sudoku('483921657967345821251876493548132976729564138136798245372689514814253769695417382') # solved (Figure 6.4.b)
#puzzle = Sudoku('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..') # easy (Figure 6.4.a)
#puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # harder (csp.py)
puzzle = Sudoku(
    '1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..'
)  # hardest (AI Escargot)

print('Start:')
puzzle.display(puzzle.infer_assignment())

# 2. Solve the puzzle.
#depth_first_graph_search(puzzle)
#AC3(puzzle)
backtracking_search(puzzle,
                    select_unassigned_variable=mrv,
                    inference=forward_checking)
#min_conflicts(puzzle)

# 3. Print the results.
if puzzle.goal_test(puzzle.infer_assignment()):
예제 #5
0
파일: sudoku.py 프로젝트: bff3/cs344
'''
Run the various CSP solvers on selected Sudoku puzzles.
These calls are mostly copied/adapted from AIMA Python.

@author: kvlinden
@version 14feb2013
'''
from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \
    forward_checking, min_conflicts
from search import depth_first_graph_search
from time import time

# 1. Set up the puzzle.
# puzzle = Sudoku('483921657967345821251876493548132976729564138136798245372689514814253769695417382') # solved (Figure 6.4.b)
puzzle = Sudoku(
    '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..'
)  # easy (Figure 6.4.a)
# puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # harder (csp.py)
# puzzle = Sudoku('1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..') # hardest (AI Escargot)

print('Start:')
puzzle.display(puzzle.infer_assignment())

# 2. Solve the puzzle.
# t0 = time()
# depth_first_graph_search(puzzle)
# t = time() - t0
t0 = time()
AC3(puzzle)
t = time() - t0
# t0 = time()
from csp import Sudoku, backtracking_search

impossible = '003020600900305001001806400008102900700000008006708200002609511800203009005010300'

prob = Sudoku(impossible)
Sudoku.display(backtracking_search(prob))
예제 #7
0
'''
Run the various CSP solvers on selected Sudoku puzzles.
These calls are mostly copied/adapted from AIMA Python.

@author: kvlinden
@version 14feb2013
'''
from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \
    forward_checking, min_conflicts
from search import depth_first_graph_search

# 1. Set up the puzzle.
#puzzle = Sudoku('483921657967345821251876493548132976729564138136798245372689514814253769695417382') # solved (Figure 6.4.b)
#puzzle = Sudoku('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..') # easy (Figure 6.4.a)
puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # harder (csp.py)
#puzzle = Sudoku('1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..') # hardest (AI Escargot)

print('Start:')
puzzle.display(puzzle.infer_assignment())

# 2. Solve the puzzle.
#depth_first_graph_search(puzzle)
#AC3(puzzle)
#backtracking_search(puzzle)
min_conflicts(puzzle)

# 3. Print the results.  
print
if puzzle.goal_test(puzzle.infer_assignment()):
    print('Solution:')
    puzzle.display(puzzle.infer_assignment())
예제 #8
0
"""
Run the various CSP solvers on selected Sudoku puzzles.
These calls are mostly copied/adapted from AIMA Python.

@author: kvlinden
@version 14feb2013
"""
from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \
    forward_checking, min_conflicts
from search import depth_first_graph_search

# 1. Set up the puzzle.
puzzle = Sudoku(
    '483921657967345821251876493548132976729564138136798245372689514814253769695417382'
)  # solved (Figure 6.4.b)
# puzzle = Sudoku('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..') # easy (Figure 6.4.a)
# puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # harder (csp.py)
# puzzle = Sudoku('1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..') # hardest (AI Escargot)

print('\nStart:')
puzzle.display(puzzle.infer_assignment())

# 2. Solve the puzzle.
depth_first_graph_search(puzzle)
# AC3(puzzle)
# backtracking_search(puzzle) # Consider adding: select_unassigned_variable=mrv & inference=forward_checking
# min_conflicts(puzzle)

# 3. Print the results.
if puzzle.goal_test(puzzle.infer_assignment()):
    print('\nSolution:')