Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rank_AI #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions katrain/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
"threshold": 3.5,
"line_weight": 2,
"endgame": 0.4
},
"ai:p:rank": {
"kyu": 8.0
}
}
}
15 changes: 14 additions & 1 deletion katrain/core/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
AI_TENUKI,
AI_TERRITORY,
AI_PICK,
AI_RANK,
)
from katrain.core.engine import EngineDiedException
from katrain.core.game import Game, GameNode, Move
Expand Down Expand Up @@ -60,10 +61,13 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
policy_grid = var_to_grid(cn.policy, size) # type: List[List[float]]
top_policy_move = policy_moves[0][1]
ai_thoughts += f"Using policy based strategy, base top 5 moves are {fmt_moves(policy_moves[:5])}. "
len_legal_policy_moves = len([(pol, mv) for pol, mv in policy_moves if not mv.is_pass if pol > 0])
if ai_mode == AI_POLICY and cn.depth <= ai_settings["opening_moves"]:
ai_mode = AI_WEIGHTED
ai_thoughts += f"Switching to weighted strategy in the opening {int(ai_settings['opening_moves'])} moves. "
ai_settings = {"pick_override": 0.9, "weaken_fac": 1, "lower_bound": 0.02}
if ai_mode == AI_RANK:
ai_settings = {"pick_override": (0.8*(1-(361-len_legal_policy_moves)/361.*.5)), "kyu": ai_settings["kyu"] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You realize katrain can play anything from 1x2 to 52x52 right? so hardcoding 361 is not prefered
also the . in 361. is redundant.

if top_5_pass:
aimove = top_policy_move
ai_thoughts += "Playing top one because one of them is pass."
Expand Down Expand Up @@ -103,7 +107,8 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
)
elif ai_mode in AI_STRATEGIES_PICK:
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 ai_mode!=AI_RANK:
n_moves = int(ai_settings["pick_frac"] * len(legal_policy_moves) + ai_settings["pick_n"])
if ai_mode in [AI_INFLUENCE, AI_TERRITORY]:

thr_line = ai_settings["threshold"] - 1 # zero-based
Expand Down Expand Up @@ -164,6 +169,14 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
for y in range(size[1])
if policy_grid[y][x] > 0
]
elif ai_mode == AI_RANK:
n_moves = int(round(10**(-0.05737*ai_settings["kyu"] + 1.9482)))
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
]
else:
raise ValueError(f"Unknown AI mode {ai_mode}")
pick_moves = weighted_selection_without_replacement(weighted_coords, n_moves)
Expand Down
5 changes: 4 additions & 1 deletion katrain/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
AI_TENUKI = "ai:p:tenuki"
AI_INFLUENCE = "ai:p:influence"
AI_TERRITORY = "ai:p:territory"
AI_RANK = "ai:p:rank"

AI_CONFIG_DEFAULT = AI_SCORELOSS

AI_STRATEGIES_ENGINE = [AI_DEFAULT, AI_SCORELOSS, AI_JIGO]
AI_STRATEGIES_PICK = [AI_PICK, AI_LOCAL, AI_TENUKI, AI_INFLUENCE, AI_TERRITORY]
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 = [
Expand All @@ -37,6 +38,7 @@
AI_TERRITORY,
AI_INFLUENCE,
AI_JIGO,
AI_RANK,
]


Expand All @@ -51,6 +53,7 @@
AI_TENUKI: "8k",
AI_INFLUENCE: "8k",
AI_TERRITORY: "5k",
AI_RANK: "12k - 3d",
}


Expand Down
Binary file modified katrain/i18n/locales/en/LC_MESSAGES/katrain.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions katrain/i18n/locales/en/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,12 @@ msgstr ""
"Picks moves biased below the `threshold` line and plays the best one. "
"Increase `line_weight` to penalize moves near the center more. Stops "
"strategy after the 'endgame' fraction of the board is filled."

msgid "ai:p:rank"
msgstr "Rank 12k - 3d"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can pick a better name, especially this might be confusing if we want to calibrate other ais like this


msgid "aihelp:p:rank"
msgstr ""
"Picks moves at random from a limited selection of moves and plays the best "
"one. Stronger bots select the best move from a larger selection.Since there "
"is no 0 kyu/dan, 3 dan = -2 kyu."
Binary file modified katrain/i18n/locales/fr/LC_MESSAGES/katrain.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions katrain/i18n/locales/fr/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,12 @@ msgstr ""
"Le coup {move} a été annulé car il perdait {points_lost:.1f} points. Vous "
"pouvez survoler le coup pour voir la réfutation. Veuillez jouer un nouveau "
"coup."

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also please just add english and run i18n.py, this adds a TODO tags to other languages.

msgid "ai:p:rank"
msgstr "Rank 12k - 3d"

msgid "aihelp:p:rank"
msgstr ""
"Picks moves at random from a limited selection of moves and plays the best "
"one. Stronger bots select the best move from a larger selection.Since there "
"is no 0 kyu/dan, 3 dan = -2 kyu."
Binary file modified katrain/i18n/locales/haha/LC_MESSAGES/katrain.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions katrain/i18n/locales/haha/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,12 @@ msgid "teaching undo message"
msgstr ""
"ㅋㅋUndid move {move} as it lost {points_lost:.1f} points. Hover over the move"
" to see expected refutation. Please try again."

msgid "ai:p:rank"
msgstr "Rank 12k - 3d"

msgid "aihelp:p:rank"
msgstr ""
"Picks moves at random from a limited selection of moves and plays the best "
"one. Stronger bots select the best move from a larger selection.Since there "
"is no 0 kyu/dan, 3 dan = -2 kyu."
Binary file modified katrain/i18n/locales/ko/LC_MESSAGES/katrain.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions katrain/i18n/locales/ko/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,12 @@ msgstr ""
msgid "teaching undo message"
msgstr ""
"{points_lost:.1f}집 손해이므로 {move}을/를 물렀습니다. 커서를 올려 상대의 대응책을 보십시오. 다시 두어 주세요."

msgid "ai:p:rank"
msgstr "Rank 12k - 3d"

msgid "aihelp:p:rank"
msgstr ""
"Picks moves at random from a limited selection of moves and plays the best "
"one. Stronger bots select the best move from a larger selection.Since there "
"is no 0 kyu/dan, 3 dan = -2 kyu."