From 62a72706e283104183698c1c8197c2b22efccc6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Thu, 19 May 2016 09:36:05 +0100 Subject: [PATCH] Increase default timeout, add parameter for desired occupancy. Update readme. --- crossword_generator.py | 8 +++++--- readme.md | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index 1de7b6d..ed729a3 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -186,13 +186,14 @@ def read_word_list(filename): return words -def generate_grid(words, dim, timeout=30): +def generate_grid(words, dim, timeout=60, occ_goal=0.5): """ This function receives a list of words and creates a new grid, which represents our puzzle. The newly-created grid is of dimensions dim[0] * dim[1] (rows * columns). The function also receives a timeout, which is used to control the time-consuming section of the code. If the timeout is reached, the functions returns the best grid it was able to - achieve thus far. + achieve thus far. Lastly, occ_goal represents the fraction of squares that + should be, ideally, filled in. Algorithm: This function operates by taking the words it receives and generating an @@ -248,7 +249,7 @@ def generate_grid(words, dim, timeout=30): # TODO: Add other limits: tries, no more words, etc # TODO: Given the performance impact of "connectedness", it should be a parameter # TODO: If connectedness is turning out to be a problem, add some large word - while occupancy < 0.5 and time.time() - start_time < timeout: + while occupancy < occ_goal and time.time() - start_time < timeout: # Generate new possibilities, if needed while not connected_possibilities: print("Getting new words!", end=" ") @@ -412,6 +413,7 @@ def write_grid(grid, screen=False, out_file="table.tex", words=[]): if __name__ == "__main__": + # TODO: argparse # Read words from file words = read_word_list("words.txt") diff --git a/readme.md b/readme.md index 137e253..783f746 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,11 @@ I purposefully designed and implemented this little project without performing r 3. The technique expands the list of words into a list of possibilities, where each possibility encodes a possible starting location for a word, as well as its direction. This essentially constitutes all possible words that can be placed into the grid. 4. A new word is taken from the list of possibilities and placed on the grid. This makes it so a number of possibilities are now invalid, and these are removed from the list. Steps 3 and 4 are repeated until the grid is as full as we want it to be. +Performance Considerations +--- + +On my consumer-grade machine (i7-6700HQ) the algorithm can generate a 20x20 grid with 50% completion. I am currently looking into ways of improving this mark, and already have a ton of ideas, so stay tuned! + Usage ---