【发布时间】:2021-02-01 19:40:17
【问题描述】:
我正在 Prolog 中构建一个国际象棋引擎。 “人工智能序言编程”中的 alpha-beta 不受深度限制。 由于无法在国际象棋中搜索整棵树,我正在尝试修改书中的代码以限制深度,但它无法正常工作。
这是书的代码:
alphabeta(Pos, Alpha, Beta, GoodPos, Val) :-
moves(Pos, PosList), !,
boundedbest( PosList, Alpha, Beta, GoodPos, Val)
;
get_pos_value(Pos,Val). % static value of Pos
boundedbest([Pos | PosList], Alpha, Beta, GoodPos, GoodVal) :-
alphabeta( Pos, Alpha, Beta, _, Val),
goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal).
goodenough([],_,_,Pos, Val, Pos, Val) :- !. % no other candidate
goodenough(_, Alpha, Beta, Pos, Val, Pos, Val) :-
min_to_move(Pos), Val > Beta, !; % Maximizer attained upper bound
max_to_move(Pos), Val < Alpha, !. % Minimizer attained lower bound
goodenough( PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal) :-
newbounds( Alpha, Beta, Pos, Val, NewAlpha, NewBeta), % Refine bounds
boundedbest(PosList, NewAlpha, NewBeta, Pos1, Val1),
betterof( Pos, Val, Pos1, Val1, GoodPos, GoodVal).
newbounds(Alpha, Beta, Pos, Val, Val, Beta) :-
min_to_move(Pos), Val > Alpha, !. % Mazximizer increased lower bound
newbounds(Alpha, Beta, Pos, Val, Alpha, Val):-
max_to_move(Pos), Val < Beta, !. % Minimizer decreased upper bound
newbounds( Alpha, Beta, _,_,Alpha, Beta). % otherwise bounds unchanged.
betterof(Pos, Val, Pos1, Val1, Pos, Val) :- % Pos better than Pos1
min_to_move(Pos), Val > Val1, !;
max_to_move(Pos), Val < Val1, !.
betterof(_,_,Pos1,Val1,Pos1,Val1). % otherwise Pos 1 better
我尝试将其修改为深度限制是:
alphabeta(Pos, Alpha, Beta, Pos, Val, 0) :- % max depth of search recieved
get_pos_value(Pos,Val). % static value of Pos
alphabeta(Pos, Alpha, Beta, GoodPos, Val, Depth) :-
Depth > 0,
moves(Pos, PosList), !,
boundedbest( PosList, Alpha, Beta, GoodPos, Val,Depth).
alphabeta(Pos, Alpha, Beta, Pos, Val, Depth) :-
Depth > 0,
get_pos_value(Pos,Val).
boundedbest([Pos | PosList], Alpha, Beta, GoodPos, GoodVal,Depth) :-
Depth is Depth - 1,
alphabeta( Pos, Alpha, Beta, _, Val,Depth1),
goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal).
goodenough([],_,_,Pos, Val, Pos, Val) :- !. % no other candidate
goodenough(_, Alpha, Beta, Pos, Val, Pos, Val) :-
min_to_move(Pos), Val > Beta, !; % Maximizer attained upper bound
max_to_move(Pos), Val < Alpha, !. % Minimizer attained lower bound
goodenough( PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal) :-
newbounds( Alpha, Beta, Pos, Val, NewAlpha, NewBeta), % Refine bounds
boundedbest(PosList, NewAlpha, NewBeta, Pos1, Val1,_),
betterof( Pos, Val, Pos1, Val1, GoodPos, GoodVal).
newbounds(Alpha, Beta, Pos, Val, Val, Beta) :-
min_to_move(Pos), Val > Alpha, !. % Mazximizer increased lower bound
newbounds(Alpha, Beta, Pos, Val, Alpha, Val):-
max_to_move(Pos), Val < Beta, !. % Minimizer decreased upper bound
newbounds( Alpha, Beta, _,_,Alpha, Beta). % otherwise bounds unchanged.
betterof(Pos, Val, Pos1, Val1, Pos, Val) :- % Pos better than Pos1
min_to_move(Pos), Val > Val1, !;
max_to_move(Pos), Val < Val1, !.
betterof(_,_,Pos1,Val1,Pos1,Val1). % otherwise Pos 1 better
我将不胜感激。
【问题讨论】:
-
仅供参考:Ivan Bratko 的“Prolog Programming for Artificial Intelligence”第 4 版,第 24.3 节 alpha-beat 算法:minimax 的有效实现,第 585 页
-
是的,第 585 页上的代码没有深度限制,不适用于国际象棋游戏,它使用 alpha-beta 进行修剪,但搜索到树的底部。
-
您使用的是哪个 Prolog?可能有类似“深度限制的调用”或类似的东西。喜欢这个:swi-prolog.org/pldoc/doc_for?object=call_with_depth_limit/3
标签: prolog chess minimax alpha-beta-pruning