pacai.student.multiagents

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

A simple agent based on pacai.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 pacai.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)
23    def __init__(self, **kwargs: typing.Any) -> None:
24        super().__init__(**kwargs)
25
26        # Put code here if you want.
def evaluate_state( self, state: pacai.core.gamestate.GameState, action: pacai.core.action.Action | None = None, **kwargs: Any) -> float:
28    def evaluate_state(self,
29            state: pacai.core.gamestate.GameState,
30            action: pacai.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)

Evaluate the state to get a decide how good an action was. 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(pacai.agents.minimax.MinimaxLikeAgent):
35class MyMinimaxLikeAgent(pacai.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: pacai.core.gamestate.GameState,
59            ply_count: int,
60            legal_actions: list[pacai.core.action.Action],
61            alpha: float,
62            beta: float,
63            ) -> tuple[list[pacai.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: pacai.core.gamestate.GameState,
69            ply_count: int,
70            legal_actions: list[pacai.core.action.Action],
71            alpha: float,
72            beta: float,
73            ) -> tuple[list[pacai.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: pacai.core.gamestate.GameState,
79            ply_count: int,
80            legal_actions: list[pacai.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)

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)
52    def __init__(self, **kwargs: typing.Any) -> None:
53        super().__init__(**kwargs)
54
55        # You can use the constructor if you need to.
def minimax_step_max( self, state: pacai.core.gamestate.GameState, ply_count: int, legal_actions: list[pacai.core.action.Action], alpha: float, beta: float) -> tuple[list[pacai.core.action.Action], float]:
57    def minimax_step_max(self,
58            state: pacai.core.gamestate.GameState,
59            ply_count: int,
60            legal_actions: list[pacai.core.action.Action],
61            alpha: float,
62            beta: float,
63            ) -> tuple[list[pacai.core.action.Action], float]:
64        # *** Your Code Here ***
65        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: pacai.core.gamestate.GameState, ply_count: int, legal_actions: list[pacai.core.action.Action], alpha: float, beta: float) -> tuple[list[pacai.core.action.Action], float]:
67    def minimax_step_min(self,
68            state: pacai.core.gamestate.GameState,
69            ply_count: int,
70            legal_actions: list[pacai.core.action.Action],
71            alpha: float,
72            beta: float,
73            ) -> tuple[list[pacai.core.action.Action], float]:
74        # *** Your Code Here ***
75        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: pacai.core.gamestate.GameState, ply_count: int, legal_actions: list[pacai.core.action.Action], alpha: float, beta: float) -> float:
77    def minimax_step_expected_min(self,
78            state: pacai.core.gamestate.GameState,
79            ply_count: int,
80            legal_actions: list[pacai.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)

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: pacai.core.gamestate.GameState, agent: typing.Any | None = None, action: pacai.core.action.Action | None = None, **kwargs: Any) -> float:
 87def better_state_eval(
 88        state: pacai.core.gamestate.GameState,
 89        agent: typing.Any | None = None,
 90        action: pacai.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 state.score

Create a better state evaluation function for your MyMinimaxLikeAgent agent!

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

* Your Text Here *