-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_fill.py
230 lines (193 loc) · 9.56 KB
/
cache_fill.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
"""
Force generate cache for all game contexts
"""
import time
import wordle_solver
import wordle_contexts
from wordle_utils import duration_fmt
def main():
# Go through all game contexts
mp = True
for context in wordle_contexts.get_all_contexts():
print(f"Building cache for {'naive' if context.naive else 'smart'} {context.name} Length {context.word_length}")
# Setup solver
guess_group = context.get_guess_group()
solution_group = context.get_solution_group()
# Get initial guesses
rank, guesses, foils = wordle_solver.best_guesses(guess_group, solution_group, mp = mp)
for guess in guesses:
unsaved_results = 0
max_unsaved_results = 10
cache_results = set()
try:
for result, new_solution_group in solution_group.partition(guess, sort = True):
# Find result for guess
context.next_turn(guess, result)
if len(new_solution_group) <= 2:
# Result too simple to cache
assert len(new_solution_group) > 0
if len(new_solution_group) == 1:
msg = "No cache (Only one solution)"
else:
msg = "No cache (Only two solutions)"
else:
cache_results.add(result)
# Check if value is already cached
r, g, f = context.load_guesses()
if r is not None:
# Show message
msg = "Cached"
else:
# Do not use the cache, override and save manually
r, g, f = wordle_solver.best_guesses(guess_group.copy(),
new_solution_group, progress = None, mp = mp, cache = False)
msg = "Added"
unsaved_results += 1
# Save without flushing to disk
context._save_guesses_internal(r, g, f)
if unsaved_results >= max_unsaved_results:
# Save to disk
context._save_guess_data()
unsaved_results = 0
context.reset()
print(f"Cache for {guess!r} ({result}): {msg}")
finally:
# Make sure all words are saved
if unsaved_results:
context._save_guess_data()
# Verify the number of results in the cache
try:
real_cache_results = set(context._cache_data[guess]["next_turn"])
except KeyError:
# No results in cache, despite filling it
# Means the guess must be very good
continue
if cache_results != real_cache_results:
print(
f"Cache for {guess!r}: {len(real_cache_results)} results "
f"in cache, but {len(cache_results)} results were calculated")
for result in sorted(cache_results.difference(real_cache_results),
key = wordle_solver._result_key):
print(f"Cache for {guess!r}: result {result} was calculated but not in cache")
for result in sorted(real_cache_results.difference(cache_results),
key = wordle_solver._result_key):
# Check if the result is possible
if wordle_solver.is_result_possible(guess, result, context):
print(f"Cache for {guess!r}: result {result} is in "
f"cache but was not calculated (but possible)")
else:
print(f"Cache for {guess!r}: result {result} is in cache but not possible")
# del context._cache_data[guess]["next_turn"][result]
# context._save_guess_data()
exit(1)
else:
print(f"Cache for {guess!r}: {len(cache_results)} results")
import os
import concurrent.futures
def main_mp(mp = True):
# Go through all game contexts
if mp is True:
mp = os.cpu_count()
max_unsaved_jobs = mp * 8
for context in wordle_contexts.get_all_contexts():
# TODO: implement a better solution than hard coding id
if context == "flappy_birdle": continue
print(f"Building cache for {'naive' if context.naive else 'smart'} {context.name} Length {context.word_length}")
# Setup solver
guess_group = context.get_guess_group()
solution_group = context.get_solution_group()
# Get initial guesses
rank, guesses, foils = wordle_solver.best_guesses(guess_group, solution_group, mp = mp)
for guess in guesses:
with concurrent.futures.ProcessPoolExecutor(mp) as executor:
fs = []
rs = []
cache_results = set()
for result, new_solution_group in solution_group.partition(guess, sort = True):
# Find result for guess
context.next_turn(guess, result)
if len(new_solution_group) <= 2:
# Result too simple to cache
assert len(new_solution_group) > 0
if len(new_solution_group) == 1:
msg = "Only one solution"
else:
msg = "Only two solutions"
# Value should not be cached
print(f"Cache for {guess!r} ({result}): No cache ({msg})")
context.reset()
continue
cache_results.add(result)
# Check if value is already cached
r, g, f = context.load_guesses()
context.reset()
if r is not None:
# Show message
print(f"Cache for {guess!r} ({result}): Cached")
else:
# Calculate, but don't wait for result
# Set cache to fails, to writing to cache can be done in bulk
future = executor.submit(wordle_solver.best_guesses,
guess_group.copy(), new_solution_group, progress = False, mp = False, cache = False)
fs.append(future)
rs.append(result)
if len(fs) >= max_unsaved_jobs:
# Get results once completed
for future, result in zip(fs, rs):
r, g, f = future.result()
context.next_turn(guess, result)
context._save_guesses_internal(r, g, f)
context.reset()
# Show message
print(f"Cache for {guess!r} ({result}): Added")
# Force save
context._save_guess_data()
# Reset futures
fs = []
rs = []
# Wait for all to complete
for future, result in zip(fs, rs):
r, g, f = future.result()
context.next_turn(guess, result)
context._save_guesses_internal(r, g, f)
context.reset()
# Show message
print(f"Cache for {guess!r} ({result}): Added")
# Force save
context._save_guess_data()
# Verify the number of results in the cache
try:
real_cache_results = set(context._cache_data[guess]["next_turn"])
except KeyError:
# No results in cache, despite filling it
# Means there are no previous cache entries for this guess
continue
if cache_results != real_cache_results:
print(
f"Cache for {guess!r}: {len(real_cache_results)} results "
f"in cache, but {len(cache_results)} results were calculated")
for result in sorted(cache_results.difference(real_cache_results),
key = wordle_solver._result_key):
print(f"Cache for {guess!r}: result {result} was calculated but not in cache")
for result in sorted(real_cache_results.difference(cache_results),
key = wordle_solver._result_key):
# Check if the result is possible
if wordle_solver.is_result_possible(guess, result, context):
print(
f"Cache for {guess!r}: result {result} is "
"in cache but was not calculated (but possible)")
else:
print(
f"Cache for {guess!r}: result {result} is "
"in cache but not possible")
# del context._cache_data[guess]["next_turn"][result]
# context._save_guess_data()
exit(1)
if __name__ == "__main__":
start = time.time()
try:
# main()
main_mp()
finally:
stop = time.time()
print(f"\nBuilt word cache in {duration_fmt(stop - start)}")