chipiron.players.move_selector.treevalue.nodes.algorithm_node package
Submodules
chipiron.players.move_selector.treevalue.nodes.algorithm_node.algorithm_node module
This module defines the AlgorithmNode class, which is a generic node used by the tree and value algorithm. It wraps tree nodes with values, minimax computation, and exploration tools.
- class chipiron.players.move_selector.treevalue.nodes.algorithm_node.algorithm_node.AlgorithmNode(tree_node: TreeNode[Any], minmax_evaluation: NodeMinmaxEvaluation, exploration_index_data: NodeExplorationData | None, board_representation: BoardRepresentation | None)[source]
Bases:
objectThe generic Node used by the tree and value algorithm. It wraps tree nodes with values, minimax computation and exploration tools
- property all_legal_moves_generated: bool
Returns True if all legal moves have been generated, False otherwise.
- Returns:
True if all legal moves have been generated, False otherwise.
- Return type:
bool
- board_representation: BoardRepresentation | None
- dot_description() str[source]
Returns the dot description of the node.
- Returns:
The dot description of the node.
- Return type:
str
- exploration_index_data: NodeExplorationData | None
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
Returns the fast representation of the node.
- Returns:
The fast representation of the node.
- Return type:
str
- property half_move: int
Returns the half move count.
- Returns:
The half move count.
- Return type:
int
- property id: int
Returns the ID of the node.
- Returns:
The ID of the node.
- Return type:
int
- is_over() bool[source]
Checks if the game is over.
- Returns:
True if the game is over, False otherwise.
- Return type:
bool
- property legal_moves: LegalMoveKeyGeneratorP
Returns the legal move generator.
- Returns:
The legal move generator.
- Return type:
chess.LegalMoveGenerator
- minmax_evaluation: NodeMinmaxEvaluation
- property moves_children: dict[int, Optional[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any]]]
Returns the bidirectional dictionary of moves and their corresponding child nodes.
- property non_opened_legal_moves: set[int]
Returns the set of non-opened legal moves.
- Returns:
The set of non-opened legal moves.
- Return type:
set[IMove]
- property parent_nodes: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any], 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.
- property player_to_move: bool
Returns the color of the player to move.
- Returns:
The color of the player to move.
- Return type:
chess.Color
chipiron.players.move_selector.treevalue.nodes.algorithm_node.node_minmax_evaluation module
- This module contains the implementation of the NodeMinmaxEvaluation class, which represents a node in a tree structure
used for the Minimax algorithm evaluation.
- The NodeMinmaxEvaluation class stores information about the evaluation of a tree node, including the estimated value
- for the white player, the computed value using the Minimax procedure, the best node sequence, and the children of
the tree node sorted by their evaluations.
- It also provides methods for accessing and manipulating the evaluation values, determining the subjective value from
the point of view of the player to move, finding the best child node, checking if the node is over, and printing information about the node.
Note: This code snippet is a partial implementation and may require additional code to work properly.
- class chipiron.players.move_selector.treevalue.nodes.algorithm_node.node_minmax_evaluation.NodeMinmaxEvaluation(tree_node: ~chipiron.players.move_selector.treevalue.nodes.tree_node.TreeNode[~chipiron.players.move_selector.treevalue.nodes.algorithm_node.node_minmax_evaluation.NodeWithValue], value_white_evaluator: float | None = None, value_white_minmax: float | None = None, best_move_sequence: list[int] = <factory>, moves_sorted_by_value_: dict[int, typing.Any] = <factory>, best_index_for_value: int = 0, moves_not_over: list[int] = <factory>, over_event: ~chipiron.players.boardevaluators.over_event.OverEvent = <factory>)[source]
Bases:
objectRepresents a node in a tree structure used for the Minimax algorithm evaluation.
- Variables:
tree_node (TreeNode) – A reference to the original tree node that is evaluated.
value_white_evaluator (float | None) – The absolute value with respect to the white player as estimated
evaluator. (by an) –
value_white_minmax (float | None) – The absolute value with respect to the white player as computed from
node (the value_white_* of the descendants of this) –
best_node_sequence (list[ITreeNode]) – The sequence of best nodes found during the Minimax evaluation.
children_sorted_by_value\_ (dict[ITreeNode, Any]) – The children of the tree node kept in a dictionary
evaluations. (that can be sorted by their) –
best_index_for_value (int) – The index of the best value in the children_sorted_by_value dictionary.
children_not_over (list[ITreeNode]) – The list of children that have not yet been found to be over.
over_event (OverEvent) – The event that determines if the node is over.
- are_almost_equal_values(value_1: float, value_2: float) bool[source]
Check if two float values are almost equal within a small epsilon.
- Parameters:
value_1 (float) – The first value to compare.
value_2 (float) – The second value to compare.
- Returns:
True if the values are almost equal, False otherwise.
- Return type:
bool
- are_considered_equal_values(value_1: tuple[T], value_2: tuple[T]) bool[source]
Check if two values are considered equal.
- Parameters:
value_1 (tuple[T]) – The first value to compare.
value_2 (tuple[T]) – The second value to compare.
- Returns:
True if the values are considered equal, False otherwise.
- Return type:
bool
- are_equal_values(value_1: T, value_2: T) bool[source]
Check if two values are equal.
- Parameters:
value_1 (T) – The first value to compare.
value_2 (T) – The second value to compare.
- Returns:
True if the values are equal, False otherwise.
- Return type:
bool
- becoming_over_from_children() None[source]
This node is asked to switch to over status.
This method is called when the node is requested to switch to the “over” status. It performs the necessary operations to update the node’s status and determine the winner.
- Raises:
AssertionError – If the node is already in the “over” status.
- best_index_for_value: int
- best_move() int | None[source]
Returns the best move node based on the subjective value.
- Returns:
The best move based on the subjective value, or None if there are no move open.
- best_move_not_over() int[source]
Returns the best move that is not leading to a game-over.
- Returns:
The best move that is not leading to a game-over.
- Raises:
Exception – If no move is found that is not over.
- best_move_sequence: list[int]
- best_move_value() float | None[source]
Returns the value of the best move.
If the moves_sorted_by_value dictionary is not empty, it returns the value of the first child node with the highest subjective value. Otherwise, it returns None.
- Returns:
The value of the best child node, or None if there are no child nodes.
- Return type:
float | None
- description_best_move_sequence() str[source]
Returns a string representation of the best move sequence.
This method iterates over the best node sequence and constructs a string representation of the moves in the sequence. Each move is appended to the result string, separated by an underscore.
- Returns:
A string representation of the best move sequence.
- description_tree_visualizer_move(child: Any) str[source]
Returns a string representation of the move for the tree visualizer.
Parameters: - child (Any): The child node representing the move.
Returns: - str: A string representation of the move for the tree visualizer.
- dot_description() str[source]
Returns a string representation of the node’s description in DOT format.
The description includes the values of value_white_minmax and value_white_evaluator, as well as the best move sequence and the over event tag.
- Returns:
A string representation of the node’s description in DOT format.
- evaluate() FloatyBoardEvaluation | ForcedOutcome[source]
- get_all_of_the_best_moves(how_equal: str | None = None) list[int][source]
Returns a list of all the best moves based on the specified equality criteria.
- Parameters:
how_equal (str | None) – The equality criteria to determine the best moves. Possible values are ‘equal’, ‘considered_equal’, ‘almost_equal’, ‘almost_equal_logistic’. Defaults to None.
- Returns:
A list of IMove representing the best moves.
- Return type:
list[IMove]
- get_value_white() float[source]
Returns the best estimation of the value for white in this node.
- Returns:
The best estimation of the value for white in this node.
- Return type:
float
- is_draw() bool[source]
Checks if the current game state is a draw.
- Returns:
True if the game state is a draw, False otherwise.
- Return type:
bool
- is_over() bool[source]
Checks if the game is over.
- Returns:
True if the game is over, False otherwise.
- Return type:
bool
- is_value_subjectively_better_than_evaluation(value_white: float) bool[source]
Checks if the given value_white is subjectively better than the value_white_evaluator.
- Parameters:
value_white (float) – The value to compare with the value_white_evaluator.
- Returns:
True if the value_white is subjectively better than the value_white_evaluator, False otherwise.
- Return type:
bool
- is_win() bool[source]
Checks if the current game state is a win.
- Returns:
True if the game state is a win, False otherwise.
- Return type:
bool
- is_winner(player: bool) bool[source]
Determines if the specified player is the winner.
- Parameters:
player (chess.Color) – The color of the player to check.
- Returns:
True if the player is the winner, False otherwise.
- Return type:
bool
- minmax_value_update_from_children(moves_with_updated_value: set[int]) tuple[bool, bool][source]
Updates the value and best move of the node based on the updated values of its children.
- Parameters:
moves_with_updated_value (set[IMove]) – A set of moves with updated values.
- Returns:
A tuple containing two boolean values indicating whether the value and best move have changed.
- Return type:
tuple[bool, bool]
- moves_not_over: list[int]
- property moves_sorted_by_value: dict[int, Any]
Returns a dictionary containing the moves of the node sorted by their values.
- Returns:
A dictionary where the keys are the moves in the node and the values are their corresponding values.
- Return type:
dict[IMove, Any]
- moves_sorted_by_value_: dict[int, Any]
- my_logit(x: float) float[source]
Applies the logit function to the input value.
- Parameters:
x (float) – The input value.
- Returns:
The result of applying the logit function to the input value.
- Return type:
float
- one_of_best_children_becomes_best_next_node() None[source]
Triggered when the value of the current best move does not match the best value.
This method selects one of the best children nodes as the next best node based on a specific condition. It updates the best_move_sequence attribute with the selected child node and its corresponding best move sequence.
- Raises:
AssertionError – If the number of best children is not equal to 1 when how_equal_ is set to ‘equal’.
AssertionError – If the selected best child is not an instance of NodeWithValue.
AssertionError – If the best_node_sequence attribute is empty after updating.
- print_best_line() None[source]
Prints the best line from the current node to the leaf node.
The best line is determined by following the sequence of child nodes with the highest values. Each child node is printed along with its corresponding move and node ID.
- Returns:
None
- print_info() None[source]
Prints information about the node.
This method prints the ID of the node, the moves of its children, the children sorted by value, and the children that are not over.
- print_moves_not_over() None[source]
Prints the moves that are not over.
This method prints the moves that are not marked as ‘over’. It iterates over the moves_not_over list and prints each child’s ID.
- Returns:
None
- print_moves_sorted_by_value() None[source]
Prints the moves sorted by their subjective sort value.
The method iterates over the move_sorted_by_value dictionary and prints each move along with its subjective sort value. The output is formatted as follows: “<move>: <subjective_sort_value> $$”
- Returns:
None
- print_moves_sorted_by_value_and_exploration() None[source]
Prints the move of the node sorted by their value and exploration.
This method prints the moves of the node along with their subjective sort value. The moves are sorted based on their value and exploration.
- Parameters:
None –
- Returns:
None
- record_sort_value_of_child(move: int) None[source]
Stores the subjective value of the move in the self.moves_sorted_by_value (automatically sorted).
- Parameters:
move (IMove) – The move whose value needs to be recorded.
- Returns:
None
- second_best_move() int[source]
Returns the second-best move based on the subjective value.
- Returns:
The second-best move.
- set_evaluation(evaluation: float) None[source]
sets the evaluation from the board evaluator
- Parameters:
evaluation (float) – The evaluation value to be set.
- Returns:
None
- sort_moves_not_over() list[int][source]
Sorts the moves that are not over based on their value.
- Returns:
A sorted list of moves that are not over.
- subjective_value() float[source]
Return the subjective value of self.value_white from the point of view of the self.tree_node.player_to_move.
If the player to move is chess.WHITE, the subjective value is self.get_value_white(). If the player to move is not chess.WHITE, the subjective value is -self.get_value_white().
- Returns:
The subjective value of self.value_white.
- Return type:
float
- subjective_value_(value_white: float) float[source]
Return the subjective value of value_white from the point of view of the self.tree_node.player_to_move.
The subjective value is calculated based on the player to move. If the player to move is chess.WHITE, then the value_white is returned as is. Otherwise, the negative of value_white is returned.
- Parameters:
value_white (float) – The value from the point of view of the white player.
- Returns:
The subjective value of value_white based on the player to move.
- Return type:
float
- subjective_value_of(another_node_eval: Self) float[source]
Calculates the subjective value of the current node evaluation based on the player to move.
- Parameters:
another_node_eval (Self) – The evaluation of another node.
- Returns:
The subjective value of the current node evaluation.
- Return type:
float
- test() None[source]
This method is used to test the functionality of the algorithm_node’s test method. It calls various test methods to ensure the correctness of the algorithm_node’s behavior.
- test_children_not_over() None[source]
Test if the children of the algorithm node are correctly classified as over or not over.
This method iterates over the moves_children dictionary of the tree_node and checks if each child is correctly classified as over or not over. It asserts that if a child is over, it should not be present in the children_not_over set, and if a child is not over, it should be present in the children_not_over set.
- test_values() None[source]
Test the values of the algorithm node and its children.
This method asserts various conditions to ensure that the values of the algorithm node and its children are correct. It checks if the values of the children are consistent with the minmax evaluation values. It also checks if the maximum or minimum value among the children matches the value of the algorithm node, depending on the player to move.
- tree_node: TreeNode[NodeWithValue]
- update_best_move_sequence(moves_with_updated_best_move_seq: set[int]) bool[source]
Updates the best move sequence based on the notification from children nodes identified through their corresponding move.
- Parameters:
moves_with_updated_best_move_seq (set[IMove]) – A set of move that have notified an updated best-move sequence.
- Returns:
True if self.best_move_sequence is modified, False otherwise.
- Return type:
bool
- update_moves_values(moves_to_consider: set[int]) None[source]
Updates the values of the moves based on the given set of moves to consider.
- Parameters:
moves_to_consider (set[IMove]) – The set of moves to consider.
- Returns:
None
- update_over(moves_with_updated_over: set[int]) bool[source]
Update the over_event of the node based on notification of change of over_event in children.
- Parameters:
moves_with_updated_over (set[IMove]) – A set of chess moves linking to the children nodes that have been updated with their over_event.
- Returns:
True if the node has become newly over, False otherwise.
- Return type:
bool
- update_value_minmax() None[source]
Updates the minmax value for the current node based on the best child node’s evaluation.
If all the children of the current node have been evaluated, the minmax value is set to the best child’s evaluation value. Otherwise, if not all children have been evaluated, the minmax value is determined by comparing the best child’s evaluation value with the current node’s own evaluation value.
Note: The evaluation values are specific to the white player.
- Returns:
None
- value_white_evaluator: float | None
- value_white_minmax: float | None
- class chipiron.players.move_selector.treevalue.nodes.algorithm_node.node_minmax_evaluation.NodeWithValue(*args, **kwargs)[source]
Bases:
ITreeNode[Any],ProtocolRepresents a node with a value in a tree structure.
- Variables:
minmax_evaluation (NodeMinmaxEvaluation) – The minmax evaluation associated with the node.
tree_node (TreeNode) – The tree node associated with the node.
- minmax_evaluation: NodeMinmaxEvaluation
Module contents
init file for algorithm_node module
- class chipiron.players.move_selector.treevalue.nodes.algorithm_node.AlgorithmNode(tree_node: TreeNode[Any], minmax_evaluation: NodeMinmaxEvaluation, exploration_index_data: NodeExplorationData | None, board_representation: BoardRepresentation | None)[source]
Bases:
objectThe generic Node used by the tree and value algorithm. It wraps tree nodes with values, minimax computation and exploration tools
- property all_legal_moves_generated: bool
Returns True if all legal moves have been generated, False otherwise.
- Returns:
True if all legal moves have been generated, False otherwise.
- Return type:
bool
- board_representation: BoardRepresentation | None
- dot_description() str[source]
Returns the dot description of the node.
- Returns:
The dot description of the node.
- Return type:
str
- exploration_index_data: NodeExplorationData | None
- property fast_rep: tuple[int, int, int, int, int, int, bool, int, int | None, int, int, int, int, int]
Returns the fast representation of the node.
- Returns:
The fast representation of the node.
- Return type:
str
- property half_move: int
Returns the half move count.
- Returns:
The half move count.
- Return type:
int
- property id: int
Returns the ID of the node.
- Returns:
The ID of the node.
- Return type:
int
- is_over() bool[source]
Checks if the game is over.
- Returns:
True if the game is over, False otherwise.
- Return type:
bool
- property legal_moves: LegalMoveKeyGeneratorP
Returns the legal move generator.
- Returns:
The legal move generator.
- Return type:
chess.LegalMoveGenerator
- minmax_evaluation: NodeMinmaxEvaluation
- property moves_children: dict[int, Optional[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any]]]
Returns the bidirectional dictionary of moves and their corresponding child nodes.
- property non_opened_legal_moves: set[int]
Returns the set of non-opened legal moves.
- Returns:
The set of non-opened legal moves.
- Return type:
set[IMove]
- property parent_nodes: dict[chipiron.players.move_selector.treevalue.nodes.itree_node.ITreeNode[Any], 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.
- property player_to_move: bool
Returns the color of the player to move.
- Returns:
The color of the player to move.
- Return type:
chess.Color