F = C + h(N) 2. OR
N) F = C + minF. |
tr**(N,F,C,oe:rri,T2,..,])
3. AND
F=C + lFt |
tree[ N, F, , and:[T1,T2,...]}
4.
solvedieaf(N, F>
F=C
N}F=C + F, |
5. OR
soivedtieef N, F, T)
f=c + 2fi |
6. AND
soJvedtree{ N, F, and:[T1,T2,...))
Puc. 13.10.
13, AND/OR
, F-. . . , 13.2. : andor(Mode, SolutionTree)
Mode - . ( ), , , . , h, . , h. , , 12 ( *). , COST(N) N . N AND/OR h(N) < COST(N), . h , . , , h = 0 . , .
13.2. AND/OR
/* AND/OR
. , , ,
:
tree(Node, F, , SubTrees)
leaf(Mode, F, )
solvedtree! Node, F, SubTrees)
solvedleaf(Mode, F)
- , Mode
F = + Hj - Node
SubTrees ; {1) ; (2) [ ) F-
:- (500, xfx,: 5.
:- 600, £,--- >).
aildOEE Mode, SolutionTree):-
|
|
expand! leaf (Node, 0, 0),999, SolutionTree, yes). % ,
% F- 9999
* expand(Tree, Bound, NewTree, Solved)
Tree Bound, NewTree,
s " " Solved
294 II. Prolog
% 1. Bound
expand{ Tree, Bound, Tree, no}:-
f [ Tree, Fj, F > Bound,!. % Bound
I F =< Bound
% 2.
expandt leaf(Node, F, C), _, SOlvedleaf[ Node, F), yes):-goal(Wode),!.
% 3. -
expand! leaf(Mode, F, C), Bound, NewTree, Solved):-{ expandnodei Node, C, TreeU,!,
expand; Treel, Bound, NewTree, Solved); Solved = never,!), % ,
% 4.
expand! tree[ Mode, F, , SubTrees), Bound, NewTree, Solved):-Boundl is Bound - C,
expandlist [ SubTrees, Boundl, NewSubs, Solvedl), continue(Solvedl, Node, C, NewSubs, Bound, NewTree, Solved),
% expandlistt Trees, Bound, NewTrees, Solved)
% ' Trees Bound,
I NewTrees, " " Solved
expandlistC Trees, Bound, NewTrees, Solved]:-
selecttree{ Trees, Tree, OtherTrees, Bound, Boundl),
exoand! Tree, Boundl, NewTree, Solvedl),
combine! OtherTrees, NewTree, Solvedl, NewTrees, Solved).
I continue %
continue! yes, Node, , SubTrees, _, solvedtree(Mode,F,SubTrees ), yes):-backup{ SubTrees, K), F is t Hr!.
continuet never, _, _, _, _, _, never):-!.
continue,- no. Node, C, SubTrees, Bound, NewTree, Solved):-backupt SubTrees, H), F is + H,!, expandt treeCNode, F, C, SubTrees), Bound, MewTree, Solved).
| combine
combine! or:_, Tree, yes, Tree, yes):-!. * OR
combine[ or:Trees, Tree, no, or:NewTrees, no):-
insert(Tree, Trees, NewTrees},! % OR
combine,- or lb, never,, never):-!. % ,
% ,
combine or:Trees, _, never, or:Trees, no):-!. % ,
%
combine; and:Trees, Tree, yes, and:[Tree Trees], yes):-
allsolved(Trees),!. % AND
combine; and:_, _, never, _, never):-!. % AND
%
13. AND/OR
combine(and:Trees, Tree, YesNo, and:NewTrees, no):-
insert; Tree, Trees, NewTrees),!. % AND
i expandnode ,
expandnode) Node, , tree (Node, F, , Op:SubTrees)):-
Node--- > Op successors,
evaluate! Successors, SubTrees), backup) OpiSubTrees, , F is + H.
evaluate) [], []).
evaluate) [Node/Cl NodesCosts], Trees):-
h{ Node, H), F is + ,
evaluate(NodesCosts, Treesl),
insert! leaf(Node, F, C), Treesl, Trees).
% allsolved , *
allsolved) []).
|
|
allsolved([Tree|Trees]): -
solved(Tree >, allsolved(Treesl.
solved; solvedtree (_,_,_)).
solved (soivedieaf (_,_)).
f (Tree, F):- % F- .
arg(2, Tree,:-";,!. % F - Tree
% insert) Tree, Trees, NewTrees): Tree Trees, - NewTrees
insert; , [], []):-!.
insert) T, [T1|TS], [T,T1|TS]):-solved (Tl),!.
insert) T, [TUTS], [TllTsl]):-solved(T),
insert) T, Ts, Tsl),!.
insert) T, [TllTs], [T,Tl|Ts]):-f (T, F., f(Tl, Fl), F =< Fl,!.
insertCT, [TllTs], [Tl|T5l]l:-
insert , ts, Tsl).
% backup F- I AND/OR
backup! or:[Treel_), F):- % OR t (Tree, F),!.
backup) and: [], 0):-!.
backupf and:[Treel|Trees], F):-
ft Treel, Fl),
backup) and:Treesr F2},
F is Fl + F2,!.
backup) Tree, F):-
. Prolog
F(Tree, F).
% Bound - Trees, % Boundl - BestTree
selecttree(Op:[Treel, Tree, oP:[], Bound, ):-!. ,
3
selecttreet Op: [Tree |Trees}, Tree, Op:Trees, Bound, Boundl):-backup{ Op:Trees, F), [ Op = or,!, mint Bound, F, Boundl); Op = and, Boundl is Bound - F).
mini A, a, A):- A < ,!.
mint , , m.
, 13.2, :
expand(Tree, Bound, Treel, Solved)
Tree Bound - "" , a Treel Solved - "". .
Tree. , .
Bound. F-, Tree.
Solved. ,
.
Solved = yes. Tree , Treel.
Solved - no. Tree Treel , F- Treel Bound , F- Bound.
Solved = never. Tree .
, Treel , Tree Bound, ( Solved = never).
expandlistc Trees, Bound, Treesl, Solved)
expand. expand, Bound , a Solved , ("yes", ":" "never"). ( AND OR), . Trees - qx:IT1, T2,... ] Trees = and:!-:, T2,... ]
expandlist Trees ( F-). Trees. Boundl, Boundl Bound, a Trees. Trees OR, Boundl Bound F- Trees (F-), Trees AND, Boundl Bound F- -
13, AND/OR
Trees. Treesl , Solved. , Solved = no, Treesl Trees Trees, Boundl. Solved = yes, Treesl Trees ( Bound), Solved = never, Treesl .
|
|
continue, , , , expandlist. , "never" ( , , ).
, combine (otherTrees, NewTree, Solvedl, HewTrees, Solved)
, expandlist. NewTree - , expandlist, OtherTrees , , a Solvedl - " " NewTree. combine , Solvedl , AND OR. , combiner, or:_. Tree, yes, Tree, ).
, , OR, Tree, Tree. combine.
, show (. 13.1). .