我试图在我的班上建立一个Alpha-beta剪枝算法,但我仍在努力。 我希望函数的返回是x的位置、y的位置和分数的列表。我从this github得到的原始代码是minimax,没有Alpha-beta修剪
以下是没有Alpha-beta修剪代码的原始minimax:
def minimax(state, depth, player):
"""
AI function that choice the best move
:param state: current state of the board
:param depth: node index in the tree (0 <= depth <= 9),
but never nine in this case (see iaturn() function)
:param player: an human or a computer
:return: a list with [the best row, best col, best score]
"""
if player == COMP:
best = [-1, -1, -infinity]
else:
best = [-1, -1, +infinity]
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1, -1, score]
for cell in empty_cells(state):
x, y = cell[0], cell[1]
state[x][y] = player
score = minimax(state, depth - 1, -player)
state[x][y] = 0
score[0], score[1] = x, y
if player == COMP:
if score[2] > best[2]:
best = score # max value
else:
if score[2] < best[2]:
best = score # min value
return best
以下是我修改并设法接近我想要的内容:
from math import inf as infinity
from random import choice
import platform
def minimax(state, depth, alpha, beta, player):
"""
state: current state of the board
player: integer, 1 is COMp while -1 is HUMAN
return a list of [x position, y position, score]
"""
if depth == 0 or game_over(state):
score = evaluate(state) # evaluate is a function to see the state of the current board;
computer win : +1, human win : -1, draw : 0
return [-1, -1, score]
if player==COMP:
best = [-1, -1, -infinity]
# empty_cells(state) find the current available positions on the board
for cell in empty_cells(state):
x, y = cell[0], cell[1]
state[x][y] = player
val = minimax(state, depth - 1, alpha, beta, -player)
best = max(best[2], val[2])
alpha = max(alpha, val[2])
# Alpha Beta Pruning
if beta <= alpha:
break
return best
else:
best = [-1, -1, +infinity]
# empty_cells(state) find the current available positions on the board
for cell in empty_cells(state):
x, y = cell[0], cell[1]
state[x][y] = player
val = minimax(state, depth - 1, alpha, beta, player)
best = min(best[2], val[2])
beta = min(beta, val[2])
# Alpha Beta Pruning
if beta <= alpha:
break
return best
我尝试将Alpha-beta剪枝添加到原始代码中,但是它的模式与我在互联网上发现的许多minimax不同,因此我尝试使用我熟悉的模式编写新代码。建议对这两个代码中的任何一个进行Alpha-beta剪枝都非常有用
目前没有回答
相关问题 更多 >
编程相关推荐