1- """Games or Adversarial Search. (Chapter 5)"""
1+ """Games or Adversarial Search (Chapter 5)"""
22
33import copy
44import itertools
55import random
66from collections import namedtuple
77
8- from utils4e import vector_add , MCT_Node , ucb , inf
8+ import numpy as np
9+
10+ from utils4e import vector_add , MCT_Node , ucb
911
1012GameState = namedtuple ('GameState' , 'to_move, utility, board, moves' )
1113StochasticGameState = namedtuple ('StochasticGameState' , 'to_move, utility, board, moves, chance' )
@@ -24,15 +26,15 @@ def minmax_decision(state, game):
2426 def max_value (state ):
2527 if game .terminal_test (state ):
2628 return game .utility (state , player )
27- v = - inf
29+ v = - np . inf
2830 for a in game .actions (state ):
2931 v = max (v , min_value (game .result (state , a )))
3032 return v
3133
3234 def min_value (state ):
3335 if game .terminal_test (state ):
3436 return game .utility (state , player )
35- v = inf
37+ v = np . inf
3638 for a in game .actions (state ):
3739 v = min (v , max_value (game .result (state , a )))
3840 return v
@@ -53,13 +55,13 @@ def expect_minmax(state, game):
5355 player = game .to_move (state )
5456
5557 def max_value (state ):
56- v = - inf
58+ v = - np . inf
5759 for a in game .actions (state ):
5860 v = max (v , chance_node (state , a ))
5961 return v
6062
6163 def min_value (state ):
62- v = inf
64+ v = np . inf
6365 for a in game .actions (state ):
6466 v = min (v , chance_node (state , a ))
6567 return v
@@ -94,7 +96,7 @@ def alpha_beta_search(state, game):
9496 def max_value (state , alpha , beta ):
9597 if game .terminal_test (state ):
9698 return game .utility (state , player )
97- v = - inf
99+ v = - np . inf
98100 for a in game .actions (state ):
99101 v = max (v , min_value (game .result (state , a ), alpha , beta ))
100102 if v >= beta :
@@ -105,7 +107,7 @@ def max_value(state, alpha, beta):
105107 def min_value (state , alpha , beta ):
106108 if game .terminal_test (state ):
107109 return game .utility (state , player )
108- v = inf
110+ v = np . inf
109111 for a in game .actions (state ):
110112 v = min (v , max_value (game .result (state , a ), alpha , beta ))
111113 if v <= alpha :
@@ -114,8 +116,8 @@ def min_value(state, alpha, beta):
114116 return v
115117
116118 # Body of alpha_beta_search:
117- best_score = - inf
118- beta = inf
119+ best_score = - np . inf
120+ beta = np . inf
119121 best_action = None
120122 for a in game .actions (state ):
121123 v = min_value (game .result (state , a ), best_score , beta )
@@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
135137 def max_value (state , alpha , beta , depth ):
136138 if cutoff_test (state , depth ):
137139 return eval_fn (state )
138- v = - inf
140+ v = - np . inf
139141 for a in game .actions (state ):
140142 v = max (v , min_value (game .result (state , a ), alpha , beta , depth + 1 ))
141143 if v >= beta :
@@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth):
146148 def min_value (state , alpha , beta , depth ):
147149 if cutoff_test (state , depth ):
148150 return eval_fn (state )
149- v = inf
151+ v = np . inf
150152 for a in game .actions (state ):
151153 v = min (v , max_value (game .result (state , a ), alpha , beta , depth + 1 ))
152154 if v <= alpha :
@@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth):
158160 # The default test cuts off at depth d or at a terminal state
159161 cutoff_test = (cutoff_test or (lambda state , depth : depth > d or game .terminal_test (state )))
160162 eval_fn = eval_fn or (lambda state : game .utility (state , player ))
161- best_score = - inf
162- beta = inf
163+ best_score = - np . inf
164+ beta = np . inf
163165 best_action = None
164166 for a in game .actions (state ):
165167 v = min_value (game .result (state , a ), best_score , beta , 1 )
0 commit comments