Commit 213ebb76 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Optimize strategy implementation:

- Instead of computing a complete score for each state, compute+compare
  the first component for each one, then the second, etc.
- Set colskip (and nrows) once and update occasionally once instead of
  constantly recomputing it.
- Add some more actions to do on the destination column.
- Fix move loop detection.
- Do not wait on bomb explosion (since they no longer cause loops).
parent a4129c62
...@@ -4,9 +4,10 @@ import time ...@@ -4,9 +4,10 @@ import time
from collections import deque from collections import deque
from itertools import count from itertools import count
from Xlib import error from Xlib import error
from strategy import State from detection import NOBLOCK
from interaction import get_exapunks_window, focus_window, screenshot_board, \ from interaction import get_exapunks_window, focus_window, screenshot_board, \
press_keys, listen_keys, KEY_DELAY press_keys, listen_keys, KEY_DELAY
from strategy import State
MAX_SPEED_ROWS = 3 MAX_SPEED_ROWS = 3
...@@ -33,7 +34,7 @@ if __name__ == '__main__': ...@@ -33,7 +34,7 @@ if __name__ == '__main__':
listen_keys({'s': lambda: save_screenshot(win)}) listen_keys({'s': lambda: save_screenshot(win)})
solutions = deque([], maxlen=3) buf = deque([], maxlen=3)
def vprint(*args, **kwargs): def vprint(*args, **kwargs):
if verbose: if verbose:
...@@ -56,10 +57,10 @@ if __name__ == '__main__': ...@@ -56,10 +57,10 @@ if __name__ == '__main__':
vprint() vprint()
start = time.time() start = time.time()
solution = state.solve() newstate = state.solve()
end = time.time() end = time.time()
vprint('thought for', round((end - start) * 1000, 1), 'ms') vprint('thought for', round((end - start) * 1000, 1), 'ms')
except (TypeError, AssertionError): except (TypeError, AssertionError) as e:
vprint('\rerror during parsing, wait for a bit...', end='') vprint('\rerror during parsing, wait for a bit...', end='')
time.sleep(0.050) time.sleep(0.050)
continue continue
...@@ -68,33 +69,33 @@ if __name__ == '__main__': ...@@ -68,33 +69,33 @@ if __name__ == '__main__':
time.sleep(0.500) time.sleep(0.500)
continue continue
if len(solutions) == 3 and solution.loops(solutions.popleft()): if state.held == NOBLOCK and any(map(newstate.loops, buf)):
vprint('\rloop detected, wait for a bit...', end='') vprint('\rloop detected, wait for a bit...', end='')
time.sleep(0.03) time.sleep(0.03)
elif solution.moves: elif newstate.moves:
vprint('moves:', solution.keys()) vprint('moves:', newstate.keys())
vprint(' score:', solution.score) vprint(' score:', newstate.score)
if solutions: if buf:
vprint('prev score:', solutions[-1].score) vprint('prev score:', buf[-1].score)
vprint() vprint()
vprint('target after moves:') vprint('target after moves:')
vprint_state(solution.newstate) vprint_state(newstate)
press_keys(win, solution.keys()) press_keys(win, newstate.keys())
#keys_delay = len(solution.moves) * 2 * KEY_DELAY #keys_delay = len(newstate.moves) * 2 * KEY_DELAY
#moves_delay = max(0, solution.delay() - keys_delay) #moves_delay = max(0, newstate.delay() - keys_delay)
#vprint('wait for', moves_delay, 'ms') #vprint('wait for', moves_delay, 'ms')
#time.sleep(moves_delay / 1000) #time.sleep(moves_delay / 1000)
time.sleep(0.070) time.sleep(0.075)
elif state.nrows() - 2 <= MAX_SPEED_ROWS: elif state.nrows - 2 <= MAX_SPEED_ROWS:
vprint('no moves, speed up') vprint('no moves, speed up')
press_keys(win, 'l') press_keys(win, 'l')
time.sleep(0.030) time.sleep(0.030)
else: else:
vprint('no moves') vprint('no moves')
solutions.append(solution) buf.append(newstate)
except KeyboardInterrupt: except KeyboardInterrupt:
print('interrupted, quitting') print('interrupted, quitting')
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment