我能找到的所有延续教程都是关于固定长度延续的(即数据结构在遍历时具有已知数量的项目
我正在实现 DepthFirstSearch Negamax(http://en.wikipedia.org/wiki/Negamax),虽然代码有效,但我想使用延续重写代码
我的代码如下
let naiveDFS driver depth game side =
List.map (fun x ->
//- negamax depth-1 childnode opposite side
(x, -(snd (driver (depth-1) (update game x) -side))))
(game.AvailableMoves.Force())
|> List.maxBy snd
let onPlay game = match game.Turn with
| Black -> -1
| White -> 1
///naive depth first search using depth limiter
let DepthFirstSearch (depth:int) (eval:Evaluator<_>) (game:GameState) : (Move * Score) =
let myTurn = onPlay game
let rec searcher depth game side =
match depth with
//terminal Node
| x when x = 0 || (isTerminal game) -> let movescore = (eval ((),game)) |> fst
(((-1,-1),(-1,-1)),(movescore * side))
//the max of the child moves, each child move gets mapped to
//it's associated score
| _ -> naiveDFS searcher depth game side
其中 update 使用给定的移动更新游戏状态, eval 评估游戏状态并返回一个增量器(当前未使用)用于增量评估, isTerminal 评估该位置是否为结束位置。
问题是我必须注册一个未知数量的动作(每个剩余的 list.map 迭代)才能继续,我实际上无法想象一种有效的方法来做到这一点。
由于这是一个指数算法,我显然希望尽可能地保持它的效率(虽然我的大脑在试图弄清楚这个是我们的,所以我确实想要答案而不是一个有效的答案)
谢谢