Prolog办公室难题



我正试图为学校做一项Prolog作业,基本上是想知道谁有什么办公室。问题是:

Hunter、Laura、Jim、Sally和Jack在同一栋楼里工作,有五间相邻的办公室。亨特不在第五办公室工作,劳拉也不在第一办公室工作。吉姆不在第一个或最后一个办公室工作,他也不在杰克或劳拉附近的办公室。莎莉在比劳拉高的某间办公室工作。谁在什么办公室工作

所以我有下面的代码,但我收到了:

uncaught exception: error(type_error(fd_evaluable,abs/1),(#=)/2)

这是代码。

puzzle(P) :-
findWhosOffice(P,Z),
labeling([],Z).
findWhosOffice(P, Z) :-
P = [hunter-Hunter, jack-Jack, jim-Jim, laura,Laura,sally,Sally],
Z = [Hunter, Laura, Jim, Sally, Jack],
Hunter #= 5,
Laura #= 1,
Jim #= 1,
Jim #= 5,
abs(Jim-Jack) #=1,
abs(Jim-Laura) #=1,
Sally #> Laura.

问题是您没有为Z中的变量定义域。为了使其成为一个有效且唯一的解决方案,还缺少一个约束。

这是我的解决方案,我发现您没有使用all_difference((:

:-use_module(library(clpfd)).
puzzle(Employee,Employee_Office_Room):-
Employee=[Hunter, Laura, Jim, Sally, Jack], Employee ins 1..5,
Employee_Office_Room=[First,_Second,_Third,_Fourth,Fifth], Employee_Office_Room ins 1..5,
all_different(Employee),
all_different(Employee_Office_Room),

Hunter#=Fifth,
Laura#=First,
Jim#=First,
Jim#=Fifth,
not_nextto(Jim,Laura),
not_nextto(Jim,Jack),
Sally#=Laura-1,
labeling([],Employee),labeling([],Employee_Office_Room).

not_nextto(A,B):-abs(A-B)#=1.
?-puzzle(Employee,Employee_Office_Room).
Employee = [1, 4, 2, 3, 5],
Employee_Office_Room = [1, 2, 3, 4, 5]
Employee = [1, 4, 2, 3, 5],
Employee_Office_Room = [1, 2, 3, 5, 4]
Employee = [1, 4, 2, 3, 5],
Employee_Office_Room = [1, 2, 4, 3, 5]
Employee = [1, 4, 2, 3, 5],
Employee_Office_Room = [1, 2, 4, 5, 3]
Employee = [1, 4, 2, 3, 5],
Employee_Office_Room = [1, 2, 5, 3, 4]....

相关内容

  • 没有找到相关文章

最新更新