Skip to content

Commit

Permalink
combine fake_think_time with time already consumed (#796)
Browse files Browse the repository at this point in the history
* combine fake_think_time, rate_limiting_delay with time already consumed

There are 3 different delays:
1/ time spend by the engine
2/ rate_limiting_delay
3/ fake_think_time formula

The problem is each of these is unaware of the other 2, and we just add them all up. This is a
recipie for disaster. Instead we want to spend the max.

* Revert "combine fake_think_time, rate_limiting_delay with time already consumed"

This reverts commit 93851ca.

* do not add fake_think_time to what the engine has spent

Also, heed move_overhear in fake_think_time formula.

* rename fake_thinking() to fake_think_time()
better name, as the function does nothing, just returns the sleep value.

* fixes
- capitalize
- no need to cap sleep at remaining, it is smaller by construction
  • Loading branch information
lucasart authored Aug 9, 2023
1 parent e38ff63 commit 4719944
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def play_move(self,
can_ponder: bool,
is_correspondence: bool,
correspondence_move_time: int,
engine_cfg: config.Configuration) -> None:
engine_cfg: config.Configuration,
min_time: float) -> None:
"""
Play a move.
Expand All @@ -120,6 +121,7 @@ def play_move(self,
:param is_correspondence: Whether this is a correspondence or unlimited game.
:param correspondence_move_time: The time the engine will think if `is_correspondence` is true.
:param engine_cfg: Options for external moves (e.g. from an opening book), and for engine resignation and draw offers.
:param min_time: Minimum time to spend, in seconds.
:return: The move to play.
"""
polyglot_cfg = engine_cfg.polyglot
Expand Down Expand Up @@ -156,6 +158,11 @@ def play_move(self,

best_move = self.search(board, time_limit, can_ponder, draw_offered, best_move)

# Heed min_time
elapsed = (time.perf_counter_ns() - start_time) / 1e9
if elapsed < min_time:
time.sleep(min_time - elapsed)

self.add_comment(best_move, board)
self.print_stats()
if best_move.resigned and len(board.move_stack) >= 2:
Expand Down
16 changes: 10 additions & 6 deletions lichess-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ def play_game(li: lichess.Lichess,
disconnect_time = correspondence_disconnect_time
say_hello(conversation, hello, hello_spectators, board)
start_time = time.perf_counter_ns()
fake_thinking(config, board, game)
print_move_number(board)
move_attempted = True
engine.play_move(board,
Expand All @@ -626,7 +625,8 @@ def play_game(li: lichess.Lichess,
can_ponder,
is_correspondence,
correspondence_move_time,
engine_cfg)
engine_cfg,
fake_think_time(config, board, game))
time.sleep(delay_seconds)
elif is_game_over(game):
tell_user_game_result(game, board)
Expand Down Expand Up @@ -671,13 +671,17 @@ def say_hello(conversation: Conversation, hello: str, hello_spectators: str, boa
conversation.send_message("spectator", hello_spectators)


def fake_thinking(config: Configuration, board: chess.Board, game: model.Game) -> None:
"""Wait some time before starting to search for a move."""
def fake_think_time(config: Configuration, board: chess.Board, game: model.Game) -> float:
"""Calculate how much time we should wait for fake_think_time."""
sleep = 0.0

if config.fake_think_time and len(board.move_stack) > 9:
delay = game.my_remaining_seconds() * 0.025
remaining = max(0, game.my_remaining_seconds() - config.move_overhead / 1000)
delay = remaining * 0.025
accel = 0.99 ** (len(board.move_stack) - 10)
sleep = delay * accel
time.sleep(sleep)

return sleep


def print_move_number(board: chess.Board) -> None:
Expand Down

0 comments on commit 4719944

Please sign in to comment.