def main(string_sudoku = "", output_name = ""):
    
    if string_sudoku == "":
        string_sudoku = "000260701680070090190004500820100040004602900050003028009300074040050036703018000"
        output_name = "output.txt"

    data_manager = data_utils(string_sudoku, output_name)
    sudoku, sudoku_board = data_manager.generate_dict()
    constraints = data_manager.constraints_generation(sudoku_board)

    ## Pass throught CSP solver
    ## AC-3 solver
    solved_AC3 = None
    solverCSP = csp(copy.deepcopy(sudoku), copy.deepcopy(sudoku_board))
    bool_ac3 = solverCSP.AC_3(constraints)
    print(bool_ac3)
    #if bool_ac3:  
    solved_AC3 = solverCSP.assignment()
    print(solved_AC3)
    
    return solved_AC3
#THE MAIN FUNCTION GOES HERE
if __name__ == "__main__":
    '''
    The function takes arguments from commandline
    Argument 1 - Python file name 
    Argument 2 - Input String Showing the Sudoku 
    '''
    array = []
    with open("sudokus_start.txt", "r") as ins:
        for line in ins:
            array.append(line)

    ins.close()
    i = 0
    boardno = 0
    start = time.time()

    for grid in array:
        prev = time.time()
        sudoku = csp(grid=grid)
        solved = AC3(sudoku)
        boardno = boardno + 1
        if isComplete(sudoku) and solved:
            print(boardno)
            print("Before solving: ", grid)
            print("After solving: ", write(sudoku.values))
            print("Running time: ", time.time() - prev, "\n")
            i = i + 1

    print("Number of problems solved is: ", i)
    print("The complete run time is: ", time.time() - start)
示例#3
0
import copy
import random
if __name__ == '__main__':
    # N= input('Number of Nodes: ',)
    # dmSize = input('Size of Domains: ',)
    cnt = 0
    total = 0
    # for i in range(0 ,100):
    while total != 20:
        # N = random.randint(1, 5)
        # dmSize = random.randint(10, 20)
        # print(N, dmSize, "N && dmSize")
        N = 5
        dmSize = 10
        csp0 = csp(N, dmSize)
        csp0.setting()
        # print(csp0.adj)
        csp1 = csp(0, 0)
        csp1.copyByValue(csp0.cons_set, csp0.dm_set, csp0.adj, csp0.eList,
                         csp0.N, csp0.dmSize, csp0.mat)
        csp2 = csp(0, 0)
        csp2.copyByValue(csp0.cons_set, csp0.dm_set, csp0.adj, csp0.eList,
                         csp0.N, csp0.dmSize, csp0.mat)

        csp3 = csp(0, 0)
        csp3.copyByValue(csp0.cons_set, csp0.dm_set, csp0.adj, csp0.eList,
                         csp0.N, csp0.dmSize, csp0.mat)

        csp4 = csp(0, 0)
        csp4.copyByValue(csp0.cons_set, csp0.dm_set, csp0.adj, csp0.eList,