简体   繁体   English

序言中的深度限制 alpha-beta

[英]Depth limited alpha-beta in prolog

I'm building a chess engine in Prolog.我正在 Prolog 中构建国际象棋引擎。 The alpha-beta from "prolog programing for artificial intelligence" is not depth limited. “人工智能序言编程”中的 alpha-beta 没有深度限制。 Since it's not possible to search the whole tree in chess I'm trying to modify the code from the book to be depth limited but it's not working properly.由于不可能在国际象棋中搜索整棵树,我试图修改书中的代码以限制深度,但它无法正常工作。

here is the book code:这是本书的代码:

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

My attempts to modify it to be depth limited are :我将其修改为深度限制的尝试是:

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

I would appreciate any help.我将不胜感激任何帮助。

You have a typo in your code.你的代码有错别字。 And more importantly, you have a typo in your code that SWI-Prolog found and diagnosed for you.更重要的是, SWI-Prolog 为您发现并诊断出您的代码中存在拼写错误。 Do not ignore its warnings.不要忽视它的警告。

Look:看:

Warning: /home/isabelle/chess.pl:1:
    Singleton variables: [Alpha,Beta]
Warning: /home/isabelle/chess.pl:11:
    Singleton variables: [Alpha,Beta]
Warning: /home/isabelle/chess.pl:16:
    Singleton variables: [Depth1]
Warning: /home/isabelle/chess.pl:40:
    Singleton variables: [Pos1]

Specifically, in this clause starting on line 16:具体来说,在从第 16 行开始的这一条款中:

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).

your use of Depth1 shows that you seem to understand that a new value needs to be computed and passed into the recursion.您对Depth1的使用表明您似乎明白需要计算一个新值并将其传递给递归。 But you forgot to use this variable on the preceding line:但是你忘记在前一行使用这个变量:

Depth is Depth - 1,

this should use Depth1 in the left hand side.这应该在左侧使用Depth1

It is a mistake to even try to test code that has singleton warnings, since singleton warnings are often an indication of very serious bugs.甚至尝试测试具有单例警告的代码也是错误的,因为单例警告通常表明存在非常严重的错误。 This is the case here.这是这里的情况。 When you see a singleton warning, the first thing you should do is to understand and fix it---any other interaction with the program is likely meaningless.当您看到单例警告时,您应该做的第一件事是理解并修复它——与程序的任何其他交互都可能毫无意义。

It is true that some of these warnings are "benign": The warnings for the clause on the first line do not seem to indicate bugs in that clause.确实,其中一些警告是“良性的”:第一行中子句的警告似乎并不表示该子句中存在错误。 Maybe you have seen some of these warnings and decided that they are harmless.也许您已经看到了其中一些警告,并认为它们是无害的。 The exact opposite is the case: Accepting such "spurious" singleton warnings means that you risk serious warnings slipping in.情况正好相反:接受这种“虚假的”单例警告意味着您冒着严重警告溜进来的风险。

(I haven't tested the corrected program since it's understandably incomplete.) (我没有测试更正后的程序,因为它不完整是可以理解的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM