minioslcc

shogi utilities derived from osl. all members should be incorporated inside miniosl module

Basic datatype

class miniosl.Player

Members:

black : first player

white : second player

property alt

opponent or alternative player color

Type:

minioslcc.Player

>>> miniosl.black.alt == miniosl.white
True
property name
property sign

+1 for black or -1 for white

to_csa(self: minioslcc.Player) str
class miniosl.Square

square (x, y) with onboard range in (1, 1) to (9, 9) and with some invalid ranges outside the board for sentinels and piece stand.

>>> sq = miniosl.Square(2, 6)
>>> sq.x
2
>>> sq.y
6
>>> sq.is_onboard()
True
>>> sq.to_xy()
(2, 6)
add(self: minioslcc.Square, offset: minioslcc.Offset) minioslcc.Square

[advanced] return a square with location offset

>>> miniosl.Square(7, 7).add(miniosl.U.to_offset()) == miniosl.Square(7, 6)
True
index81(self: minioslcc.Square) int

return index in range [0, 80]

is_onboard(self: minioslcc.Square) bool
is_piece_stand(self: minioslcc.Square) bool
is_promote_area(self: minioslcc.Square, color: minioslcc.Player = <Player.black: 0>) bool

test this is in promote area for color

>>> miniosl.Square(1, 3).is_promote_area(miniosl.black)
True
>>> miniosl.Square(1, 3).is_promote_area()  # default for black
True
>>> miniosl.Square(1, 3).is_promote_area(miniosl.white)
False
>>> miniosl.Square(5, 5).is_promote_area(miniosl.black)
False
neighbor(self: minioslcc.Square, dir: minioslcc.Direction, color: minioslcc.Player = <Player.black: 0>) minioslcc.Square

return a neighbor for direction

>>> miniosl.Square(7, 7).neighbor(miniosl.U) == miniosl.Square(7, 6)
True
rotate180(self: minioslcc.Square) minioslcc.Square

return a square after rotation.

>>> sq = miniosl.Square(2, 6)
>>> sq.rotate180().to_xy()
(8, 4)
to_csa(self: minioslcc.Square) str
to_ja(self: minioslcc.Square) str

Japanese label

to_usi(self: minioslcc.Square) str
to_xy(self: minioslcc.Square) tuple[int, int]
property x

int in [1, 9]

property y

int in [1, 9]

class miniosl.Move

move in shogi.

containing the source and destination positions, moving piece, and captured one if any

>>> shogi = miniosl.UI()
>>> move = shogi.to_move('+7776FU')
>>> move.src == miniosl.Square(7, 7)
True
>>> move.dst == miniosl.Square(7, 6)
True
>>> move.ptype == miniosl.pawn
True
>>> move.color == miniosl.black
True
>>> move.is_drop()
False
>>> move.is_capture()
False
>>> move.to_usi()
'7g7f'
property capture_ptype

captured Ptype by move

property color

Player of move

static declare_win() minioslcc.Move

return win declaration

property dst

arrived Square

is_capture(self: minioslcc.Move) bool
is_drop(self: minioslcc.Move) bool
is_normal(self: minioslcc.Move) bool
is_promotion(self: minioslcc.Move) bool
property old_ptype

piece Ptype before move

property policy_move_label

move index in int for cross-entropy loss in training

property ptype

piece Ptype after move

static resign() minioslcc.Move

return resign

rotate180(self: minioslcc.Move) minioslcc.Move
property src

departed Square (can be piece stand)

to_csa(self: minioslcc.Move) str
to_ja(self: minioslcc.Move, state: osl::EffectState, prev_to: Optional[minioslcc.Square] = None) str

Japanese for human

to_usi(self: minioslcc.Move) str
class miniosl.Ptype

piece type

Members:

pawn

lance

knight

silver

gold

bishop

rook

king

ppawn : promoted pawn

plance : promoted lance

pknight : promoted knight

psilver : promoted silver

pbishop : promoted bishop

prook : promoted rook

empty

edge

property count

number of pieces in the standard rule

property direction_set

bitset of Direction indicating legal moves

>>> miniosl.pawn.direction_set == miniosl.U.one_hot
True
property has_long_move

True if lance, bishop, rook, pbishop, or prook

Type:

bool

is_piece(self: minioslcc.Ptype) bool

True (False) is a normal piece type (empty or edge)

>>> miniosl.rook.is_piece()
True
>>> miniosl.empty.is_piece()
False
property move_type

return gold if move type is equivalent

Type:

minioslcc.Ptype

>>> miniosl.pawn.move_type == miniosl.gold
False
>>> miniosl.ppawn.move_type == miniosl.gold
True
property name
property one_hot

building block for bitset

Type:

int

property piece_id

range of piece id [0,39] assigned to the ptype

>>> miniosl.rook.piece_id
(38, 40)
property piece_id_set

bitset of piece_id

>>> miniosl.rook.piece_id_set
824633720832
>>> miniosl.rook.piece_id_set == ((1<<38) | (1<<39))
True
promote(self: minioslcc.Ptype) minioslcc.Ptype

return promoted type if legal

>>> miniosl.rook.promote() == miniosl.prook
True
>>> miniosl.prook.promote() == miniosl.prook
True
to_csa(self: minioslcc.Ptype) str

return csa representation

>>> miniosl.rook.to_csa()
'HI'
to_ja(self: minioslcc.Ptype) str

return Japanese representation

>>> miniosl.pawn.to_ja()
'歩'
to_ja1(self: minioslcc.Ptype) str

Japanese label

unpromote(self: minioslcc.Ptype) minioslcc.Ptype

return the original type if promoted

>>> miniosl.prook.unpromote() == miniosl.rook
True
class miniosl.Piece

a state of piece placed in a corresponding BaseState

property color

Player

equals(self: minioslcc.Piece, arg0: minioslcc.Piece) bool

equality w.r.t. PtypeO (i.e., ignoring piece id or location)

has(self: minioslcc.Piece, ptype: minioslcc.Ptype, color: minioslcc.Player) bool

test if piece has a specified piece type with color

property id

id in [0,39]

Type:

int

is_piece(self: minioslcc.Piece) bool
property ptype

Ptype

property square

Square

State

class miniosl.BaseState

parent of State. Please use State for usual cases.

count_hand(self: minioslcc.BaseState, color: minioslcc.Player, ptype: minioslcc.Ptype) int
decode_move_label(self: minioslcc.BaseState, code: int) minioslcc.Move

interpret move index generated by Move.policy_move_label()

export_features(self: minioslcc.BaseState, moves: minioslcc.MoveVector) numpy.ndarray[numpy.float32]

return np array of the standard set of features after moves are played

export_features_after_move(self: minioslcc.BaseState, moves: minioslcc.MoveVector, lookahead: minioslcc.Move) tuple[numpy.ndarray[numpy.float32], minioslcc.GameResult]

return pair of (1) np array of the standard set of features after moves and lookahead are played and (2) GameResult indicating game termination by the move

guess_variant(self: minioslcc.BaseState) tuple[minioslcc.GameVariant, int | None]

game variant based on active pieces

hash_code(self: minioslcc.BaseState) tuple[int, int]

64bit int for board and 32bit for (black) hand pieces

is_empty(self: minioslcc.BaseState, square: minioslcc.Square) bool

true when no piece at square

king_square(self: minioslcc.BaseState, color: minioslcc.Player) minioslcc.Square
property oturn

opponent side to move

pawn_in_file(self: minioslcc.BaseState, color: minioslcc.Player, x: int) bool

true if unpromoted pawn exists in file x

piece(self: minioslcc.BaseState, internal_id: int) minioslcc.Piece
piece_at(self: minioslcc.BaseState, square: minioslcc.Square) minioslcc.Piece

a piece at given square

rotate180(self: minioslcc.BaseState) minioslcc.BaseState

make a rotated state

>>> s = miniosl.State()
>>> print(s.to_csa(), end='')
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 *  *  *  *  *  *  *  *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+
>>> _ = s.make_move('+7776FU')
>>> print(s.rotate180().to_csa(), end='')
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 *  *  *  *  *  * -FU *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+
shogi816k_id(self: minioslcc.BaseState) int | None

id of Shogi816K if match

to_csa(self: minioslcc.BaseState) str
to_img(*, decorate: bool = False, plane: ndarray | None = None, plane_color: str = 'orange', last_move_ja: str = '', last_to: Square | None = None, move_number: int = 0, repeat_distance: int = 0, repeat_count: int = 0, flip_if_white: bool = False, id: int = 4081) Figure

make ShogiFig object including matplotlib figure as .fig

Parameters:
  • state – state,

  • decorate – highlight king location and piece covers for each color,

  • plane – 9x9 numpy array to make a mark on squares,

  • last_move_ja – last move in japanese,

  • last_to – the destination square of the last move,

  • move_number – ply in a game record,

  • repeat_distance – distance to the latest same position,

  • repeat_count – number of the occurrence of this state,

  • flip_if_whiterotate180() if white to move

to_np_44ch(self: minioslcc.BaseState) numpy.ndarray[numpy.float32]

a simple set of state features including board and hands

to_usi(self: minioslcc.BaseState) str
property turn

player to move

class miniosl.State

Bases: BaseState

primary class of shogi state

functions:

  • main: board position + pieces in hand (mochigoma)

  • additional: covering pieces of each square, pins, etc

>>> state = miniosl.State()
>>> state.turn == miniosl.black
True
>>> state.piece_at(miniosl.Square(5, 9)).ptype == miniosl.king
True
>>> state.king_square(miniosl.white) == miniosl.Square(5, 1)
True
>>> state.count_hand(miniosl.black, miniosl.pawn)
0
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: minioslcc.State) -> None

make a initial state

  1. __init__(self: minioslcc.State, src: minioslcc.State) -> None

make a copy of src

  1. __init__(self: minioslcc.State, src: minioslcc.BaseState) -> None

make a copy of src

count_cover(self: minioslcc.State, color: minioslcc.Player, sq: minioslcc.Square) int

the number of pieces reachable to given square

>>> s = miniosl.State()
>>> s.count_cover(miniosl.black, miniosl.Square(1, 7))
2
>>> s.count_cover(miniosl.black, miniosl.Square(5, 7))
0
>>> s.count_cover(miniosl.black, miniosl.Square(9, 7))
3
genmove(self: minioslcc.State) minioslcc.MoveVector

genmove suitable for most cases (some moves are excluded)

>>> s = miniosl.State()
>>> len(s.genmove())
30
genmove_check(self: minioslcc.State) minioslcc.MoveVector

generate check moves only

genmove_full(self: minioslcc.State) minioslcc.MoveVector

genenerate full moves, including skip-promotion of pawn, bishop, and rook.

in_check(self: minioslcc.State) bool
in_checkmate(self: minioslcc.State) bool
in_no_legalmoves(self: minioslcc.State) bool
long_piece_reach(self: minioslcc.State, arg0: minioslcc.Piece) list[tuple[minioslcc.Direction, minioslcc.Square]]

inspect furthest square reachable by long move of piece

>>> s = miniosl.State()
>>> s.long_piece_reach(s.piece_at(miniosl.Square(1, 9)))
[(<Direction.U: 1>, <Square '1g'>)]
make_move(*args, **kwargs)

Overloaded function.

  1. make_move(self: minioslcc.State, move_string: str) -> None

take usa or csa representation

  1. make_move(self: minioslcc.State, move: minioslcc.Move) -> None

make_move_pass(self: minioslcc.State) None
pieces_cover(self: minioslcc.State, color: minioslcc.Player, square: minioslcc.Square, ptype: minioslcc.Ptype | None = None, strict_promotion: bool = False) int

the bitset of piece-ids covering (reachable to) given square

>>> s = miniosl.State()
>>> pieces = s.pieces_cover(miniosl.black, miniosl.Square(5, 8))
>>> bin(pieces).count('1')
4
>>> pieces = s.pieces_cover(miniosl.black, miniosl.Square(5, 8), miniosl.gold)
>>> bin(pieces).count('1')
2
pieces_on_board(self: minioslcc.State, color: minioslcc.Player) int

the bitset of piece-ids on board (not captured) of color

>>> s = miniosl.State()
>>> bin(s.pieces_on_board(miniosl.black)).count('1')
20
pieces_promoted(self: minioslcc.State) int

the bitset of promoted piece-ids

>>> s = miniosl.State()
>>> s.pieces_promoted()
0
pinned(self: minioslcc.State, color: minioslcc.Player) int

the bitset of pinned piece-ids of color

read_japanese_move(self: minioslcc.State, move: str, last_to: minioslcc.Square = <Square '0_'>) minioslcc.Move

parse and return move

reset(self: minioslcc.State, src: minioslcc.State) None

re-initialize self copying src

rotate180(self: minioslcc.State) minioslcc.State
to_img(*, decorate: bool = False, plane: ndarray | None = None, plane_color: str = 'orange', last_move_ja: str = '', last_to: Square | None = None, move_number: int = 0, repeat_distance: int = 0, repeat_count: int = 0, flip_if_white: bool = False, id: int = 4081) Figure

make ShogiFig object including matplotlib figure as .fig

Parameters:
  • state – state,

  • decorate – highlight king location and piece covers for each color,

  • plane – 9x9 numpy array to make a mark on squares,

  • last_move_ja – last move in japanese,

  • last_to – the destination square of the last move,

  • move_number – ply in a game record,

  • repeat_distance – distance to the latest same position,

  • repeat_count – number of the occurrence of this state,

  • flip_if_whiterotate180() if white to move

to_move(self: minioslcc.State, move_string: str) minioslcc.Move

parse and return move

>>> s = miniosl.State()
>>> s.to_move('+2726FU') == s.to_move('2g2f')
True
to_np_cover(self: minioslcc.State) numpy.ndarray[numpy.int8]

squares covered by pieces as numpy array

to_np_state_feature(self: minioslcc.State, flipped: bool = False) numpy.ndarray[numpy.float32]

a subset of features without history information

try_checkmate_1ply(self: minioslcc.State) minioslcc.Move

try to find a checkmate move

win_if_declare(self: minioslcc.State) bool

Game record

class miniosl.MiniRecord

a game record

>>> record = miniosl.usi_record('startpos moves 7g7f')
>>> record.move_size()
1
>>> record.moves[0].to_usi()
'7g7f'
>>> s = record.replay(-1)
>>> s.piece_at(miniosl.Square(7, 6)).ptype == miniosl.pawn
True
consecutive_in_check(self: minioslcc.MiniRecord, id: int = 0) int
property final_move

resign or win declaration in Move

has_winner(self: minioslcc.MiniRecord) bool
property history

list of HashStatus s

property initial_state
move_size(self: minioslcc.MiniRecord) int

#moves played so far

property moves

list of Move s

pack_record(self: minioslcc.MiniRecord) list[int]

encode in uint64 array

previous_repeat_index(self: minioslcc.MiniRecord, id: int = 0) int
repeat_count(self: minioslcc.MiniRecord, id: int = 0) int
replay(n: int) State

return state after n-th move

Parameters:

n – number of moves from initial state or the last state if negative

property result

GameResult

state_size(self: minioslcc.MiniRecord) int
to_anim(n: int = -1, start: int = 0, decorate: bool = False)

return animation of the specified range of record

to_ja() list[str]

return list of moves in Japanese

to_usi(self: minioslcc.MiniRecord) str

export to usi string

class miniosl.SubRecord

subset of MiniRecord, suitable when initial state is known

property final_move

resign or win declaration in Move

make_state(self: minioslcc.SubRecord, n: int) minioslcc.BaseState

make a state after the first n moves

property moves

list of Move s

replay(n: int) BaseState

return state after n-th move

Parameters:

n – number of moves from initial state or the last state if negative

>>> record = miniosl.usi_sub_record('startpos moves 7g7f')
>>> s = record.replay(-1)
>>> s.piece_at(miniosl.Square(7, 6)).ptype == miniosl.pawn
True
property result

GameResult

sample_feature_labels(self: minioslcc.SubRecord, idx: int | None = None) tuple[numpy.ndarray[numpy.int8], int, int, numpy.ndarray[numpy.int8], numpy.ndarray[numpy.uint8]]

randomly samle index and call export_feature_labels()

Parameters:

idx – move id or None for random,

Returns:

tuple of (input_features, move_label, value_label, aux_label, legal_moves).

sample_feature_labels_to(self: minioslcc.SubRecord, offset: int, inputs: numpy.ndarray[numpy.int8], policy_labels: numpy.ndarray[numpy.int32], value_labels: numpy.ndarray[numpy.float32], aux_labels: numpy.ndarray[numpy.int8], inputs2: numpy.ndarray[numpy.int8], legalmove_labels: numpy.ndarray[numpy.uint8]) None

randomly samle index and export features to given ndarray (must be zerofilled in advance)

Parameters:
  • offset – offset in output index,

  • inputs – input features to store,

  • policy_labels – policy label to store,

  • value_labels – value label to store,

  • aux_labels – labels for auxiliary tasks to store,

  • inputs2 – input features for successor state to store,

  • legalmove_labels – legal moves to store.

class miniosl.GameResult

Members:

BlackWin

WhiteWin

Draw

InGame

flip(self: minioslcc.GameResult) minioslcc.GameResult

invert winner

has_winner(self: minioslcc.GameResult) bool
property name
class miniosl.RecordSet
from_npz(name: str = '', *, limit: int | None = None)

load miniosl.RecordSet from npz file

static from_usi_file(path: str) minioslcc.RecordSet

read usi lines

property records

list of MiniRecord s

save_npz(filename: str, name: str = '') None

save recordset to npz by np.savez_compressed

class miniosl.OpeningTree
class Node
property age
black_advantage(self: minioslcc.OpeningTree.Node, prior: float = 0.001) float
black_advantage_tree(self: minioslcc.OpeningTree.Node, prior: float = 0.001) float
property black_value_backup
count(self: minioslcc.OpeningTree.Node) int
property depth
property result_count

list of int for all GameResult

export_all(self: minioslcc.OpeningTree) tuple[list[int], list[int], list[int], list[int], list[float]]
static load_binary(filename: str, threshold: int = 1, modify: bool = True) minioslcc.OpeningTree

load from file

mutable_clone(self: minioslcc.OpeningTree) osl::OpeningTreeEditable

make an editable clone

mutable_view(self: minioslcc.OpeningTree) Optional[osl::OpeningTreeEditable]

try to get an editable view

prune(self: minioslcc.OpeningTree, threshold: int, filename: str) None

prune entries by visit counts

retrieve_children(state: BaseState) list[minioslcc.OpeningTree.Node, minioslcc.Move]
root_count(self: minioslcc.OpeningTree) int

visit count for the initial state

save_binary(self: minioslcc.OpeningTree, filename: str) None

save to file

save_npz(filename: str) ndarray

save OpeningTree to npz

size(self: minioslcc.OpeningTree) int
miniosl.csa_board(state_string: str) osl::EffectState

parse and return State

miniosl.usi_board(state_string: str) osl::EffectState

parse and return State

miniosl.csa_record(record_string: str) minioslcc.MiniRecord

read str as a game record

miniosl.usi_record(record_string: str) minioslcc.MiniRecord

read str as a game record

miniosl.usi_sub_record(record_string: str) osl::SubRecord

read str as a game record in SubRecord

miniosl.csa_file(path: str) minioslcc.MiniRecord

load a game record

miniosl.usi_file(path: str, line_id: int = 0) minioslcc.MiniRecord

load a game record

miniosl.kif_file(filepath: os.PathLike) minioslcc.MiniRecord

load a game record

Details

class miniosl.Direction

direction.

>>> direction_set = miniosl.pawn.direction_set
>>> bin(direction_set)
'0b10'
>>> (direction_set & miniosl.U.one_hot) != 0
True

Members:

UL : up left

U

UR

L

R

DL

D

DR

UUL : up of up left for knight

UUR

Long_UL : UL for bishops

Long_U

Long_UR

Long_L

Long_R

Long_DL

Long_D

Long_DR

property black_dx

delta x in black’s view

Type:

int

property black_dy

delta y in black’s view

Type:

int

property inverse

make inverse direction

>>> miniosl.UL.inverse == miniosl.DR
True
>>> miniosl.Long_D.inverse == miniosl.Long_U
True
>>> miniosl.UUL.inverse == miniosl.UUL  # DDR is not defined
True
is_base8(self: minioslcc.Direction) bool

True if eight neighbors

>>> miniosl.UL.is_base8()
True
>>> miniosl.UUL.is_base8()
False
is_long(self: minioslcc.Direction) bool

True if long direction

>>> miniosl.U.is_long()
False
>>> miniosl.Long_U.is_long()
True
property name
property one_hot

building block for bitset

Type:

int

to_base8(self: minioslcc.Direction) minioslcc.Direction

make corresponding base8 direction from long one

>>> miniosl.Long_U.to_base8() == miniosl.U
True
to_long(self: minioslcc.Direction) minioslcc.Direction

make corresponding long direction from base8

>>> miniosl.U.to_long() == miniosl.Long_U
True
to_offset(self: minioslcc.Direction, color: minioslcc.Player = <Player.black: 0>) osl::Offset

obtain move offset for color

>>> sq, dir = miniosl.Square(7, 7), miniosl.U
>>> sq.add(dir.to_offset()) == miniosl.Square(7, 6)
True
>>> sq.add(dir.to_offset(miniosl.white)) == miniosl.Square(7, 8)
True
class miniosl.Offset

relative square location

Members:

property name
miniosl.collate_features(block_vector: std::vector<std::vector<osl::SubRecord, std::allocator<osl::SubRecord> >, std::allocator<std::vector<osl::SubRecord, std::allocator<osl::SubRecord> > > >, indices: list, inputs: numpy.ndarray[numpy.int8], policy_labels: numpy.ndarray[numpy.int32], value_labels: numpy.ndarray[numpy.float32], aux_labels: numpy.ndarray[numpy.int8], inputs2: Optional[numpy.ndarray[numpy.int8]] = None, legalmove_labels: Optional[numpy.ndarray[numpy.uint8]] = None, sampled_id: Optional[numpy.ndarray[numpy.uint16]] = None) None

collate function for GameDataset

Parameters:
  • block_vector – game record db

  • indices – list of pairs each of which forms (block_id, record_id)

  • inputs – output buffer for all input features

  • policy_labels – output buffer for all policy labels

  • value_labels – output buffer for all value labels

  • inputs2 – afterstate features (optional)

  • legalmoves – legal moves in bitset (optional)

  • sampled_id – list of move_id sampled for each game (optional)