0

抱歉,之前在此发布的新手,试图让此列表通过每个操作进行递归,保持其已访问的列表项,然后如果元素不在状态将更改为的列表中,则检查递归。我需要找出为什么这不能按预期工作,它似乎得到了结果 7,0、0,0、0,4 和 7,4,并且检查似乎在该程序的一次运行中显示了相同的 4 个结果,随机吐出其中一个,是动作顺序错误,因为我知道你必须从最困难的条件开始匹配,目标是使用调用solve(state(0,0))填充它。然后你应该在结束调用中得到 5 在水罐中,并打印出显示它使用的路径的列表,然后找到另一个解决方案,因为在 7L 水罐中有 5L 的地方只能有两个。

action(state(L,R), P, state(7,R)) :- 
    L < 4, 
    not(lists:member(state(7,R),P)), 
    print(state(7,R)).
action(state(L,R), P, state(L,4)) :- 
    R < 4, 
    not(lists:member(state(L,4),P)), 
    print(state(L,4)).
action(state(L,R), P, state(0,R)) :- 
    L > 0, 
    not(lists:member(state(0,R),P)), 
    print(state(0,R)).
action(state(L,R), P, state(L,0)) :- 
    R > 0, 
    not(lists:member(state(L,0),P)), 
    print(state(L,0)).
action(state(L,R), P, state(7,C)) :- 
    not(lists:member(state(7,C),P)), 
    C is L + R -7,  
    C > 7,   
    print(state(7,C)).
action(state(L,R), P, state(C,4)) :- 
    not(lists:member(state(C,4),P)), 
    C is L + R -4, 
    C > 4, 
    print(state(C,4)).
action(state(L,R), P, state(0,C)) :- 
    not(lists:member(state(0,C),P)), 
    C is L + R, 
    C @=< 4, 
    print(state(0,C)).
action(state(L,R), P, state(C,0)) :- 
    not(lists:member(state(C,0),P)), 
    C is L + R, 
    C @=< 7, 
    print(state(C,0)).

solve(X) :- 
    reachedgoal(X,[],A).

reachedgoal(state(5,_),L,L).
reachedgoal(State1,P,L) :- 
    action(State1,P,State2),   
    not(lists:member(State2,P)), 
    reachedgoal(State2,[State1|P],L).
4

1 回答 1

1

有一些小的逻辑错误,在计算值之前检查重复操作是没有意义的。

solve(X) :-
  reachedgoal(X,[],_).

reachedgoal(state(5,_),L,L).
reachedgoal(State1,P,L) :-
  action(State1,P,State2),
  \+ member(State2,P),
  reachedgoal(State2,[State1|P],L).

action(state(_L,R),P,state(7,R)) :- % fill left jug (7l)
  \+ member(state(7,R),P),
  print(state(7,R)),nl.
action(state(L,_R),P,state(L,4)) :- % fill right jug (4l)
  \+ member(state(L,4),P),
  print(state(L,4)),nl.

action(state(_L,R),P,state(0,R)) :- % empty left jug
  \+ member(state(0,R),P),
  print(state(0,R)),nl.
action(state(L,_R),P,state(L,0)) :- % empty right jug
  \+ member(state(L,0),P),
  print(state(L,0)),nl.

action(state(L,R),P,state(7,C)) :- % from right jug to left jug until left full
  C is L + R - 7,
  C > 0, C =< 4,
  \+ member(state(7,C),P),
  print(state(7,C)),nl.
action(state(L,R),P,state(C,4)) :- % from left jug to right jug until right full
  C is L + R - 4,
  C > 0, C =< 7,
  \+ member(state(C,4),P),
  print(state(C,4)),nl.

action(state(L,R),P,state(0,C)) :- % from left jug to right jug until left empty
  C is L + R,
  C =< 4,
  \+ member(state(0,C),P),
  print(state(0,C)),nl.
action(state(L,R),P,state(C,0)) :- % from right jug to left jug until right empty
  C is L + R,
  C =< 7,
  \+ member(state(C,0),P),
  print(state(C,0)),nl.

这会产生:

| ?- solve(state(0,0)).
state(7,0)
state(7,0)
state(7,4)
state(7,4)
state(0,4)
state(0,4)
state(4,0)
state(4,4)
state(4,4)
state(7,1)
state(7,1)
state(0,1)
state(0,1)
state(1,0)
state(1,4)
state(1,4)
state(5,0)
yes

重复是因为操作中的条件不足(见下文)。

检查每个操作中的重复状态是浪费的,因为您也在检查它reachedgoal/3,并且在最后打印列表更好更清晰。

solve :-
  solve(state(0,0),RevStates), reverse(RevStates,States),
  write(States), nl.

solve(X,States) :-
  reachedgoal(X,[],States).

reachedgoal(state(5,_),L,L).
reachedgoal(State1,P,L) :-
  action(State1,State2),
  \+ member(State2,P),
  reachedgoal(State2,[State1|P],L).

action(state(L,R),state(7,R)) :- % fill left jug (7l)
  L < 7.
action(state(L,R),state(L,4)) :- % fill right jug (4l)
  R < 4.
action(state(L,R),state(0,R)) :- % empty left jug
  L > 0.
action(state(L,R),state(L,0)) :- % empty right jug
  R > 0.
action(state(L,R),state(7,C)) :- % from right jug to left jug until left full
  R > 0, L < 7,
  C is L + R - 7, C > 0, C =< 4.
action(state(L,R),state(C,4)) :- % from left jug to right jug until right full
  L > 0, R < 4,
  C is L + R - 4, C > 0, C =< 7.
action(state(L,R),state(0,C)) :- % from left jug to right jug until left empty
  L > 0, C is L + R, C =< 4.
action(state(L,R),state(C,0)) :- % from right jug to left jug until right empty
  R > 0, C is L + R, C =< 7.

现在没有重复:

| ?- solve.
[state(0,0),state(7,0),state(7,4),state(0,4),state(4,0),state(4,4),state(7,1),state(0,1),state(1,0),state(1,4)]
yes
于 2017-11-19T12:46:54.410 回答