在Prolog中使用BFS制作STRIPS计划程序

时间:2015-02-27 10:14:36

标签: prolog breadth-first-search

我有Prolog的以下通用广度优先搜索代码,我想采用简单节点表示s(a,b,go(a,b))并将其更改为谓词,以便go(a,b)代表STRIPS运算符说{ {1}}所以我可能有两个谓词:stack(A,B)s(S0,S,stack(A,B))(经典块世界问题),可以在下面的广度优先搜索中使用。我不确定这是否可能或我将如何做到这一点。我的第一个想法是有一个谓词如下:

s(S0,S,unstack(B,A))

广度优先搜索如下。

% S0 is the given state and S is the successor for the 'stack(A,B)' predicate if S0 
% A and B are not the same blocks, and we want the next state S to contain the 
% same state/preconditions information except we want to add 'on(A,B)'
% to S and we want to remove 'clear(B)' from S0

s(S0,S,stack(A,B)) :- 
 A \== B,
 % other predicates etc

1 个答案:

答案 0 :(得分:0)

bfs(S0,Goal,Plan) :-
    bfs_a(Goal1,[n(S0,[])],R),
    subset(Goal,Goal1),
    reverse(R,Plan).

bfs_a(Y,[n(Y,P)|_],P).
bfs_a(Y,[n(S,P1)|Ns],P) :-
    findall(n(S1,[A|P1]), s(S,S1,A), Es),
    append(Ns,Es,O),
    bfs_a(Y,O,P).

s(State,NextState,Operation) :-
    opn(Operation, PreList), subset(PreList, State),
    deletes(Operation, DelList), subtract(State, DelList, TmpState),               
    adds(Operation, AddList), union(AddList, TmpState, NextState).

subset([ ],_).
subset([H|T],List) :-
        member(H,List),
        subset(T,List).

opn(move(Block,X1,X2),[clear(Block),clear(X2),on(Block,X1)]) :-
    block(Block),object(X1),object(X2),
    Block \== X1, X2 \== Block, X1 \== X2.

adds(move(Block,X1,X2),[on(Block,X2),clear(X1)]).

deletes(move(Block,X1,X2),[on(Block,X1),clear(X2)]).

object(X) :- place(X) ; block(X).

block(a).
block(b).
block(c).
block(d).
place(x1).
place(x2).
place(x3).