我是一个初学者,回答集编程。我想把所有的学生分成一个不同的组: 1.每组有3到4名学生,2.没有两名不喜欢对方的学生在同一组。3.我们不能把同一个学生分配给不同的群体。
我写过这样的话:
%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.我增加了每一组学生可以包含多少学生的限制,但不知道如何满足其他两个条件。
你的帮助将不胜感激。谢谢。
发布于 2015-12-08 06:52:19
试试这个:
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.https://stackoverflow.com/questions/34149410
复制相似问题