微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Negamax 算法中设置最佳走法的问题

如何解决在 Negamax 算法中设置最佳走法的问题

嘿,我想制作一个国际象棋引擎,但是当我运行代码时,它运行良好,问题是有时它没有设置最佳移动,并且显示错误,因为搜索没有返回要播放的移动。我的 negamax 包括 AB 修剪、MD 修剪、QSearch、ID 和 TT 表。这是我的实现。

    Entry=namedtuple('Entry','score depth flag')
    
    class Search():
        def __init__(self):
            self.nodes=0
            self.tt_score={}
    
        def search(self,position,api):
            self.nodes=0
            for depth in range(1,1000):
                ret=self.negamax(position,api,-INFINITY,INFINITY,depth,ply=1)
                yield ret
    
        def negamax(self,alpha,beta,depth=3,ply=1):
            best,bmove=-INFINITY,()
            for move in position.moves():
                score=self.negasearch(position.move(move),-beta,-alpha,depth-1,ply+1)
                if score>=beta: return [move,score,self.nodes]
                if score>best:
                    best,bmove=score,move
                    #api.arrow("clear")
                    #api.arrow(coordinates[move[0]]+coordinates[move[1]])
                    if score>alpha: alpha=score
            return [bmove,best,self.nodes]
    
        def negasearch(self,ply):
            best,aorig=-INFINITY,alpha
            self.nodes+=1
            if MATE-ply<beta:
                beta=MATE-ply
                if alpha>=MATE-ply: return MATE-ply
            if -MATE+ply>alpha:
                alpha=-MATE+ply
                if beta<=-MATE+ply: return -MATE+ply
            entry=self.tt_score.get(position.hash(),Entry(0,-1,'exact'))
            if entry.depth>=depth:
                if entry.flag=='exact': return entry.score
                elif entry.flag=='lower': alpha=max(alpha,entry.score)
                elif entry.flag=='upper': beta=min(beta,entry.score)
                if alpha>=beta: return entry.score
            if depth<=0: return self.qsearch(position,ply+1)
            for move in position.moves():
                score=self.negasearch(position.move(move),ply+1)
                if score>=beta:
                    best=score
                    break
                if score>best:
                    best=score
                    if score>alpha: alpha=score
            if best<=aorig: self.tt_score[position.hash()]=Entry(best,'upper')
            elif best>=beta: self.tt_score[position.hash()]=Entry(best,'lower')
            else: self.tt_score[position.hash()]=Entry(best,'exact')
            return best
    
        def qsearch(self,ply):
            stand_pat=position.score
            if stand_pat>=beta: return beta
            if alpha<stand_pat: alpha=stand_pat
            self.nodes+=1
            for move in position.moves():
                if move[2]==0: continue
                score=self.qsearch(position.move(move),ply+1)
                if score>=beta: return beta
                if score>alpha: alpha=score
            return alpha

您可能已经注意到我没有按应有的方式否定每回合的分数。原因是因为当您调用 move 函数时,它会返回分数已经否定的位置。例如,

    def move(self,move):
        # copy the current position board,turn,and score
        # if move is a capture then preform:
        #     score+=pvs[pieceBeingCaptured]
        # preform move
        # if the next player is mated after this move then set score to MATE
        return Position(board,-score,self.turn*-1)

它不是移动生成,因为它返回每个位置状态的正确移动。最佳值是 -INFINITY 和 negamax,它应该始终设置最佳移动。任何帮助都会有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。