Esempio n. 1
0
def solve():

    matrix = dl.matrix_from_file('gaya.txt')

#    matrix.add_column_headers([str(x) for x in range(size ** 3)])
#
#    all_rows = []
#    for point_vector in spin(5):
#        
#        r = list(make_row(point_vector, size))
#        all_rows.append(r)
#        matrix.add_row(r)
#
##        knob = find_outlier(point_vector)
##        cell = xyz_to_cell(knob, size)
##        print r
#
#
    initial_rows = [100]
    dlx = dl.DLXAlgorithm(matrix, seeds=initial_rows)

    dlx.dlx1()

    scount = 0
    for s in dlx.solutions:
        print s
        scount += 1

    print '%d solutions' % scount
Esempio n. 2
0
    def test_initial_solution_set(self):

        '''
        test behavior when algorithm is seeded with a set of rows
        that are expected to be part of the solution
        '''

        # solution for test.txt is [0,3,4]
        # so if we seed with row 1 we should get no solution

#        matrix = dl.matrix_from_file('test.txt')
        matrix = dl.matrix_from_file('two_solutions.txt')

        matrix.display_by_columns()

        matrix.display_sloppy()

        seeds = None
        seeds = [3]

        dlx = dl.DLXAlgorithm(matrix, seeds=seeds)
        dlx.dlx1()

        for s in dlx.solutions:
            matrix.display_by_rows(list(s))
Esempio n. 3
0
    def test_cover_and_reduction(self):

        matrix = dl.matrix_from_file('test.txt')

        updates = 0

        matrix.display_by_columns()

        # reduce by the first row with a 1 in the leftmost column

        cheader = matrix.root.r
        row = cheader.d

        print 'covering column %s' % cheader
        matrix.cover_column(cheader)

        print 'reducing by row %d' % row.row_header.n

        updates = matrix.reduce_by_row(row)
        matrix.display_by_columns()

        updates = matrix.unreduce_by_row(row)
        matrix.uncover_column(cheader)

        matrix.display_by_columns()
Esempio n. 4
0
        print '%s [row %d]' % (a, row_count)
        row_count += 1

def show_solution(matrix, solution, w, h):

    b = Board(w, h)

    for r in solution:
        p = piece_from_row(matrix, r, w, h)
        b.place_piece(p)

    b.print_()

if __name__ == '__main__':

    matrix = dl.matrix_from_file('15x15.txt')
    seed = [1008, 658, 851, 167]
    w = h = 15

    dlx = dl.DLXAlgorithm(matrix, seeds=seed)

    dlx.dlx1()

    scount = 0
    for s in dlx.solutions:
        scount += 1
    
    print '%d solutions' % (scount)
    if scount > 0:
        show_solution(matrix, dlx.solutions[0], w, h)
Esempio n. 5
0
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "arg count:  |%s|" % ("".join(sys.argv))
        sys.exit(1)

    size, initial_rows = tableau_from_file(sys.argv[1])

    initial_tableau = rows_to_tableau(initial_rows, size)
    print_tableau(initial_tableau)

    #    print 'seed:', initial_rows

    datafile = "sudoku%d_data.txt" % size

    matrix = dl.matrix_from_file(datafile)

    dlx = dl.DLXAlgorithm(matrix, seeds=initial_rows)

    dlx.dlx1()

    scount = 0
    for s in dlx.solutions:
        solution_tableau = rows_to_tableau(s, size)
        print "=" * 44
        #        print s
        print_tableau(solution_tableau)
        scount += 1

    print "%d solutions" % (scount)