سؤال

I am trying to write a program in Prolog to find a Latin Square of size N.

I have this right now:

delete(X, [X|T], T).
delete(X, [H|T], [H|S]) :-
   delete(X, T, S).

permutation([], []).
permutation([H|T], R) :-
   permutation(T, X),
   delete(H, R, X).

latinSqaure([_]).
latinSquare([A,B|T], N) :-
   permutation(A,B),
   isSafe(A,B),
   latinSquare([B|T]).

isSafe([], []).
isSafe([H1|T1], [H2|T2]) :- 
   H1 =\= H2, 
   isSafe(T1, T2).
هل كانت مفيدة؟

المحلول

using SWI-Prolog library:

:- module(latin_square, [latin_square/2]).
:- use_module(library(clpfd), [transpose/2]).

latin_square(N, S) :-
    numlist(1, N, Row),
    length(Rows, N),
    maplist(copy_term(Row), Rows),
    maplist(permutation, Rows, S),
    transpose(S, T),
    maplist(valid, T).

valid([X|T]) :-
    memberchk(X, T), !, fail.
valid([_|T]) :- valid(T).
valid([_]).

test:

?- aggregate(count,S^latin_square(4,S),C).
C = 576.

edit your code, once corrected removing typos, it's a verifier, not a generator, but (as noted by ssBarBee in a deleted comment), it's flawed by missing test on not adjacent rows. Here the corrected code

delete(X, [X|T], T).
delete(X, [H|T], [H|S]) :-
    delete(X, T, S).

permutation([], []).
permutation([H|T], R):-
    permutation(T, X),
    delete(H, R, X).

latinSquare([_]).
latinSquare([A,B|T]) :-
    permutation(A,B),
    isSafe(A,B),
    latinSquare([B|T]).

isSafe([], []).
isSafe([H1|T1], [H2|T2]) :-
    H1 =\= H2,
    isSafe(T1, T2).

and some test

?- latinSquare([[1,2,3],[2,3,1],[3,2,1]]).
false.

?- latinSquare([[1,2,3],[2,3,1],[3,1,2]]).
true .

?- latinSquare([[1,2,3],[2,3,1],[1,2,3]]).
true .

note the last test it's wrong, should give false instead.

نصائح أخرى

Like @CapelliC, I recommend using CLP(FD) constraints for this, which are available in all serious Prolog systems.

In fact, consider using constraints more pervasively, to benefit from constraint propagation.

For example:

:- use_module(library(clpfd)).

latin_square(N, Rows, Vs) :-
        length(Rows, N),
        maplist(same_length(Rows), Rows),
        maplist(all_distinct, Rows),
        transpose(Rows, Cols),
        maplist(all_distinct, Cols),
        append(Rows, Vs),
        Vs ins 1..N.

Example, counting all solutions for N = 4:

?- findall(., (latin_square(4,_,Vs),labeling([ff],Vs)), Ls), length(Ls, L).
L = 576,
Ls = [...].

The CLP(FD) version is much faster than the other version.

Notice that it is good practice to separate the core relation from the actual search with labeling/2. This lets you quickly see that the core relation terminates also for larger N:

?- latin_square(20, _, _), false.
false.

Thus, we directly see that this terminates, hence this plus any subsequent search with labeling/2 is guaranteed to find all solutions.

I have better solution, @CapelliC code takes very long time for squares with N length higher than 5.

:- use_module(library(clpfd)).

make_square(0,_,[]) :- !.
make_square(I,N,[Row|Rest]) :-
   length(Row,N),
   I1 is I - 1,
   make_square(I1,N,Rest).

 all_different_in_row([]) :- !.
 all_different_in_row([Row|Rest]) :-
    all_different(Row),
    all_different_in_row(Rest).


all_different_in_column(Square) :-
   transpose(Square,TSquare),
   all_different_in_row(TSquare).   

all_different_in_column1([[]|_]) :- !.
all_different_in_column1(Square) :-
   maplist(column,Square,Column,Rest),
   all_different(Column),
   all_different_in_column1(Rest).


   latin_square(N,Square) :-
   make_square(N,N,Square),
   append(Square,AllVars),
   AllVars ins 1..N,
   all_different_in_row(Square),
   all_different_in_column(Square),
   labeling([ff],AllVars).
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top