Search code examples
answer-set-programmingclingo

create subset and use each atom only once


I am totally new in asp. I need to create a group of teams. Each group must consist of 3 randomly chosen teams. A team can be in only one group.

Thanks in advance. Here is my code

team(fener;galata;besik;van;adana;mardin).

neq(X,Y) :- X!=Y,team(X),team(Y).

count(C) :- C = #count{ T : team(T)}.

C/3 {group(X,Y,Z):team(X),team(Y),team(Z), neq(X,Y),neq(X,Z),neq(Z,Y) } C/3 :- count(C).

#show group/3.

a possible output could be

group(fener;besik;van) group(galata;mardin;adana)

Solution

  • I think, have found the solution.

    team(fener;galata;besik;van;adana;mardin).
    
    neq(X,Y) :- X!=Y,team(X),team(Y).
    
    count(C) :- C = #count{ T : team(T)}.
    
    C/3 {group(X,Y,Z):team(X),team(Y),team(Z), neq(X,Y),neq(X,Z),neq(Z,Y) } C/3 :- count(C).
    
    exist_in_group(T) :- group(T,_,_).
    exist_in_group(T) :- group(_,T,_).
    exist_in_group(T) :- group(_,_,T).
    
    :- team(T), not exist_in_group(T).
    
    #show group/3.
    

    The output:

    clingo version 5.0.0
    Solving...
    Answer: 1
    group(besik,fener,adana) group(galata,mardin,van)
    SATISFIABLE
    
    Models       : 1+
    Calls        : 1
    Time         : 0.011s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
    CPU Time     : 0.000s