| 1 | |
|---|
| 2 | :- object(best_first(Threshold), |
|---|
| 3 | instantiates(heuristic_search(Threshold))). |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | :- info([ |
|---|
| 7 | version is 1.2, |
|---|
| 8 | author is 'Paulo Moura', |
|---|
| 9 | date is 2008/6/9, |
|---|
| 10 | comment is 'Best first heuristic state space search strategy.', |
|---|
| 11 | source is 'Example adapted from the book "Prolog Programming for Artificial Intelligence" by Ivan Bratko.', |
|---|
| 12 | parnames is ['Threshold']]). |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | :- uses(list, [member/2, reverse/2]). |
|---|
| 16 | |
|---|
| 17 | :- private(expand/8). |
|---|
| 18 | :- private(succlist/5). |
|---|
| 19 | :- private(bestf/3). |
|---|
| 20 | :- private(continue/9). |
|---|
| 21 | :- private(f/2). |
|---|
| 22 | :- private(insert/4). |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | search(Space, State, Threshold, Solution, Cost) :- |
|---|
| 26 | expand([], l(State, 0/0), Threshold, _, yes, Path, Space, Cost), |
|---|
| 27 | reverse(Path, Solution). |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | expand(Path, l(State,Cost/_), _, _, yes, [State|Path], Space, Cost) :- |
|---|
| 31 | Space::goal_state(State). |
|---|
| 32 | |
|---|
| 33 | expand(Path, l(State,F/G), Threshold, Tree, Solved, Solution, Space, Cost) :- |
|---|
| 34 | F =< Threshold, |
|---|
| 35 | (bagof(Next/Cost2, |
|---|
| 36 | (Space::next_state(State, Next, Cost2), \+ Space::member_path(Next, Path)), |
|---|
| 37 | Successors) -> |
|---|
| 38 | succlist(G, Successors, Trees, Threshold, Space), |
|---|
| 39 | bestf(Trees, F2, Threshold), |
|---|
| 40 | expand(Path, t(State, F2/G, Trees), Threshold, Tree, Solved, Solution, Space, Cost) |
|---|
| 41 | ; |
|---|
| 42 | Solved = never). |
|---|
| 43 | |
|---|
| 44 | expand(Path, t(State, F/G,[Tree| Trees]), Threshold, Tree3, Solved, Solution, Space, Cost) :- |
|---|
| 45 | F =< Threshold, |
|---|
| 46 | bestf(Trees, Threshold2, Threshold), |
|---|
| 47 | expand([State|Path], Tree, Threshold2, Tree2, Solved2, Solution, Space, Cost), |
|---|
| 48 | continue(Path, t(State, F/G, [Tree2| Trees]), Threshold, Tree3, Solved2, Solved, Solution, Space, Cost). |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | expand(_, t(_, _, []), _, _, never, _, _, _) :- |
|---|
| 52 | !. |
|---|
| 53 | |
|---|
| 54 | expand(_, Tree, Threshold, Tree, no, _, _, _) :- |
|---|
| 55 | f(Tree, F), |
|---|
| 56 | F > Threshold. |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | continue(_, _, _, _, yes, yes, _, _, _). |
|---|
| 60 | |
|---|
| 61 | continue(Path, t(State, _/G, [Tree| Trees]), Threshold, Tree2, no, Solved, Solution, Space, Cost) :- |
|---|
| 62 | insert(Tree, Trees, NewTrees, Threshold), |
|---|
| 63 | bestf(NewTrees, F, Threshold), |
|---|
| 64 | expand(Path, t(State, F/G, NewTrees), Threshold, Tree2, Solved, Solution, Space, Cost). |
|---|
| 65 | |
|---|
| 66 | continue(Path,t(State, _/G, [_| Trees]), Threshold, Tree2, never, Solved, Solution, Space, Cost) :- |
|---|
| 67 | bestf(Trees, F, Threshold), |
|---|
| 68 | expand(Path, t(State, F/G, Trees), Threshold, Tree2, Solved, Solution, Space, Cost). |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | succlist(_, [], [], _, _). |
|---|
| 72 | |
|---|
| 73 | succlist(G0, [State/Cost| Rest], Trees, Threshold, Space) :- |
|---|
| 74 | G is G0 + Cost, |
|---|
| 75 | Space::heuristic(State, H), |
|---|
| 76 | F is G + H, |
|---|
| 77 | succlist(G0, Rest, Trees2, Threshold, Space), |
|---|
| 78 | insert(l(State, F/G), Trees2, Trees, Threshold). |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | insert(Tree, [], [Tree], _) :- |
|---|
| 82 | !. |
|---|
| 83 | |
|---|
| 84 | insert(Tree, Trees, [Tree| Trees], Threshold) :- |
|---|
| 85 | f(Tree, F), |
|---|
| 86 | bestf(Trees, F2, Threshold), |
|---|
| 87 | F =< F2, |
|---|
| 88 | !. |
|---|
| 89 | |
|---|
| 90 | insert(Tree, [Tree1| Trees], [Tree1| Trees1], Threshold) :- |
|---|
| 91 | insert(Tree, Trees, Trees1, Threshold). |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | f(l(_, F/_), F). |
|---|
| 95 | f(t(_, F/_, _), F). |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | bestf([Tree| _], F, _) :- |
|---|
| 99 | f(Tree, F). |
|---|
| 100 | |
|---|
| 101 | bestf([], Threshold, Threshold). |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | :- end_object. |
|---|