我正在尝试开发一种AI,以最佳方式玩1人桌游。我正在使用深度优先搜索功能。
我尝试通过对所有循环进行迭代的初始循环进行多线程并递归到游戏树中来加快速度。我的想法是,每个线程都会将可能的初始移动板分割成多个块,并在单独的递归函数中进一步进行评估。调用的所有函数均为nogil
但是,由于多线程解决方案给出了不同的结果,所以我只能猜测是一个竞争条件,而且我不确定如何解决它。
cdef struct Move:
int x
int y
int score
cdef Move search( board_t& board,int prevClears,int maxDepth,int depth ) nogil:
cdef Move bestMove
cdef Move recursiveMove
cdef vector[ Move ] moves = generateMoves( board )
cdef board_t nextBoard
cdef int i,clears
bestMove.score = 0
# Split the initial possible move boards amongst threads
for i in prange( <int> moves.size(),nogil = True ):
# Applies move and calculates the move score
nextBoard = applyMove( board,moves[ i ],prevClears,maxDepth,depth )
# Recursively evaluate further moves
if maxDepth - depth > 0:
clears = countClears( nextBoard )
recursiveMove = recursiveSearch( nextBoard,clears,depth + 1 )
moves[ i ].score += recursiveMove.score
# Update bestMove
if moves[ i ].score > bestMove.score:
bestMove = moves[ i ]
return bestMove