simple ai testing

This commit is contained in:
Sander Land committed 2020-09-27 16:09:57 +02:00
1 parent dbe4011ab6
commit e74d7c7166
4 files changed
+79 -17

No files matched your search

+63 -11
View File
@@ -8,29 +8,31 @@ from katrain.core.constants import (
AI_DEFAULT,
AI_HANDICAP,
AI_INFLUENCE,
AI_INFLUENCE_ELO_GRID,
AI_JIGO,
AI_LOCAL,
AI_LOCAL_ELO_GRID,
AI_PICK,
AI_PICK_ELO_GRID,
AI_POLICY,
AI_RANK,
AI_SCORELOSS,
AI_SCORELOSS_ELO,
AI_SIMPLE,
AI_SIMPLE_OWNERSHIP,
AI_STRATEGIES_PICK,
AI_STRATEGIES_POLICY,
AI_STRENGTH,
AI_TENUKI,
AI_TENUKI_ELO_GRID,
AI_TERRITORY,
AI_TERRITORY_ELO_GRID,
AI_WEIGHTED,
AI_WEIGHTED_ELO,
CALIBRATED_RANK_ELO,
OUTPUT_DEBUG,
OUTPUT_ERROR,
OUTPUT_INFO,
AI_WEIGHTED_ELO,
AI_SCORELOSS_ELO,
CALIBRATED_RANK_ELO,
AI_LOCAL_ELO_GRID,
AI_TENUKI_ELO_GRID,
AI_TERRITORY_ELO_GRID,
AI_INFLUENCE_ELO_GRID,
AI_PICK_ELO_GRID,
)
from katrain.core.game import Game, GameNode, Move
from katrain.core.utils import var_to_grid
@@ -168,7 +170,7 @@ def request_ai_analysis(game: Game, cn: GameNode, extra_settings: Dict) -> Optio
def set_error(a):
nonlocal error
game.katrain.log("Error in PDA-based analysis", a)
game.katrain.log(f"Error in additional analysis query: {a}")
error = True
engine = game.engines[cn.player]
@@ -203,6 +205,13 @@ def generate_ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move,
game.katrain.log(f"Error getting handicap-based move", OUTPUT_ERROR)
ai_mode = AI_DEFAULT
if ai_mode == AI_SIMPLE:
simple_moves = ai_settings["simple_moves"]
simple_analysis = request_ai_analysis(game, cn, {"simpleMovesBias": simple_moves, "wideRootNoise": 0.10})
if not simple_analysis:
game.katrain.log(f"Error getting simple-biased move", OUTPUT_ERROR)
ai_mode = AI_DEFAULT
while not cn.analysis_ready:
time.sleep(0.01)
game.engines[cn.next_player].check_alive(exception_if_dead=True)
@@ -326,9 +335,25 @@ def generate_ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move,
candidate_ai_moves = cn.candidate_moves
if ai_mode == AI_HANDICAP:
candidate_ai_moves = handicap_analysis["moveInfos"]
if ai_mode == AI_SIMPLE:
candidate_ai_moves = simple_analysis["moveInfos"]
for data in candidate_ai_moves:
print(
"{order} {move}: visits {visits} utility {util} utilityLcb {lcb}".format(
visits=data["visits"],
order=data["order"],
move=data["move"],
util=data["utility"],
lcb=data["utilityLcb"],
)
)
top_cand = Move.from_gtp(candidate_ai_moves[0]["move"], player=cn.next_player)
if top_cand.is_pass and ai_mode not in [AI_DEFAULT, AI_HANDICAP]: # don't play suicidal to balance score
if top_cand.is_pass and ai_mode not in [
AI_DEFAULT,
AI_HANDICAP,
AI_SIMPLE,
]: # don't play suicidal to balance score
aimove = top_cand
ai_thoughts += f"Top move is pass, so passing regardless of strategy. "
else:
@@ -352,13 +377,40 @@ def generate_ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move,
topmove = weighted_selection_without_replacement(moves, 1)[0]
aimove = topmove[2]
ai_thoughts += f"ScoreLoss strategy found {len(candidate_ai_moves)} candidate moves (best {top_cand.gtp()}) and chose {aimove.gtp()} (weight {topmove[1]:.3f}, point loss {topmove[0]:.1f}) based on score weights."
elif ai_mode == AI_SIMPLE_OWNERSHIP:
def settledness(d):
return sum([abs(o) for o in d["ownership"]])
moves_with_settledness = sorted(
[
(Move.from_gtp(d["move"], player=cn.next_player), settledness(d), d)
for d in candidate_ai_moves
if d["pointsLost"] < ai_settings["max_points_lost"] and "ownership" in d
],
key=lambda t: t[2]["pointsLost"] - ai_settings["settled_weight"] * t[1],
)
if moves_with_settledness:
cands = [
f"{move.gtp()} ({d['pointsLost']:.1f} pt lost, {settled:.1f} settledness)"
for move, settled, d in moves_with_settledness
]
ai_thoughts += f"Simple ownership strategy. Candidates {', '.join(cands)} "
aimove = moves_with_settledness[0][0]
else:
game.katrain.log(
"No moves found - are you using an older KataGo with no per-move ownership info?", OUTPUT_ERROR
)
aimove = top_cand
else:
if ai_mode not in [AI_DEFAULT, AI_HANDICAP]:
if ai_mode not in [AI_DEFAULT, AI_HANDICAP, AI_SIMPLE]:
game.katrain.log(f"Unknown AI mode {ai_mode} or policy missing, using default.", OUTPUT_INFO)
ai_thoughts += f"Strategy {ai_mode} not found or unexpected fallback."
aimove = top_cand
if ai_mode == AI_HANDICAP:
ai_thoughts += f"Handicap strategy found {len(candidate_ai_moves)} moves returned from the engine and chose {aimove.gtp()} as top move. PDA based score {cn.format_score(handicap_analysis['rootInfo']['scoreLead'])} and win rate {cn.format_winrate(handicap_analysis['rootInfo']['winrate'])}"
elif ai_mode == AI_SIMPLE:
ai_thoughts += f"Simple moves strategy found {len(candidate_ai_moves)} moves returned from the engine and chose {aimove.gtp()} as top move. "
else:
ai_thoughts += f"Default strategy found {len(candidate_ai_moves)} moves returned from the engine and chose {aimove.gtp()} as top move"
game.katrain.log(f"AI thoughts: {ai_thoughts}", OUTPUT_DEBUG)
+9 -3
View File
@@ -1,6 +1,6 @@
VERSION = "1.5.0"
VERSION = "1.5.1"
HOMEPAGE = "https://github.com/sanderland/katrain"
CONFIG_MIN_VERSION = "1.4.0" # keep config files from this version
CONFIG_MIN_VERSION = "1.5.1" # keep config files from this version
OUTPUT_ERROR = -1
OUTPUT_KATAGO_STDERR = -0.5
@@ -33,16 +33,19 @@ AI_TENUKI = "ai:p:tenuki"
AI_INFLUENCE = "ai:p:influence"
AI_TERRITORY = "ai:p:territory"
AI_RANK = "ai:p:rank"
AI_SIMPLE = "ai:disabled"
AI_SIMPLE_OWNERSHIP = "ai:simple"
AI_CONFIG_DEFAULT = AI_RANK
AI_STRATEGIES_ENGINE = [AI_DEFAULT, AI_HANDICAP, AI_SCORELOSS, AI_JIGO]
AI_STRATEGIES_ENGINE = [AI_DEFAULT, AI_HANDICAP, AI_SIMPLE, AI_SCORELOSS, AI_JIGO]
AI_STRATEGIES_PICK = [AI_PICK, AI_LOCAL, AI_TENUKI, AI_INFLUENCE, AI_TERRITORY, AI_RANK]
AI_STRATEGIES_POLICY = [AI_WEIGHTED, AI_POLICY] + AI_STRATEGIES_PICK
AI_STRATEGIES = AI_STRATEGIES_ENGINE + AI_STRATEGIES_POLICY
AI_STRATEGIES_RECOMMENDED_ORDER = [
AI_DEFAULT,
AI_RANK,
AI_SIMPLE_OWNERSHIP,
AI_HANDICAP,
AI_SCORELOSS,
AI_POLICY,
@@ -67,6 +70,7 @@ AI_STRENGTH = { # dan ranks, backup if model is missing. TODO: remove some?
AI_INFLUENCE: -7,
AI_TERRITORY: -7,
AI_RANK: float("nan"),
AI_SIMPLE_OWNERSHIP: 9,
}
AI_OPTION_VALUES = {
@@ -85,6 +89,8 @@ AI_OPTION_VALUES = {
"threshold": [2, 2.5, 3, 3.5, 4, 4.5],
"automatic": "bool",
"pda": [(x / 10, f"{'W' if x<0 else 'B'}+{abs(x/10):.1f}") for x in range(-30, 31)],
"max_points_lost": [x/10 for x in range(51)],
"settled_weight": [x/10 for x in range(-100,101)],
}
AI_KEY_PROPERTIES = {"kyu_rank", "strength", "weaken_fac", "pick_frac", "pick_n", "automatic"}