Changed the multiprocessing implimentation
Each thread now handles a single word at a time instead of a whole list. This allows the workload to be more evenly distributed across threads and stay more parralel
This commit is contained in:
Binary file not shown.
@@ -51,6 +51,7 @@ def words_with_letter_positions(guess, dictionary):
|
|||||||
for letter in guess:
|
for letter in guess:
|
||||||
if letter != "_": match_amount += 1
|
if letter != "_": match_amount += 1
|
||||||
if match_amount == 0: return dictionary
|
if match_amount == 0: return dictionary
|
||||||
|
if len(dictionary) == 0: return dictionary
|
||||||
|
|
||||||
valid_words = []
|
valid_words = []
|
||||||
for word in dictionary:
|
for word in dictionary:
|
||||||
@@ -66,6 +67,27 @@ def words_with_letter_positions(guess, dictionary):
|
|||||||
continue
|
continue
|
||||||
return valid_words
|
return valid_words
|
||||||
|
|
||||||
|
''' this funcion takes a dictionary like this
|
||||||
|
dictionary = {
|
||||||
|
'a' : [False, False, False, False, False],
|
||||||
|
'b' : [False, True, False, False, False]
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
def words_letter_not_position(guess, dictionary):
|
||||||
|
if len(guess) == 0: return dictionary
|
||||||
|
valid_words = []
|
||||||
|
for word in dictionary:
|
||||||
|
match = 5
|
||||||
|
for letter in guess:
|
||||||
|
for i, value in enumerate(guess[letter]):
|
||||||
|
if word[i] == letter and value == True:
|
||||||
|
match -= 1
|
||||||
|
#print(word, word[i], letter, i, value, match)
|
||||||
|
if match == 5:
|
||||||
|
valid_words.append(word)
|
||||||
|
return valid_words
|
||||||
|
|
||||||
def get_dict_stats(dictionary):
|
def get_dict_stats(dictionary):
|
||||||
letter_occurrences = {'a' : 0,
|
letter_occurrences = {'a' : 0,
|
||||||
'b' : 0,'c' : 0,'d' : 0,'e' : 0,'f' : 0,
|
'b' : 0,'c' : 0,'d' : 0,'e' : 0,'f' : 0,
|
||||||
@@ -73,6 +95,7 @@ def get_dict_stats(dictionary):
|
|||||||
'l' : 0,'m' : 0,'n' : 0,'o' : 0,'p' : 0,
|
'l' : 0,'m' : 0,'n' : 0,'o' : 0,'p' : 0,
|
||||||
'q' : 0,'r' : 0,'s' : 0,'t' : 0,'u' : 0,
|
'q' : 0,'r' : 0,'s' : 0,'t' : 0,'u' : 0,
|
||||||
'v' : 0,'w' : 0,'x' : 0,'y' : 0,'z' : 0}
|
'v' : 0,'w' : 0,'x' : 0,'y' : 0,'z' : 0}
|
||||||
|
if len(dictionary) == 0: return letter_occurrences
|
||||||
letter_count = 0
|
letter_count = 0
|
||||||
for word in dictionary:
|
for word in dictionary:
|
||||||
word = word.strip()
|
word = word.strip()
|
||||||
@@ -98,20 +121,3 @@ def get_entropy(guess, dict_stats):
|
|||||||
def entropy_sort_list(dictionary, dict_stats):
|
def entropy_sort_list(dictionary, dict_stats):
|
||||||
# sort valid_words according to entropy
|
# sort valid_words according to entropy
|
||||||
return dictionary.sort(key=lambda word: get_entropy(word, dict_stats))
|
return dictionary.sort(key=lambda word: get_entropy(word, dict_stats))
|
||||||
|
|
||||||
'''
|
|
||||||
words = read_in_dict(dictionary)
|
|
||||||
dict_stats = get_dict_stats(words)
|
|
||||||
entropy_sort_list(words, dict_stats)
|
|
||||||
|
|
||||||
|
|
||||||
found_irate = False
|
|
||||||
i = 0
|
|
||||||
while not found_irate:
|
|
||||||
print(words[i], get_entropy(words[i], dict_stats))
|
|
||||||
if words[i] == "irate":
|
|
||||||
found_irate = True
|
|
||||||
i+=1
|
|
||||||
|
|
||||||
|
|
||||||
dictionary.close()'''
|
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import wordle_solver
|
import wordle_solver
|
||||||
import multiprocessing as mul
|
import multiprocessing as mul
|
||||||
|
|
||||||
def wordle_guess(not_letters, has_letters, position_letters):
|
def wordle_guess(not_letters, has_letters, position_letters, not_position_letters):
|
||||||
input_file_path = 'cleaned_words.txt'
|
input_file_path = 'cleaned_words.txt'
|
||||||
dictionary = open(input_file_path, 'r')
|
dictionary = open(input_file_path, 'r')
|
||||||
guesses = wordle_solver.read_in_dict(dictionary)
|
guesses = wordle_solver.read_in_dict(dictionary)
|
||||||
guesses = wordle_solver.words_without_letters(not_letters, guesses)
|
guesses = wordle_solver.words_without_letters(not_letters, guesses)
|
||||||
guesses = wordle_solver.words_with_letters(has_letters, guesses)
|
guesses = wordle_solver.words_with_letters(has_letters, guesses)
|
||||||
guesses = wordle_solver.words_with_letter_positions(position_letters, guesses)
|
guesses = wordle_solver.words_with_letter_positions(position_letters, guesses)
|
||||||
|
guesses = wordle_solver.words_letter_not_position(not_position_letters, guesses)
|
||||||
|
|
||||||
dict_stats = wordle_solver.get_dict_stats(guesses)
|
dict_stats = wordle_solver.get_dict_stats(guesses)
|
||||||
wordle_solver.entropy_sort_list(guesses, dict_stats)
|
wordle_solver.entropy_sort_list(guesses, dict_stats)
|
||||||
@@ -15,7 +16,7 @@ def wordle_guess(not_letters, has_letters, position_letters):
|
|||||||
|
|
||||||
return guesses
|
return guesses
|
||||||
|
|
||||||
def check_guess(guess, word, not_letters, has_letters, position_letters):
|
def check_guess(guess, word, not_letters, has_letters, position_letters, not_position_letters):
|
||||||
|
|
||||||
for i, letter in enumerate(guess):
|
for i, letter in enumerate(guess):
|
||||||
if letter in word and letter not in has_letters:
|
if letter in word and letter not in has_letters:
|
||||||
@@ -26,20 +27,25 @@ def check_guess(guess, word, not_letters, has_letters, position_letters):
|
|||||||
if letter == word[i]:
|
if letter == word[i]:
|
||||||
position_letters = position_letters[:i] + letter + position_letters[i+1:]
|
position_letters = position_letters[:i] + letter + position_letters[i+1:]
|
||||||
|
|
||||||
return not_letters, has_letters, position_letters
|
if letter in word and word[i] != letter:
|
||||||
|
if letter not in not_position_letters:
|
||||||
|
new_letter_list = [False] * 5
|
||||||
|
new_letter_list[i] = True
|
||||||
|
not_position_letters[letter] = new_letter_list
|
||||||
|
else:
|
||||||
|
not_position_letters[letter][i] = True
|
||||||
|
|
||||||
def get_word_stats(letter):
|
|
||||||
input_file_path = 'cleaned_words.txt'
|
|
||||||
dictionary = open(input_file_path, 'r')
|
|
||||||
words = wordle_solver.read_in_dict(dictionary)
|
|
||||||
dictionary.close()
|
|
||||||
words = wordle_solver.words_with_letter_positions("{}____".format(letter), words)
|
|
||||||
|
|
||||||
|
return not_letters, has_letters, position_letters, not_position_letters
|
||||||
|
|
||||||
|
def get_word_stats(word):
|
||||||
|
words =[word]
|
||||||
|
total_num_guesses = 0
|
||||||
not_letters = ""
|
not_letters = ""
|
||||||
has_letters = ""
|
has_letters = ""
|
||||||
position_letters = "_____"
|
position_letters = "_____"
|
||||||
|
not_position_letters = {}
|
||||||
guesses = []
|
guesses = []
|
||||||
total_num_guesses = 0
|
|
||||||
total_words = 0
|
total_words = 0
|
||||||
percentage = 0
|
percentage = 0
|
||||||
last_percentage = 0
|
last_percentage = 0
|
||||||
@@ -47,16 +53,20 @@ def get_word_stats(letter):
|
|||||||
for total_words, word in enumerate(words):
|
for total_words, word in enumerate(words):
|
||||||
for x in range(20):
|
for x in range(20):
|
||||||
total_num_guesses += 1
|
total_num_guesses += 1
|
||||||
guess = wordle_guess(not_letters, has_letters, position_letters)
|
guess = wordle_guess(not_letters, has_letters, position_letters, not_position_letters)
|
||||||
|
if len(guess) == 0:
|
||||||
|
print("No more guesses")
|
||||||
|
exit()
|
||||||
new_guess = guess[0]
|
new_guess = guess[0]
|
||||||
i = 0
|
i = 0
|
||||||
while new_guess in guesses:
|
while new_guess in guesses:
|
||||||
new_guess = guess[i]
|
new_guess = guess[i]
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
not_letters, has_letters, position_letters = check_guess(new_guess, word, not_letters, has_letters, position_letters)
|
not_letters, has_letters, position_letters, not_position_letters = \
|
||||||
|
check_guess(new_guess, word, not_letters, has_letters, position_letters, not_position_letters)
|
||||||
|
|
||||||
guesses.append(new_guess)
|
guesses.append(new_guess)
|
||||||
#print(new_guess, not_letters, has_letters, position_letters)
|
|
||||||
if position_letters == word:
|
if position_letters == word:
|
||||||
percentage = round(total_words*100/len(words), 0)
|
percentage = round(total_words*100/len(words), 0)
|
||||||
if percentage - last_percentage >= 5:
|
if percentage - last_percentage >= 5:
|
||||||
@@ -66,14 +76,13 @@ def get_word_stats(letter):
|
|||||||
has_letters = ""
|
has_letters = ""
|
||||||
position_letters = "_____"
|
position_letters = "_____"
|
||||||
guesses = []
|
guesses = []
|
||||||
|
not_position_letters = {}
|
||||||
break
|
break
|
||||||
|
return total_num_guesses
|
||||||
print("The average number of guesses per {} word is ".format(word[0]) + str(total_num_guesses/total_words))
|
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
alphabet = list("abcdefghijklmnopqrstuvwxyz")
|
alphabet = list("ab")
|
||||||
|
|
||||||
input_file_path = 'cleaned_words.txt'
|
input_file_path = 'cleaned_words.txt'
|
||||||
dictionary = open(input_file_path, 'r')
|
dictionary = open(input_file_path, 'r')
|
||||||
words = wordle_solver.read_in_dict(dictionary)
|
words = wordle_solver.read_in_dict(dictionary)
|
||||||
@@ -81,6 +90,21 @@ if __name__ == "__main__":
|
|||||||
timer = time()
|
timer = time()
|
||||||
|
|
||||||
testers = mul.Pool()
|
testers = mul.Pool()
|
||||||
testers.map(get_word_stats, alphabet)
|
total_guesses = 0
|
||||||
testers.close()
|
results = []
|
||||||
|
|
||||||
|
for i, word in enumerate(words):
|
||||||
|
results.append(testers.apply_async(get_word_stats, (word,)))
|
||||||
|
if i % 100 == 0:
|
||||||
|
print(i, "words tested")
|
||||||
|
print("Recovering Results")
|
||||||
|
for i, result in enumerate(results):
|
||||||
|
try:
|
||||||
|
total_guesses += result.get(timeout=1)
|
||||||
|
if i % 100 == 0:
|
||||||
|
print(i, "results recovered. Current average:", total_guesses/(i+1))
|
||||||
|
except mul.TimeoutError:
|
||||||
|
print("Timed out")
|
||||||
|
|
||||||
print("Time taken:", time() - timer)
|
print("Time taken:", time() - timer)
|
||||||
|
print("The average number of guesses per {} word is " + str(total_guesses/10))
|
||||||
Reference in New Issue
Block a user