Exemple #1
0
    def test_search_s(self):
        opt = 's'
        for n, s in [(3, 3), (4, 5), (5, 9), (6, 12)]:
            net = search(n, 0, s, opt=opt)
            print(net)
            self.assertTrue(net.sorts(n, log=True))
            self.assertEqual(net.size(), s)

            net = search(n, 0, s-1, opt=opt)
            self.assertTrue(net, 'UNSATISFIABLE')
Exemple #2
0
def run_solver():
    global nodes
    colors = get_color_set()
    print('puzzle_colors', colors)
    solver_input = []
    for row in nodes:
        for node in row:
            if node.color == blank_color:
                solver_input.append(-1)
            else:
                solver_input.append(colors.index(node.color))
    return solver.search(solver_input)
Exemple #3
0
    def test_search_d(self):
        opt = 'd'
        for n, d, s in [(3, 3, 3), (4, 3, 5), (5, 5, 9), (6, 5, 12)]:
            net = search(n, d, s, opt=opt)
            self.assertTrue(net.sorts(n, log=True))
            self.assertEqual(net.depth(), d)
            self.assertEqual(net.size(), s)

            net = search(n, d-1, s, opt=opt)
            self.assertTrue(net, 'UNSATISFIABLE')
    
            net = search(n, d, s-1, opt=opt)
            self.assertTrue(net, 'UNSATISFIABLE')
    
            net = search(n, d, 0, opt=opt)
            print(net)
            self.assertTrue(net.sorts(n, log=True))
            self.assertEqual(net.depth(), d)

            net = search(n, d-1, 0, opt=opt)
            self.assertTrue(net, 'UNSATISFIABLE')
Exemple #4
0
        board_set_colors(indices)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='kami2 solver')
    parser.add_argument('-a', help='exhaustive search', action='store_true')
    parser.add_argument('-m', help='moves limit', type=int)
    parser.add_argument('file',
                        help='input file',
                        type=argparse.FileType('rb'))
    args = parser.parse_args()

    master = tkinter.Tk()
    canvas = tkinter.Canvas(master,
                            width=canvas_width,
                            height=canvas_height,
                            bg=canvas_bg_color,
                            borderwidth=0,
                            highlightthickness=0)
    canvas.pack()
    polygons = draw_polygons(canvas)
    palette, indices = get_palette_indices_from_file(args.file)
    board_set_colors(indices)
    if args.a:
        starting_cell, solution = search.search(indices, args.m)
    else:
        starting_cell, solution = solver.search(indices, args.m)
    solution_index = 0
    master.bind('<KeyRelease>', user_keyrelease)
    master.mainloop()
Exemple #5
0
def main(model, image, debug=False):
    """
    Params: 
        (str) model: path to model with .h5 extension
        (str) image: path to image
        (bool) debug: Set to true if you want the intermediate results (default is False)

        Displays the end result. Might also display the intermediate results if debug = True
    """

    model = load_model(model)

    image = cv2.imread(image)
    image = imutils.resize(image, width=600)


    puzzle, warped = find_puzzle(image, debug)
    board = np.zeros((9, 9), dtype="int")

    stepX = warped.shape[1] // 9
    stepY = warped.shape[0] // 9

    cell_locations = []

    for y in range(0, 9):
        row = []
        
        for x in range(0, 9):
            startX = x * stepX
            startY = y * stepY
            endX = (x + 1) * stepX
            endY = (y + 1) * stepY

            row.append((startX, startY, endX, endY))

            cell = warped[startY:endY, startX:endX]
            digit = extract_digit(cell, False)

            if digit is not None:
                roi = cv2.resize(digit, (28, 28)) # for the model
                roi = roi.astype("float") / 255.0
                roi = img_to_array(roi)
                roi = np.expand_dims(roi, axis=0)

                prediction = model.predict(roi).argmax(axis=1)[0] # get the class with the max prob
                board[y, x] = prediction

        cell_locations.append(row)

    flattened_board = list(itertools.chain.from_iterable(board))
    flattened_board = [str(x) if x != 0 else '.' for x in flattened_board]
    board = "".join(flattened_board)

    # record the time for algorithm to solve the board, shown in the results window name
    start = time.time()

    # solving the puzzle
    values = solver.grid2values(board)
    result = solver.search(values)

    time_taken = time.time() - start

    row_results = np.array([val for val in result.values()]) \
        .reshape(9, 9) \
        .tolist()

    # putting the text on the image and then displaying it
    for (cellRow, boardRow) in zip(cell_locations, row_results):

        for (box, digit) in zip(cellRow, boardRow):
            startX, startY, endX, endY = box

            textX = int((endX - startX) * 0.33) 
            textY = int((endY - startY) * -0.2)
            textX += startX
            textY += endY

            cv2.putText(puzzle,
                str(digit), 
                (textX, textY),
                cv2.FONT_HERSHEY_COMPLEX, 
                0.8, (0, 0, 255), 2)

    cv2.imshow(f"Result, time taken {time_taken * 1000:.2f}ms", puzzle)
    cv2.waitKey(0)
Exemple #6
0
 def test_search_3x3(self):
     self.assertEqual(search(305419896, 305419896, 3, 3), 0, 'solved 3x3')
     self.assertEqual(search(305419896, 13193465976, 3, 3), 9,
                      'left column down 3x3')
Exemple #7
0
 def test_search_3x2(self):
     self.assertEqual(search(5349, 5349, 3, 2), 0, 'solved 3x2')
     self.assertEqual(search(5349, 13665, 3, 2), 6, 'right column down 3x2')
Exemple #8
0
 def test_search_2x3(self):
     self.assertEqual(search(5349, 5349, 2, 3), 0, 'solved 2x3')
     self.assertEqual(search(5349, 41189, 2, 3), 4, 'top row right 2x3')
Exemple #9
0
 def test_search_2x2(self):
     self.assertEqual(search(27, 27, 2, 2), 0, 'solved 2x2')
     self.assertIn(search(27, 147, 2, 2), (1, 6), 'left column up or down')