4

首先让我说这个问题可能由没有 Prolog 经验的 AI 向导来回答。

优秀的Prolog Programming for AI书有这个非常简洁和聪明的 minimax 实现:

minimax( Pos, BestSucc, Val)  :-
  moves( Pos, PosList), !,               % Legal moves in Pos produce PosList
  best( PosList, BestSucc, Val)
   ;
   staticval( Pos, Val).                 % Pos has no successors: evaluate statically 

best( [ Pos], Pos, Val)  :-
  minimax( Pos, _, Val), !.

best( [Pos1 | PosList], BestPos, BestVal)  :-
  minimax( Pos1, _, Val1),
  best( PosList, Pos2, Val2),
  betterof( Pos1, Val1, Pos2, Val2, BestPos, BestVal).

betterof( Pos0, Val0, Pos1, Val1, Pos0, Val0)  :-        % Pos0 better than Pos1
  min_to_move( Pos0),                                    % MIN to move in Pos0
  Val0 > Val1, !                                         % MAX prefers the greater value
  ;
  max_to_move( Pos0),                                    % MAX to move in Pos0
  Val0 < Val1, !.                                % MIN prefers the lesser value 

betterof( Pos0, Val0, Pos1, Val1, Pos1, Val1).           % Otherwise Pos1 better than Pos0

然而,作者并没有详细描述它,我想知道它是什么min_to_move/1max_to_move/1是什么。

谁能向我解释这些?

提前致谢!

4

1 回答 1

4

显然,当且仅当“最小化”玩家要在 Pos 位置移动时,min_to_move(Pos) 才为真。max_to_move/1 则相反。就个人而言,我发现这里描述的编码风格不是很好。例如,在某些情况下,if-then-else ((->)/2 和 (;)/2) 似乎更适合表达意图。谓词名称也可以更具描述性(例如考虑“positions_best/2”来描述位置列表和最佳选择之间的关系,而不仅仅是“best/3”)并且更具可读性(例如“ betterof”,除了比“better_of”更难阅读?)。

于 2011-11-02T12:09:02.870 回答