-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation.py
152 lines (124 loc) · 5.06 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import random
import os
import csv
from itertools import product
from pytest import fail
from tqdm import tqdm
from operator import itemgetter
from generate_entropy import *
from wordle import *
def generate_guess(previous_board, previous_guess, wordlist, turn, total_words, combs, guess, second_guess_scores, data):
if turn == 0:
return guess, wordlist
if turn == 1:
board_name = "".join([str(int) for int in list(previous_board)])
guess_list = second_guess_scores[board_name]
filtered_words = {guess["word"]: guess["word"] for guess in guess_list}
return max(guess_list, key=itemgetter('score'))["word"], filtered_words
else:
filtered_words = filter_words(previous_board, previous_guess, wordlist)
next_guess = {}
for word in filtered_words:
entropy = get_entropy(word, filtered_words, combs, total_words)
next_guess[word] = calculate_score(entropy, data[word])
filtered_words = {word: word for word in filtered_words}
sorted_next_guess = list(dict(
sorted(next_guess.items(), key=lambda item: item[1], reverse=True)).keys())[0]
return sorted_next_guess, filtered_words
def generate_guess_matrix(previous_board, previous_guess, turn, guesses, words_ordered, match_matrix, comb_map, total_words, guess, second_guess_scores, data):
if turn == 0:
return guess
if turn == 1:
board_name = "".join([str(int) for int in list(previous_board)])
guess_list = second_guess_scores[board_name]
return guess_list["word"]
else:
guess_index = words_ordered.index(previous_guess)
guess_combs = match_matrix[guess_index]
board_name = "".join([str(int) for int in list(previous_board)])
comb_number = comb_map[board_name]
indices = np.where(guess_combs == comb_number)
# print(board_name, np.array(words)[indices])
score_list = []
if indices[0].size != 0:
for i, row in enumerate(match_matrix[indices]):
word_matches = row[indices]
freq_map = dict(collections.Counter(word_matches))
entropy = get_entropy_from_map(freq_map, total_words)
score_list.append({
"word": words_ordered[int(indices[0][i])],
"score": calculate_score(entropy, data[words_ordered[int(indices[0][i])]])
})
sorted_guess = sorted(score_list, reverse=True, key=lambda d: d['score'])
for i in range(len(score_list)):
final_guess = sorted_guess[i]['word']
if final_guess not in guesses:
break
return final_guess
# word = random.choice(words)
# board = [0, 0, 0, 0, 0]
# print("Answer: ", word)
# guess = list(first_guess_list.keys())[0]
# for turn in range(6):
# previous_guess = guess
# guess = generate_guess(board, previous_guess, wordlist, turn)
# print(guess)
# board = check_guess(word, guess)
# print_board(board, outcomes)
# if board == [2, 2, 2, 2, 2]:
# print("Completed in ", turn + 1,"/ 6")
# break
def run_simulation(outcomes, wordlist, words, first_guess, second_guess_scores, total_words, combs, data, extended=False):
# comb_map = {"".join([str(int) for int in list(comb)]): i for i, comb in enumerate(combs)}
# match_matrix = np.load(os.path.join('datasets', 'match_matrix.npy'))
# words_ordered = [word for word in wordlist]
# words_ordered.sort()
score_total = 0
failed_games = 0
total_games = len(words)
# total_games = 10
record = {}
record["games"] = {}
if extended:
turns = 30
else:
turns = 6
for index, word in enumerate(tqdm(words)):
board = [0, 0, 0, 0, 0]
guess = first_guess
score = 1
filtered_wordlist = wordlist
game = {}
game["answer"] = word
game["guesses"] = []
game["share"] = "Wordle " + str(index) + " {}/6*\n\n"
boards = ""
for turn in range(turns):
previous_guess = guess
guess, filtered_wordlist = generate_guess(
board, previous_guess, filtered_wordlist, turn, total_words, combs, guess, second_guess_scores, data)
board = check_guess(word, guess)
game["guesses"].append(guess)
boards += print_board(board, outcomes)+"\n"
if board == [2, 2, 2, 2, 2]:
break
score += 1
if turn == turns-1:
score = 0
if score == 0:
failed_games += 1
game["share"] = game["share"].format("X")
game["score"] = "X"
else:
game["share"] = game["share"].format(score)
game["score"] = score
game["share"] += boards
record["games"][index] = game
score_total += score
# print(score_total/(index +1 -failed_games))
record["stats"] = {
"total": total_games,
"failed": failed_games,
"average": score_total/(total_games-failed_games)
}
return record