{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GAMES OR ADVERSARIAL SEARCH\n",
"\n",
"This notebook serves as supporting material for topics covered in **Chapter 5 - Adversarial Search** in the book *Artificial Intelligence: A Modern Approach.* This notebook uses implementations from [games.py](https://github.com/aimacode/aima-python/blob/master/games.py) module. Let's import required classes, methods, global variables etc., from games module."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CONTENTS\n",
"\n",
"* Game Representation\n",
"* Game Examples\n",
" * Tic-Tac-Toe\n",
" * Figure 5.2 Game\n",
"* Min-Max\n",
"* Alpha-Beta\n",
"* Players\n",
"* Let's Play Some Games!"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from games import *\n",
"from notebook import psource, pseudocode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GAME REPRESENTATION\n",
"\n",
"To represent games we make use of the `Game` class, which we can subclass and override its functions to represent our own games. A helper tool is the namedtuple `GameState`, which in some cases can come in handy, especially when our game needs us to remember a board (like chess)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `GameState` namedtuple\n",
"\n",
"`GameState` is a [namedtuple](https://docs.python.org/3.5/library/collections.html#collections.namedtuple) which represents the current state of a game. It is used to help represent games whose states can't be easily represented normally, or for games that require memory of a board, like Tic-Tac-Toe.\n",
"\n",
"`Gamestate` is defined as follows:\n",
"\n",
"`GameState = namedtuple('GameState', 'to_move, utility, board, moves')`\n",
"\n",
"* `to_move`: It represents whose turn it is to move next.\n",
"\n",
"* `utility`: It stores the utility of the game state. Storing this utility is a good idea, because, when you do a Minimax Search or an Alphabeta Search, you generate many recursive calls, which travel all the way down to the terminal states. When these recursive calls go back up to the original callee, we have calculated utilities for many game states. We store these utilities in their respective `GameState`s to avoid calculating them all over again.\n",
"\n",
"* `board`: A dict that stores the board of the game.\n",
"\n",
"* `moves`: It stores the list of legal moves possible from the current position."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## `Game` class\n",
"\n",
"Let's have a look at the class `Game` in our module. We see that it has functions, namely `actions`, `result`, `utility`, `terminal_test`, `to_move` and `display`.\n",
"\n",
"We see that these functions have not actually been implemented. This class is just a template class; we are supposed to create the class for our game, by inheriting this `Game` class and implementing all the methods mentioned in `Game`."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"%psource Game"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's get into details of all the methods in our `Game` class. You have to implement these methods when you create new classes that would represent your game.\n",
"\n",
"* `actions(self, state)`: Given a game state, this method generates all the legal actions possible from this state, as a list or a generator. Returning a generator rather than a list has the advantage that it saves space and you can still operate on it as a list.\n",
"\n",
"\n",
"* `result(self, state, move)`: Given a game state and a move, this method returns the game state that you get by making that move on this game state.\n",
"\n",
"\n",
"* `utility(self, state, player)`: Given a terminal game state and a player, this method returns the utility for that player in the given terminal game state. While implementing this method assume that the game state is a terminal game state. The logic in this module is such that this method will be called only on terminal game states.\n",
"\n",
"\n",
"* `terminal_test(self, state)`: Given a game state, this method should return `True` if this game state is a terminal state, and `False` otherwise.\n",
"\n",
"\n",
"* `to_move(self, state)`: Given a game state, this method returns the player who is to play next. This information is typically stored in the game state, so all this method does is extract this information and return it.\n",
"\n",
"\n",
"* `display(self, state)`: This method prints/displays the current state of the game."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GAME EXAMPLES\n",
"\n",
"Below we give some examples for games you can create and experiment on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tic-Tac-Toe\n",
"\n",
"Take a look at the class `TicTacToe`. All the methods mentioned in the class `Game` have been implemented here."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"%psource TicTacToe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The class `TicTacToe` has been inherited from the class `Game`. As mentioned earlier, you really want to do this. Catching bugs and errors becomes a whole lot easier.\n",
"\n",
"Additional methods in TicTacToe:\n",
"\n",
"* `__init__(self, h=3, v=3, k=3)` : When you create a class inherited from the `Game` class (class `TicTacToe` in our case), you'll have to create an object of this inherited class to initialize the game. This initialization might require some additional information which would be passed to `__init__` as variables. For the case of our `TicTacToe` game, this additional information would be the number of rows `h`, number of columns `v` and how many consecutive X's or O's are needed in a row, column or diagonal for a win `k`. Also, the initial game state has to be defined here in `__init__`.\n",
"\n",
"\n",
"* `compute_utility(self, board, move, player)` : A method to calculate the utility of TicTacToe game. If 'X' wins with this move, this method returns 1; if 'O' wins return -1; else return 0.\n",
"\n",
"\n",
"* `k_in_row(self, board, move, player, delta_x_y)` : This method returns `True` if there is a line formed on TicTacToe board with the latest move else `False.`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TicTacToe GameState\n",
"\n",
"Now, before we start implementing our `TicTacToe` game, we need to decide how we will be representing our game state. Typically, a game state will give you all the current information about the game at any point in time. When you are given a game state, you should be able to tell whose turn it is next, how the game will look like on a real-life board (if it has one) etc. A game state need not include the history of the game. If you can play the game further given a game state, you game state representation is acceptable. While we might like to include all kinds of information in our game state, we wouldn't want to put too much information into it. Modifying this game state to generate a new one would be a real pain then.\n",
"\n",
"Now, as for our `TicTacToe` game state, would storing only the positions of all the X's and O's be sufficient to represent all the game information at that point in time? Well, does it tell us whose turn it is next? Looking at the 'X's and O's on the board and counting them should tell us that. But that would mean extra computing. To avoid this, we will also store whose move it is next in the game state.\n",
"\n",
"Think about what we've done here. We have reduced extra computation by storing additional information in a game state. Now, this information might not be absolutely essential to tell us about the state of the game, but it does save us additional computation time. We'll do more of this later on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To store game states will will use the `GameState` namedtuple.\n",
"\n",
"* `to_move`: A string of a single character, either 'X' or 'O'.\n",
"\n",
"* `utility`: 1 for win, -1 for loss, 0 otherwise.\n",
"\n",
"* `board`: All the positions of X's and O's on the board.\n",
"\n",
"* `moves`: All the possible moves from the current state. Note here, that storing the moves as a list, as it is done here, increases the space complexity of Minimax Search from `O(m)` to `O(bm)`. Refer to Sec. 5.2.1 of the book."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Representing a move in TicTacToe game\n",
"\n",
"Now that we have decided how our game state will be represented, it's time to decide how our move will be represented. Becomes easy to use this move to modify a current game state to generate a new one.\n",
"\n",
"For our `TicTacToe` game, we'll just represent a move by a tuple, where the first and the second elements of the tuple will represent the row and column, respectively, where the next move is to be made. Whether to make an 'X' or an 'O' will be decided by the `to_move` in the `GameState` namedtuple."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fig52 Game\n",
"\n",
"For a more trivial example we will represent the game in **Figure 5.2** of the book.\n",
"\n",
"\n",
"\n",
"The states are represented with capital letters inside the triangles (eg. \"A\") while moves are the labels on the edges between states (eg. \"a1\"). Terminal nodes carry utility values. Note that the terminal nodes are named in this example 'B1', 'B2' and 'B2' for the nodes below 'B', and so forth.\n",
"\n",
"We will model the moves, utilities and initial state like this:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"moves = dict(A=dict(a1='B', a2='C', a3='D'),\n",
" B=dict(b1='B1', b2='B2', b3='B3'),\n",
" C=dict(c1='C1', c2='C2', c3='C3'),\n",
" D=dict(d1='D1', d2='D2', d3='D3'))\n",
"utils = dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)\n",
"initial = 'A'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In `moves`, we have a nested dictionary system. The outer's dictionary has keys as the states and values the possible moves from that state (as a dictionary). The inner dictionary of moves has keys the move names and values the next state after the move is complete.\n",
"\n",
"Below is an example that showcases `moves`. We want the next state after move 'a1' from 'A', which is 'B'. A quick glance at the above image confirms that this is indeed the case."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B\n"
]
}
],
"source": [
"print(moves['A']['a1'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now take a look at the functions we need to implement. First we need to create an object of the `Fig52Game` class."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"fig52 = Fig52Game()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`actions`: Returns the list of moves one can make from a given state."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
def actions(self, state):\n",
" return list(self.succs.get(state, {}).keys())\n",
"
def result(self, state, move):\n",
" return self.succs[state][move]\n",
"
def utility(self, state, player):\n",
" if player == 'MAX':\n",
" return self.utils[state]\n",
" else:\n",
" return -self.utils[state]\n",
"
def terminal_test(self, state):\n",
" return state not in ('A', 'B', 'C', 'D')\n",
"
def to_move(self, state):\n",
" return 'MIN' if state in 'BCD' else 'MAX'\n",
"
class Fig52Game(Game):\n",
" """The game represented in [Figure 5.2]. Serves as a simple test case."""\n",
"\n",
" succs = dict(A=dict(a1='B', a2='C', a3='D'),\n",
" B=dict(b1='B1', b2='B2', b3='B3'),\n",
" C=dict(c1='C1', c2='C2', c3='C3'),\n",
" D=dict(d1='D1', d2='D2', d3='D3'))\n",
" utils = dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)\n",
" initial = 'A'\n",
"\n",
" def actions(self, state):\n",
" return list(self.succs.get(state, {}).keys())\n",
"\n",
" def result(self, state, move):\n",
" return self.succs[state][move]\n",
"\n",
" def utility(self, state, player):\n",
" if player == 'MAX':\n",
" return self.utils[state]\n",
" else:\n",
" return -self.utils[state]\n",
"\n",
" def terminal_test(self, state):\n",
" return state not in ('A', 'B', 'C', 'D')\n",
"\n",
" def to_move(self, state):\n",
" return 'MIN' if state in 'BCD' else 'MAX'\n",
"
def minimax_decision(state, game):\n",
" """Given a state in a game, calculate the best move by searching\n",
" forward all the way to the terminal states. [Figure 5.3]"""\n",
"\n",
" player = game.to_move(state)\n",
"\n",
" def max_value(state):\n",
" if game.terminal_test(state):\n",
" return game.utility(state, player)\n",
" v = -infinity\n",
" for a in game.actions(state):\n",
" v = max(v, min_value(game.result(state, a)))\n",
" return v\n",
"\n",
" def min_value(state):\n",
" if game.terminal_test(state):\n",
" return game.utility(state, player)\n",
" v = infinity\n",
" for a in game.actions(state):\n",
" v = min(v, max_value(game.result(state, a)))\n",
" return v\n",
"\n",
" # Body of minimax_decision:\n",
" return argmax(game.actions(state),\n",
" key=lambda a: min_value(game.result(state, a)))\n",
"