Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Games: Alpha-Beta + Updates #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 140 additions & 11 deletions games.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
" * Tic-Tac-Toe\n",
" * Figure 5.2 Game\n",
"* Min-Max\n",
"* Alpha-Beta\n",
"* Players\n",
"* Let's Play Some Games!"
]
Expand Down Expand Up @@ -347,7 +348,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"`utility`: Returns the value of the terminal state for a player ('MAX' and 'MIN')."
"`utility`: Returns the value of the terminal state for a player ('MAX' and 'MIN'). Note that for 'MIN' the value returned is the negative of the utility."
]
},
{
Expand All @@ -363,19 +364,21 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
"3\n",
"-3\n"
]
}
],
"source": [
"print(fig52.utility('B1', 'MAX'))"
"print(fig52.utility('B1', 'MAX'))\n",
"print(fig52.utility('B1', 'MIN'))"
]
},
{
Expand Down Expand Up @@ -472,9 +475,20 @@
"source": [
"# MIN-MAX\n",
"\n",
"This algorithm (often called *Minimax*) computes the next move for a player (MIN or MAX) at their current state. It recursively computes the minimax value of successor states, until it reaches terminals (the leaves of the tree). Using the `utility` value of the terminal states, it computes the values of parent states until it reaches the initial node (the root of the tree). The algorithm returns the move that returns the optimal value of the initial node's successor states.\n",
"## Overview\n",
"\n",
"Below is the code for the algorithm:"
"This algorithm (often called *Minimax*) computes the next move for a player (MIN or MAX) at their current state. It recursively computes the minimax value of successor states, until it reaches terminals (the leaves of the tree). Using the `utility` value of the terminal states, it computes the values of parent states until it reaches the initial node (the root of the tree).\n",
"\n",
"It is worth noting that the algorithm works in a depth-first manner."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation\n",
"\n",
"In the implementation we are using two functions, `max_value` and `min_value` to calculate the best move for MAX and MIN respectively. These functions interact in an alternating recursion; one calls the other until a terminal state is reached. When the recursion halts, we are left with scores for each move. We return the max. Despite returning the max, it will work for MIN too since for MIN the values are their negative (hence the order of values is reversed, so the higher the better for MIN too)."
]
},
{
Expand All @@ -492,6 +506,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example\n",
"\n",
"We will now play the Fig52 game using this algorithm. Take a look at the Fig52Game from above to follow along.\n",
"\n",
"It is the turn of MAX to move, and he is at state A. He can move to B, C or D, using moves a1, a2 and a3 respectively. MAX's goal is to maximize the end value. So, to make a decision, MAX needs to know the values at the aforementioned nodes and pick the greatest one. After MAX, it is MIN's turn to play. So MAX wants to know what will the values of B, C and D be after MIN plays.\n",
Expand Down Expand Up @@ -546,6 +562,119 @@
"print(minimax_decision('A', fig52))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ALPHA-BETA\n",
"\n",
"## Overview\n",
"\n",
"While *Minimax* is great for computing a move, it can get tricky when the number of games states gets bigger. The algorithm needs to search all the leaves of the tree, which increase exponentially to its depth.\n",
"\n",
"For Tic-Tac-Toe, where the depth of the tree is 9 (after the 9th move, the game ends), we can have at most 9! terminal states (at most because not all terminal nodes are at the last level of the tree; some are higher up because the game ended before the 9th move). This isn't so bad, but for more complex problems like chess, we have over $10^{40}$ terminal nodes. Unfortunately we have not found a way to cut the exponent away, but we nevertheless have found ways to alleviate the workload.\n",
"\n",
"Here we examine *pruning* the game tree, which means removing parts of it that we do not need to examine. The particular type of pruning is called *alpha-beta*, and the search in whole is called *alpha-beta search*.\n",
"\n",
"To showcase what parts of the tree we don't need to search, we will take a look at the example `Fig52Game`.\n",
"\n",
"In the example game, we need to find the best move for player MAX at state A, which is the maximum value of MIN's possible moves at successor states.\n",
"\n",
"`MAX(A) = MAX( MIN(B), MIN(C), MIN(D) )`\n",
"\n",
"`MIN(B)` is the minimum of 3, 12, 8 which is 3. So the above formula becomes:\n",
"\n",
"`MAX(A) = MAX( 3, MIN(C), MIN(D) )`\n",
"\n",
"Next move we will check is c1, which leads to a terminal state with utility of 2. Before we continue searching under state C, let's pop back into our formula with the new value:\n",
"\n",
"`MAX(A) = MAX( 3, MIN(2, c2, .... cN), MIN(D) )`\n",
"\n",
"We do not know how many moves state C allows, but we know that the first one results in a value of 2. Do we need to keep searching under C? The answer is no. The value MIN will pick on C will at most be 2. Since MAX already has the option to pick something greater than that, 3 from B, he does not need to keep searching under C.\n",
"\n",
"In *alpha-beta* we make use of two additional parameters for each state/node, *a* and *b*, that describe bounds on the possible moves. The parameter *a* denotes the best choice (highest value) for MAX along that path, while *b* denotes the best choice (lowest value) for MIN. As we go along we update *a* and *b* and prune a node branch when the value of the node is worse than the value of *a* and *b* for MAX and MIN respectively.\n",
"\n",
"In the above example, after the search under state B, MAX had an *a* value of 3. So, when searching node C we found a value less than that, 2, we stopped searching under C."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation\n",
"\n",
"Like *minimax*, we again make use of functions `max_value` and `min_value`, but this time we utilise the *a* and *b* values, updating them and stopping the recursive call if we end up on nodes with values worse than *a* and *b* (for MAX and MIN). The algorithm finds the maximum value and returns the move that results in it.\n",
"\n",
"The implementation:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource alphabeta_search"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example\n",
"\n",
"We will play the Fig52 Game with the *alpha-beta* search algorithm. It is the turn of MAX to play at state A."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a1\n"
]
}
],
"source": [
"print(alphabeta_search('A', fig52))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The optimal move for MAX is a1, for the reasons given above. MIN will pick move b1 for B resulting in a value of 3, updating the *a* value of MAX to 3. Then, when we find under C a node of value 2, we will stop searching under that sub-tree since it is less than *a*. From D we have a value of 2. So, the best move for MAX is the one resulting in a value of 3, which is a1.\n",
"\n",
"Below we see the best moves for MIN starting from B, C and D respectively. Note that the algorithm in these cases works the same way as *minimax*, since all the nodes below the aforementioned states are terminal."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b1\n",
"c1\n",
"d3\n"
]
}
],
"source": [
"print(alphabeta_search('B', fig52))\n",
"print(alphabeta_search('C', fig52))\n",
"print(alphabeta_search('D', fig52))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -561,7 +690,7 @@
"The `random_player` is a function that plays random moves in the game. That's it. There isn't much more to this guy. \n",
"\n",
"## alphabeta_player\n",
"The `alphabeta_player`, on the other hand, calls the `alphabeta_full_search` function, which returns the best move in the current game state. Thus, the `alphabeta_player` always plays the best move given a game state, assuming that the game tree is small enough to search entirely.\n",
"The `alphabeta_player`, on the other hand, calls the `alphabeta_search` function, which returns the best move in the current game state. Thus, the `alphabeta_player` always plays the best move given a game state, assuming that the game tree is small enough to search entirely.\n",
"\n",
"## play_game\n",
"The `play_game` function will be the one that will actually be used to play the game. You pass as arguments to it an instance of the game you want to play and the players you want in this game. Use it to play AI vs AI, AI vs human, or even human vs human matches!"
Expand All @@ -580,7 +709,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -672,7 +801,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -681,13 +810,13 @@
"'a1'"
]
},
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alphabeta_full_search('A', game52)"
"alphabeta_search('A', game52)"
]
},
{
Expand Down
10 changes: 5 additions & 5 deletions games.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def min_value(state):
# ______________________________________________________________________________


def alphabeta_full_search(state, game):
def alphabeta_search(state, game):
"""Search game to determine best action; use alpha-beta pruning.
As in [Figure 5.7], this version searches all the way to the leaves."""

Expand Down Expand Up @@ -71,7 +71,7 @@ def min_value(state, alpha, beta):
beta = min(beta, v)
return v

# Body of alphabeta_search:
# Body of alphabeta_cutoff_search:
best_score = -infinity
beta = infinity
best_action = None
Expand All @@ -83,7 +83,7 @@ def min_value(state, alpha, beta):
return best_action


def alphabeta_search(state, game, d=4, cutoff_test=None, eval_fn=None):
def alphabeta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
"""Search game to determine best action; use alpha-beta pruning.
This version cuts off search and uses an evaluation function."""

Expand Down Expand Up @@ -114,7 +114,7 @@ def min_value(state, alpha, beta, depth):
beta = min(beta, v)
return v

# Body of alphabeta_search starts here:
# Body of alphabeta_cutoff_search starts here:
# The default test cuts off at depth d or at a terminal state
cutoff_test = (cutoff_test or
(lambda state, depth: depth > d or
Expand Down Expand Up @@ -154,7 +154,7 @@ def random_player(game, state):


def alphabeta_player(game, state):
return alphabeta_full_search(state, game)
return alphabeta_search(state, game)


# ______________________________________________________________________________
Expand Down
29 changes: 9 additions & 20 deletions tests/test_games.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
"""A lightweight test suite for games.py"""

# You can run this test suite by doing: py.test tests/test_games.py
# Of course you need to have py.test installed to do this.

import pytest

from games import *

# Creating the game instances
Expand Down Expand Up @@ -36,27 +29,27 @@ def test_minimax_decision():
assert minimax_decision('D', f52) == 'd3'


def test_alphabeta_full_search():
assert alphabeta_full_search('A', f52) == 'a1'
assert alphabeta_full_search('B', f52) == 'b1'
assert alphabeta_full_search('C', f52) == 'c1'
assert alphabeta_full_search('D', f52) == 'd3'
def test_alphabeta_search():
assert alphabeta_search('A', f52) == 'a1'
assert alphabeta_search('B', f52) == 'b1'
assert alphabeta_search('C', f52) == 'c1'
assert alphabeta_search('D', f52) == 'd3'

state = gen_state(to_move='X', x_positions=[(1, 1), (3, 3)],
o_positions=[(1, 2), (3, 2)])
assert alphabeta_full_search(state, ttt) == (2, 2)
assert alphabeta_search(state, ttt) == (2, 2)

state = gen_state(to_move='O', x_positions=[(1, 1), (3, 1), (3, 3)],
o_positions=[(1, 2), (3, 2)])
assert alphabeta_full_search(state, ttt) == (2, 2)
assert alphabeta_search(state, ttt) == (2, 2)

state = gen_state(to_move='O', x_positions=[(1, 1)],
o_positions=[])
assert alphabeta_full_search(state, ttt) == (2, 2)
assert alphabeta_search(state, ttt) == (2, 2)

state = gen_state(to_move='X', x_positions=[(1, 1), (3, 1)],
o_positions=[(2, 2), (3, 1)])
assert alphabeta_full_search(state, ttt) == (1, 3)
assert alphabeta_search(state, ttt) == (1, 3)


def test_random_tests():
Expand All @@ -67,7 +60,3 @@ def test_random_tests():

# The player 'X' (one who plays first) in TicTacToe never loses:
assert ttt.play_game(alphabeta_player, random_player) >= 0


if __name__ == '__main__':
pytest.main()