首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >国际象棋:静态搜索主导运行时

国际象棋:静态搜索主导运行时
EN

Stack Overflow用户
提问于 2017-05-17 23:46:08
回答 1查看 629关注 0票数 1

我已经实现了一个α-β搜索与安静搜索我的象棋引擎。然而,在大多数位置,静态搜索占用了总执行时间的80-90%,正如我的分析器所指出的。我修剪的时候有虫子吗?

我包括了α-β程序和静态例程。

我的静态搜索直接基于这个伪码

代码语言:javascript
复制
// Perform the alpha-beta search.
func ab(b *dragontoothmg.Board, alpha int16, beta int16, depth int8, halt chan bool, stop *bool) (int16, dragontoothmg.Move) {
    nodeCount++

    if *stop {
        return alpha, 0
    }

    found, tableMove, tableEval, tableDepth, tableNodeType := transtable.Get(b)
    if found && tableDepth >= depth {
        if tableNodeType == transtable.Exact {
            return tableEval, tableMove
        } else if tableNodeType == transtable.LowerBound {
            alpha = max(alpha, tableEval)
        } else { // upperbound
            beta = min(beta, tableEval)
        }
        if alpha >= beta {
            return tableEval, tableMove
        }
    }
    if depth == 0 {
        //return eval.Evaluate(b), 0
        return quiesce(b, alpha, beta, stop), 0
    }

    alpha0 := alpha
    bestVal := int16(negInf) 
    moves := b.GenerateLegalMoves()
    var bestMove dragontoothmg.Move
    if len(moves) > 0 {
        bestMove = moves[0] // randomly pick some move
    }
    for _, move := range moves {
        unapply := b.Apply(move)
        var score int16
        score, _ = ab(b, -beta, -alpha, depth-1, halt, stop)
        score = -score
        unapply()
        if score > bestVal {
            bestMove = move
            bestVal = score
        }
        alpha = max(alpha, score)
        if alpha >= beta {
            break
        }
    }

    if *stop {
        return bestVal, bestMove
    }

    var nodeType uint8
    if bestVal <= alpha0 {
        nodeType = transtable.UpperBound
    } else if bestVal >= beta {
        nodeType = transtable.LowerBound
    } else {
        nodeType = transtable.Exact
    }
    transtable.Put(b, bestMove, bestVal, depth, nodeType)
    return bestVal, bestMove
}

func quiesce(b *dragontoothmg.Board, alpha int16, beta int16, stop *bool) int16 {
    nodeCount++
    if *stop {
        return alpha
    }
    var standPat int16
    found, _, evalresult, _, ntype := transtable.Get(b)
    if found && ntype == transtable.Exact {
        standPat = evalresult
    } else {
        standPat = eval.Evaluate(b)
        transtable.Put(b, 0, standPat, 0, transtable.Exact)
    }
    if standPat >= beta {
        return beta
    }
    if alpha < standPat {
        alpha = standPat
    }
    moves := b.GenerateLegalMoves()
    if len(moves) == 0 { // TODO(dylhunn): What about stalemate?
        return negInf
    }
    for _, move := range moves {
        if !isCapture(move, b) {
            continue
        }
        unapply := b.Apply(move)
        score := -quiesce(b, -beta, -alpha, stop)
        unapply()
        if score >= beta {
            return beta
        }
        if score > alpha {
            alpha = score
        }
    }
    return alpha
}

func isCapture(m dragontoothmg.Move, b *dragontoothmg.Board) bool {
    toBitboard := (uint64(1) << m.To())
    return (toBitboard&b.White.All != 0) || (toBitboard&b.Black.All != 0)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-07 20:39:26

如果我正确地阅读了您的代码,您将搜索所有捕获。为了节省工作,你能做的是修剪无望的抓捕动作。事实证明,移动非常糟糕,跳过它们是安全的,所以这种技术是相当安全的。

例如,看看这个职位:

FEN:rnbqkbnr/pppppppp/8/8/8/8/1PP1PPP1/RNBQKBNR w KQkq - 0 1

有三条截图:

  • Rxa7
  • Qxd7+
  • Rxh7

让我们假设引擎首先尝试与女王的捕捉移动。黑色有四种方法来捕获回来,但任何这些动作都可能导致截止符。

例如,黑色播放Bxd7。现在white在结果位置有两个捕获,Rxa7或Rxh7。

在这里,大多数引擎将认识到,白色已经倒退的材料(相较于测试版),即使捕获一个棋子也不会有帮助。因此,这两个车捕获不太可能导致一个截止。

在这里,您当前的搜索仍将继续搜索这些移动。发现这样的情况并跳过这些动作将节省大量的工作。

还有进一步的优化。例如,强大的引擎与静态交换评价将立即看到Qxd7将赢得一个棋子,但将失去女王。由于这是一个糟糕的交易,引擎可以立即跳过这一步。另外两辆车的捕获也是如此。

不过,和往常一样,我们也有一种权衡。如果你修剪的太积极,你最终也会修剪好的动作。一般来说,我建议花更多的时间在正常的搜索,而不是在安静的搜索,所以积极的修剪应该是好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44036416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档