1

嘿伙计们,我有一个关于 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). 时它只是说全局堆栈之外。有人可以帮我解决这个问题

4

1 回答 1

3

你只需要看看这个:

计划(状态,目标,[],状态):- false满意(状态,目标)。
计划(状态,目标,计划,最终状态):-
    append(PrePlan,[Action|PostPlan],Plan), false ,
     select(State,Goals,Goal) ,
    实现(Action,Goal) ,
     can(Action,Condition) ,
     plan(State,Condition,PrePlan,MidState1) ,
    应用(MidState1,Action,MidState2) ,
    计划(MidState2,Goals,PostPlan,FinalState)。

?- 计划(state1,on(b1,b2),Plan,FinalState)。

由于该程序已经循环,因此相同的程序将在false删除附加目标的情况下循环。你需要先解决这个问题。有关更多信息,请参见

于 2015-02-10T14:50:38.087 回答