mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Add multiprocessing for board solving
This commit is contained in:
+1
-2
@@ -16,12 +16,11 @@ if __name__ == '__main__':
|
|||||||
elif len(sys.argv) == 4:
|
elif len(sys.argv) == 4:
|
||||||
try :
|
try :
|
||||||
boggle_board = BoggleBoard(board, int(sys.argv[3]))
|
boggle_board = BoggleBoard(board, int(sys.argv[3]))
|
||||||
print(boggle_board)
|
|
||||||
boggle_tree = build_full_boggle_tree(boggle_board, Path(sys.argv[2]))
|
boggle_tree = build_full_boggle_tree(boggle_board, Path(sys.argv[2]))
|
||||||
|
|
||||||
print("\nBOARD")
|
print("\nBOARD")
|
||||||
print(boggle_board)
|
print(boggle_board)
|
||||||
|
|
||||||
for start_pos, tree in boggle_tree.items():
|
for start_pos, tree in boggle_tree.items():
|
||||||
print(f"\nStarting @ {start_pos}...")
|
print(f"\nStarting @ {start_pos}...")
|
||||||
for word in tree.word_paths:
|
for word in tree.word_paths:
|
||||||
|
|||||||
+14
-11
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from os import path
|
from os import path
|
||||||
|
from multiprocessing import Pool
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
class BoardCell:
|
class BoardCell:
|
||||||
@@ -326,8 +327,6 @@ class WordTree:
|
|||||||
# TODO: return all possible paths, maybe create minature WordTree? Or just of list of paths.
|
# TODO: return all possible paths, maybe create minature WordTree? Or just of list of paths.
|
||||||
return curr_node
|
return curr_node
|
||||||
|
|
||||||
# TODO: implement tree walk
|
|
||||||
|
|
||||||
def build_boggle_tree(self, board: BoggleBoard, board_cell: BoardCell, subtree: WordTree, depth: int = 1) -> WordTree:
|
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).
|
'''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)
|
self.build_boggle_tree(board, board.board[cell.pos], subtree, depth=depth+1)
|
||||||
#print(f"depth: {depth}")
|
#print(f"depth: {depth}")
|
||||||
|
|
||||||
# print(f"DONE SEARCHING at {subtree.active_node}")
|
|
||||||
|
|
||||||
self.active_node = self.active_node.parent
|
self.active_node = self.active_node.parent
|
||||||
subtree.active_node = subtree.active_node.parent
|
subtree.active_node = subtree.active_node.parent
|
||||||
return subtree
|
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]:
|
def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str, WordTree]:
|
||||||
'''Return dictionary of WordTree(s) for every letter on a BoggleBoard'''
|
'''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()]))
|
alphabet = sorted(set([cell.letters for cell in board.board.values()]))
|
||||||
board_tree = {}
|
board_tree = {}
|
||||||
index = {}
|
index = {}
|
||||||
@@ -390,11 +393,11 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str,
|
|||||||
index[letter] = wordlist
|
index[letter] = wordlist
|
||||||
|
|
||||||
print("Generating WordTrees...")
|
print("Generating WordTrees...")
|
||||||
for cell in board.board.values():
|
params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()]
|
||||||
print(f">> {cell}")
|
with Pool(processes=len(board.board)) as pool:
|
||||||
dict_tree = WordTree(alphabet, WordNode(cell.letters), index[cell.letters])
|
for i, res in enumerate(pool.map(build_boggle_tree, params)):
|
||||||
sub_tree = WordTree(alphabet, WordNode(cell.letters, False, board_pos=cell.pos))
|
print(f">> {params[i][2]}")
|
||||||
board_tree[cell.pos] = dict_tree.build_boggle_tree(board, cell, sub_tree)
|
board_tree[params[i][2].pos] = res
|
||||||
|
|
||||||
return board_tree
|
return board_tree
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user