lczerolens.search#
Search utilities.
Classes#
Heuristic protocol for evaluating chess positions. |
|
Simple heuristic for MCTS. |
|
Heuristic that outputs uniform policy and material advantage as value. |
|
Evaluate boards using a neural network model for MCTS. |
|
Node for MCTS using LczeroBoard. |
|
Monte Carlo Tree Search with PUCT formula. |
Module Contents#
- class lczerolens.search.Heuristic[source]#
Bases:
ProtocolHeuristic protocol for evaluating chess positions.
- evaluate(board)[source]#
Evaluate a single board.
- Parameters:
board (LczeroBoard) – LczeroBoard instance representing the current board state
- Returns:
Dictionary with fields value and policy.
- Return type:
TensorDict
- class lczerolens.search.RandomHeuristic[source]#
Simple heuristic for MCTS.
- evaluate(board)[source]#
Evaluate a single board.
- Parameters:
board (LczeroBoard) – LczeroBoard instance
- Returns:
Dictionary with fields value and policy.
- Return type:
TensorDict
- class lczerolens.search.MaterialHeuristic(piece_values=None, normalization_constant=0.1, activation=torch.tanh)[source]#
Heuristic that outputs uniform policy and material advantage as value.
- Parameters:
piece_values (Optional[Dict[int, int]])
normalization_constant (float)
activation (Callable[[torch.Tensor], torch.Tensor])
- evaluate(board)[source]#
Compute the label for a given model and input.
- Parameters:
board (lczerolens.board.LczeroBoard)
- Return type:
tensordict.TensorDict
- class lczerolens.search.ModelHeuristic(model)[source]#
Evaluate boards using a neural network model for MCTS.
- Parameters:
model (lczerolens.model.LczeroModel)
- evaluate(board)[source]#
Evaluate a single board using the Lczero model.
Returns TensorDict with ‘value’ and ‘policy’.
- Parameters:
board (lczerolens.board.LczeroBoard)
- Return type:
tensordict.TensorDict
- class lczerolens.search.Node(board, parent)[source]#
Node for MCTS using LczeroBoard.
- Parameters:
board (lczerolens.board.LczeroBoard)
parent (Node)
- class lczerolens.search.MCTS(c_puct=1.0, n_parallel_rollouts=1)[source]#
Monte Carlo Tree Search with PUCT formula.
- Parameters:
c_puct (float)
n_parallel_rollouts (int)
- _select_(node)[source]#
Select the move to explore based on the PUCT formula.
- Parameters:
node (Node)
- Return type:
chess.Move
- _backpropagate_(node, value)[source]#
Backpropagate the reward from the leaf node to the root node.
- Parameters:
node (Node) – Node instance representing the leaf node.
value (float) – Float value to backpropagate.
- Return type:
None
- render_tree(max_depth=3, save_to=None, min_visit_percentage=0.0)[source]#
Render the MCTS tree as an SVG.
- Parameters:
root (Node) – Root node of the tree.
max_depth (int, default=3) – Maximum depth to render.
save_to (Optional[str], default=None) – Path to save the SVG. If None, returns the SVG string.
min_visit_percentage (float)
- Returns:
SVG string of the tree, or None if saved to file.
- Return type:
Optional[str]