拥有一个"全球堆栈"在prolog

时间:2015-02-10 14:03:44

标签: prolog planning failure-slice

嘿伙计们,我对Prolog有一个相当简单的问题。

%on(Block,Object).
% clear(Object).
block(b1).
block(b2).
block(b3).

place(p1).
place(p2).
place(p3).
place(p4).
state1([clear(p2),clear(p4),clear(b2),clear(b3),on(b1,p1),on(b2,p3),on(b3,b1)]).
% visual state1
% b3
% b1    b2
% =  =  =  =
% 1  2  3  4     <----Positions
% can(Action,Condition).
% adds(Action,AddRelationship).
% deletes(Action,DeleteRelationship).
% move(Block,From,To).
can( move( Block, From, To), [ clear( Block), clear( To), on( Block, From)]) :-
  block( Block),      % Block to be moved
  object( To),           % "To" is a block or a place
  To \== Block,          % Block cannot bå moved to itself
  object( From),         % "From" is a block or a place
  From \== To,           % Move to new position
  Block \== From.        % Block not moved from itself
adds(move(X,From,To),[on(X,To),clear(From)]).
deletes(move(X,From,To),[on(X,From),clear(To)]).
object(X):-
      place(X)
      ;   
      block(X).
% plan(State,Goals,Plan,FinalState).
plan(State,Goals,[],State):-
    satisfied(State,Goals).
plan(State,Goals,Plan,FinalState) :-
    append(PrePlan,[Action|PostPlan],Plan),
    select(State,Goals,Goal),
    achieves(Action,Goal),
    can(Action,Condition),
    plan(State,Condition,PrePlan,MidState1),
    apply(MidState1,Action,MidState2),
    plan(MidState2,Goals,PostPlan,FinalState).
% satisfied(State,[]).
satisfied(State,[Goal|Goals]):-
    member(Goal,State),
    satisfied(State,Goals).
select(State,Goals,Goal):-
    member(Goal,Goals),
    not(member(Goal,State)).
achieves(Action,Goal):-
    adds(Action,Goals),
    member(Goal,Goals).
apply(State,Action,NewState):-
    deletes(Action,DelList),
    delete_all(State,DelList,State1),!,
    adds(action,AddList),
    append(AddList,State1,NewState).
delete_all([],_,[]).
delete_all([X|L1],L2,Diff):-
    member(X,L2),!,
    delete_all(L1,L2,Diff).
delete_all([X|L1],L2,[X|Diff]):-
    delete_all(L1,L2,Diff).

在编译器中运行后,它说它没有问题但是当我尝试执行命令时 plan(state1,on(b1,b2),Plan,FinalState). 它只是说出了全局堆栈。有人可以帮我修复这个

1 个答案:

答案 0 :(得分:3)

你只需要看一下:

plan(State,Goals,[],State):- false,
    satisfied(State,Goals).
plan(State,Goals,Plan,FinalState) :-
    append(PrePlan,[Action|PostPlan],Plan), false,
    select(State,Goals,Goal),
    achieves(Action,Goal),
    can(Action,Condition),
    plan(State,Condition,PrePlan,MidState1),
    apply(MidState1,Action,MidState2),
    plan(MidState2,Goals,PostPlan,FinalState).

?- plan(state1,on(b1,b2),Plan,FinalState).

由于此程序已经循环,因此相同的程序将循环删除其他 false 目标。您需要先解决此问题。有关详情,请参阅