chipiron.players.move_selector.treevalue.nodes package
Subpackages
- chipiron.players.move_selector.treevalue.nodes.algorithm_node package
- Submodules
- chipiron.players.move_selector.treevalue.nodes.algorithm_node.algorithm_node module
AlgorithmNodeAlgorithmNode.add_parent()AlgorithmNode.all_legal_moves_generatedAlgorithmNode.boardAlgorithmNode.board_representationAlgorithmNode.dot_description()AlgorithmNode.exploration_index_dataAlgorithmNode.fast_repAlgorithmNode.half_moveAlgorithmNode.idAlgorithmNode.is_over()AlgorithmNode.legal_movesAlgorithmNode.minmax_evaluationAlgorithmNode.moves_childrenAlgorithmNode.non_opened_legal_movesAlgorithmNode.parent_nodesAlgorithmNode.player_to_moveAlgorithmNode.tree_node
- chipiron.players.move_selector.treevalue.nodes.algorithm_node.node_minmax_evaluation module
NodeMinmaxEvaluationNodeMinmaxEvaluation.are_almost_equal_values()NodeMinmaxEvaluation.are_considered_equal_values()NodeMinmaxEvaluation.are_equal_values()NodeMinmaxEvaluation.becoming_over_from_children()NodeMinmaxEvaluation.best_index_for_valueNodeMinmaxEvaluation.best_move()NodeMinmaxEvaluation.best_move_not_over()NodeMinmaxEvaluation.best_move_sequenceNodeMinmaxEvaluation.best_move_value()NodeMinmaxEvaluation.description_best_move_sequence()NodeMinmaxEvaluation.description_tree_visualizer_move()NodeMinmaxEvaluation.dot_description()NodeMinmaxEvaluation.evaluate()NodeMinmaxEvaluation.get_all_of_the_best_moves()NodeMinmaxEvaluation.get_value_white()NodeMinmaxEvaluation.is_draw()NodeMinmaxEvaluation.is_over()NodeMinmaxEvaluation.is_value_subjectively_better_than_evaluation()NodeMinmaxEvaluation.is_win()NodeMinmaxEvaluation.is_winner()NodeMinmaxEvaluation.minmax_value_update_from_children()NodeMinmaxEvaluation.moves_not_overNodeMinmaxEvaluation.moves_sorted_by_valueNodeMinmaxEvaluation.moves_sorted_by_value_NodeMinmaxEvaluation.my_logit()NodeMinmaxEvaluation.one_of_best_children_becomes_best_next_node()NodeMinmaxEvaluation.over_eventNodeMinmaxEvaluation.print_best_line()NodeMinmaxEvaluation.print_info()NodeMinmaxEvaluation.print_moves_not_over()NodeMinmaxEvaluation.print_moves_sorted_by_value()NodeMinmaxEvaluation.print_moves_sorted_by_value_and_exploration()NodeMinmaxEvaluation.record_sort_value_of_child()NodeMinmaxEvaluation.second_best_move()NodeMinmaxEvaluation.set_evaluation()NodeMinmaxEvaluation.sort_moves_not_over()NodeMinmaxEvaluation.subjective_value()NodeMinmaxEvaluation.subjective_value_()NodeMinmaxEvaluation.subjective_value_of()NodeMinmaxEvaluation.test()NodeMinmaxEvaluation.test_children_not_over()NodeMinmaxEvaluation.test_over()NodeMinmaxEvaluation.test_values()NodeMinmaxEvaluation.tree_nodeNodeMinmaxEvaluation.update_best_move_sequence()NodeMinmaxEvaluation.update_moves_values()NodeMinmaxEvaluation.update_over()NodeMinmaxEvaluation.update_value_minmax()NodeMinmaxEvaluation.value_white_evaluatorNodeMinmaxEvaluation.value_white_minmax
NodeWithValue
- Module contents
AlgorithmNodeAlgorithmNode.add_parent()AlgorithmNode.all_legal_moves_generatedAlgorithmNode.boardAlgorithmNode.board_representationAlgorithmNode.dot_description()AlgorithmNode.exploration_index_dataAlgorithmNode.fast_repAlgorithmNode.half_moveAlgorithmNode.idAlgorithmNode.is_over()AlgorithmNode.legal_movesAlgorithmNode.minmax_evaluationAlgorithmNode.moves_childrenAlgorithmNode.non_opened_legal_movesAlgorithmNode.parent_nodesAlgorithmNode.player_to_moveAlgorithmNode.tree_node
Submodules
chipiron.players.move_selector.treevalue.nodes.itree_node module
This module defines the interface for a tree node in a chess move selector.
The ITreeNode protocol represents a node in a tree structure used for selecting chess moves. It provides properties and methods for accessing information about the node, such as its ID, the chess board state, the half move count, the child nodes, and the parent nodes.
The ITreeNode protocol also defines methods for adding a parent node, generating a dot description for visualization, checking if all legal moves have been generated, accessing the legal moves, and checking if the game is over.
Note: This is an interface and should not be instantiated directly.
- class chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode(*args, **kwargs)[source]
Bases:
Protocol,GenericThe ITreeNode protocol represents a node in a tree structure used for selecting chess moves.
- add_parent(move: moveKey, new_parent_node: ITreeNode[T]) None[source]
Add a parent node to the node.
- Parameters:
new_parent_node – The parent node to add.
move (chess.Move) – the move that led to the node from the new_parent_node
- property all_legal_moves_generated: bool
Check if all legal moves have been generated.
- Returns:
True if all legal moves have been generated, False otherwise.
- property board: IBoard
Get the chess board state of the node.
- Returns:
The chess board state of the node.
- dot_description() str[source]
Generate a dot description for visualization.
- Returns:
A string containing the dot description.
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
Get the fast representation of the node.
- Returns:
The fast representation of the node as a string.
- property half_move: int
Get the half move count of the node.
- Returns:
The half move count of the node.
- property id: int
Get the ID of the node.
- Returns:
The ID of the node.
- is_over() bool[source]
Check if the game is over.
- Returns:
True if the game is over, False otherwise.
- property legal_moves: LegalMoveKeyGeneratorP
Get the legal moves of the node.
- Returns:
A generator for iterating over the legal moves.
- property moves_children: dict[moveKey, T | None]
Get the child nodes of the node.
- Returns:
A bidirectional dictionary mapping chess moves to child nodes.
chipiron.players.move_selector.treevalue.nodes.noisy_value_tree_node module
This module contains the NoisyValueTreeNode class, which is a subclass of TreeNode.
- class chipiron.players.move_selector.treevalue.nodes.noisy_value_tree_node.NoisyValueTreeNode(board: BoardChi, half_move: int, id_number: int, parent_node: ITreeNode[Any], last_move: Move)[source]
Bases:
TreeNode[Any]A class representing a node in a noisy value tree. Inherits from TreeNode.
- all_legal_moves_generated: bool
- board_: boards.IBoard
- dot_description() str[source]
Returns the dot description of the node.
- Returns:
The dot description.
- Return type:
str
- half_move_: int
- id_: int
- moves_children_: dict[moveKey, ChildrenType | None]
- non_opened_legal_moves: set[moveKey]
- player_to_move_: chess.Color
chipiron.players.move_selector.treevalue.nodes.test_nodes module
chipiron.players.move_selector.treevalue.nodes.tree_node module
This module defines the TreeNode class, which represents a node in a tree structure for a chess game.
- class chipiron.players.move_selector.treevalue.nodes.tree_node.TreeNode(id_: int, half_move_: int, board_: ~chipiron.environments.chess_env.board.iboard.IBoard, parent_nodes_: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int], all_legal_moves_generated: bool = False, non_opened_legal_moves: set[int] = <factory>, moves_children_: dict[int, typing.Optional[ChildrenType]] = <factory>, player_to_move_: bool = <factory>)[source]
Bases:
GenericThe TreeNode class stores information about a specific board position, including the board representation, the player to move, the half-move count, and the parent-child relationships with other nodes.
- Variables:
id\_ (int) – The number to identify this node for easier debugging.
half_move\_ (int) – The number of half-moves since the start of the game to reach the board position.
board\_ (boards.BoardChi) – The board representation of the node.
parent_nodes\_ (set[ITreeNode]) – The set of parent nodes to this node.
all_legal_moves_generated (bool) – A boolean indicating whether all moves have been generated.
non_opened_legal_moves (set[chess.Move]) – The set of non-opened legal moves.
moves_children\_ (dict[chess.Move, ITreeNode | None]) – The dictionary mapping moves to child nodes.
fast_rep (str) – The fast representation of the board.
player_to_move\_ (chess.Color) – The color of the player that has to move in the board.
- id()
Returns the id of the node.
- player_to_move()
Returns the color of the player to move.
- board()
Returns the board representation.
- half_move()
Returns the number of half-moves.
- moves_children()
Returns the dictionary mapping moves to child nodes.
- parent_nodes()
Returns the set of parent nodes.
- legal_moves()
Returns the legal moves of the board.
- add_parent(new_parent_node
ITreeNode): Adds a parent node to the current node.
- get_descendants()
Returns a dictionary of descendants of the node.
- __post_init__() None[source]
Performs post-initialization tasks for the TreeNode class.
This method is automatically called after the object is initialized. It sets the fast_rep attribute based on the board’s fast representation and assigns the current player to player_to_move_.
- Parameters:
None –
- Returns:
None
- add_parent(move: int, new_parent_node: ITreeNode) None[source]
Adds a new parent node to the current node.
- Parameters:
move (chess.Move) – the move that led to the node from the new_parent_node
new_parent_node (ITreeNode) – The new parent node to be added.
- Raises:
AssertionError – If the new parent node is already in the parent nodes set.
- Returns:
None
- all_legal_moves_generated: bool
- property board: IBoard
Returns the board associated with this tree node.
- Returns:
The board associated with this tree node.
- Return type:
boards.BoardChi
- dot_description() str[source]
Returns a string representation of the node in the DOT format.
The string includes the node’s ID, half move, and board FEN.
- Returns:
A string representation of the node in the DOT format.
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
- property half_move: int
Returns the number of half moves made in the game.
- Returns:
The number of half moves made.
- Return type:
int
- half_move_: int
- property id: int
Returns the ID of the tree node.
- Returns:
The ID of the tree node.
- Return type:
int
- id_: int
- is_over() bool[source]
Checks if the game is over.
- Returns:
True if the game is over, False otherwise.
- Return type:
bool
- is_root_node() bool[source]
Check if the current node is a root node.
- Returns:
True if the node is a root node, False otherwise.
- Return type:
bool
- property legal_moves: LegalMoveKeyGeneratorP
Returns a generator that yields the legal moves for the current board state.
- Returns:
A generator that yields the legal moves.
- Return type:
chess.LegalMoveGenerator
- property moves_children: dict[int, Optional[ChildrenType]]
Returns a bidirectional dictionary containing the children nodes of the current tree node, along with the corresponding chess moves that lead to each child node.
- Returns:
A bidirectional dictionary mapping chess moves to the corresponding child nodes. If a move does not have a corresponding child node, it is mapped to None.
- Return type:
dict[chess.Move, ITreeNode | None]
- moves_children_: dict[int, Optional[ChildrenType]]
- non_opened_legal_moves: set[int]
- property parent_nodes: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int]
Returns the dictionary of parent nodes of the current tree node with associated move.
- Returns:
A dictionary of parent nodes of the current tree node with associated move.
- parent_nodes_: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int]
- property player_to_move: bool
Returns the color of the player who is to make the next move.
- Returns:
The color of the player who is to make the next move.
- Return type:
chess.Color
- player_to_move_: bool
- print_moves_children() None[source]
Prints the moves-children link of the node.
This method prints the moves-children link of the node, showing the move and the ID of the child node. If a child node is None, it will be displayed as ‘None’.
- Returns:
None
- test() None[source]
This method is used to test the node. It calls the test_all_legal_moves_generated method to test all legal moves generated.
- test_all_legal_moves_generated() None[source]
Test whether all legal moves are generated correctly.
This method checks if all legal moves are correctly generated by comparing them with the moves stored in the moves_children_ attribute. If all_legal_moves_generated is True, it asserts that each move in board.legal_moves is either present in moves_children_ or not present in non_opened_legal_moves. If all_legal_moves_generated is False, it checks if there are any moves in board.legal_moves that are not present in moves_children_.
- Raises:
AssertionError – If the generated moves do not match the expected moves.
chipiron.players.move_selector.treevalue.nodes.tree_traversal module
This module provides functions for traversing a tree of nodes.
The functions in this module allow you to retrieve descendants of a given node in a tree structure.
- chipiron.players.move_selector.treevalue.nodes.tree_traversal.get_descendants(from_tree_node: ITreeNode[Any]) dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any], None][source]
Get all descendants of a given tree node.
- chipiron.players.move_selector.treevalue.nodes.tree_traversal.get_descendants_candidate_not_over(from_tree_node: ITreeNode[Any], max_depth: int | None = None) list[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any]][source]
Get descendants of a given tree node that are not over.
- chipiron.players.move_selector.treevalue.nodes.tree_traversal.get_descendants_candidate_to_open(from_tree_node: AlgorithmNode, max_depth: int | None = None) list[chipiron.players.move_selector.treevalue.nodes.algorithm_node.algorithm_node.AlgorithmNode][source]
Get descendants of a given tree node that are not over.
- Parameters:
from_tree_node (AlgorithmNode) – The starting tree node.
max_depth (int | None, optional) – The maximum depth to traverse. Defaults to None.
- Returns:
A list of descendants that are not over.
- Return type:
list[AlgorithmNode]
chipiron.players.move_selector.treevalue.nodes.utils module
This module contains utility functions for working with tree nodes in the move selector.
Functions: - are_all_moves_and_children_opened(tree_node: TreeNode) -> bool: Checks if all moves and children of a tree node are opened. - a_move_sequence_from_root(tree_node: ITreeNode) -> list[str]: Returns a list of move sequences from the root node to a given tree node. - print_a_move_sequence_from_root(tree_node: TreeNode) -> None: Prints the move sequence from the root node to a given tree node. - is_winning(node_minmax_evaluation: NodeMinmaxEvaluation, color: chess.Color) -> bool: Checks if the color to play in the node is winning.
- chipiron.players.move_selector.treevalue.nodes.utils.a_move_key_sequence_from_root(tree_node: ITreeNode[Any]) list[str][source]
Returns a list of move sequences from the root node to a given tree node.
- Parameters:
tree_node (ITreeNode) – The tree node to get the move sequence for.
- Returns:
A list of move sequences from the root node to the given tree node.
- Return type:
list[str]
- chipiron.players.move_selector.treevalue.nodes.utils.a_move_uci_sequence_from_root(tree_node: ITreeNode[Any]) list[str][source]
Returns a list of move sequences from the root node to a given tree node.
- Parameters:
tree_node (ITreeNode) – The tree node to get the move sequence for.
- Returns:
A list of move sequences from the root node to the given tree node.
- Return type:
list[str]
- chipiron.players.move_selector.treevalue.nodes.utils.are_all_moves_and_children_opened(tree_node: TreeNode[Any]) bool[source]
Checks if all moves and children of a tree node are opened.
- Parameters:
tree_node (TreeNode) – The tree node to check.
- Returns:
True if all moves and children are opened, False otherwise.
- Return type:
bool
- chipiron.players.move_selector.treevalue.nodes.utils.best_node_sequence_from_node(tree_node: AlgorithmNode) list[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any]][source]
- chipiron.players.move_selector.treevalue.nodes.utils.is_winning(node_minmax_evaluation: NodeMinmaxEvaluation, color: bool) bool[source]
Checks if the color to play in the node is winning.
- Parameters:
node_minmax_evaluation (NodeMinmaxEvaluation) – The evaluation of the node.
color (chess.Color) – The color to check.
- Returns:
True if the color is winning, False otherwise.
- Return type:
bool
Module contents
This module contains the implementation of tree nodes for move selection.
The tree nodes are used in the move selector to represent different moves and their values.
Classes: - TreeNode: Represents a tree node for move selection. - ITreeNode: Interface for tree nodes.
- class chipiron.players.move_selector.treevalue.nodes.ITreeNode(*args, **kwargs)[source]
Bases:
Protocol,GenericThe ITreeNode protocol represents a node in a tree structure used for selecting chess moves.
- add_parent(move: moveKey, new_parent_node: ITreeNode[T]) None[source]
Add a parent node to the node.
- Parameters:
new_parent_node – The parent node to add.
move (chess.Move) – the move that led to the node from the new_parent_node
- property all_legal_moves_generated: bool
Check if all legal moves have been generated.
- Returns:
True if all legal moves have been generated, False otherwise.
- property board: IBoard
Get the chess board state of the node.
- Returns:
The chess board state of the node.
- dot_description() str[source]
Generate a dot description for visualization.
- Returns:
A string containing the dot description.
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
Get the fast representation of the node.
- Returns:
The fast representation of the node as a string.
- property half_move: int
Get the half move count of the node.
- Returns:
The half move count of the node.
- property id: int
Get the ID of the node.
- Returns:
The ID of the node.
- is_over() bool[source]
Check if the game is over.
- Returns:
True if the game is over, False otherwise.
- property legal_moves: LegalMoveKeyGeneratorP
Get the legal moves of the node.
- Returns:
A generator for iterating over the legal moves.
- property moves_children: dict[moveKey, T | None]
Get the child nodes of the node.
- Returns:
A bidirectional dictionary mapping chess moves to child nodes.
- class chipiron.players.move_selector.treevalue.nodes.TreeNode(id_: int, half_move_: int, board_: ~chipiron.environments.chess_env.board.iboard.IBoard, parent_nodes_: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int], all_legal_moves_generated: bool = False, non_opened_legal_moves: set[int] = <factory>, moves_children_: dict[int, typing.Optional[ChildrenType]] = <factory>, player_to_move_: bool = <factory>)[source]
Bases:
GenericThe TreeNode class stores information about a specific board position, including the board representation, the player to move, the half-move count, and the parent-child relationships with other nodes.
- Variables:
id\_ (int) – The number to identify this node for easier debugging.
half_move\_ (int) – The number of half-moves since the start of the game to reach the board position.
board\_ (boards.BoardChi) – The board representation of the node.
parent_nodes\_ (set[ITreeNode]) – The set of parent nodes to this node.
all_legal_moves_generated (bool) – A boolean indicating whether all moves have been generated.
non_opened_legal_moves (set[chess.Move]) – The set of non-opened legal moves.
moves_children\_ (dict[chess.Move, ITreeNode | None]) – The dictionary mapping moves to child nodes.
fast_rep (str) – The fast representation of the board.
player_to_move\_ (chess.Color) – The color of the player that has to move in the board.
- id()
Returns the id of the node.
- player_to_move()
Returns the color of the player to move.
- board()
Returns the board representation.
- half_move()
Returns the number of half-moves.
- moves_children()
Returns the dictionary mapping moves to child nodes.
- parent_nodes()
Returns the set of parent nodes.
- legal_moves()
Returns the legal moves of the board.
- add_parent(new_parent_node
ITreeNode): Adds a parent node to the current node.
- get_descendants()
Returns a dictionary of descendants of the node.
- __post_init__() None[source]
Performs post-initialization tasks for the TreeNode class.
This method is automatically called after the object is initialized. It sets the fast_rep attribute based on the board’s fast representation and assigns the current player to player_to_move_.
- Parameters:
None –
- Returns:
None
- add_parent(move: int, new_parent_node: ITreeNode) None[source]
Adds a new parent node to the current node.
- Parameters:
move (chess.Move) – the move that led to the node from the new_parent_node
new_parent_node (ITreeNode) – The new parent node to be added.
- Raises:
AssertionError – If the new parent node is already in the parent nodes set.
- Returns:
None
- all_legal_moves_generated: bool
- property board: IBoard
Returns the board associated with this tree node.
- Returns:
The board associated with this tree node.
- Return type:
boards.BoardChi
- dot_description() str[source]
Returns a string representation of the node in the DOT format.
The string includes the node’s ID, half move, and board FEN.
- Returns:
A string representation of the node in the DOT format.
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
- property half_move: int
Returns the number of half moves made in the game.
- Returns:
The number of half moves made.
- Return type:
int
- half_move_: int
- property id: int
Returns the ID of the tree node.
- Returns:
The ID of the tree node.
- Return type:
int
- id_: int
- is_over() bool[source]
Checks if the game is over.
- Returns:
True if the game is over, False otherwise.
- Return type:
bool
- is_root_node() bool[source]
Check if the current node is a root node.
- Returns:
True if the node is a root node, False otherwise.
- Return type:
bool
- property legal_moves: LegalMoveKeyGeneratorP
Returns a generator that yields the legal moves for the current board state.
- Returns:
A generator that yields the legal moves.
- Return type:
chess.LegalMoveGenerator
- property moves_children: dict[int, Optional[ChildrenType]]
Returns a bidirectional dictionary containing the children nodes of the current tree node, along with the corresponding chess moves that lead to each child node.
- Returns:
A bidirectional dictionary mapping chess moves to the corresponding child nodes. If a move does not have a corresponding child node, it is mapped to None.
- Return type:
dict[chess.Move, ITreeNode | None]
- moves_children_: dict[int, Optional[ChildrenType]]
- non_opened_legal_moves: set[int]
- property parent_nodes: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int]
Returns the dictionary of parent nodes of the current tree node with associated move.
- Returns:
A dictionary of parent nodes of the current tree node with associated move.
- parent_nodes_: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[ChildrenType], int]
- property player_to_move: bool
Returns the color of the player who is to make the next move.
- Returns:
The color of the player who is to make the next move.
- Return type:
chess.Color
- player_to_move_: bool
- print_moves_children() None[source]
Prints the moves-children link of the node.
This method prints the moves-children link of the node, showing the move and the ID of the child node. If a child node is None, it will be displayed as ‘None’.
- Returns:
None
- test() None[source]
This method is used to test the node. It calls the test_all_legal_moves_generated method to test all legal moves generated.
- test_all_legal_moves_generated() None[source]
Test whether all legal moves are generated correctly.
This method checks if all legal moves are correctly generated by comparing them with the moves stored in the moves_children_ attribute. If all_legal_moves_generated is True, it asserts that each move in board.legal_moves is either present in moves_children_ or not present in non_opened_legal_moves. If all_legal_moves_generated is False, it checks if there are any moves in board.legal_moves that are not present in moves_children_.
- Raises:
AssertionError – If the generated moves do not match the expected moves.