Created
July 11, 2015 18:13
-
-
Save kartikkukreja/e58a77d6380f1af9b1f3 to your computer and use it in GitHub Desktop.
Iterative Deepening Alpha Beta Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def iterativeDeepeningAlphaBeta(state, evaluationFunc): | |
startTime = time() | |
def alphaBetaSearch(state, alpha, beta, depth): | |
def maxValue(state, alpha, beta, depth): | |
val = -MaxUtility | |
for successor in state.getSuccessors(): | |
val = max(val, alphaBetaSearch(successor, alpha, beta, depth)) | |
if val >= beta: return val | |
alpha = max(alpha, val) | |
return val | |
def minValue(state, alpha, beta, depth): | |
val = MaxUtility | |
for successor in state.getSuccessors(): | |
val = min(val, alphaBetaSearch(successor, alpha, beta, depth - 1)) | |
if val <= alpha: return val | |
beta = min(beta, val) | |
return val | |
if state.isTerminalState(): return state.getTerminalUtility() | |
if depth <= 0 or time() - startTime > MaxAllowedTimeInSeconds: return evaluationFunc(state) | |
return maxValue(state, alpha, beta, depth) if state.blackToMove == IsPlayerBlack else minValue(state, alpha, beta, depth) | |
bestMove = None | |
for depth in xrange(1, MaxDepth): | |
if time() - startTime > MaxAllowedTimeInSeconds: break | |
val = -MaxUtility | |
for successor in state.getSuccessors(): | |
score = alphaBetaSearch(successor, -MaxUtility, MaxUtility, depth) | |
if score > val: | |
val, bestMove = score, successor.moves | |
return bestMove |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment