简体   繁体   English

Prolog Logic拼图不工作?

[英]Prolog Logic Puzzle not working?

I have been working on this Prolog logic problem for awhile. 我一直在研究这个Prolog逻辑问题。 It is getting incorrect results and it is taking awhile to run the program itself which is what concerns me more. 它得到了不正确的结果,并且需要一段时间来运行程序本身,这是我更关心的问题。 The logic problem goes as follows: 逻辑问题如下:

Alex, Bret, Chris, Derek, Eddie, Fred, Greg, Harold, and John are nine students who live in a three story building, with three rooms on each floor. 亚历克斯,布雷特,克里斯,德里克,埃迪,弗雷德,格雷格,哈罗德和约翰是九个学生,住在一栋三层楼的建筑里,每层有三个房间。 A room in the West wing, one in the centre, and one in the East wing. 西翼的一个房间,一个在中心,一个在东翼。 If you look directly at the building, the left side is West and the right side is East. 如果你直视建筑物,左边是西边,右边是东边。 Each student is assigned exactly one room. 每个学生只分配一个房间。 Can you find where each of their rooms is: 你能找到他们每个房间的位置:

  1. Harold does not live on the bottom floor. 哈罗德不住在底层。
  2. Fred lives directly above John and directly next to Bret (who lives in the West wing). 弗雷德直接住在约翰的上方,紧邻布雷特(住在西翼)。
  3. Eddie lives in the East wing and one floor higher than Fred. 埃迪住在东翼,比弗雷德高一层。
  4. Derek lives directly above Fred. 德里克住在弗雷德身上。
  5. Greg lives directly above Chris. 格雷格直接生活在克里斯身上。

Here is the code that I have written, that takes awhile to complete and gives incorrect results: 这是我编写的代码,需要一段时间才能完成并给出不正确的结果:

rooms([west,center,east]).
floors([bottom,middle,top]). 

students([alex,bret,chris,derek,eddie,fred,greg,harold,john]).

student(S) :-
        students(L), member(S,L).

empty_building(building(floor(_,_,_),floor(_,_,_),floor(_,_,_))).

location(P,1,1,building(floor(P,_,_),_,_)).
location(P,1,2,building(floor(_,P,_),_,_)).
location(P,1,3,building(floor(_,_,P),_,_)).
location(P,2,1,building(_,floor(P,_,_),_)).
location(P,2,2,building(_,floor(_,P,_),_)).
location(P,2,3,building(_,floor(_,_,P),_)).
location(P,3,1,building(_,_,floor(P,_,_))).
location(P,3,2,building(_,_,floor(_,P,_))).
location(P,3,3,building(_,_,floor(_,_,P))).

puzzle_soln(BLDG) :-
    empty_building(BLDG), 
    location(harold,HFN,_,BLDG), 
    location(fred,FFN,FRN,BLDG), 
    location(john,JFN,JRN,BLDG),
    location(bret,BFN,BRN,BLDG),
    location(eddie,EFN,ERN,BLDG),
    location(derek,DFN,DRN,BLDG),
    location(greg,GFN,GRN,BLDG),
    location(chris,CFN,CRN,BLDG),
    location(alex,_,_,BLDG),        

    HFN \= 1,                           
    FFN is JFN + 1,                     
    FRN = JRN,
    1 =:= abs(FRN - BRN),               
    FFN = BFN,
    BRN is 1,                           
    ERN is 3,                           
    EFN is FFN + 1,                     
    DFN is FFN + 1,                      
    DRN = FRN,
    GFN is CFN + 1,                     
    GRN = CRN. 

Here is a link to a site that has the problem and the solution: https://www.brainbashers.com/showpuzzles.asp?puzzle=ZQJZ 以下是指向存在问题和解决方案的网站的链接: https//www.brainbashers.com/showpuzzles.asp?puzzle = ZQJZ

Any help would be appreciated. 任何帮助,将不胜感激。

CLP(FD) is usefull for this puzzle : CLP(FD)对于这个难题很有用:

:- use_module(library(clpfd)).
%
%   7 8 9
%   4 5 6
%   1 2 3
solve(L) :-
    L = [A, B, C, D, E, F ,G, H, J],
    L ins 1..9,
    all_distinct(L),
    % 1. Harold does not live on the bottom floor.
    H #> 3,
    % 2. Fred lives directly above John and directly next to Bret
    % (who lives in the West wing).
    F #= J + 3,
    B #= F - 1,
    B in 1 \/ 4 \/ 7,
    % 3. Eddie lives in the East wing and one floor higher than Fred.
    E #> F,
    E in 3 \/ 6 \/ 9,
    (   (   F in 1..3 #<==> E in 4..6) #\/ (  F in 4..6 #<==> E in 7..9)),
    % answer edited after Mat's remark
    % (   (   F in 1..3 #<==> E in 4..6) ; (  F in 4..6 #<==> E in 7..9)),
    % 4. Derek lives directly above Fred.
    D #= F + 3,
    % 5. Greg lives directly above Chris.
    G #= C + 3,
    label(L).

Which gives the solution(unfortunately 2 times !) 这给出了解决方案(不幸的是2次!)

EDIT now, only one solution ! 编辑现在,只有一个解决方案!

?- solve( [A, B, C, D, E, F ,G, H, J]).
A = 1,
B = 4,
C = 3,
D = 8,
E = 9,
F = 5,
G = 6,
H = 7,
J = 2.

You are getting the correct answer. 你得到了正确的答案。

?- puzzle_soln(X).
X = building(floor(alex, john, chris), floor(bret, fred, greg), floor(harold, derek, eddie))

same as this (counting floors from the bottom) 与此相同(从底部计算楼层)

West    Centre  East
====    ======  ====
Harold  Derek   Eddie
Bret    Fred    Greg
Alex    John    Chris

If you want to speed it up you can interleave generation and testing: 如果你想加快速度,可以交错生成和测试:

puzzle_soln(BLDG) :-
  empty_building(BLDG),
  BRN is 1,
  ERN is 3,
  location(harold,HFN,_,BLDG),
  HFN \= 1,
  location(fred,FFN,FRN,BLDG),
  location(john,JFN,JRN,BLDG),
  FFN is JFN + 1,
  EFN is FFN + 1,
  DFN is FFN + 1,
  FRN = JRN,
  location(bret,BFN,BRN,BLDG),
  1 =:= abs(FRN - BRN),
  FFN = BFN,
  location(eddie,EFN,ERN,BLDG),
  location(derek,DFN,DRN,BLDG),
  DRN = FRN,
  location(greg,GFN,GRN,BLDG),
  location(chris,CFN,CRN,BLDG),
  GFN is CFN + 1,
  GRN = CRN,
  location(alex,_,_,BLDG).

Old version: 869,727 inferences, 0.533 CPU in 0.533 seconds 旧版本:869,727次推断,0.533秒内0.533 CPU

New version: 310 inferences, 0.000 CPU in 0.000 seconds 新版本:310推断,0.000 CPU,0.000秒

CLP(FD) solution: 61,356 inferences, 0.011 CPU in 0.011 seconds CLP(FD)解决方案:61,356次推断,0.011秒内0.011 CPU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM