Advertisement
Guest User

alphabeta iterativedeepening

a guest
Feb 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. # Minimax AI for Connect 4 with alpha-beta pruning
  2. # @author Everett Harding
  3.  
  4. from minimax import *
  5. import time
  6. class AlphaBeta(Minimax):
  7. """ alphabeta object that takes a current connect four"""
  8. timeLimit = 0
  9.  
  10. def __init__(self, board):
  11. #copy the board
  12. self.timeLimit = 70
  13. super(AlphaBeta, self).__init__(board)
  14.  
  15. def bestMove(self, depth, state, curr_player):
  16. """ Returns the best move (as a column number) and the associated alpha
  17. Calls search()
  18. """
  19. print ("alphabeta pruning move")
  20. # determine opponent's color
  21. if curr_player == self.colors[0]:
  22. opp_player = self.colors[1]
  23. else:
  24. opp_player = self.colors[0]
  25.  
  26. # enumerate all legal moves
  27. legal_moves = {} # will map legal move states to their alpha values
  28. # for col in range(7):
  29. # # if column i is a legal move...
  30. # if self.isLegalMove(col, state):
  31. # # make the move in column 'col' for curr_player
  32. # temp = self.makeMove(state, col, curr_player)
  33. # legal_moves[col] = self.search(depth, temp, opp_player, -1000000, 1000000, curr_player)
  34.  
  35. legal_moves = self.iterativeDeep(depth, self.timeLimit, state, curr_player, opp_player)
  36. best_alpha = -99999999
  37. best_move = None
  38. moves = legal_moves.items()
  39. random.shuffle(list(moves))
  40. for move, alpha in moves:
  41. print("Compare", alpha, "to", best_alpha)
  42. if alpha > best_alpha:
  43. best_alpha = alpha
  44. best_move = move
  45.  
  46. return best_move, best_alpha
  47.  
  48. # iterative deepening wrapper for alphabeta search
  49. def iterativeDeep(self, depth, timeLimit, state, curr_player, opp_player):
  50. d = 0
  51. startTime = time.clock()
  52. legal_moves = {}
  53. alpha = -1000000
  54. beta = 1000000
  55. while d <= depth and time.clock() - startTime <= timeLimit:
  56. for col in range(7):
  57. if self.isLegalMove(col, state):
  58. # make the move in column 'col' for curr_player
  59. temp = self.makeMove(state, col, curr_player)
  60. substart = time.clock()
  61. legal_moves[col] = self.search(d, temp, opp_player, alpha, beta, curr_player, substart, timeLimit/7)
  62. d += 1
  63. return legal_moves
  64.  
  65. # based on pseudocode from wikipedia
  66. def search(self, depth, state, curr_player, alpha, beta, me, startTime, limit):
  67. """ minimax search with alpha-beta pruning"""
  68. children = []
  69.  
  70. for col in range (7):
  71. if self.isLegalMove(col, state):
  72. temp = self.makeMove(state, col, curr_player)
  73.  
  74. children.append(temp)
  75. children = sorted(children, key = lambda x: -self.value(x, me))
  76. # determine opponent's color
  77. if curr_player == self.colors[0]:
  78. opp_player = self.colors[1]
  79. else:
  80. opp_player = self.colors[0]
  81.  
  82. if depth == 0 or len(children) == 0 or self.gameIsOver(state) or time.clock() - startTime >= limit:
  83. #print("Max Depth heuristic : ", self.value(state, curr_player))
  84. # for row in temp:
  85. # print(row)
  86. # print("\n")
  87. # print("Value:", self.value(state, me))
  88. # input("continue?\n")
  89. return self.value(state, curr_player)
  90.  
  91. if curr_player == me:
  92. #do max search
  93. val = -10000000
  94. for child in children:
  95. val = max(val, self.search(depth - 1, child, opp_player, alpha, beta, me, startTime, limit))
  96. alpha = max(alpha, val)
  97. if(beta <= alpha):
  98. break
  99. return val
  100. else :
  101. #do min search
  102. val = 10000000
  103. for child in children:
  104. val = min(val, self.search(depth - 1, child, opp_player, alpha, beta, me, startTime, limit))
  105. beta = min(beta, val)
  106. if (beta <= alpha):
  107. break
  108. return val
  109.  
  110. def value(self, state, color):
  111. """ Simple heuristic to evaluate board configurations
  112. Heuristic is (num of 4-in-a-rows)*99999 + (num of 3-in-a-rows)*100 +
  113. (num of 2-in-a-rows)*10 - (num of opponent 4-in-a-rows)*99999 - (num of opponent
  114. 3-in-a-rows)*100 - (num of opponent 2-in-a-rows)*10
  115. """
  116. if color == self.colors[0]:
  117. o_color = self.colors[1]
  118. else:
  119. o_color = self.colors[0]
  120.  
  121. my_fours = self.checkForStreak(state, color, 4)
  122. my_threes = self.checkForStreak(state, color, 3)
  123. my_twos = self.checkForStreak(state, color, 2)
  124. opp_fours = self.checkForStreak(state, o_color, 4)
  125. opp_threes = self.checkForStreak(state, o_color, 3)
  126. opp_twos = self.checkForStreak(state, o_color, 2)
  127. if opp_fours > 0:
  128. return -10000000
  129. else:
  130. return my_fours * 100000 + my_threes * 100 + my_twos - (opp_threes *1500 + opp_twos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement