我有一个家庭作业,我必须写一个谓词seatingChart(X):-
,它有8个席位。规则是:
我以为我编写了下面的代码来创建正确的案例。
person(jim,m). person(tom,m). person(joe,m). person(bob,m). person(fay,f). person(beth,f). person(sue,f). person(cami,f). % Database of hobbies % hobbies(name,hobby). -> People can have multiple hobbies) hobbies(jim, sup). hobbies(jim, fish). hobbies(jim, kayak). hobbies(tom, hike). hobbies(tom, fish). hobbies(tom, ski). hobbies(joe, gamer). hobbies(joe, chess). hobbies(joe, climb). hobbies(bob, paint). hobbies(bob, yoga). hobbies(bob, run). hobbies(fay, sup). hobbies(fay, dance). hobbies(fay, run). hobbies(beth, climb). hobbies(beth, cycle). hobbies(beth, fish). hobbies(sue, yoga). hobbies(sue, skate). hobbies(sue, ski). hobbies(cami, run). hobbies(cami, kayak). hobbies(cami, gamer). %% ANSWER %% % return a pair of opposite gender people gender(PersonX, PersonY):- person(PersonX,GenderX), person(PersonY,GenderY), GenderX \= GenderY. % return the pair of similar interests. similarHobbies(PersonX, PersonY):- hobbies(PersonX, HobbyX), hobbies(PersonY, HobbyY), HobbyX == HobbyY. % Create the rules for our seating chart list seatingRules([P1,P2,P3,P4,P5,P6,P7,P8|_]):- % Have each adjacent person be of the opposite gender gender(P1,P2), gender(P3,P4), gender(P5,P6), gender(P7,P8), gender(P8,P1), % Have each adjacent person have at least one of the same hobby similarHobbies(P1,P2), similarHobbies(P3,P4), similarHobbies(P5,P6), similarHobbies(P7,P8). % Generate a list of all the names from person(...) people(P):- findall(X, person(X,_), P). % Generate a list of permutations of people permPeople([P1,P2,P3,P4,P5,P6,P7,P8]):- permutation([P1,P2,P3,P4,P5,P6,P7,P8], [jim,tom,joe,bob,fay,beth,sue,cami]), \+error([P1,P2,P3,P4,P5,P6,P7,P8]). error([P1,P2,P3,P4,P5,P6,P7,P8]):- \+seatingRules([P1,P2,P3,P4,P5,P6,P7,P8]). seatingChart(X):- permPeople(X).
当我在SWI-Prolog中使用seatingChart(X).
运行时,我首先得到以下答案:
X = [jim, fay, tom, beth, joe, cami, bob, sue] ;
然而,我后来的排列似乎是错误的......在点击;
几次之后,这说明它是一个有效的答案:
X = [jim, beth, sue, tom, joe, cami, bob, fay] .
我做错了什么?或者是什么导致我的排列开始不遵守座位表规则?
答案 0 :(得分:0)
座位规则谓词是否包含所有对?
% Create the rules for our seating chart list
seatingRules([P1,P2,P3,P4,P5,P6,P7,P8|_]):-
% Have each adjacent person be of the opposite gender
gender(P1,P2),
gender(P2,P3),
gender(P3,P4),
gender(P4,P5),
gender(P5,P6),
gender(P6,P7),
gender(P7,P8),
gender(P8,P1),
% Have each adjacent person have at least one of the same hobby
similarHobbies(P1,P2),
similarHobbies(P2,P3),
similarHobbies(P3,P4),
similarHobbies(P4,P5),
similarHobbies(P5,P6),
similarHobbies(P6,P7),
similarHobbies(P7,P8),
similarHobbies(P8,P1).