-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolvers.py
316 lines (260 loc) · 10.8 KB
/
solvers.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import numpy as np
from time import time
import itertools as itt
import pickle
from compiler_options import compiler_decorator, prange, generated_jit
def word_checker(test_word, true_word):
test_word = remove_accents(np.array((test_word,))).view(np.int32)
true_word = remove_accents(np.array((true_word,))).view(np.int32)
return word_checker_inner(test_word, true_word)
@compiler_decorator()
def word_checker_inner(test_word, true_word):
test_checked = test_word == true_word
test = 2*test_checked
true_checked = test_checked.copy()
for i in range(5):
if not test_checked[i]:
for j in range(5):
if not true_checked[j] and test_word[i]==true_word[j]:
test[i] = 1
true_checked[j] = True
return test
class KeyDict(dict):
def __missing__(self, key):
return key
def remove_accents(words):
accent_dictionary = KeyDict(
á='a', â='a', ã='a', ç='c', è='e', é='e',
ê='e', í='i', ï='i', ó='o', ô='o', õ='o',
ú='u', û='u', ü='u'
)
letters, indices = np.unique(words.view('U1'), return_inverse=True)
letters_na = np.array([accent_dictionary[k] for k in letters])
return letters_na[indices].view('U5')
class WordleSolver():
def __init__(self, words, n_bestwords):
self.n_bestwords = n_bestwords
self.words = words
self.n_words = len(self.words)
def start(self, n_words):
# Start the algorithm
# n_words: number of words in challenge
# Examples 1: termooo, 2: dueto, 4: quarteto
# Returns your favorite starting word
raise NotImplementedError
def guess(self, answer_2_previous):
# Gets the answer from the previous guess
# Provides a new guess
raise NotImplementedError
def word_table(words, words_before):
return word_table_inner(remove_accents(words).view(int).reshape(-1, 5), words_before)
@compiler_decorator(parallel=True)
def word_table_inner(lines, words_before):
nw = lines.shape[0]
table = np.zeros((nw, words_before), dtype=np.ubyte)
for i in range(nw):
for j in range(words_before):
v = word_checker_inner(lines[i], lines[j])
s = v[0]
for k in range(1, 5):
s = 3 * s + v[k]
table[i, j] = s
return table
@compiler_decorator(parallel=True)
def word_buckets(table, subset=None):
nw, nw2 = table.shape
word_buckets = np.empty((nw,243))
if subset is None:
for i in range(nw):
word_buckets[i, :] = np.bincount(table[i, :], minlength=243)
else:
for i in range(nw):
v = table[i, :]
word_buckets[i, :] = np.bincount(v[subset], minlength=243)
return word_buckets
@compiler_decorator(parallel=True)
def calculate_entropy(word_buckets_scaled):
nw = word_buckets_scaled.shape[0]
word_entropy = np.empty(nw)
for i in prange(nw):
v = word_buckets_scaled[i, :]
v = v[v>0]
word_entropy[i] = -np.dot(v, np.log(v))
return word_entropy
@compiler_decorator(parallel=True)
def max_row(M):
a = M.shape[0]
res = np.zeros(a, dtype=M.dtype)
for i in prange(a):
res[i] = M[i].max()
return res
class EntropySolver(WordleSolver):
def __init__(self, words, n_bestwords):
super(EntropySolver, self).__init__(words, n_bestwords)
self.table = word_table(words, n_bestwords)
#cb = np.full((self.n_bestwords,), True)
buckets = word_buckets(self.table)
self.first_word_ind = calculate_entropy(buckets / self.n_bestwords).argmax()
self.first_word = self.words[self.first_word_ind]
def start(self, n_words=1):
self.cb = np.full((n_words, self.n_bestwords), True)
self.still = np.full((n_words,), True)
self.last_guess_ind = self.first_word_ind
return self.first_word
def guess(self, answer_2_previous):
answer_short = np.matmul(answer_2_previous, 3**np.arange(5)[::-1])
self.still[answer_short == 242] = False
for j in np.nonzero(self.still)[0]:
self.cb[j][self.cb[j]] = (self.table[self.last_guess_ind, self.cb[j]] == answer_short[j])
scb = np.count_nonzero(self.cb, axis=1)
found_word = (scb==1) & self.still
if np.any(found_word):
j = np.nonzero(found_word)[0][0]
guess_ind = np.nonzero(self.cb[j])[0][0]
else:
entropy = np.zeros((self.n_words))
for j in np.nonzero(self.still)[0]:
buckets = word_buckets(self.table, self.cb[j])
entropy += calculate_entropy(buckets / scb[j])
entropy[:self.n_bestwords][self.cb[j]] += (np.log(scb[j]) / scb[j])
guess_ind = entropy.argmax()
self.last_guess_ind = guess_ind
return self.words[guess_ind]
class MinMaxSolver(WordleSolver):
def __init__(self, words, n_bestwords):
super(MinMaxSolver, self).__init__(words, n_bestwords)
self.table = word_table(words, n_bestwords)
#cb = np.full((self.n_bestwords,), True)
buckets = word_buckets(self.table)
self.first_word_ind = np.max(buckets, axis=1).argmin()
self.first_word = self.words[self.first_word_ind]
def start(self, n_words=1):
self.cb = np.full((n_words, self.n_bestwords), True)
self.still = np.full((n_words,), True)
self.last_guess_ind = self.first_word_ind
return self.first_word
def guess(self, answer_2_previous):
answer_short = np.matmul(answer_2_previous, 3**np.arange(5)[::-1])
self.still[answer_short == 242] = False
for j in np.nonzero(self.still)[0]:
self.cb[j][self.cb[j]] = (self.table[self.last_guess_ind, self.cb[j]] == answer_short[j])
scb = np.count_nonzero(self.cb, axis=1)
found_word = (scb==1) & self.still
if np.any(found_word):
j = np.nonzero(found_word)[0][0]
guess_ind = np.nonzero(self.cb[j])[0][0]
else:
log_bag = np.zeros((self.n_words))
for j in np.nonzero(self.still)[0]:
buckets = word_buckets(self.table, self.cb[j])
log_bag += -np.log(np.max(buckets, axis=1))
log_bag[:self.n_bestwords][self.cb[j]] += (np.log(scb[j]) / scb[j])
guess_ind = log_bag.argmax()
self.last_guess_ind = guess_ind
return self.words[guess_ind]
class SolutionMatrixSolver(WordleSolver):
def __init__(self, words, n_bestwords, solution_matrix=None):
super().__init__(words, n_bestwords)
self.table = word_table(words, n_bestwords)
if type(solution_matrix) is np.ndarray:
self.solution_matrix = solution_matrix
elif type(solution_matrix) is str:
with open('solvers/' + solution_matrix + '.sm', 'rb') as f:
self.solution_matrix = pickle.load(f)
elif solution_matrix is None:
raise TypeError('SolutionMatrixSolver() missing required argument \'solution_matrix\' (pos 2)')
else:
raise NotImplementedError
self.first_word_ind = self.solution_matrix[0,0]
self.first_word = self.words[self.first_word_ind]
def start(self, n_words=1):
if n_words > 1:
raise NotImplementedError
self.cb = np.full(self.n_bestwords, True)
self.last_guess_ind = self.first_word_ind
self.level = 0
return self.first_word
def guess(self, answer_2_previous):
self.level += 1
answer_short = np.matmul(answer_2_previous, 3**np.arange(5)[::-1])
self.cb[self.cb] = self.table[self.last_guess_ind, self.cb] == answer_short
j = np.nonzero(self.cb)[0][0]
guess_ind = self.solution_matrix[j, self.level]
self.last_guess_ind = guess_ind
return self.words[guess_ind]
@compiler_decorator()
def get_minexpt_order_entropy(table, col_subset, row_subset):
nw, nwb = table.shape
buckets = word_buckets(table[:, col_subset][row_subset])
bmorethanone = np.count_nonzero(buckets, axis=1) > 1
row_subset[row_subset] = bmorethanone
buckets = buckets[bmorethanone]
scb = np.count_nonzero(col_subset)
entropy = np.zeros(nw)
entropy[row_subset] = -calculate_entropy(buckets / scb)
entropy[:nwb][col_subset] -= np.log(scb) / scb
ind_ord = entropy.argsort()
return ind_ord
@compiler_decorator()
def min_expected_tries(table, max_words=np.zeros(10), E_max = 10000,
row_subset=np.full(0, True), col_subset = np.full(0, True),
level=0, solution_matrix = np.zeros((0,0), dtype=np.int64),
print_level = 1):
nw, nwb = table.shape
if row_subset.shape[0] == 0:
row_subset = np.full(nw, True)
if col_subset.shape[0] == 0:
col_subset = np.full(nwb, True)
row_subset = np.copy(row_subset)
if solution_matrix.shape[0]==0:
solution_matrix = np.full((nwb, 10), -1)
solution_matrix_c = solution_matrix.copy()
scb = np.count_nonzero(col_subset)
E = E_max
ind_ord = get_minexpt_order_entropy(table, col_subset, row_subset)
max_words_ = np.count_nonzero(row_subset)
if max_words[0] > 0:
max_words_ = min(max_words_, max_words[0])
for i_ in range(max_words_):
if E <= 2 * scb - 2:
return E, solution_matrix
elif (E <= 2 * scb) and i_ > 0:
return E, solution_matrix
i = ind_ord[i_]
E_try = scb
#tics = table[i][col_subset]
nzss = np.zeros(243, dtype=np.int64)
subsets = np.full((243, nwb), False)
nzssgt0 = 0
for k in np.nonzero(col_subset)[0]:
j = table[i, k]
subsets[j, k] = True
nzss[j] += 1
if nzss[j] == 1:
nzssgt0 += 1
at_least = 2*scb - nzssgt0 - nzss[242]
for j in range(242):
if nzss[j] == 0:
continue
at_least -= 2 * nzss[j] - 1
if nzss[j] <= 2:
E_try += 2 * nzss[j] - 1
inds = np.nonzero(subsets[j])[0]
solution_matrix_c[inds[0], level + 1] = inds[0]
if nzss[j] == 2:
solution_matrix_c[inds[1], level + 1] = inds[0]
solution_matrix_c[inds[1], level + 2] = inds[1]
else:
E_try += min_expected_tries(table, max_words[1:], E - E_try - at_least,
row_subset, subsets[j], level + 1, solution_matrix_c)[0]
if E_try + at_least >= E:
E_try = E
break
if E_try < E:
E = E_try
for j in col_subset.nonzero()[0]:
solution_matrix[j, level+1:] = solution_matrix_c[j, level+1:]
solution_matrix[j, level] = i
if level <= print_level:
print(level, i_, scb, E, i)
return E, solution_matrix