lczerolens.play#

Play module for the lczerolens package.

Submodules#

Classes#

ModelSampler

Helper class that provides a standard way to create an ABC using

Sampler

Helper class that provides a standard way to create an ABC using

RandomSampler

Helper class that provides a standard way to create an ABC using

PolicySampler

Helper class that provides a standard way to create an ABC using

Game

Puzzle

Package Contents#

class lczerolens.play.ModelSampler[source]#

Bases: Sampler

Helper class that provides a standard way to create an ABC using inheritance.

model: lczerolens.model.LczeroModel#
use_argmax: bool = True#
alpha: float = 1.0#
beta: float = 1.0#
gamma: float = 1.0#
draw_score: float = 0.0#
m_max: float = 0.0345#
m_slope: float = 0.0027#
k_0: float = 0.0#
k_1: float = 1.6521#
k_2: float = -0.6521#
q_threshold: float = 0.8#
choose_move(board, utility, legal_indices)[source]#

Choose the next moves.

Parameters:
  • board (LczeroBoard) – The board.

  • utility (torch.Tensor) – The utility of the board.

  • legal_indices (torch.Tensor) – The legal indices.

Returns:

The iterable over the moves.

Return type:

Iterable[chess.Move]

get_utilities(boards, **kwargs)[source]#

Get the utility of the board.

Parameters:

boards (Iterable[LczeroBoard]) – The boards to evaluate.

Returns:

The iterable over utilities, legal indices, and log dictionaries.

Return type:

Iterable[Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]]

_get_batched_stats(boards, batch_size, use_next_boards=True, **kwargs)[source]#
_get_q_values(batch_stats, to_log)[source]#
_get_m_values(batch_stats, q_values, to_log)[source]#
_get_p_values(batch_stats, legal_indices, to_log)[source]#
_use_callback(callback, batch_stats, to_log)[source]#
class lczerolens.play.Sampler[source]#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstract get_utilities(boards, **kwargs)[source]#

Get the utility of the board.

Parameters:

boards (Iterable[LczeroBoard]) – The boards to evaluate.

Returns:

The iterable over utilities, legal indices, and log dictionaries.

Return type:

Iterable[Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]]

choose_move(board, utility, legal_indices)[source]#

Choose the next moves.

Parameters:
  • board (LczeroBoard) – The board.

  • utility (torch.Tensor) – The utility of the board.

  • legal_indices (torch.Tensor) – The legal indices.

Returns:

The iterable over the moves.

Return type:

Iterable[chess.Move]

get_next_moves(boards, **kwargs)[source]#

Get the next move.

Parameters:

boards (Iterable[LczeroBoard]) – The boards to evaluate.

Returns:

The iterable over the moves and log dictionaries.

Return type:

Iterable[Tuple[chess.Move, Dict[str, float]]]

class lczerolens.play.RandomSampler[source]#

Bases: Sampler

Helper class that provides a standard way to create an ABC using inheritance.

get_utilities(boards, **kwargs)[source]#

Get the utility of the board.

Parameters:

boards (Iterable[LczeroBoard]) – The boards to evaluate.

Returns:

The iterable over utilities, legal indices, and log dictionaries.

Return type:

Iterable[Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]]

class lczerolens.play.PolicySampler[source]#

Bases: ModelSampler

Helper class that provides a standard way to create an ABC using inheritance.

use_suboptimal: bool = False#
get_utilities(boards, **kwargs)[source]#

Get the utility of the board.

Parameters:

boards (Iterable[LczeroBoard]) – The boards to evaluate.

Returns:

The iterable over utilities, legal indices, and log dictionaries.

Return type:

Iterable[Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]]

class lczerolens.play.Game[source]#
gameid: str#
moves: List[str]#
book_exit: int | None = None#
classmethod from_dict(obj)[source]#
Parameters:

obj (Dict[str, str])

Return type:

Game

to_boards(n_history=0, skip_book_exit=False, skip_first_n=0, output_dict=True)[source]#
Parameters:
  • n_history (int)

  • skip_book_exit (bool)

  • skip_first_n (int)

Return type:

List[Union[Dict[str, Any], lczerolens.board.LczeroBoard]]

static board_collate_fn(batch)[source]#
class lczerolens.play.Puzzle[source]#
puzzle_id: str#
fen: str#
initial_move: chess.Move#
moves: List[chess.Move]#
rating: int#
rating_deviation: int#
popularity: int#
nb_plays: int#
themes: List[str]#
game_url: str#
opening_tags: List[str]#
classmethod from_dict(obj)[source]#
Parameters:

obj (Dict[str, Union[str, int, None]])

Return type:

Puzzle

__len__()[source]#
Return type:

int

property initial_board: lczerolens.board.LczeroBoard#
Return type:

lczerolens.board.LczeroBoard

board_move_generator(all_moves=False)[source]#
Parameters:

all_moves (bool)

Return type:

Iterable[Tuple[lczerolens.board.LczeroBoard, chess.Move]]

classmethod evaluate_multiple(puzzles, sampler, all_moves=False, compute_metrics=True, **kwargs)[source]#
Parameters:
Return type:

Union[Iterable[Dict[str, float]], Iterable[Tuple[torch.Tensor, torch.Tensor, chess.Move]]]

evaluate(sampler, all_moves=False, **kwargs)[source]#
Parameters:
Return type:

Tuple[float, Optional[float]]

static compute_metrics(puzzles, inputs, all_moves=False)[source]#
Parameters:
  • puzzles (Iterable[Puzzle])

  • inputs (Iterable[Tuple[torch.Tensor, torch.Tensor, chess.Move]])

  • all_moves (bool)

Return type:

Iterable[Dict[str, float]]

_repr_svg_()[source]#
Return type:

str