[英]prolog alpha-beta unexpected results
I have modified a generic alpha-beta from a book to be depth limited.我已将书中的通用 alpha-beta 修改为深度受限。 When printing out the best position search results it sometimes works and sometimes I get a nuisance result such a the number 8.
当打印出最佳位置搜索结果时,它有时会起作用,有时我会得到一个令人讨厌的结果,例如数字 8。
This is a very generic alpha-beta from "prolog for artificial intelligence".这是“人工智能序言”中非常通用的 alpha-beta。 I'm trying to narrow down to whether the issue is with the alpha-beta or somewhere else in my code.
我试图缩小问题是与 alpha-beta 还是我的代码中的其他地方有关。
Can someone please tell me if this depth limited alpha-beta seems ok?有人可以告诉我这个深度受限的 alpha-beta 是否合适?
Here is the code:这是代码:
alphabeta(Pos, Alpha, Beta, GoodPos, Val, Depth):-
Depth > 0,
moves(Pos, PosList), !,
boundedbest(PosList, Alpha, Beta, GoodPos, Val, Depth);
get_pos_value(Pos,Val). % static value of position
boundedbest([Pos | PosList], Alpha, Beta, GoodPos, GoodVal, Depth):-
NewDepth is Depth - 1,
alphabeta(Pos, Alpha, Beta,_,Val, NewDepth),
goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal, Depth).
goodenough([],_,_,Pos, Val, Pos, Val,_):- !. % no other candidate
goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
min_to_move(Pos), Val > Beta, !; % Maximizer attainded upper bound
max_to_move(Pos), Val < Alpha,!. % Minimizer attained lower bound
goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal, Depth):-
newbounds(Alpha, Beta, Pos, Val, NewAlpha, NewBeta), % refine bounds
boundedbest(PosList, NewAlpha, NewBeta, Pos1, Val1, Depth),
betterof(Pos,Val, Pos1, Val1, GoodPos, GoodVal).
newbounds(Alpha, Beta, Pos, Val, Val, Beta):-
min_to_move(Pos), Val > Alpha, !. % Maximizer 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 Pos1 better
Here's some comments on these predicates goodenough and betterof which are written in the same way.这里有一些关于这些谓词 goodenough 和 betterof 的评论,它们以相同的方式编写。 I suppose when Val > Val1 or Beta, you want min_to_move and in the case Val < Alpha or Val1, you want max_to_move.
我想当 Val > Val1 或 Beta 时,您需要 min_to_move,而在 Val < Alpha 或 Val1 的情况下,您需要 max_to_move。
goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
min_to_move(Pos), Val > Beta, !; % Maximizer attainded upper bound
max_to_move(Pos), Val < Alpha,!. % Minimizer attained lower bound
betterof(Pos, Val,Pos1, Val1, Pos, Val):- % Pos better than Pos1
min_to_move(Pos), Val > Val1,!;
max_to_move(Pos), Val < Val1,!.
Perhaps more explicitly written:也许更明确地写成:
goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
Val > Beta, !,
min_to_move(Pos).
goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
Val < Alpha, !,
max_to_move(Pos).
This code shows that there are cases not handled by your disjunction in goodenough, which interprets as fail.这段代码表明,有些情况在 goodenough 中没有被你的析取处理,这解释为失败。 Is this intentional ?
这是故意的吗?
In goodenough/8, if argument 5 Val is different from argument 7 Val, the predicate fails.在 goodenough/8 中,如果参数 5 Val 与参数 7 Val 不同,则谓词失败。 Is this intentional ?
这是故意的吗?
I have the impression you are optimizing the code before time!我的印象是您正在优化代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.