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

[Shogi] Add and fix buggy test samples #358

Merged
merged 12 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions pgx/shogi.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,7 @@ def _sfen_to_state(sfen):
else:
s_turn = jnp.int8(1)
s_hand = jnp.zeros(14, dtype=jnp.int8)
if hand == "-":
s_hand = jnp.reshape(s_hand, (2, 7))
else:
if hand != "-":
num_piece = 1
for char in hand:
if char.isdigit():
Expand All @@ -942,7 +940,7 @@ def _sfen_to_state(sfen):
return State(
turn=s_turn,
piece_board=jnp.rot90(piece_board.reshape((9, 9)), k=1).flatten(),
hand=s_hand,
hand=jnp.reshape(s_hand, (2, 7)),
)


Expand Down
28 changes: 27 additions & 1 deletion tests/test_shogi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jax.numpy as jnp

from pgx.shogi import *
from pgx.shogi import _init, _step, _step_move, _step_drop, _flip, _apply_effects, _legal_actions, _rotate, _to_direction
from pgx.shogi import _init, _step, _step_move, _step_drop, _flip, _apply_effects, _legal_actions, _rotate, _to_direction, _sfen_to_state


# check visualization results by image preview plugins
Expand Down Expand Up @@ -373,3 +373,29 @@ def test_step():
s = step(s, 3 * 81 + xy2i(3, 8))
visualize(s, "tests/assets/shogi/step_003.svg")
assert not s.legal_action_mask[3 * 81 + xy2i(3, 8)]


# 今やると落ちる
def test_legal_action_mask():
# 歩以外の持ち駒に対しての二歩判定回避
sfen = "9/9/9/9/9/9/PPPPPPPPP/9/9 b NLP 1"
legal_action = _to_direction(_legal_actions(_sfen_to_state(sfen)))
# 歩は二歩になるので打てない
assert (legal_action[81*20:81*21] == False).all()
# 香車は1列目と6列目(歩がいる)二は打てない
# 二列目には打てる
assert (legal_action[81*21+1:81*22:9] == True).all()
assert (legal_action[81*21:81*22:9] == False).all()
assert (legal_action[81*21+5:81*22:9] == False).all()
# 桂馬は1.2列目に打てない
# 三列目には打てる
assert (legal_action[81*22+1:81*23:9] == False).all()
assert (legal_action[81*22:81*23:9] == False).all()
assert (legal_action[81*21+5:81*22:9] == False).all()
Copy link
Owner

Choose a reason for hiding this comment

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

== False みたいなコードを書くのはやめましょう

(~legal_action[81*21+5:81*22:9]).all()


# 成駒のpromotion判定
sfen = "9/2+B1G1+P2/9/9/9/9/9/9/9 b - 1"
legal_action = _to_direction(_legal_actions(_sfen_to_state(sfen)))
legal_action = jnp.where(legal_action, 1, 0)
# promotionは生成されてたらダメ
assert jnp.all(legal_action[810:] == 0)
Copy link
Owner

@sotetsuk sotetsuk Feb 19, 2023

Choose a reason for hiding this comment

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

このテスト意味がわからないんですけどどういう意味ですか?
legal_action = jnp.where(legal_action, 1, 0) で全部0になってますよね?

@youyou-ku

Copy link
Owner

Choose a reason for hiding this comment

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

とりあえずコメントを参考に修正済