Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Policy ラベル

policy 学習向けの move label 変換を提供するモジュールです。

インポート

from rsshogi.policy import (
    MOVE_LABEL_COUNT,
    COMPACT_MOVE_LABEL_COUNT,
    MOVE_LABEL_TO_COMPACT,
    COMPACT_TO_MOVE_LABEL,
    move_label,
    compact_move_label,
    move_label_to_compact,
    compact_move_label_to_move_label,
    is_structurally_valid_move_label,
)

概要

  • move_label(move, turn) は 2187 クラスの label を返します
  • compact_move_label(move, turn) は 1496 クラスへ圧縮します
  • MOVE_LABEL_TO_COMPACT / COMPACT_TO_MOVE_LABEL でテーブル変換できます
  • ここでの compact は「局面で合法」ではなく「構造的に現れうる」ラベルだけを残す意味です

move 引数には次を渡せます。

  • Move
  • Move32
  • int
  • USI 文字列

turnColor を渡します。

基本例

from rsshogi.core import Move
from rsshogi.policy import compact_move_label, move_label
from rsshogi.types import Color

mv = Move.from_usi("7g7f")

label = move_label(mv, Color.BLACK)
compact = compact_move_label(mv, Color.BLACK)

assert compact is not None
print(label)
print(compact)

白番の正規化

後手番の手は 180 度回転してからラベル化されます。 そのため、対称な手は同じラベルになります。

from rsshogi.policy import move_label
from rsshogi.types import Color

assert move_label("7g7f", Color.BLACK) == move_label("3c3d", Color.WHITE)

構造的に無効なラベル

たとえば一段目への歩打ちや最上段への桂不成は、どの局面でも構造的に現れません。 このような手は 2187 クラスの move_label には写りますが、compact_move_label では None になります。

from rsshogi.policy import compact_move_label, is_structurally_valid_move_label, move_label
from rsshogi.types import Color

label = move_label("P*5a", Color.BLACK)

assert not is_structurally_valid_move_label(label)
assert compact_move_label("P*5a", Color.BLACK) is None

テーブル変換

from rsshogi.policy import (
    COMPACT_TO_MOVE_LABEL,
    MOVE_LABEL_TO_COMPACT,
    compact_move_label,
    move_label,
)
from rsshogi.types import Color

raw = move_label("7g7f", Color.BLACK)
compact = compact_move_label("7g7f", Color.BLACK)

assert compact is not None
assert MOVE_LABEL_TO_COMPACT[raw] == compact
assert COMPACT_TO_MOVE_LABEL[compact] == raw

関連項目