chessai.student.multiagents

  1import typing
  2
  3import chessai.agents.greedy
  4import chessai.agents.minimax
  5import chessai.chess.gamestate
  6import chessai.core.action
  7import chessai.core.gamestate
  8
  9class ReflexAgent(chessai.agents.greedy.GreedyAgent):
 10    """
 11    A simple agent based on chessai.agents.greedy.GreedyAgent.
 12
 13    You job is to make this agent better (it is pretty bad right now).
 14    You can change whatever you want about it,
 15    but it should still be a child of chessai.agents.greedy.GreedyAgent
 16    and be a "reflex" agent.
 17    This means that it shouldn't do any formal planning or searching,
 18    instead it should just look at the state of the game and try to make a good choice in the moment.
 19    You can make a great agent just by implementing a custom evaluate_state() method
 20    (and maybe add to the constructor if you want).
 21    """
 22
 23    def __init__(self, **kwargs: typing.Any) -> None:
 24        super().__init__(**kwargs)
 25
 26        # Put code here if you want.
 27
 28    def evaluate_state(self,
 29            state: chessai.core.gamestate.GameState,
 30            action: chessai.core.action.Action | None = None,
 31            **kwargs: typing.Any) -> float:
 32        # This would be a great place to improve your reflex agent.
 33        return super().evaluate_state(state, action, **kwargs)
 34
 35class MyMinimaxLikeAgent(chessai.agents.minimax.MinimaxLikeAgent):
 36    """
 37    An agent that implements all the required methods for the minimax family of algorithms.
 38    Default implementations are supplied, so the agent should run right away,
 39    but it will not be very good.
 40
 41    To implement minimax, minimax_step_max() and minimax_step_min() are required
 42    (you can ignore alpha and beta).
 43
 44    To implement minimax with alpha-beta pruning,
 45    minimax_step_max() and minimax_step_min() with alpha and beta are required.
 46
 47    To implement expectimax, minimax_step_max() and minimax_step_expected_min() are required.
 48
 49    You are free to implement/override any methods you need to.
 50    """
 51
 52    def __init__(self, **kwargs: typing.Any) -> None:
 53        super().__init__(**kwargs)
 54
 55        # You can use the constructor if you need to.
 56
 57    def minimax_step_max(self,
 58            state: chessai.core.gamestate.GameState,
 59            ply_count: int,
 60            legal_actions: list[chessai.core.action.Action],
 61            alpha: float,
 62            beta: float,
 63            ) -> tuple[list[chessai.core.action.Action], float]:
 64        # *** Your Code Here ***
 65        return super().minimax_step_max(state, ply_count, legal_actions, alpha, beta)
 66
 67    def minimax_step_min(self,
 68            state: chessai.core.gamestate.GameState,
 69            ply_count: int,
 70            legal_actions: list[chessai.core.action.Action],
 71            alpha: float,
 72            beta: float,
 73            ) -> tuple[list[chessai.core.action.Action], float]:
 74        # *** Your Code Here ***
 75        return super().minimax_step_min(state, ply_count, legal_actions, alpha, beta)
 76
 77    def minimax_step_expected_min(self,
 78            state: chessai.core.gamestate.GameState,
 79            ply_count: int,
 80            legal_actions: list[chessai.core.action.Action],
 81            alpha: float,
 82            beta: float,
 83            ) -> float:
 84        # *** Your Code Here ***
 85        return super().minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta)
 86
 87def better_state_eval(
 88        state: chessai.core.gamestate.GameState,
 89        agent: typing.Any | None = None,
 90        action: chessai.core.action.Action | None = None,
 91        **kwargs: typing.Any) -> float:
 92    """
 93    Create a better state evaluation function for your MyMinimaxLikeAgent agent!
 94
 95    In this comment, include a description of what you are doing.
 96
 97    *** Your Text Here ***
 98    """
 99
100    # *** Your Code Here ***
101    return chessai.chess.gamestate.base_eval(state, agent, kwargs)
class ReflexAgent(chessai.agents.greedy.GreedyAgent):
10class ReflexAgent(chessai.agents.greedy.GreedyAgent):
11    """
12    A simple agent based on chessai.agents.greedy.GreedyAgent.
13
14    You job is to make this agent better (it is pretty bad right now).
15    You can change whatever you want about it,
16    but it should still be a child of chessai.agents.greedy.GreedyAgent
17    and be a "reflex" agent.
18    This means that it shouldn't do any formal planning or searching,
19    instead it should just look at the state of the game and try to make a good choice in the moment.
20    You can make a great agent just by implementing a custom evaluate_state() method
21    (and maybe add to the constructor if you want).
22    """
23
24    def __init__(self, **kwargs: typing.Any) -> None:
25        super().__init__(**kwargs)
26
27        # Put code here if you want.
28
29    def evaluate_state(self,
30            state: chessai.core.gamestate.GameState,
31            action: chessai.core.action.Action | None = None,
32            **kwargs: typing.Any) -> float:
33        # This would be a great place to improve your reflex agent.
34        return super().evaluate_state(state, action, **kwargs)

A simple agent based on chessai.agents.greedy.GreedyAgent.

You job is to make this agent better (it is pretty bad right now). You can change whatever you want about it, but it should still be a child of chessai.agents.greedy.GreedyAgent and be a "reflex" agent. This means that it shouldn't do any formal planning or searching, instead it should just look at the state of the game and try to make a good choice in the moment. You can make a great agent just by implementing a custom evaluate_state() method (and maybe add to the constructor if you want).

ReflexAgent(**kwargs: Any)
24    def __init__(self, **kwargs: typing.Any) -> None:
25        super().__init__(**kwargs)
26
27        # Put code here if you want.
def evaluate_state( self, state: chessai.core.gamestate.GameState, action: chessai.core.action.Action | None = None, **kwargs: Any) -> float:
29    def evaluate_state(self,
30            state: chessai.core.gamestate.GameState,
31            action: chessai.core.action.Action | None = None,
32            **kwargs: typing.Any) -> float:
33        # This would be a great place to improve your reflex agent.
34        return super().evaluate_state(state, action, **kwargs)

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.

class MyMinimaxLikeAgent(chessai.agents.minimax.MinimaxLikeAgent):
36class MyMinimaxLikeAgent(chessai.agents.minimax.MinimaxLikeAgent):
37    """
38    An agent that implements all the required methods for the minimax family of algorithms.
39    Default implementations are supplied, so the agent should run right away,
40    but it will not be very good.
41
42    To implement minimax, minimax_step_max() and minimax_step_min() are required
43    (you can ignore alpha and beta).
44
45    To implement minimax with alpha-beta pruning,
46    minimax_step_max() and minimax_step_min() with alpha and beta are required.
47
48    To implement expectimax, minimax_step_max() and minimax_step_expected_min() are required.
49
50    You are free to implement/override any methods you need to.
51    """
52
53    def __init__(self, **kwargs: typing.Any) -> None:
54        super().__init__(**kwargs)
55
56        # You can use the constructor if you need to.
57
58    def minimax_step_max(self,
59            state: chessai.core.gamestate.GameState,
60            ply_count: int,
61            legal_actions: list[chessai.core.action.Action],
62            alpha: float,
63            beta: float,
64            ) -> tuple[list[chessai.core.action.Action], float]:
65        # *** Your Code Here ***
66        return super().minimax_step_max(state, ply_count, legal_actions, alpha, beta)
67
68    def minimax_step_min(self,
69            state: chessai.core.gamestate.GameState,
70            ply_count: int,
71            legal_actions: list[chessai.core.action.Action],
72            alpha: float,
73            beta: float,
74            ) -> tuple[list[chessai.core.action.Action], float]:
75        # *** Your Code Here ***
76        return super().minimax_step_min(state, ply_count, legal_actions, alpha, beta)
77
78    def minimax_step_expected_min(self,
79            state: chessai.core.gamestate.GameState,
80            ply_count: int,
81            legal_actions: list[chessai.core.action.Action],
82            alpha: float,
83            beta: float,
84            ) -> float:
85        # *** Your Code Here ***
86        return super().minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta)

An agent that implements all the required methods for the minimax family of algorithms. Default implementations are supplied, so the agent should run right away, but it will not be very good.

To implement minimax, minimax_step_max() and minimax_step_min() are required (you can ignore alpha and beta).

To implement minimax with alpha-beta pruning, minimax_step_max() and minimax_step_min() with alpha and beta are required.

To implement expectimax, minimax_step_max() and minimax_step_expected_min() are required.

You are free to implement/override any methods you need to.

MyMinimaxLikeAgent(**kwargs: Any)
53    def __init__(self, **kwargs: typing.Any) -> None:
54        super().__init__(**kwargs)
55
56        # You can use the constructor if you need to.
def minimax_step_max( self, state: chessai.core.gamestate.GameState, ply_count: int, legal_actions: list[chessai.core.action.Action], alpha: float, beta: float) -> tuple[list[chessai.core.action.Action], float]:
58    def minimax_step_max(self,
59            state: chessai.core.gamestate.GameState,
60            ply_count: int,
61            legal_actions: list[chessai.core.action.Action],
62            alpha: float,
63            beta: float,
64            ) -> tuple[list[chessai.core.action.Action], float]:
65        # *** Your Code Here ***
66        return super().minimax_step_max(state, ply_count, legal_actions, alpha, beta)

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).

def minimax_step_min( self, state: chessai.core.gamestate.GameState, ply_count: int, legal_actions: list[chessai.core.action.Action], alpha: float, beta: float) -> tuple[list[chessai.core.action.Action], float]:
68    def minimax_step_min(self,
69            state: chessai.core.gamestate.GameState,
70            ply_count: int,
71            legal_actions: list[chessai.core.action.Action],
72            alpha: float,
73            beta: float,
74            ) -> tuple[list[chessai.core.action.Action], float]:
75        # *** Your Code Here ***
76        return super().minimax_step_min(state, ply_count, legal_actions, alpha, beta)

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).

def minimax_step_expected_min( self, state: chessai.core.gamestate.GameState, ply_count: int, legal_actions: list[chessai.core.action.Action], alpha: float, beta: float) -> float:
78    def minimax_step_expected_min(self,
79            state: chessai.core.gamestate.GameState,
80            ply_count: int,
81            legal_actions: list[chessai.core.action.Action],
82            alpha: float,
83            beta: float,
84            ) -> float:
85        # *** Your Code Here ***
86        return super().minimax_step_expected_min(state, ply_count, legal_actions, alpha, beta)

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.

def better_state_eval( state: chessai.core.gamestate.GameState, agent: typing.Any | None = None, action: chessai.core.action.Action | None = None, **kwargs: Any) -> float:
 88def better_state_eval(
 89        state: chessai.core.gamestate.GameState,
 90        agent: typing.Any | None = None,
 91        action: chessai.core.action.Action | None = None,
 92        **kwargs: typing.Any) -> float:
 93    """
 94    Create a better state evaluation function for your MyMinimaxLikeAgent agent!
 95
 96    In this comment, include a description of what you are doing.
 97
 98    *** Your Text Here ***
 99    """
100
101    # *** Your Code Here ***
102    return chessai.chess.gamestate.base_eval(state, agent, kwargs)

Create a better state evaluation function for your MyMinimaxLikeAgent agent!

In this comment, include a description of what you are doing.

*** Your Text Here ***