From 0cdd3895eeb4bbdaaf23e276451571410ab4ea8a Mon Sep 17 00:00:00 2001 From: Cameron Blankenbuehler Date: Thu, 1 Sep 2022 12:42:02 -0400 Subject: [PATCH] Add multiprocessing for board solving --- boggler.py | 2 +- boggler_utils.py | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/boggler.py b/boggler.py index a569a97..f0949a8 100755 --- a/boggler.py +++ b/boggler.py @@ -21,7 +21,7 @@ if __name__ == '__main__': print("\nBOARD") print(boggle_board) - + for start_pos, tree in boggle_tree.items(): print(f"\nStarting @ {start_pos}...") for word in tree.word_paths: diff --git a/boggler_utils.py b/boggler_utils.py index 72ac94a..1a476f1 100644 --- a/boggler_utils.py +++ b/boggler_utils.py @@ -2,6 +2,7 @@ from __future__ import annotations from os import path +from multiprocessing import Pool import sys class BoardCell: @@ -326,8 +327,6 @@ class WordTree: # TODO: return all possible paths, maybe create minature WordTree? Or just of list of paths. return curr_node - # TODO: implement tree walk - def build_boggle_tree(self, board: BoggleBoard, board_cell: BoardCell, subtree: WordTree, depth: int = 1) -> WordTree: '''Return subtree of board given a particular root (first letter). @@ -368,16 +367,20 @@ class WordTree: self.build_boggle_tree(board, board.board[cell.pos], subtree, depth=depth+1) #print(f"depth: {depth}") - # print(f"DONE SEARCHING at {subtree.active_node}") - self.active_node = self.active_node.parent subtree.active_node = subtree.active_node.parent return subtree +def build_boggle_tree(args): + '''Build boggle tree from arguments for process Pool''' + (alphabet, board, cell, wordlist) = args + root_node = WordNode(cell.letters) + dict_tree = WordTree(alphabet, root_node, wordlist) + sub_tree = WordTree(alphabet, WordNode(cell.letters, False, board_pos=cell.pos)) + return dict_tree.build_boggle_tree(board, cell, sub_tree) + def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str, WordTree]: '''Return dictionary of WordTree(s) for every letter on a BoggleBoard''' - # TODO: implement multiprocessing / multitthreading for each start letter - # TODO: benchmark on laptop and chromebook alphabet = sorted(set([cell.letters for cell in board.board.values()])) board_tree = {} index = {} @@ -390,11 +393,10 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str, index[letter] = wordlist print("Generating WordTrees...") - for cell in board.board.values(): - print(f">> {cell}") - dict_tree = WordTree(alphabet, WordNode(cell.letters), index[cell.letters]) - sub_tree = WordTree(alphabet, WordNode(cell.letters, False, board_pos=cell.pos)) - board_tree[cell.pos] = dict_tree.build_boggle_tree(board, cell, sub_tree) + params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()] + with Pool(processes=len(board.board)) as pool: + for i, res in enumerate(pool.map(build_boggle_tree, params)): + board_tree[params[i][2].pos] = res return board_tree