示例#1
0
def p2(args):
    d, a = Sudoku.domainAssigned('../data/sudoku_example/sudoku_ex.txt')
    s = Sudoku(d, a)
    result, sol, guessCnt = csp2.simpleBacktrackSearch(
        s,
        selectUnassignedVariableIndex=csp2.noReorder,
        start=dt.datetime.now(),
        # inferences=[InferenceType.ac3],
        # inferences=[InferenceType.nc],
    )
    print(sol)
    assert np.all(
        np.equal(
            sol.values,
            np.array([
                [4, 3, 5, 2, 6, 9, 7, 8, 1],
                [6, 8, 2, 5, 7, 1, 4, 9, 3],
                [1, 9, 7, 8, 3, 4, 5, 6, 2],
                [8, 2, 6, 1, 9, 5, 3, 4, 7],
                [3, 7, 4, 6, 8, 2, 9, 1, 5],
                [9, 5, 1, 7, 4, 3, 6, 2, 8],
                [5, 1, 9, 3, 2, 6, 8, 7, 4],
                [2, 4, 8, 9, 5, 7, 1, 3, 6],
                [7, 6, 3, 4, 1, 8, 2, 5, 9],
            ])))
示例#2
0
def pEach(argsFnOrderInf):
    args, fn, order, infTypes = argsFnOrderInf
    # if fn.find('015') == -1:
    #     return
    order = csp2.noReorder if order == csp2.noReorder.__name__ else csp2.mrv
    outFile = os.path.join(
        args.outdir,
        'p{}_{}_{}{}.csv'.format(
            args.number,
            os.path.splitext(fn)[0],
            order.__name__,
            '_{}'.format('-'.join(infTypes)) if len(infTypes) > 0 else '',
        ),
    )
    if os.path.isfile(outFile):
        return
    print('{} Starting: {} {}'.format(dt.datetime.now(),
                                      os.path.splitext(fn)[0], order.__name__))
    d, a = Sudoku.domainAssigned(os.path.join(args.puzzledir, fn))
    start = dt.datetime.now()
    s = Sudoku(d, a)
    s.ac3(repeat=False)
    # print('Product of domain sizes: {}'.format(
    #     reduce(operator.mul, [len(x)/2. for x in s.d.values()])
    # ))
    # return
    result, sol, guessCnt = csp2.simpleBacktrackSearch(
        s,
        selectUnassignedVariableIndex=order,
        inferences=infTypes,
        start=dt.datetime.now(),
    )
    compTime = (dt.datetime.now() - start).total_seconds()
    if result == csp2.SearchResult.failure:
        print('Error on {}'.format(fn))
        return
    with open(outFile, 'w') as f:
        f.write('puzzle, order, guessCnt, compTime, solution\n')
        f.write('"{}","{}","{}","{}","{}","{}"'.format(
            os.path.splitext(fn)[0],
            order.__name__,
            '-'.join(infTypes),
            guessCnt,
            compTime,
            sol.values.tolist() if sol is not None else 'timeout',
        ))
    print('{} Finished: {} {} {} {}'.format(dt.datetime.now(),
                                            os.path.splitext(fn)[0],
                                            order.__name__, guessCnt,
                                            compTime))