Skip to content

Commit 54ab400

Browse files
committed
Add window refresher to avoid "game is not responding" on Linux when the algo is running for too long before picking a move
1 parent 88f5722 commit 54ab400

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

squadro/agents/alphabeta_agent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from squadro.agents.agent import Agent
55
from squadro.algorithms import minimax
66
from squadro.state.state import State, get_next_state
7+
from squadro.tools.animation import PygameRefresher
78
from squadro.tools.constants import DefaultParams
89
from squadro.tools.evaluation import evaluate_advancement
910
from squadro.tools.logs import alpha_beta_logger as logger
@@ -120,7 +121,7 @@ class AlphaBetaRelativeAdvancementAgent(AdvancementAgent):
120121
(best move so far).
121122
"""
122123

123-
def __init__(self, max_depth=15, **kwargs):
124+
def __init__(self, max_depth=20, **kwargs):
124125
# use fixed time for now
125126
kwargs.setdefault('max_time_per_move', DefaultParams.max_time_per_move)
126127
super().__init__(**kwargs)
@@ -140,6 +141,8 @@ def get_action(self, state, last_action=None, time_left=None):
140141
self.depth = 0
141142
self.start_time = time()
142143

144+
pygame_refresher = PygameRefresher()
145+
143146
# if self.total_time is None:
144147
# self.total_time = time_left
145148
# if time_left / self.total_time > 0.2:
@@ -153,6 +156,7 @@ def get_action(self, state, last_action=None, time_left=None):
153156
minimax_action = minimax.search(state, self)
154157
if minimax_action is None:
155158
raise ValueError('No best move found, check cutoff function')
159+
pygame_refresher.refresh()
156160
if time() - self.start_time < self.max_time_per_move:
157161
# Only keep the minimax action computed for the deepest depth if it got time to
158162
# explore all the leaf nodes at that depth

squadro/agents/montecarlo_agent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from squadro.state.evaluators.rl import QLearningEvaluator, DeepQLearningEvaluator
2424
from squadro.state.evaluators.rollout import RolloutEvaluator
2525
from squadro.state.state import State, get_next_state
26+
from squadro.tools.animation import PygameRefresher
2627
from squadro.tools.constants import DefaultParams, inf, EPS
2728
from squadro.tools.evaluation import evaluate_advancement
2829
from squadro.tools.logs import monte_carlo_logger as logger
@@ -266,13 +267,15 @@ def simulate(self) -> None:
266267
def get_action(self):
267268
Debug.clear(self.root)
268269
start_time = time()
270+
pygame_refresher = PygameRefresher()
269271
n = 0
270272

271273
# Needs at least two simulations to give backfill value to one root edge
272274
while (time() - start_time < self.max_time or n <= 1) and n < self.max_steps:
273275
logger.debug(f'\nSIMULATION {n}')
274276
self.simulate()
275277
n += 1
278+
pygame_refresher.refresh()
276279

277280
logger.info(f'{n} simulations performed.\n'
278281
f'Root edges:\n{self.root.get_edge_stats(to_string=True)}')

squadro/tools/animation.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from time import time
2+
3+
import pygame
4+
5+
from squadro.tools.constants import PYGAME_REFRESH_SECONDS
6+
7+
8+
def refresh_pygame_window():
9+
pygame.event.pump()
10+
11+
12+
class PygameRefresher:
13+
def __init__(self):
14+
self.pygame_refresh_time = time()
15+
self.is_pygame_init = pygame.get_init()
16+
17+
def refresh(self):
18+
if self.is_pygame_init and time() - self.pygame_refresh_time > PYGAME_REFRESH_SECONDS:
19+
refresh_pygame_window()
20+
self.pygame_refresh_time = time()

squadro/tools/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ def update(cls, **kwargs):
5858
DQL_PATH = DATA_PATH / "deep_q_learning"
5959
MAX_INT = sys.maxsize
6060
inf = float("inf")
61+
PYGAME_REFRESH_SECONDS = 3

0 commit comments

Comments
 (0)