Increase default timeout, add parameter for desired occupancy. Update readme.
This commit is contained in:
parent
8f249c641d
commit
62a72706e2
2 changed files with 10 additions and 3 deletions
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
---
|
||||
|
||||
|
|
Loading…
Reference in a new issue