answer-set-programmingclingogringo

Answer Set Programming: how to assign students to a group such that no two students who dislike each other are in a same group


I am a beginner to Answer Set Programming. I want to group all students in a different groups such that: 1. Each group has 3 to 4 students 2. No two students who dislike each other are in the same group. 3. And we can not assign same student to different groups.

I have written like:

%suppose there are total 6 students 
student(1..6). 

%suppose there are 2 groups
group(1..2). 

%1 and 4 like each other, 4 and 5 dislike each other
dislike(1,4). dislike(5,4). 

% each group has 3 to 4 students 
:- group(G), #count {S : in(S,G)} < 3. 
:- group(G), #count {S : in(S,G)} > 4.

I have added the constraint for how many students each group can contain but have got no clue about how satisfy other two conditions.

Your help will be greatly appreciated. Thanks.


Solution

  • Try this:

    student(1..6). 
    group(1..2). 
    
    dislike(1,4). dislike(5,4). 
    
    
    % each group has 3 to 4 students 
    :- group(G), #count {S : in(S,G)} < 3. 
    :- group(G), #count {S : in(S,G)} > 4.
    
    %no two students who dislike each other are in the same group
    :-in(X, G1), in(Y,G2), dislike(X,Y), group(G1), group(G2), G1==G2.
    
    %each student should be assigned to only one group
    1{in(S,G): group(G)}1 :- student(S).
    
    #show in/2.