root/trunk/examples/searching/breadth_first1.lgt

Revision 4601, 1.5 KB (checked in by pmoura, 7 weeks ago)

Added svn:mime-type property to source files (set to text/x-logtalk).

  • Property svn:mime-type set to text/x-logtalk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2:- object(breadth_first(Bound),
3    instantiates(blind_search(Bound))).
4
5
6    :- info([
7        version is 1.2,
8        author is 'Paulo Moura',
9        date is 2008/6/9,
10        comment is 'Breadth first state space search strategy.',
11        source is 'Example adapted from the book "Prolog Programming for Artificial Intelligence" by Ivan Bratko.',
12        parnames is ['Bound']]).
13
14
15    :- uses(list, [member/2, reverse/2]).
16
17
18    search(Space, State, Bound, Solution) :-
19        breadt(Space, l(State), Bound, Path),
20        reverse(Path, Solution).
21
22
23    breadt(Space, Tree, Bound, Solution) :-
24        expand([], Tree, Tree2, Solved, Solution, Space, Bound),
25        (   Solved ->
26            true
27        ;   breadt(Space, Tree2, Bound, Solution)
28        ).
29
30
31    expand(Path, l(State), _, true, [State| Path], Space, _) :-
32        Space::goal_state(State).
33
34    expand(Path, l(State), t(State, Subs), fail, _, Space, Bound) :-
35        Bound > 0,
36        bagof(l(Next), (Space::next_state(State, Next), \+ Space::member_path(Next, [State| Path])), Subs).
37
38    expand(Path, t(State,Subs), t(State, Subs2), Solved, Solution, Space, Bound) :-
39        expandall([State| Path], Subs, [], Subs2, Solved, Solution, Space, Bound).
40
41
42    expandall(_, [], [Tree| Trees], [Tree| Trees], fail, _, _, _).
43
44    expandall(Path, [Tree| Trees], Trees2, Subs2, Solved, Solution, Space, Bound) :-
45        Bound > 0,
46        Bound2 is Bound -1,
47        expand(Path, Tree, Tree2, Solved2, Solution, Space, Bound2),
48        (   Solved2 ->
49            Solved = true
50        ;   expandall(Path, Trees, [Tree2| Trees2], Subs2, Solved, Solution, Space, Bound)
51        )
52        ;
53        expandall(Path, Trees, Trees2, Subs2, Solved, Solution, Space, Bound).
54
55
56:- end_object.
Note: See TracBrowser for help on using the browser.