def generate(bot, update, args): ''' Generate random message starting from words given (/with command) ''' argument = ' '.join(args) B = Board(9) B.generate_puzzle(1, to_remove=30) draw_matrix(B.grid, name='puzzle.png') bot.send_photo(chat_id=update.message.chat_id, photo=open('puzzle.png', 'rb'))
def solve(bot, update): file_id = update.message.photo[-1] newFile = bot.getFile(file_id) newFile.download('download.jpg') bot.sendMessage(chat_id=update.message.chat_id, text="Processing...") img = imread('download.jpg', IMREAD_GRAYSCALE) json_file = open(args.model + '.json', 'r') loaded_model_json = json_file.read() json_file.close() model = model_from_json(loaded_model_json) model.load_weights(args.model + '.h5') grid = get_grid(img, model, method='cnn') B = Board(9, grid) # print(B.grid) solution_img(B.solution().T, B.grid.T, 'solution.png') bot.send_photo(chat_id=update.message.chat_id, photo=open('solution.png', 'rb'))
def save_generated_dataset(folder, number, randfont=True): ''' Save <number> of pictures in the given <folder>s orig subfolder and data as data.csv ''' y = np.zeros((number, 81), int) for i in range(number): picname = folder + 'orig/grid' + str(i).zfill(4) + '.png' B = Board(9) grid = B.solution() fonts = [ 'arial.ttf', 'arialbd.ttf', 'APHont-Regular_q15c.ttf', 'APHont-Bold_q15c.ttf', 'Hack.ttf', 'DejaVuSansMono-Bold.ttf', 'DroidSerif-Regular.ttf', 'OpenSans-Bold.ttf' ] if randfont: font = ImageFont.truetype('fonts/' + choice(fonts), size=45) else: font = ImageFont.truetype('fonts/APHont-Regular_q15c.ttf', size=45) draw_matrix(grid, picname, frame=True, font=font) y[i] = grid.reshape(81) np.savetxt(folder + 'data.csv', y, delimiter=',', fmt='%d')
def test_case01_empty(): b = Board([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) assert repr(SudokuSolver.solve(b)) == repr([[1, 2, 3, 4, 5, 8, 9, 6, 7], [4, 5, 8, 6, 7, 9, 1, 2, 3], [9, 6, 7, 1, 2, 3, 8, 4, 5], [2, 1, 9, 8, 3, 4, 5, 7, 6], [3, 8, 4, 5, 6, 7, 2, 1, 9], [5, 7, 6, 9, 1, 2, 3, 8, 4], [8, 9, 1, 3, 4, 6, 7, 5, 2], [6, 3, 2, 7, 8, 5, 4, 9, 1], [7, 4, 5, 2, 9, 1, 6, 3, 8]]), "Wrong answer"
def test_case01_easy(): b = Board([ [1, 0, 6, 0, 0, 2, 3, 0, 0], [0, 5, 0, 0, 0, 6, 0, 9, 1], [0, 0, 9, 5, 0, 1, 4, 6, 2], [0, 3, 7, 9, 0, 5, 0, 0, 0], [5, 8, 1, 0, 2, 7, 9, 0, 0], [0, 0, 0, 4, 0, 8, 1, 5, 7], [0, 0, 0, 2, 6, 0, 5, 4, 0], [0, 0, 4, 1, 5, 0, 6, 0, 9], [9, 0, 0, 8, 7, 4, 2, 1, 0], ]) assert repr(SudokuSolver.solve(b)) == repr([[1, 4, 6, 7, 9, 2, 3, 8, 5], [2, 5, 8, 3, 4, 6, 7, 9, 1], [3, 7, 9, 5, 8, 1, 4, 6, 2], [4, 3, 7, 9, 1, 5, 8, 2, 6], [5, 8, 1, 6, 2, 7, 9, 3, 4], [6, 9, 2, 4, 3, 8, 1, 5, 7], [7, 1, 3, 2, 6, 9, 5, 4, 8], [8, 2, 4, 1, 5, 3, 6, 7, 9], [9, 6, 5, 8, 7, 4, 2, 1, 3]]), "Wrong answer"
def test_case01_hard(): b = Board([ [4, 0, 9, 3, 7, 0, 0, 0, 0], [1, 0, 0, 4, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 9, 0, 1, 0], [5, 0, 0, 0, 0, 6, 0, 7, 0], [0, 6, 2, 0, 0, 0, 5, 8, 0], [0, 1, 0, 2, 0, 0, 0, 0, 3], [0, 2, 0, 8, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 2, 0, 0, 8], [0, 0, 0, 0, 9, 7, 6, 0, 5], ]) assert repr(SudokuSolver.solve(b)) == repr([[4, 8, 9, 3, 7, 1, 2, 5, 6], [1, 5, 6, 4, 2, 8, 9, 3, 7], [2, 7, 3, 5, 6, 9, 8, 1, 4], [5, 4, 8, 9, 3, 6, 1, 7, 2], [3, 6, 2, 7, 1, 4, 5, 8, 9], [9, 1, 7, 2, 8, 5, 4, 6, 3], [6, 2, 5, 8, 4, 3, 7, 9, 1], [7, 9, 1, 6, 5, 2, 3, 4, 8], [8, 3, 4, 1, 9, 7, 6, 2, 5]]), "Wrong answer"
return initial_blocks if __name__ == '__main__': # initial_blocks = random_initial_blocks() # initial_blocks = [ # [8, 1, 3], # [4, 0, 2], # [7, 6, 5] # ] initial_blocks = [[0, 1, 3], [4, 2, 5], [7, 8, 6]] DIRECTIONS = ['UP', 'DOWN', 'LEFT', 'RIGHT'] initial = Board(initial_blocks) initial.print_elements("Initial") current_move = initial while not current_move.is_goal(): possible_moves: List[Board] = [] for direction in DIRECTIONS: if current_move.is_move_possible(direction): new = current_move.go(direction) possible_moves.append(new) else: print(f'Moving in {direction} not possible.') current_move = min(possible_moves, key=lambda x: x.manhattan())
from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler import sys import argparse parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('task', help='Solve or generate') parser.add_argument('-i', help='Image to solve') parser.add_argument('--method', help='Which ML algorithm to use - cnn or knn') parser.add_argument( '--model', help='Location of the model.json and model.h5 files for CNN') args = parser.parse_args() if args.task == 'generate': B = Board(9) B.generate_puzzle(1, to_remove=30) draw_matrix(B.grid, name='puzzle.png') # solution_img(B.solution(), B.grid, 'solution.png') sys.exit() elif args.task == 'solve': img = imread(args.i, IMREAD_GRAYSCALE) if args.method == 'knn': scaler = StandardScaler() model = KNeighborsClassifier(n_neighbors=3) x_train, y_train = generate_dataset(250) x_train = x_train.reshape(250, 784) scaler.fit(x_train) model.fit(x_train, y_train) elif args.method == 'cnn': json_file = open(args.model + '.json', 'r')
def get_args(): parser = argparse.ArgumentParser() parser.add_argument( "--input", "-i", help= "set input path in txt, must be rectangular n x m board, first line is 'n m' separated by space, then followed by n lines: fill the number separated by space or 0 if empty cell (must have exactly 1 empty cell)" ) return parser.parse_args() def get_config(): args = get_args() filename = args.input if args.input else input("Input filename: ") with open(filename, "r") as f: config = f.readlines() return config if __name__ == "__main__": config = get_config() B = Board(config) print("==== Initial matrix ====") B.nodes[0].print() print() if B.solvable(output=True): B.solve() else: print("Puzzle is unsolvable!")
board[i][j] = info['shape'][0] if info['end_points']: board[i][j] = board[i][j].upper() else: board[i][j] = str(info['visits']) return board, (x_levels[0], x_interval), (y_levels[0], y_interval) if __name__ == '__main__': import subprocess import pyscreenshot as ImageGrab from solver import Board print("Please click on the window of the game LYNE.") res = subprocess.getoutput('xwininfo') num = [int(s) for s in res.split() if s.isdigit()] while True: input("Press enter for starting.") img = ImageGrab.grab(bbox=(num[0], num[1], num[0] + num[4], num[1] + num[5])) img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) board = get_board(img)[0] from IPython import embed embed() board = Board(board) paths = board.solve() for path in paths: board._print_path(path)