2

我使用爬山和光束搜索算法在 Prolog 中为 nqueens 拼图制作了两个程序。

不幸的是,我没有检查程序是否正确的经验,我陷入了死胡同。

如果有人可以帮助我,我将不胜感激。不幸的是,爬山的程序不正确。:( 束搜索中的程序是:

queens(N, Qs) :-  
  range(1, N, Ns), 
  queens(Ns, [], Qs).

range(N, N, [N]) :- !.
range(M, N, [M|Ns]) :- 
  M < N, 
  M1 is M+1, 
  range(M1, N, Ns).

queens([], Qs, Qs).
queens(UnplacedQs, SafeQs, Qs) :- 
  select(UnplacedQs, UnplacedQs1,Q),
  not_attack(SafeQs, Q),  
  queens(UnplacedQs1, [Q|SafeQs], Qs).  

not_attack(Xs, X) :- 
  not_attack(Xs, X, 1).
not_attack([], _, _) :- !.
not_attack([Y|Ys], X, N) :-
  X =\= Y+N,  
  X =\= Y-N, 
  N1 is N+1, 
  not_attack(Ys, X, N1).

select([X|Xs], Xs, X).
select([Y|Ys], [Y|Zs], X) :- select(Ys, Zs, X).
4

3 回答 3

1

我想提一下这个问题是一个典型的约束满足问题,可以使用SWI-Prolog的 CSP 模块来高效解决。这是完整的算法:

:- use_module(library(clpfd)).

queens(N, L) :-
    N #> 0,
    length(L, N),
    L ins 1..N,
    all_different(L),
    applyConstraintOnDescDiag(L),
    applyConstraintOnAscDiag(L),
    label(L).

applyConstraintOnDescDiag([]) :- !.
applyConstraintOnDescDiag([H|T]) :-
    insertConstraintOnDescDiag(H, T, 1),
    applyConstraintOnDescDiag(T).

insertConstraintOnDescDiag(_, [], _) :- !.
insertConstraintOnDescDiag(X, [H|T], N) :-
    H #\= X + N,
    M is N + 1,
    insertConstraintOnDescDiag(X, T, M).

applyConstraintOnAscDiag([]) :- !.
applyConstraintOnAscDiag([H|T]) :-
    insertConstraintOnAscDiag(H, T, 1),
    applyConstraintOnAscDiag(T).

insertConstraintOnAscDiag(_, [], _) :- !.
insertConstraintOnAscDiag(X, [H|T], N) :-
    H #\= X - N,
    M is N + 1,
    insertConstraintOnAscDiag(X, T, M).

N是皇后的数量或棋盘的大小 ( N\cdot N),L={X_1,\cdots,X_n}其中是皇后在线X_i \in [1,N]X的位置一世

让我们详细介绍上面算法的每个部分,以了解发生了什么。

:- use_module(library(clpfd)).

它指示SWI-Prolog加载包含约束满足问题的谓词的模块。

queens(N, L) :-
        N #> 0,
        length(L, N),
        L ins 1..N,
        all_different(L),
        applyConstraintOnDescDiag(L),
        applyConstraintOnAscDiag(L),
        label(L).

queens谓词是算法的入口点,并检查术语的格式是否正确(数字范围、列表的长度)。它检查皇后是否也在不同的线上。

applyConstraintOnDescDiag([]) :- !.
applyConstraintOnDescDiag([H|T]) :-
    insertConstraintOnDescDiag(H, T, 1),
    applyConstraintOnDescDiag(T).

insertConstraintOnDescDiag(_, [], _) :- !.
insertConstraintOnDescDiag(X, [H|T], N) :-
    H #\= X + N,
    M is N + 1,
    insertConstraintOnDescDiag(X, T, M).

它检查迭代的当前皇后的后代对角线上是否有皇后。

applyConstraintOnAscDiag([]) :- !.
applyConstraintOnAscDiag([H|T]) :-
    insertConstraintOnAscDiag(H, T, 1),
    applyConstraintOnAscDiag(T).

insertConstraintOnAscDiag(_, [], _) :- !.
insertConstraintOnAscDiag(X, [H|T], N) :-
    H #\= X - N,
    M is N + 1,
    insertConstraintOnAscDiag(X, T, M).

与之前相同,但它检查上升对角线上是否有皇后。

最后可以通过调用谓词找到结果queens/2,如:

?- findall(X, queens(4, X), L).
L = [[2, 4, 1, 3], [3, 1, 4, 2]]
于 2017-03-08T16:24:31.530 回答
0

如果我正确阅读了您的代码,则您尝试实现的算法是简单的深度优先搜索而不是光束搜索。没关系,因为它应该是(我不知道光束搜索如何有效解决这个问题,而且它可能很难编程)。

我不会为你调试这段代码,但我会给你一个建议:自下而上构建棋盘

queens(0, []).
queens(N, [Q|Qs]) :-
    M is N-1,
    queens(M, Qs),
    between(1, N, Q),
    safe(Q, Qs).

如果没有攻击,哪里safe(Q,Qs)是真的。然后是简单检查(参见 SWI-Prolog 手册)和您的谓词的结合,乍一看似乎是正确的。QsQsafe/2memberchk/2not_attack/2

于 2011-02-05T12:54:47.620 回答
-1

Google上进行快速检查后,您可以找到一些 候选者与您的代码进行比较并找出要更改的内容。

对于纯粹的清晰度,我最喜欢的解决方案是上面链接的第二个:

% This program finds a solution to the 8 queens problem.  That is, the problem of placing 8
% queens on an 8x8 chessboard so that no two queens attack each other.  The prototype
% board is passed in as a list with the rows instantiated from 1 to 8, and a corresponding
% variable for each column.  The Prolog program instantiates those column variables as it
%  finds the solution.

% Programmed by Ron Danielson, from an idea by Ivan Bratko.

% 2/17/00


queens([]).                                 % when place queen in empty list, solution found

queens([ Row/Col | Rest]) :-                % otherwise, for each row
            queens(Rest),                   % place a queen in each higher numbered row
            member(Col, [1,2,3,4,5,6,7,8]), % pick one of the possible column positions
            safe( Row/Col, Rest).           % and see if that is a safe position
                                            % if not, fail back and try another column, until
                                            % the columns are all tried, when fail back to
                                            % previous row

safe(Anything, []).                         % the empty board is always safe

safe(Row/Col, [Row1/Col1 | Rest]) :-        % see if attack the queen in next row down
            Col =\= Col1,                   % same column?
            Col1 - Col =\= Row1 - Row,      % check diagonal
            Col1 - Col =\= Row - Row1,
            safe(Row/Col, Rest).            % no attack on next row, try the rest of board

member(X, [X | Tail]).                      % member will pick successive column values

member(X, [Head | Tail]) :-
            member(X, Tail).

board([1/C1, 2/C2, 3/C3, 4/C4, 5/C5, 6/C6, 7/C7, 8/C8]). % prototype board

但是,最后一个链接以三种不同的方式解决它,因此您可以与三种已知的解决方案进行比较。

于 2011-02-05T15:58:33.207 回答