序言中的广度优先搜索给我一个错误
Breadth first search in prolog gives me an error
这是我在 prolog 中的广度优先搜索策略代码:
s(a, b).
s(a, c).
s(b, g).
s(b, f).
s(c, r).
s(c, e).
goal(g).
solve( Start, Solution) :-
breadthlirst( [ [Start] ], Solution).
breadthfirst( [ [Node | Path] |_], [Node | Path] ) :-
goal( Node).
breadthfirst( [ [N | Path] | Paths], Solution) :-
bagof([M,N|Path],
( s( N, M), \+ member( M, [N | Path] ) ), NewPaths),
conc( Paths, NewPaths, Pathsl), !,
breadthfirs( Pathsl, Solution);
breadthfirst( Paths, Solution).
但是当我 运行 这段代码时,它会发出异常:
?- solve(a, S).
uncaught exception: error(existence_error(procedure,breadthlirst/2),solve/2)
这是怎么回事?另外,有没有比这个更简单的广度优先搜索版本?
抱歉,没有街头信誉,所以不对 MDG 发表评论:-(
如果你想要广度优先也许最好使用队列??我不知道。我可以用队列显示广度优先的代码,但这不是你的问题,有这么多这样的问题只是搜索没问题。
对于这个解决方案,我使用了 SWI-Prolog
What's going on here?
uncaught exception: error(existence_error(procedure,breadthlirst/2),solve/2)
compiler/interpreter 向您表明,在尝试解决您的查询时,它从谓词 solve/2
开始,然后试图找到它找不到的 breadthlirst/2
。
修正拼写错误并将 conc/3
更改为 append/3
结果
s(a, b).
s(a, c).
s(b, g).
s(b, f).
s(c, r).
s(c, e).
goal(g).
solve( Start, Solution) :-
breadthfirst( [ [Start] ], Solution).
breadthfirst( [ [Node | Path] |_], [Node | Path] ) :-
goal( Node).
breadthfirst( [ [N | Path] | Paths], Solution) :-
bagof([M,N|Path],
( s( N, M), \+ member( M, [N | Path] ) ), NewPaths),
%conc( Paths, NewPaths, Pathsl), !,
append(Paths, NewPaths, Pathsl), !,
breadthfirst( Pathsl, Solution);
breadthfirst( Paths, Solution).
正在执行查询
?- solve(a,S).
S = [g, b, a] ;
通常我会期望目标,在这种情况下 g
是解决的参数,而不是硬编码为事实 goal(g).
Is there any easier version of breadth first search than this one ?
在 Prolog 中使用呼吸优先搜索时,我更喜欢使用 meta-interpreters。
是 Prolog 中 BFS 的另一个版本,如果你有任何关于 Prolog 的后裔书籍,它应该涵盖 BFS。
The Power of Prolog 将帮助您更好地理解 Prolog。请注意,这是更高级的东西,在您了解基础知识之前不要开始玩。
这是我在 prolog 中的广度优先搜索策略代码:
s(a, b).
s(a, c).
s(b, g).
s(b, f).
s(c, r).
s(c, e).
goal(g).
solve( Start, Solution) :-
breadthlirst( [ [Start] ], Solution).
breadthfirst( [ [Node | Path] |_], [Node | Path] ) :-
goal( Node).
breadthfirst( [ [N | Path] | Paths], Solution) :-
bagof([M,N|Path],
( s( N, M), \+ member( M, [N | Path] ) ), NewPaths),
conc( Paths, NewPaths, Pathsl), !,
breadthfirs( Pathsl, Solution);
breadthfirst( Paths, Solution).
但是当我 运行 这段代码时,它会发出异常:
?- solve(a, S).
uncaught exception: error(existence_error(procedure,breadthlirst/2),solve/2)
这是怎么回事?另外,有没有比这个更简单的广度优先搜索版本?
抱歉,没有街头信誉,所以不对 MDG 发表评论:-(
如果你想要广度优先也许最好使用队列??我不知道。我可以用队列显示广度优先的代码,但这不是你的问题,有这么多这样的问题只是搜索没问题。
对于这个解决方案,我使用了 SWI-Prolog
What's going on here?
uncaught exception: error(existence_error(procedure,breadthlirst/2),solve/2)
compiler/interpreter 向您表明,在尝试解决您的查询时,它从谓词 solve/2
开始,然后试图找到它找不到的 breadthlirst/2
。
修正拼写错误并将 conc/3
更改为 append/3
结果
s(a, b).
s(a, c).
s(b, g).
s(b, f).
s(c, r).
s(c, e).
goal(g).
solve( Start, Solution) :-
breadthfirst( [ [Start] ], Solution).
breadthfirst( [ [Node | Path] |_], [Node | Path] ) :-
goal( Node).
breadthfirst( [ [N | Path] | Paths], Solution) :-
bagof([M,N|Path],
( s( N, M), \+ member( M, [N | Path] ) ), NewPaths),
%conc( Paths, NewPaths, Pathsl), !,
append(Paths, NewPaths, Pathsl), !,
breadthfirst( Pathsl, Solution);
breadthfirst( Paths, Solution).
正在执行查询
?- solve(a,S).
S = [g, b, a] ;
通常我会期望目标,在这种情况下 g
是解决的参数,而不是硬编码为事实 goal(g).
Is there any easier version of breadth first search than this one ?
在 Prolog 中使用呼吸优先搜索时,我更喜欢使用 meta-interpreters。
The Power of Prolog 将帮助您更好地理解 Prolog。请注意,这是更高级的东西,在您了解基础知识之前不要开始玩。