我正在尝试实现一种规划器,对于给定的整数N,使用N个动作生成X个可能的计划。动作具有必须满足的条件和限制,以及将应用于当前状态的效果列表。我实现了检查限制和条件的谓词以及应用效果的谓词。我创建的这个方法已经生成了一个包含N个动作的计划,但是当我按下&#34 ;;"在swi-prolog中看到其他结果,我得到以下错误:
ERROR: Out of local stack
这是我的代码:
makePlan(0,_,List):- List = [].
makePlan(N,I,R):- makeSinglePlan(N,I,R).
makeSinglePlan(0, _ ,_).
makeSinglePlan(N,I,[X|LIST]):-
accao(nome : X, condicoes : Y, efeitos : Z, restricoes : W),
checkAllConditions(Y, I),
checkRestrictions(W),
applyEffects(I, Z, Current),
decrement(N, B),
list_to_set(Current, NC),
makeSinglePlan(B,NC,LIST).
decrement(N,B):- B is N-1.
这是我从控制台调用谓词的方式,第一个参数是整数N,表示计划应该具有的操作数,第二个是初始状态,第三个是返回值:
makePlan(2, [clear(b),on(b,a),on(a,mesa),clear(d),on(d,c),on(c,mesa)], R).´
行动示例:
accao(nome : putOn(X,Y), %name
condicoes : [on(X,Z),clear(X),clear(Y)], %conditions
efeitos : [clear(Z),on(X,Y),-on(X,Z),clear(b)], %effects
restricoes : [(Y\==mesa),(X\==Y),(X\==Z),(Y\==Z)]) %restrictions
Auxiliar Predicates:
% 1 - conditions to be checked 2 - current state
checkAllConditions([],_).
checkAllConditions([X|T],L):- checkCond(X,L) , checkAllConditions(T,L) .
checkCond(X,[X|_]).
checkCond(X,[_|T]):-checkCond(X,T).
% 1 - restrictions to be checked
checkRestrictions([]).
checkRestrictions([X|T]):- X, checkRestrictions(T).
% 1 -current state 2 - effects to be applied 3 - result
applyEffects(L,[],L).
applyEffects(L, [-X|YTail], A):- ! ,remove(X, L, B), applyEffects(B,YTail, A).
applyEffects(L, [Y|YTail], A):- insert(Y, L, B), applyEffects(B,YTail, A).
insert(E, L1, [E|L1] ).
remove(_,[],[]).
remove(X, [X|L1], A):- !, remove(X,L1,A).
remove(X, [Y|L1], [Y|A]):- remove(X,L1,A).
答案 0 :(得分:3)
必须进行两项更改:
makeSinglePlan(0, _ ,[]).
makeSinglePlan(N,I,[X|LIST]):-
N > 0,
....
操作列表应以[]
结尾,该规则仅适用于N > 0
。
?- makePlan(2, [clear(b),on(b,a),on(a,mesa),clear(d),on(d,c),on(c,mesa)], R).
R = [putOn(b, d), putOn(b, a)] ;
R = [putOn(b, d), putOn(a, b)] ;
R = [putOn(b, d), putOn(a, d)] ;
R = [putOn(b, d), putOn(d, b)] ;
R = [putOn(b, d), putOn(d, a)] ;
R = [putOn(d, b), putOn(d, c)] ;
R = [putOn(d, b), putOn(b, c)] ;
R = [putOn(d, b), putOn(b, d)] ;
R = [putOn(d, b), putOn(c, b)] ;
R = [putOn(d, b), putOn(c, d)] ;
false.