From f4e3624758b91b0064c06f4a64a8dba4e01fb753 Mon Sep 17 00:00:00 2001 From: Sander Land Date: Sat, 9 May 2020 22:51:36 +0200 Subject: [PATCH] endgame option to influence and territory --- config.json | 8 +++++--- core/ai.py | 15 ++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/config.json b/config.json index 99af279..9c70b7e 100644 --- a/config.json +++ b/config.json @@ -101,7 +101,7 @@ "stddev": 7.5, "pick_n": 5, "pick_frac": 0.5, - "endgame": 0.5, + "endgame": 0.45, "_help_left": "Samples `pick_n + pick_frac * ` away from the last move and plays the best one.", "_help_right": "Increase `stddev` makes it prefer moves further away. Stops tenukiing after the 'endgame' fraction of the board is filled." }, @@ -111,8 +111,9 @@ "pick_frac": 0.4, "threshold": 3.5, "line_weight": 10, + "endgame": 0.4, "_help_left": "Samples `pick_n + pick_frac * ` and plays the best one, biased to above the `threshold` line.", - "_help_right": "Increase `line_weight` to penalize moves near the edge more." + "_help_right": "Increase `line_weight` to penalize moves near the edge more. Stops strategy after the 'endgame' fraction of the board is filled." }, "P:Territory": { "pick_override": 0.95, @@ -120,8 +121,9 @@ "pick_frac": 0.4, "threshold": 3.5, "line_weight": 2, + "endgame": 0.4, "_help_left": "Samples `pick_n + pick_frac * ` and plays the best one, biased to below the `threshold` line.", - "_help_right": "Increase `line_weight` to penalize moves closer to the center more." + "_help_right": "Increase `line_weight` to penalize moves closer to the center more. Stops strategy after the 'endgame' fraction of the board is filled." } }, "board_ui": { diff --git a/core/ai.py b/core/ai.py index 2fe9fec..c86d904 100644 --- a/core/ai.py +++ b/core/ai.py @@ -95,13 +95,18 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode legal_policy_moves = [(pol, mv) for pol, mv in policy_moves if not mv.is_pass if pol > 0] n_moves = int(ai_settings["pick_frac"] * len(legal_policy_moves) + ai_settings["pick_n"]) if "influence" in ai_mode or "territory" in ai_mode: + thr_line = ai_settings["threshold"] - 1 # zero-based - if "influence" in ai_mode: - weight = lambda x, y: (1 / ai_settings["line_weight"]) ** (max(0, thr_line - min(size[0] - 1 - x, x)) + max(0, thr_line - min(size[1] - 1 - y, y))) + if cn.depth >= ai_settings["endgame"] * size[0] * size[1]: + weighted_coords = [(policy_grid[y][x], 1, x, y) for x in range(size[0]) for y in range(size[1]) if policy_grid[y][x] > 0] + ai_thoughts += f"Generated equal weights as move number >= {ai_settings['endgame'] * size[0] * size[1]}. " else: - weight = lambda x, y: (1 / ai_settings["line_weight"]) ** (max(0, min(size[0] - 1 - x, x, size[1] - 1 - y, y) - thr_line)) - weighted_coords = [(policy_grid[y][x] * weight(x, y), weight(x, y), x, y) for x in range(size[0]) for y in range(size[1]) if policy_grid[y][x] > 0] - ai_thoughts += f"Generated weights for {ai_mode} according to weight factor {ai_settings['line_weight']} and distance from {thr_line+1}th line. " + if "influence" in ai_mode: + weight = lambda x, y: (1 / ai_settings["line_weight"]) ** (max(0, thr_line - min(size[0] - 1 - x, x)) + max(0, thr_line - min(size[1] - 1 - y, y))) + else: + weight = lambda x, y: (1 / ai_settings["line_weight"]) ** (max(0, min(size[0] - 1 - x, x, size[1] - 1 - y, y) - thr_line)) + weighted_coords = [(policy_grid[y][x] * weight(x, y), weight(x, y), x, y) for x in range(size[0]) for y in range(size[1]) if policy_grid[y][x] > 0] + ai_thoughts += f"Generated weights for {ai_mode} according to weight factor {ai_settings['line_weight']} and distance from {thr_line+1}th line. " elif "local" in ai_mode or "tenuki" in ai_mode: var = ai_settings["stddev"] ** 2 if not cn.move or cn.move.coords is None: