chessai.agents.minimax
1import logging 2import math 3import typing 4 5import chessai.core.action 6import chessai.core.agent 7import chessai.core.gamestate 8import chessai.util.parse 9 10DEFAULT_PLY_COUNT: int = 2 11 12class MinimaxLikeAgent(chessai.core.agent.Agent): 13 """ 14 An agent that follow the general procedure of Minimax, 15 but is abstracted to support things like alpha-beta pruning and expectimax. 16 17 Currently, minimax_step_max(), minimax_step_min(), and minimax_step_expected_min() are all filled with dummy implementations. 18 Child classes should implement those to get proper minimax functionality. 19 """ 20 21 def __init__(self, 22 ply_count: int = DEFAULT_PLY_COUNT, 23 alphabeta_prune: bool = False, 24 expectimax: bool = False, 25 **kwargs: typing.Any) -> None: 26 super().__init__(**kwargs) 27 28 # Parse (possibly string) arguments. 29 ply_count = int(ply_count) 30 alphabeta_prune = chessai.util.parse.boolean(alphabeta_prune) 31 expectimax = chessai.util.parse.boolean(expectimax) 32 33 if (ply_count < 1): 34 raise ValueError(f"Ply count must be at least 1, found {ply_count}.") 35 36 self.ply_count: int = ply_count 37 """ 38 How many minimax plys to descend. 39 A "ply" is one set of actions by each agent. 40 When thinking about minimax in terms of a search tree, 41 a ply will be a max layer followed by as many (usually min) layers until the original agent is reached again. 42 """ 43 44 self.alphabeta_prune: bool = alphabeta_prune 45 """ Whether or not to use alpha-beta pruning. """ 46 47 self.expectimax: bool = expectimax 48 """ Whether or not to use expectimax. """ 49 50 self._stats_states_evaluated: list[int] = [] 51 """ Track how many states have been evaluated for each call to get_action(). """ 52 53 self._stats_nodes_visited: list[int] = [] 54 """ Track how many search nodes have been visited for each call to get_action(). """ 55 56 self._move_count: int = 0 57 """ Track the number of moves taken this game. """ 58 59 def evaluate_state(self, 60 state: chessai.core.gamestate.GameState, 61 action: chessai.core.action.Action | None = None, 62 **kwargs: typing.Any) -> float: 63 self._stats_states_evaluated[-1] += 1 64 return super().evaluate_state(state, action) 65 66 def game_complete(self, final_state: chessai.core.gamestate.GameState) -> None: 67 logging.debug(("Minimax-like agent complete." 68 + " Player: %s, Ply Count: %d, Use Alpha-Beta Pruning: %s, Use Expectimax: %s," 69 + " States Evaluated: %d, Nodes Visited: %d."), 70 self.player, 71 self.ply_count, self.alphabeta_prune, self.expectimax, 72 sum(self._stats_states_evaluated), sum(self._stats_nodes_visited)) 73 74 def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action: 75 # Start the stat collection for this round at 0. 76 self._stats_states_evaluated.append(0) 77 self._stats_nodes_visited.append(0) 78 79 actions, score = self.minimax_step(state, self.ply_count + 1, -math.inf, math.inf) 80 action = self.rng.choice(actions) 81 82 logging.trace("Turn: %d, Available actions: %s.", self._move_count, actions) # type: ignore[attr-defined] # pylint: disable=no-member 83 84 logging.debug("Turn: %d, Minimax Score: %d, Chosen Action: %s, States Evaluated: %d, Nodes Visited: %d.", 85 self._move_count, score, action.uci(), 86 self._stats_states_evaluated[-1], self._stats_nodes_visited[-1]) 87 88 self._move_count += 1 89 90 if (action is None): 91 raise ValueError("Did not get an action out of Minimax.") 92 93 return action 94 95 def minimax_step(self, 96 state: chessai.core.gamestate.GameState, 97 ply_count: int, 98 alpha: float, 99 beta: float, 100 ) -> tuple[list[chessai.core.action.Action], float]: 101 """ 102 Step through one layer (one agent) of minimax and return all the best actions (there may be ties) along with their score. 103 This method will handle various book keeping and call the correct minimax_step_min() or minimax_step_max() method. 104 105 When doing alpha-beta pruning, 106 alpha represents the "best minimum" score 107 while beta represents the "best maximum" score. 108 They will typically start at inf and -inf, respectively. 109 110 Return: ([best action, ...], best score). 111 """ 112 113 self._stats_nodes_visited[-1] += 1 114 115 # If we see ourselves, then we have descended a full ply. 116 if (state.turn == self.player): 117 ply_count -= 1 118 119 # At ply count zero, we just evaluate the current state and return up the tree. 120 # Note that we only hit this when we are the target agent (since we just decremented the ply count). 121 if (ply_count <= 0): 122 return [], self.evaluate_state(state) 123 124 # If the game is over, then stop descending. 125 if (state.is_game_over()): 126 return [], self.evaluate_state(state) 127 128 legal_actions = state.get_legal_actions() 129 130 # Don't consider forfeiting unless we can do nothing else. 131 # This will help keep the game moving along. 132 if ((len(legal_actions) > 1) and (chessai.core.action.ForfeitAction() in legal_actions)): 133 legal_actions.remove(chessai.core.action.ForfeitAction()) 134 135 # Don't consider proposing a draw unless we can do nothing else. 136 # This will help keep the game moving along. 137 if ((len(legal_actions) > 1) and (chessai.core.action.ProposeDrawAction() in legal_actions)): 138 legal_actions.remove(chessai.core.action.ProposeDrawAction()) 139 140 if (state.turn == self.player): 141 # We are considering ourselves, get the max. 142 return self.minimax_step_max(state, ply_count, legal_actions, alpha, beta) 143 144 # We are considering an opposing agent, get the min or expected min. 145 if (self.expectimax): 146 return [], self.minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta) 147 148 return self.minimax_step_min(state, ply_count, legal_actions, alpha, beta) 149 150 def minimax_step_max(self, 151 state: chessai.core.gamestate.GameState, 152 ply_count: int, 153 legal_actions: list[chessai.core.action.Action], 154 alpha: float, 155 beta: float, 156 ) -> tuple[list[chessai.core.action.Action], float]: 157 """ 158 Perform a max step in minimax. 159 minimax_step() has already taken care of all the bookkeeping, 160 this method just needs to return the best actions along with their score. 161 162 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 163 164 The default implementation is just random and does not follow any minimax procedure. 165 Child classes should override this method. 166 167 Return: ([best action, ...], best score). 168 """ 169 170 # Randomly choose an action. 171 action = self.rng.choice(legal_actions) 172 173 # Score the action. 174 successor = state.generate_successor(action, self.rng) 175 _, score = self.minimax_step(successor, ply_count, alpha, beta) 176 177 return [action], score 178 179 def minimax_step_min(self, 180 state: chessai.core.gamestate.GameState, 181 ply_count: int, 182 legal_actions: list[chessai.core.action.Action], 183 alpha: float, 184 beta: float, 185 ) -> tuple[list[chessai.core.action.Action], float]: 186 """ 187 Perform a min step in minimax. 188 minimax_step() has already taken care of all the bookkeeping, 189 this method just needs to return the best actions along with their score. 190 191 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 192 193 The default implementation is just random and does not follow any minimax procedure. 194 Child classes should override this method. 195 196 Return: ([best action, ...], best score). 197 """ 198 199 # Randomly choose an action. 200 action = self.rng.choice(legal_actions) 201 202 # Score the action. 203 successor = state.generate_successor(action, self.rng) 204 _, score = self.minimax_step(successor, ply_count, alpha, beta) 205 206 return [action], score 207 208 def minimax_step_expected_min(self, 209 state: chessai.core.gamestate.GameState, 210 ply_count: int, 211 legal_actions: list[chessai.core.action.Action], 212 alpha: float, 213 beta: float, 214 ) -> float: 215 """ 216 Perform a min step in expectimax. 217 minimax_step() has already taken care of all the bookkeeping, 218 this method just needs to return the expected score. 219 220 Note that unlike minimax_step_max() and minimax_step_min(), 221 no action is returned. 222 223 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 224 225 The default implementation is just random and does not follow any minimax procedure. 226 Child classes should override this method. 227 """ 228 229 # Randomly choose an action. 230 action = self.rng.choice(legal_actions) 231 232 # Score the action. 233 successor = state.generate_successor(action, self.rng) 234 _, score = self.minimax_step(successor, ply_count, alpha, beta) 235 236 return score
13class MinimaxLikeAgent(chessai.core.agent.Agent): 14 """ 15 An agent that follow the general procedure of Minimax, 16 but is abstracted to support things like alpha-beta pruning and expectimax. 17 18 Currently, minimax_step_max(), minimax_step_min(), and minimax_step_expected_min() are all filled with dummy implementations. 19 Child classes should implement those to get proper minimax functionality. 20 """ 21 22 def __init__(self, 23 ply_count: int = DEFAULT_PLY_COUNT, 24 alphabeta_prune: bool = False, 25 expectimax: bool = False, 26 **kwargs: typing.Any) -> None: 27 super().__init__(**kwargs) 28 29 # Parse (possibly string) arguments. 30 ply_count = int(ply_count) 31 alphabeta_prune = chessai.util.parse.boolean(alphabeta_prune) 32 expectimax = chessai.util.parse.boolean(expectimax) 33 34 if (ply_count < 1): 35 raise ValueError(f"Ply count must be at least 1, found {ply_count}.") 36 37 self.ply_count: int = ply_count 38 """ 39 How many minimax plys to descend. 40 A "ply" is one set of actions by each agent. 41 When thinking about minimax in terms of a search tree, 42 a ply will be a max layer followed by as many (usually min) layers until the original agent is reached again. 43 """ 44 45 self.alphabeta_prune: bool = alphabeta_prune 46 """ Whether or not to use alpha-beta pruning. """ 47 48 self.expectimax: bool = expectimax 49 """ Whether or not to use expectimax. """ 50 51 self._stats_states_evaluated: list[int] = [] 52 """ Track how many states have been evaluated for each call to get_action(). """ 53 54 self._stats_nodes_visited: list[int] = [] 55 """ Track how many search nodes have been visited for each call to get_action(). """ 56 57 self._move_count: int = 0 58 """ Track the number of moves taken this game. """ 59 60 def evaluate_state(self, 61 state: chessai.core.gamestate.GameState, 62 action: chessai.core.action.Action | None = None, 63 **kwargs: typing.Any) -> float: 64 self._stats_states_evaluated[-1] += 1 65 return super().evaluate_state(state, action) 66 67 def game_complete(self, final_state: chessai.core.gamestate.GameState) -> None: 68 logging.debug(("Minimax-like agent complete." 69 + " Player: %s, Ply Count: %d, Use Alpha-Beta Pruning: %s, Use Expectimax: %s," 70 + " States Evaluated: %d, Nodes Visited: %d."), 71 self.player, 72 self.ply_count, self.alphabeta_prune, self.expectimax, 73 sum(self._stats_states_evaluated), sum(self._stats_nodes_visited)) 74 75 def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action: 76 # Start the stat collection for this round at 0. 77 self._stats_states_evaluated.append(0) 78 self._stats_nodes_visited.append(0) 79 80 actions, score = self.minimax_step(state, self.ply_count + 1, -math.inf, math.inf) 81 action = self.rng.choice(actions) 82 83 logging.trace("Turn: %d, Available actions: %s.", self._move_count, actions) # type: ignore[attr-defined] # pylint: disable=no-member 84 85 logging.debug("Turn: %d, Minimax Score: %d, Chosen Action: %s, States Evaluated: %d, Nodes Visited: %d.", 86 self._move_count, score, action.uci(), 87 self._stats_states_evaluated[-1], self._stats_nodes_visited[-1]) 88 89 self._move_count += 1 90 91 if (action is None): 92 raise ValueError("Did not get an action out of Minimax.") 93 94 return action 95 96 def minimax_step(self, 97 state: chessai.core.gamestate.GameState, 98 ply_count: int, 99 alpha: float, 100 beta: float, 101 ) -> tuple[list[chessai.core.action.Action], float]: 102 """ 103 Step through one layer (one agent) of minimax and return all the best actions (there may be ties) along with their score. 104 This method will handle various book keeping and call the correct minimax_step_min() or minimax_step_max() method. 105 106 When doing alpha-beta pruning, 107 alpha represents the "best minimum" score 108 while beta represents the "best maximum" score. 109 They will typically start at inf and -inf, respectively. 110 111 Return: ([best action, ...], best score). 112 """ 113 114 self._stats_nodes_visited[-1] += 1 115 116 # If we see ourselves, then we have descended a full ply. 117 if (state.turn == self.player): 118 ply_count -= 1 119 120 # At ply count zero, we just evaluate the current state and return up the tree. 121 # Note that we only hit this when we are the target agent (since we just decremented the ply count). 122 if (ply_count <= 0): 123 return [], self.evaluate_state(state) 124 125 # If the game is over, then stop descending. 126 if (state.is_game_over()): 127 return [], self.evaluate_state(state) 128 129 legal_actions = state.get_legal_actions() 130 131 # Don't consider forfeiting unless we can do nothing else. 132 # This will help keep the game moving along. 133 if ((len(legal_actions) > 1) and (chessai.core.action.ForfeitAction() in legal_actions)): 134 legal_actions.remove(chessai.core.action.ForfeitAction()) 135 136 # Don't consider proposing a draw unless we can do nothing else. 137 # This will help keep the game moving along. 138 if ((len(legal_actions) > 1) and (chessai.core.action.ProposeDrawAction() in legal_actions)): 139 legal_actions.remove(chessai.core.action.ProposeDrawAction()) 140 141 if (state.turn == self.player): 142 # We are considering ourselves, get the max. 143 return self.minimax_step_max(state, ply_count, legal_actions, alpha, beta) 144 145 # We are considering an opposing agent, get the min or expected min. 146 if (self.expectimax): 147 return [], self.minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta) 148 149 return self.minimax_step_min(state, ply_count, legal_actions, alpha, beta) 150 151 def minimax_step_max(self, 152 state: chessai.core.gamestate.GameState, 153 ply_count: int, 154 legal_actions: list[chessai.core.action.Action], 155 alpha: float, 156 beta: float, 157 ) -> tuple[list[chessai.core.action.Action], float]: 158 """ 159 Perform a max step in minimax. 160 minimax_step() has already taken care of all the bookkeeping, 161 this method just needs to return the best actions along with their score. 162 163 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 164 165 The default implementation is just random and does not follow any minimax procedure. 166 Child classes should override this method. 167 168 Return: ([best action, ...], best score). 169 """ 170 171 # Randomly choose an action. 172 action = self.rng.choice(legal_actions) 173 174 # Score the action. 175 successor = state.generate_successor(action, self.rng) 176 _, score = self.minimax_step(successor, ply_count, alpha, beta) 177 178 return [action], score 179 180 def minimax_step_min(self, 181 state: chessai.core.gamestate.GameState, 182 ply_count: int, 183 legal_actions: list[chessai.core.action.Action], 184 alpha: float, 185 beta: float, 186 ) -> tuple[list[chessai.core.action.Action], float]: 187 """ 188 Perform a min step in minimax. 189 minimax_step() has already taken care of all the bookkeeping, 190 this method just needs to return the best actions along with their score. 191 192 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 193 194 The default implementation is just random and does not follow any minimax procedure. 195 Child classes should override this method. 196 197 Return: ([best action, ...], best score). 198 """ 199 200 # Randomly choose an action. 201 action = self.rng.choice(legal_actions) 202 203 # Score the action. 204 successor = state.generate_successor(action, self.rng) 205 _, score = self.minimax_step(successor, ply_count, alpha, beta) 206 207 return [action], score 208 209 def minimax_step_expected_min(self, 210 state: chessai.core.gamestate.GameState, 211 ply_count: int, 212 legal_actions: list[chessai.core.action.Action], 213 alpha: float, 214 beta: float, 215 ) -> float: 216 """ 217 Perform a min step in expectimax. 218 minimax_step() has already taken care of all the bookkeeping, 219 this method just needs to return the expected score. 220 221 Note that unlike minimax_step_max() and minimax_step_min(), 222 no action is returned. 223 224 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 225 226 The default implementation is just random and does not follow any minimax procedure. 227 Child classes should override this method. 228 """ 229 230 # Randomly choose an action. 231 action = self.rng.choice(legal_actions) 232 233 # Score the action. 234 successor = state.generate_successor(action, self.rng) 235 _, score = self.minimax_step(successor, ply_count, alpha, beta) 236 237 return score
An agent that follow the general procedure of Minimax, but is abstracted to support things like alpha-beta pruning and expectimax.
Currently, minimax_step_max(), minimax_step_min(), and minimax_step_expected_min() are all filled with dummy implementations. Child classes should implement those to get proper minimax functionality.
22 def __init__(self, 23 ply_count: int = DEFAULT_PLY_COUNT, 24 alphabeta_prune: bool = False, 25 expectimax: bool = False, 26 **kwargs: typing.Any) -> None: 27 super().__init__(**kwargs) 28 29 # Parse (possibly string) arguments. 30 ply_count = int(ply_count) 31 alphabeta_prune = chessai.util.parse.boolean(alphabeta_prune) 32 expectimax = chessai.util.parse.boolean(expectimax) 33 34 if (ply_count < 1): 35 raise ValueError(f"Ply count must be at least 1, found {ply_count}.") 36 37 self.ply_count: int = ply_count 38 """ 39 How many minimax plys to descend. 40 A "ply" is one set of actions by each agent. 41 When thinking about minimax in terms of a search tree, 42 a ply will be a max layer followed by as many (usually min) layers until the original agent is reached again. 43 """ 44 45 self.alphabeta_prune: bool = alphabeta_prune 46 """ Whether or not to use alpha-beta pruning. """ 47 48 self.expectimax: bool = expectimax 49 """ Whether or not to use expectimax. """ 50 51 self._stats_states_evaluated: list[int] = [] 52 """ Track how many states have been evaluated for each call to get_action(). """ 53 54 self._stats_nodes_visited: list[int] = [] 55 """ Track how many search nodes have been visited for each call to get_action(). """ 56 57 self._move_count: int = 0 58 """ Track the number of moves taken this game. """
How many minimax plys to descend. A "ply" is one set of actions by each agent. When thinking about minimax in terms of a search tree, a ply will be a max layer followed by as many (usually min) layers until the original agent is reached again.
60 def evaluate_state(self, 61 state: chessai.core.gamestate.GameState, 62 action: chessai.core.action.Action | None = None, 63 **kwargs: typing.Any) -> float: 64 self._stats_states_evaluated[-1] += 1 65 return super().evaluate_state(state, action)
Evaluate the state to get a decide how good the current position is.
The base implementation for this function just calls self.evaluation_function,
but child classes may override this method to easily implement their own evaluations.
67 def game_complete(self, final_state: chessai.core.gamestate.GameState) -> None: 68 logging.debug(("Minimax-like agent complete." 69 + " Player: %s, Ply Count: %d, Use Alpha-Beta Pruning: %s, Use Expectimax: %s," 70 + " States Evaluated: %d, Nodes Visited: %d."), 71 self.player, 72 self.ply_count, self.alphabeta_prune, self.expectimax, 73 sum(self._stats_states_evaluated), sum(self._stats_nodes_visited))
Notify this agent that the game has concluded. Agents should use this as an opportunity to make any final calculations and close any game-related resources.
75 def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action: 76 # Start the stat collection for this round at 0. 77 self._stats_states_evaluated.append(0) 78 self._stats_nodes_visited.append(0) 79 80 actions, score = self.minimax_step(state, self.ply_count + 1, -math.inf, math.inf) 81 action = self.rng.choice(actions) 82 83 logging.trace("Turn: %d, Available actions: %s.", self._move_count, actions) # type: ignore[attr-defined] # pylint: disable=no-member 84 85 logging.debug("Turn: %d, Minimax Score: %d, Chosen Action: %s, States Evaluated: %d, Nodes Visited: %d.", 86 self._move_count, score, action.uci(), 87 self._stats_states_evaluated[-1], self._stats_nodes_visited[-1]) 88 89 self._move_count += 1 90 91 if (action is None): 92 raise ValueError("Did not get an action out of Minimax.") 93 94 return action
Get an action for this agent given the current state of the game. This is simplified version of get_action_full(), see that method for full details.
96 def minimax_step(self, 97 state: chessai.core.gamestate.GameState, 98 ply_count: int, 99 alpha: float, 100 beta: float, 101 ) -> tuple[list[chessai.core.action.Action], float]: 102 """ 103 Step through one layer (one agent) of minimax and return all the best actions (there may be ties) along with their score. 104 This method will handle various book keeping and call the correct minimax_step_min() or minimax_step_max() method. 105 106 When doing alpha-beta pruning, 107 alpha represents the "best minimum" score 108 while beta represents the "best maximum" score. 109 They will typically start at inf and -inf, respectively. 110 111 Return: ([best action, ...], best score). 112 """ 113 114 self._stats_nodes_visited[-1] += 1 115 116 # If we see ourselves, then we have descended a full ply. 117 if (state.turn == self.player): 118 ply_count -= 1 119 120 # At ply count zero, we just evaluate the current state and return up the tree. 121 # Note that we only hit this when we are the target agent (since we just decremented the ply count). 122 if (ply_count <= 0): 123 return [], self.evaluate_state(state) 124 125 # If the game is over, then stop descending. 126 if (state.is_game_over()): 127 return [], self.evaluate_state(state) 128 129 legal_actions = state.get_legal_actions() 130 131 # Don't consider forfeiting unless we can do nothing else. 132 # This will help keep the game moving along. 133 if ((len(legal_actions) > 1) and (chessai.core.action.ForfeitAction() in legal_actions)): 134 legal_actions.remove(chessai.core.action.ForfeitAction()) 135 136 # Don't consider proposing a draw unless we can do nothing else. 137 # This will help keep the game moving along. 138 if ((len(legal_actions) > 1) and (chessai.core.action.ProposeDrawAction() in legal_actions)): 139 legal_actions.remove(chessai.core.action.ProposeDrawAction()) 140 141 if (state.turn == self.player): 142 # We are considering ourselves, get the max. 143 return self.minimax_step_max(state, ply_count, legal_actions, alpha, beta) 144 145 # We are considering an opposing agent, get the min or expected min. 146 if (self.expectimax): 147 return [], self.minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta) 148 149 return self.minimax_step_min(state, ply_count, legal_actions, alpha, beta)
Step through one layer (one agent) of minimax and return all the best actions (there may be ties) along with their score. This method will handle various book keeping and call the correct minimax_step_min() or minimax_step_max() method.
When doing alpha-beta pruning, alpha represents the "best minimum" score while beta represents the "best maximum" score. They will typically start at inf and -inf, respectively.
Return: ([best action, ...], best score).
151 def minimax_step_max(self, 152 state: chessai.core.gamestate.GameState, 153 ply_count: int, 154 legal_actions: list[chessai.core.action.Action], 155 alpha: float, 156 beta: float, 157 ) -> tuple[list[chessai.core.action.Action], float]: 158 """ 159 Perform a max step in minimax. 160 minimax_step() has already taken care of all the bookkeeping, 161 this method just needs to return the best actions along with their score. 162 163 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 164 165 The default implementation is just random and does not follow any minimax procedure. 166 Child classes should override this method. 167 168 Return: ([best action, ...], best score). 169 """ 170 171 # Randomly choose an action. 172 action = self.rng.choice(legal_actions) 173 174 # Score the action. 175 successor = state.generate_successor(action, self.rng) 176 _, score = self.minimax_step(successor, ply_count, alpha, beta) 177 178 return [action], score
Perform a max step in minimax. minimax_step() has already taken care of all the bookkeeping, this method just needs to return the best actions along with their score.
alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning.
The default implementation is just random and does not follow any minimax procedure. Child classes should override this method.
Return: ([best action, ...], best score).
180 def minimax_step_min(self, 181 state: chessai.core.gamestate.GameState, 182 ply_count: int, 183 legal_actions: list[chessai.core.action.Action], 184 alpha: float, 185 beta: float, 186 ) -> tuple[list[chessai.core.action.Action], float]: 187 """ 188 Perform a min step in minimax. 189 minimax_step() has already taken care of all the bookkeeping, 190 this method just needs to return the best actions along with their score. 191 192 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 193 194 The default implementation is just random and does not follow any minimax procedure. 195 Child classes should override this method. 196 197 Return: ([best action, ...], best score). 198 """ 199 200 # Randomly choose an action. 201 action = self.rng.choice(legal_actions) 202 203 # Score the action. 204 successor = state.generate_successor(action, self.rng) 205 _, score = self.minimax_step(successor, ply_count, alpha, beta) 206 207 return [action], score
Perform a min step in minimax. minimax_step() has already taken care of all the bookkeeping, this method just needs to return the best actions along with their score.
alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning.
The default implementation is just random and does not follow any minimax procedure. Child classes should override this method.
Return: ([best action, ...], best score).
209 def minimax_step_expected_min(self, 210 state: chessai.core.gamestate.GameState, 211 ply_count: int, 212 legal_actions: list[chessai.core.action.Action], 213 alpha: float, 214 beta: float, 215 ) -> float: 216 """ 217 Perform a min step in expectimax. 218 minimax_step() has already taken care of all the bookkeeping, 219 this method just needs to return the expected score. 220 221 Note that unlike minimax_step_max() and minimax_step_min(), 222 no action is returned. 223 224 alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning. 225 226 The default implementation is just random and does not follow any minimax procedure. 227 Child classes should override this method. 228 """ 229 230 # Randomly choose an action. 231 action = self.rng.choice(legal_actions) 232 233 # Score the action. 234 successor = state.generate_successor(action, self.rng) 235 _, score = self.minimax_step(successor, ply_count, alpha, beta) 236 237 return score
Perform a min step in expectimax. minimax_step() has already taken care of all the bookkeeping, this method just needs to return the expected score.
Note that unlike minimax_step_max() and minimax_step_min(), no action is returned.
alpha and beta can be ignored (and just passed along) when not doing alpha-beta pruning.
The default implementation is just random and does not follow any minimax procedure. Child classes should override this method.