@@ -34,8 +34,7 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
34
34
raise EngineDiedException (f"Engine for { cn .next_player } ({ engine .config } ) died" )
35
35
ai_mode = ai_mode .lower ()
36
36
ai_thoughts = ""
37
- candidate_ai_moves = cn .candidate_moves
38
- if ("policy" in ai_mode or "p:" in ai_mode ) and cn .policy :
37
+ if ("policy" in ai_mode or "p:" in ai_mode ) and cn .policy : # pure policy based move
39
38
policy_moves = cn .policy_ranking
40
39
pass_policy = cn .policy [- 1 ]
41
40
top_5_pass = any ([polmove [1 ].is_pass for polmove in policy_moves [:5 ]]) # dont make it jump around for the last few sensible non pass moves
@@ -131,32 +130,35 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
131
130
ai_thoughts += f"Pick policy strategy { ai_mode } failed to find legal moves, so is playing top policy move { aimove .gtp ()} ."
132
131
else :
133
132
raise ValueError (f"Unknown AI mode { ai_mode } " )
134
- elif "balance" in ai_mode and candidate_ai_moves [0 ]["move" ] != "pass" : # don't play suicidal to balance score - pass when it's best
135
- sign = cn .player_sign (cn .next_player )
136
- sel_moves = [ # top move, or anything not too bad, or anything that makes you still ahead
137
- move
138
- for i , move in enumerate (candidate_ai_moves )
139
- if i == 0
140
- or move ["visits" ] >= ai_settings ["min_visits" ]
141
- and (move ["pointsLost" ] < ai_settings ["random_loss" ] or move ["pointsLost" ] < ai_settings ["max_loss" ] and sign * move ["scoreLead" ] > ai_settings ["target_score" ])
142
- ]
143
- aimove = Move .from_gtp (random .choice (sel_moves )["move" ], player = cn .next_player )
144
- ai_thoughts += f"Balance strategy selected moves { sel_moves } based on target score and max points lost, and randomly chose { aimove .gtp ()} ."
145
- elif "jigo" in ai_mode and candidate_ai_moves [0 ]["move" ] != "pass" :
146
- sign = cn .player_sign (cn .next_player )
147
- jigo_move = min (candidate_ai_moves , key = lambda move : abs (sign * move ["scoreLead" ] - ai_settings ["target_score" ]))
148
- aimove = Move .from_gtp (jigo_move ["move" ], player = cn .next_player )
149
- ai_thoughts += f"Jigo strategy found candidate moves { candidate_ai_moves } moves and chose { aimove .gtp ()} as closest to 0.5 point win"
150
- else :
151
- if "default" not in ai_mode and "katago" not in ai_mode :
152
- game .katrain .log (f"Unknown AI mode { ai_mode } or policy missing, using default." , OUTPUT_INFO )
153
- ai_thoughts += f"Strategy { ai_mode } not found or unexpected fallback."
154
- aimove = Move .from_gtp (candidate_ai_moves [0 ]["move" ], player = cn .next_player )
155
- ai_thoughts += f"Default strategy found { len (candidate_ai_moves )} moves returned from the engine and chose { aimove .gtp ()} as top move"
133
+ else : # Engine based move
134
+ candidate_ai_moves = cn .candidate_moves
135
+ if "balance" in ai_mode and candidate_ai_moves [0 ]["move" ] != "pass" : # don't play suicidal to balance score - pass when it's best
136
+ sign = cn .player_sign (cn .next_player )
137
+ sel_moves = [ # top move, or anything not too bad, or anything that makes you still ahead
138
+ move
139
+ for i , move in enumerate (candidate_ai_moves )
140
+ if i == 0
141
+ or move ["visits" ] >= ai_settings ["min_visits" ]
142
+ and (move ["pointsLost" ] < ai_settings ["random_loss" ] or move ["pointsLost" ] < ai_settings ["max_loss" ] and sign * move ["scoreLead" ] > ai_settings ["target_score" ])
143
+ ]
144
+ aimove = Move .from_gtp (random .choice (sel_moves )["move" ], player = cn .next_player )
145
+ ai_thoughts += f"Balance strategy selected moves { sel_moves } based on target score and max points lost, and randomly chose { aimove .gtp ()} ."
146
+ elif "jigo" in ai_mode and candidate_ai_moves [0 ]["move" ] != "pass" :
147
+ sign = cn .player_sign (cn .next_player )
148
+ jigo_move = min (candidate_ai_moves , key = lambda move : abs (sign * move ["scoreLead" ] - ai_settings ["target_score" ]))
149
+ aimove = Move .from_gtp (jigo_move ["move" ], player = cn .next_player )
150
+ ai_thoughts += f"Jigo strategy found candidate moves { candidate_ai_moves } moves and chose { aimove .gtp ()} as closest to 0.5 point win"
151
+ else :
152
+ if "default" not in ai_mode and "katago" not in ai_mode :
153
+ game .katrain .log (f"Unknown AI mode { ai_mode } or policy missing, using default." , OUTPUT_INFO )
154
+ ai_thoughts += f"Strategy { ai_mode } not found or unexpected fallback."
155
+ aimove = Move .from_gtp (candidate_ai_moves [0 ]["move" ], player = cn .next_player )
156
+ ai_thoughts += f"Default strategy found { len (candidate_ai_moves )} moves returned from the engine and chose { aimove .gtp ()} as top move"
156
157
game .katrain .log (f"AI thoughts: { ai_thoughts } " , OUTPUT_DEBUG )
157
158
try :
158
159
played_node = game .play (aimove )
159
160
played_node .ai_thoughts = ai_thoughts
160
161
return aimove , played_node
161
162
except IllegalMoveException as e :
162
163
game .katrain .log (f"AI Strategy { ai_mode } generated illegal move { aimove .gtp ()} : { e } " , OUTPUT_ERROR )
164
+ return None , None
0 commit comments