Search code examples
prologswi-prologknights-tour

Knight's tour in Prolog: Stack limit exceeded


After I tried to optimize the program using Warnsdorff's rule, the compiler started issuing Stack limit exceeded. All parts separately seem to work, but I have no idea how this could be optimized. I am writing a program on an old laptop with 32-bit windows, so I can’t increase the size of the stack manually, as it is written on the official website https://www.swi-prolog.org/FAQ/StackSizes.html.

knightpath(Board, [1 / 1 | Path]) :-
    Jumps is Board * Board,
    the_way(Jumps, [1 / 1 | Path]).
the_way(1, [X / Y]) : -
    between(1, 5, X),
    between(1, 5, Y).
the_way(Jumps, [X1 / Y1, X2 / Y2 | Path]) : -
    Jumps1 is Jumps - 1,
    the_way(Jumps1, [X2 / Y2 | Path]),
    warnsdorff(X2 / Y2, Path, X1 / Y1).
jump(X1 / Y1, X2 / Y2) : -
    ((X1 is X2 + 2;
      X1 is X2 - 2),
     (Y1 is Y2 + 1;
      Y1 is Y2 - 1);
     (X1 is X2 + 1;
      X1 is X2 - 1),
     (Y1 is Y2 + 2;
      Y1 is Y2 - 2)),
    between(1, 5, X1),
    between(1, 5, Y1).
warnsdorff(X1 / Y1, Path, X2 / Y2) :-
    find_posible(X1 / Y1, Path, Pos),
    find_best(_, [X1 / Y1 | Path], Pos, X2 / Y2).
find_best(N, Path, [X / Y], X / Y) : -
    find_posible(X / Y, Path, Pos),
    length(Pos, N).
find_best(N1, Path, [X / Y | List], X / Y) : -
    find_best(N2, Path, List, _),
    find_posible(X / Y, Path, Pos),
    length(Pos, N1),
    N1 < N2.
find_best(N2, Path, [X1 / Y1 | List], X2 / Y2) : -
    find_best(N2, Path, List, X2 / Y2),
    find_posible(X1 / Y1, Path, Pos),
    length(Pos, N1),
    N1 >= N2.
find_posible(X1 / Y1, Path, Pos) : -
    findall(X2 / Y2, jump(X2 / Y2, X1 / Y1), All_tog),
    filter_path(All_tog, Path, Pos).
filter_path([], _, []).
filter_path([X / Y | All_tog], Path, [X / Y | Pos]) : -
    not(member(X / Y, Path)),
    filter_path(All_tog, Path, Pos).
filter_path([X / Y | All_tog], Path, Pos) : -
    member(X / Y, Path),
    filter_path(All_tog, Path, Pos).

This is what the compiler produces

ERROR: Stack limit (0.5Gb) exceeded
ERROR:   Stack sizes: local: 0.1Gb, global: 42.7Mb, trail: 0Kb
ERROR:   Stack depth: 1,863,822, last-call: 0%, Choice points: 3
ERROR:   In:
ERROR:     [1,863,822] user:the_way(-1863788, [length:1|_22365890])
ERROR:     [1,863,821] user:the_way(-1863787, '<garbage_collected>')
ERROR:     [1,863,820] user:the_way(-1863786, '<garbage_collected>')
ERROR:     [1,863,819] user:the_way(-1863785, '<garbage_collected>')
ERROR:     [1,863,818] user:the_way(-1863784, '<garbage_collected>')

Solution

  • The problem was that Warnsdorff's rule does not provide a solution for the case where the first (last in terms of the stack) square is 1/1. I have to write

    knightpath(Answ) :-
        Jumps is Board * Board,
        the_way(Jumps, Path),
        reverse(Path, Answ).
    ...
    
    the_way(1, [1/1]).
    

    By the way, if I write, as advised by Willem Van Onsem https://stackoverflow.com/a/57348007/11779964, then error can be avoided, and the compiler will simply output 'false'.