feat(maia): human-like think time
public
feat(maia): human-like think time Maia-3 neither predicts time nor exposes policy probabilities over UCI, so rules of thumb: book moves and recaptures 1-2 s, middlegame about 6 s, log-normal spread, max 12 s, with a clock a thirtieth of the remaining time. Subtitle shows "Maia überlegt …". Switch in preferences, on by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 451e78617b8b4ebdb70515157e20143982ded371 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 985f9dd25504 |
| Children | 5c0f00182235 |
| Pushlog | by timo via ssh by timo via ssh |
modified ROADMAP.md +1 −1
| @@ -87,7 +87,7 @@ Figurensätzen kommen Dinge dazu, die man selten ändert und die ins Menü nicht passen. Das ist der klassische Fall für | ||
| 87 | 87 | |
| 88 | 88 | | Seite | Inhalt | |
| 89 | 89 | | --- | --- | |
| 90 | -| Allgemein | Standardfarbe, Gegner-Streuung (heute `OPPONENT_TEMPERATURE` im Code), Schalter „Zugzeichen sofort zeigen“ (umgesetzt: ausgeschaltet kein rotes `??` direkt nach dem Zug, nur in Zugliste und Analyse) | | |
| 90 | +| Allgemein | Schalter „Maia denkt wie ein Mensch nach“ (umgesetzt, `schach/thinktime.py`: Faustregeln statt Vorhersage, weil Maia-3 keine Bedenkzeit kennt; Buchzug und Zurückschlagen 1–2 s, Mittelspiel um 6 s, höchstens 12 s, mit Uhr ein Dreißigstel der Restzeit), Standardfarbe, Gegner-Streuung (heute `OPPONENT_TEMPERATURE` im Code), Schalter „Zugzeichen sofort zeigen“ (umgesetzt: ausgeschaltet kein rotes `??` direkt nach dem Zug, nur in Zugliste und Analyse) | | |
| 91 | 91 | | Darstellung | Figurensatz (aus dem Menü hierher) und Brettfarben, beide als Auswahl mit kleiner Vorschau (siehe unten) | |
| 92 | 92 | | Engines | Analyse-Engine, Standard-Helfer für Hinweise, Threads und Hash für Stockfish, Maia-Modell (5M … 79M), Liste erkannter Engines mit „Engine hinzufügen …“ | |
| 93 | 93 | | Lichess | Anmeldung und Abmeldung mit Avatar und Name, aus dem Menü hierher. Im Menü bleibt nur der Status. | |
modified schach/app.py +18 −3
| @@ -28,6 +28,7 @@ from .duo import DuoMixin # noqa: E402 | ||
| 28 | 28 | from .setup import SetupMixin # noqa: E402 |
| 29 | 29 | from .explorer import ExplorerMixin # noqa: E402 |
| 30 | 30 | from .clock import TIME_CONTROLS, GameClock, time_control # noqa: E402 |
| 31 | +from .thinktime import think_seconds # noqa: E402 | |
| 31 | 32 | |
| 32 | 33 | APP_ID = "app.schach.Schach" |
| 33 | 34 | # Wer gibt den Hinweis? Maia schlägt menschliche Züge vor, Stockfish prüft sie (≤ 3 % schlechter); |
| @@ -77,7 +78,7 @@ DEFAULTS = { | ||
| 77 | 78 | "elo": 1200, "color": "white", "theme": "cburnett", "puzzle_difficulty": "normal", |
| 78 | 79 | "temperature": OPPONENT_TEMPERATURE, "scheme": "holz", "hint_helper": DEFAULT_HELPER, |
| 79 | 80 | "threat_source": "opponent", "notation": notation.DEFAULT, "time_control": "Ohne Uhr", |
| 80 | - "live_glyphs": True, | |
| 81 | + "live_glyphs": True, "human_time": True, | |
| 81 | 82 | } |
| 82 | 83 | |
| 83 | 84 | |
| @@ -624,7 +625,8 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 624 | 625 | self.title.set_subtitle("Maia wird geladen …") |
| 625 | 626 | else: |
| 626 | 627 | clock = f" · {self.clock_label}" if self.clock is not None else "" |
| 627 | - self.title.set_subtitle(f"Maia {self.elo} · du spielst {side}{clock}") | |
| 628 | + thinking = " · Maia überlegt …" if self.maia_thinking and not self._over() else "" | |
| 629 | + self.title.set_subtitle(f"Maia {self.elo} · du spielst {side}{clock}{thinking}") | |
| 628 | 630 | default_title = {"puzzle": "Rätsel", "online": "Lichess", "opening": self._opening_title(), |
| 629 | 631 | "duo": "Zwei Spieler", "setup": "Stellung aufbauen"}.get( |
| 630 | 632 | self.mode, "Schach") |
| @@ -926,10 +928,23 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 926 | 928 | if not self._over(): |
| 927 | 929 | self._request_maia_move() |
| 928 | 930 | |
| 931 | + def _maia_think_seconds(self, move): | |
| 932 | + """Wie lange Maia vor ``move`` wartet: menschlich (Einstellung) oder gleich.""" | |
| 933 | + if not self.settings.get("human_time", True): | |
| 934 | + return MIN_REPLY_SECONDS | |
| 935 | + from .training import _book | |
| 936 | + remaining = increment = None | |
| 937 | + if self.clock is not None: | |
| 938 | + remaining = self.clock.remaining()[self.board.turn] | |
| 939 | + increment = self.clock.increment | |
| 940 | + return think_seconds(self.board, move, book=_book().contains(self.board, move), | |
| 941 | + remaining_ms=remaining, increment_ms=increment or 0) | |
| 942 | + | |
| 929 | 943 | def _request_maia_move(self): |
| 930 | 944 | gen = self.gen |
| 931 | 945 | started = time.monotonic() |
| 932 | 946 | self.maia_thinking = True |
| 947 | + self._refresh_chrome() # „überlegt …“ zeigen | |
| 933 | 948 | |
| 934 | 949 | def apply(move): |
| 935 | 950 | if gen != self.gen or self.mode != "maia" or move not in self.board.legal_moves: |
| @@ -952,7 +967,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 952 | 967 | self.maia_thinking = False |
| 953 | 968 | self.toasts.add_toast(Adw.Toast(title=f"Maia-Fehler: {error}")) |
| 954 | 969 | return |
| 955 | - wait = MIN_REPLY_SECONDS - (time.monotonic() - started) | |
| 970 | + wait = self._maia_think_seconds(move) - (time.monotonic() - started) | |
| 956 | 971 | if wait > 0: |
| 957 | 972 | GLib.timeout_add(int(wait * 1000), apply, move) |
| 958 | 973 | else: |
modified schach/prefs.py +8 −0
| @@ -73,6 +73,14 @@ class Preferences(Adw.PreferencesDialog) | ||
| 73 | 73 | ) |
| 74 | 74 | glyphs.connect("notify::active", lambda row, _p: self._set("live_glyphs", row.get_active())) |
| 75 | 75 | feedback.add(glyphs) |
| 76 | + think = Adw.SwitchRow( | |
| 77 | + title="Maia denkt wie ein Mensch nach", | |
| 78 | + subtitle="Ein paar Sekunden Bedenkzeit, bei schwierigen Stellungen länger – so hetzt die " | |
| 79 | + "Partie nicht. Ausgeschaltet antwortet Maia sofort.", | |
| 80 | + active=bool(settings.get("human_time", True)), | |
| 81 | + ) | |
| 82 | + think.connect("notify::active", lambda row, _p: self._set("human_time", row.get_active())) | |
| 83 | + opponent.add(think) | |
| 76 | 84 | page.add(feedback) |
| 77 | 85 | return page |
| 78 | 86 | |
added schach/thinktime.py +58 −0
| @@ -0,0 +1,58 @@ | ||
| 1 | +"""Bedenkzeit für Maia, damit es sich wie ein Mensch anfühlt – und Florian nicht hetzt. | |
| 2 | + | |
| 3 | +Maia-3 sagt keine Bedenkzeit voraus (und gibt über UCI auch keine Wahrscheinlichkeiten | |
| 4 | +heraus). Darum Faustregeln, wie Menschen spielen: Buchzüge und Zurückschlagen gehen | |
| 5 | +schnell, im Mittelspiel mit vielen Möglichkeiten dauert es länger. Die Streuung ist | |
| 6 | +log-normal wie bei echten Bedenkzeiten. Mit Uhr richtet sich die Zeit nach der Restzeit. | |
| 7 | +""" | |
| 8 | + | |
| 9 | +import math | |
| 10 | +import random | |
| 11 | + | |
| 12 | +import chess | |
| 13 | + | |
| 14 | +MIN_SECONDS = 0.8 | |
| 15 | +MAX_SECONDS = 12.0 # ohne Uhr: länger wartet niemand gern | |
| 16 | + | |
| 17 | + | |
| 18 | +def think_seconds(board, move, *, book=False, remaining_ms=None, increment_ms=0, rng=random): | |
| 19 | + """Sekunden, die Maia vor ``move`` (in ``board``, vor dem Zug) „nachdenkt“.""" | |
| 20 | + legal = board.legal_moves.count() | |
| 21 | + if legal <= 1: | |
| 22 | + return MIN_SECONDS | |
| 23 | + ply = board.ply() | |
| 24 | + pieces = chess.popcount(board.occupied & ~board.pawns & ~board.kings) | |
| 25 | + | |
| 26 | + if book and ply < 20: | |
| 27 | + base = 1.4 # Eröffnung, die man kennt | |
| 28 | + elif _recapture(board, move): | |
| 29 | + base = 1.6 # Zurückschlagen ist selbstverständlich | |
| 30 | + elif ply < 12: | |
| 31 | + base = 2.5 | |
| 32 | + elif pieces <= 4: | |
| 33 | + base = 4.0 # Endspiel: weniger Figuren, aber genau rechnen | |
| 34 | + else: | |
| 35 | + base = 6.0 # Mittelspiel | |
| 36 | + if board.is_check(): | |
| 37 | + base *= 0.7 # wenige sinnvolle Antworten | |
| 38 | + base *= min(1.3, max(0.7, math.sqrt(legal / 30))) | |
| 39 | + | |
| 40 | + seconds = base * rng.lognormvariate(0, 0.35) | |
| 41 | + limit = MAX_SECONDS | |
| 42 | + if remaining_ms is not None: | |
| 43 | + remaining = remaining_ms / 1000 | |
| 44 | + budget = remaining / 30 + increment_ms / 1000 * 0.8 | |
| 45 | + if remaining < 30: | |
| 46 | + budget = min(budget, remaining / 15) | |
| 47 | + limit = min(limit, max(MIN_SECONDS / 2, budget)) | |
| 48 | + return max(min(MIN_SECONDS, limit), min(seconds, limit)) | |
| 49 | + | |
| 50 | + | |
| 51 | +def _recapture(board, move): | |
| 52 | + """Schlägt ``move`` die Figur zurück, die gerade auf diesem Feld geschlagen hat?""" | |
| 53 | + if not board.move_stack or not board.is_capture(move): | |
| 54 | + return False | |
| 55 | + last = board.peek() | |
| 56 | + before = board.copy() | |
| 57 | + before.pop() | |
| 58 | + return last.to_square == move.to_square and before.is_capture(last) | |
added tests/test_thinktime.py +54 −0
| @@ -0,0 +1,54 @@ | ||
| 1 | +import random | |
| 2 | +import unittest | |
| 3 | + | |
| 4 | +import chess | |
| 5 | + | |
| 6 | +from schach import thinktime | |
| 7 | + | |
| 8 | + | |
| 9 | +def board(*sans, fen=None): | |
| 10 | + b = chess.Board(fen) if fen else chess.Board() | |
| 11 | + for san in sans: | |
| 12 | + b.push_san(san) | |
| 13 | + return b | |
| 14 | + | |
| 15 | + | |
| 16 | +def average(b, move, **kw): | |
| 17 | + rng = random.Random(1) | |
| 18 | + return sum(thinktime.think_seconds(b, move, rng=rng, **kw) for _ in range(200)) / 200 | |
| 19 | + | |
| 20 | + | |
| 21 | +class ThinkTimeTest(unittest.TestCase): | |
| 22 | + def test_book_faster_than_middlegame(self): | |
| 23 | + opening = board("e4") | |
| 24 | + middle = chess.Board("r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2PBPN2/PP1N1PPP/R1BQ1RK1 w - - 0 9") | |
| 25 | + book = average(opening, chess.Move.from_uci("e7e5"), book=True) | |
| 26 | + think = average(middle, chess.Move.from_uci("d1e2")) | |
| 27 | + self.assertLess(book, 2.5) | |
| 28 | + self.assertGreater(think, 4.0) | |
| 29 | + | |
| 30 | + def test_recapture_is_quick(self): | |
| 31 | + b = board("e4", "d5", "exd5") | |
| 32 | + self.assertTrue(thinktime._recapture(b, chess.Move.from_uci("d8d5"))) | |
| 33 | + self.assertLess(average(b, chess.Move.from_uci("d8d5")), 2.5) | |
| 34 | + | |
| 35 | + def test_only_move_is_instant(self): | |
| 36 | + b = chess.Board("k7/8/8/8/8/8/1q6/K7 w - - 0 1") # Dame b2 gibt Schach, nur Kxb2 | |
| 37 | + self.assertEqual(list(b.legal_moves), [chess.Move.from_uci("a1b2")]) | |
| 38 | + self.assertEqual(thinktime.think_seconds(b, chess.Move.from_uci("a1b2")), thinktime.MIN_SECONDS) | |
| 39 | + | |
| 40 | + def test_bounds_and_clock(self): | |
| 41 | + middle = chess.Board("r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2PBPN2/PP1N1PPP/R1BQ1RK1 w - - 0 9") | |
| 42 | + move = chess.Move.from_uci("d1e2") | |
| 43 | + rng = random.Random(2) | |
| 44 | + for _ in range(300): | |
| 45 | + t = thinktime.think_seconds(middle, move, rng=rng) | |
| 46 | + self.assertTrue(thinktime.MIN_SECONDS <= t <= thinktime.MAX_SECONDS) | |
| 47 | + # nur noch 20 Sekunden auf der Uhr: höchstens gut eine Sekunde | |
| 48 | + self.assertLessEqual(thinktime.think_seconds(middle, move, remaining_ms=20000, rng=rng), 20 / 15) | |
| 49 | + # 3 Minuten: höchstens 6 Sekunden | |
| 50 | + self.assertLessEqual(thinktime.think_seconds(middle, move, remaining_ms=180000, rng=rng), 6.0) | |
| 51 | + | |
| 52 | + | |
| 53 | +if __name__ == "__main__": | |
| 54 | + unittest.main() | |