Skip to content

Commit

Permalink
Update games.py to third edition.
Browse files Browse the repository at this point in the history
  • Loading branch information
darius committed Nov 6, 2011
1 parent 7113a6c commit 9273ba7
Showing 1 changed file with 48 additions and 59 deletions.
107 changes: 48 additions & 59 deletions games.py
@@ -1,8 +1,5 @@
"""Games, or Adversarial Search. (Chapter 6)
"""Games, or Adversarial Search. (Chapter 5)
"""
# (Written for the second edition of AIMA; expect some discrepanciecs
# from the third edition until this gets reviewed.)

from utils import *
import random
Expand All @@ -12,46 +9,44 @@

def minimax_decision(state, game):
"""Given a state in a game, calculate the best move by searching
forward all the way to the terminal states. [Fig. 6.4]"""
forward all the way to the terminal states. [Fig. 5.3]"""

player = game.to_move(state)

def max_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = -infinity
for (a, s) in game.successors(state):
v = max(v, min_value(s))
for a in game.actions(state):
v = max(v, min_value(game.result(state, a)))
return v

def min_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = infinity
for (a, s) in game.successors(state):
v = min(v, max_value(s))
for a in game.actions(state):
v = min(v, max_value(game.result(state, a)))
return v

# Body of minimax_decision starts here:
action, state = argmax(game.successors(state),
lambda ((a, s)): min_value(s))
return action

# Body of minimax_decision:
return argmax(game.actions(state),
lambda a: min_value(game.result(state, a)))

#______________________________________________________________________________

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

player = game.to_move(state)

def max_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = -infinity
for (a, s) in game.successors(state):
v = max(v, min_value(s, alpha, beta))
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta))
if v >= beta:
return v
alpha = max(alpha, v)
Expand All @@ -61,17 +56,17 @@ def min_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = infinity
for (a, s) in game.successors(state):
v = min(v, max_value(s, alpha, beta))
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta))
if v <= alpha:
return v
beta = min(beta, v)
return v

# Body of alphabeta_search starts here:
action, state = argmax(game.successors(state),
lambda ((a, s)): min_value(s, -infinity, infinity))
return action
# Body of alphabeta_search:
return argmax(game.actions(state),
lambda a: min_value(game.result(state, a),
-infinity, infinity))

def alphabeta_search(state, game, d=4, cutoff_test=None, eval_fn=None):
"""Search game to determine best action; use alpha-beta pruning.
Expand All @@ -83,8 +78,9 @@ def max_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = -infinity
for (a, s) in game.successors(state):
v = max(v, min_value(s, alpha, beta, depth+1))
for a in game.actions(state):
v = max(v, min_value(game.result(state, a),
alpha, beta, depth+1))
if v >= beta:
return v
alpha = max(alpha, v)
Expand All @@ -94,8 +90,9 @@ def min_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = infinity
for (a, s) in game.successors(state):
v = min(v, max_value(s, alpha, beta, depth+1))
for a in game.actions(state):
v = min(v, max_value(game.result(state, a),
alpha, beta, depth+1))
if v <= alpha:
return v
beta = min(beta, v)
Expand All @@ -106,9 +103,9 @@ def min_value(state, alpha, beta, depth):
cutoff_test = (cutoff_test or
(lambda state,depth: depth>d or game.terminal_test(state)))
eval_fn = eval_fn or (lambda state: game.utility(state, player))
action, state = argmax(game.successors(state),
lambda (a, s): min_value(s, -infinity, infinity, 0))
return action
return argmax(game.actions(state),
lambda a: min_value(game.result(state, a),
-infinity, infinity, 0))

#______________________________________________________________________________
# Players for Games
Expand All @@ -120,21 +117,21 @@ def query_player(game, state):

def random_player(game, state):
"A player that chooses a legal move at random."
return random.choice(game.legal_moves(state))
return random.choice(game.actions(state))

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

def play_game(game, *players):
"""Play an n-person, move-alternating game.
>>> play_game(Fig62Game(), alphabeta_player, alphabeta_player)
>>> play_game(Fig52Game(), alphabeta_player, alphabeta_player)
3
"""
state = game.initial
while True:
for player in players:
move = player(game, state)
state = game.make_move(move, state)
state = game.result(state, move)
if game.terminal_test(state):
return game.utility(state, game.to_move(game.initial))

Expand All @@ -144,17 +141,17 @@ def play_game(game, *players):
class Game:
"""A game is similar to a problem, but it has a utility for each
state and a terminal test instead of a path cost and a goal
test. To create a game, subclass this class and implement
legal_moves, make_move, utility, and terminal_test. You may
override display and successors or you can inherit their default
methods. You will also need to set the .initial attribute to the
initial state; this can be done in the constructor."""
test. To create a game, subclass this class and implement actions,
result, utility, and terminal_test. You may override display and
successors or you can inherit their default methods. You will also
need to set the .initial attribute to the initial state; this can
be done in the constructor."""

def legal_moves(self, state):
def actions(self, state):
"Return a list of the allowable moves at this point."
abstract

def make_move(self, move, state):
def result(self, state, move):
"Return the state that results from making a move from a state."
abstract

Expand All @@ -164,7 +161,7 @@ def utility(self, state, player):

def terminal_test(self, state):
"Return True if this is a final state for the game."
return not self.legal_moves(state)
return not self.actions(state)

def to_move(self, state):
"Return the player whose move it is in this state."
Expand All @@ -174,17 +171,12 @@ def display(self, state):
"Print or otherwise display the state."
print state

def successors(self, state):
"Return a list of legal (move, state) pairs."
return [(move, self.make_move(move, state))
for move in self.legal_moves(state)]

def __repr__(self):
return '<%s>' % self.__class__.__name__

class Fig62Game(Game):
"""The game represented in [Fig. 6.2]. Serves as a simple test case.
>>> g = Fig62Game()
class Fig52Game(Game):
"""The game represented in [Fig. 5.2]. Serves as a simple test case.
>>> g = Fig52Game()
>>> minimax_decision('A', g)
'a1'
>>> alphabeta_full_search('A', g)
Expand All @@ -199,15 +191,12 @@ class Fig62Game(Game):
utils = Dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)
initial = 'A'

def legal_moves(self, state):
return [move for (move, next) in self.successors(state)]
def actions(self, state):
return self.succs.get(state, {}).keys()

def make_move(self, move, state):
def result(self, state, move):
return self.succs[state][move]

def successors(self, state):
return self.succs.get(state, {}).items()

def utility(self, state, player):
if player == 'MAX':
return self.utils[state]
Expand All @@ -231,11 +220,11 @@ def __init__(self, h=3, v=3, k=3):
for y in range(1, v+1)]
self.initial = Struct(to_move='X', utility=0, board={}, moves=moves)

def legal_moves(self, state):
def actions(self, state):
"Legal moves are any square not yet taken."
return state.moves

def make_move(self, move, state):
def result(self, state, move):
if move not in state.moves:
return state # Illegal move has no effect
board = state.board.copy(); board[move] = state.to_move
Expand Down Expand Up @@ -291,12 +280,12 @@ class ConnectFour(TicTacToe):
def __init__(self, h=7, v=6, k=4):
TicTacToe.__init__(self, h, v, k)

def legal_moves(self, state):
def actions(self, state):
return [(x, y) for (x, y) in state.moves
if y == 0 or (x, y-1) in state.board]

__doc__ += random_tests("""
>>> play_game(Fig62Game(), random_player, random_player)
>>> play_game(Fig52Game(), random_player, random_player)
6
>>> play_game(TicTacToe(), random_player, random_player)
0
Expand Down

0 comments on commit 9273ba7

Please sign in to comment.