Search code examples
clingo

clingo: placing element according to the rules


Hello i'm trying to learn more about clingo, have this terms: v(1,2).v(2,1).v(3,4).v(4,3), means that first element cannot be in the same row of the second element, the second cannot be in the same of the first, ecc.. would like to write some rules to find a matrix 2x2 in which element (I,J,N) are placed according to that limits. thanks in advance

v(1,2).v(2,1).v(3,4).v(4,3)
rows(1..2).
col(1..2).
1{m(I,J,N) : v(N)}1 :- rows(I), col(J).  
1{m(I,J,N) : rows(I), col(J)}1 :- v(N).
...code...
output 
[1,1,1][1,2,4][2,1,2][2,2,3]
[1,1,4][1,2,1][2,1,2][2,2,3]
[1,1,1][1,2,4][2,1,3][2,2,2]
[1,1,4][1,2,1][2,1,3][2,2,2]

Solution

  • The first rule

    1{m(I,J,N) : v(N)}1 :- rows(I), col(J).

    puts into each location of the matrix one v(N) but you do not have v(N) defined, you have v(N,M) defined.

    The second rule

    1{m(I,J,N) : rows(I), col(J)}1 :- v(N).
    

    Puts each v(N) in exactly one row and column.

    I suggest you replace v(X) by w(X) and define

    w(N) :- v(N,_).
    w(N) :- v(_,N).
    

    That means you get all possible values form the v(X,Y) into w.