I bestfiratt Start, Solution):
Solution. - Start
bestfirst) Start, Solution):-
expand([], 1(Start, 3/0), SS99, _, yes, Solution).
, f- 9999
% expand! Pathr Tree, Bound, Treel, Solved, Solution):
12.
Path . Tree 1
Tree, Bound; , Solution . Solved = yes
1. Y.fiT.v. |
-, yes, [HIP]):-
expand (, 1 (S, _), goal(Hi.
\ 2. -; f- Bound.
\ - 3ound
expand! P, 1(N,F/G), Bound, Treel, Solved, Sol):-F -< Bound, (bagoft /, [s(N,M,C}, not member (M, P)), Succ),
!, %
succlist(G, Succ, Ts), % Ts
bestf{ Ts, Fit, % f-
expand! P, t(N,Fl/G,Ts), Bound, Treel, Solved, Sol)
)
Solved
% *
3. -.--; f- Bound. ; continue
expand! P, t(N, F/G, [T[Ts]), Bound, Treel, Solved, Sol):-F =< Bound,
bestf (Ts, BF!, mint Bound, BF, Boundl), %. Boundl - min (Bound, BF] expandt [H|F], T, Boundl, Tl, Solvedl, Sol), continue(P, t(N,F/G, [Tl|Ts]), Bound, Treel, Solvedl, Solved, Sol).
....-; 4... ■.■-..;...'.: .
,
expand; _, t(_,_, []), _, _, never, _):-!.
I 5. f- Bound. '
expand _, Tree, Bound, Tree, no, _):-ft Tree, F), F > Bound.
continue; Path, Tree, Bound, HewTree, SubtreeSolved, TreeSolved, Solution)
continue; _, _, _, _, yes, yes, Sol).
continue; P, t(N,F/G,[Tl|Ts]), Bound, Treel, no, Solved, Sol):-insert; Tl, Ts, NTs), bestf; NTs, Fl), expand! P, t[N,Fl/G,NTS), Bound, Treel, Solved, Sol).
continue! P, t(N,F/G,1_ITs]), Bound, Treel, never, Solved, Sol):-bestf; Ts, Fl), expand!' P, t (, Fl/G, Ts), Bound, Treel, Solved, Sol).
% succlist(GO, [ Nodel/Costl,...],! 1(BestHode,BestF/G),...)!: 't F-
succlist(
[], []) -
succlist GO, [/ i NCs], Ts) G is GO + C,
: -
252 II. Prolog
ht , ), % h(N)
F is G + H,
succlist(GO, NCs, Tsl),
insert! 1{H,F/G), Tsl, Ts i.
% Ts ; f-
insert; T, Ts, [ | Ts]):-f(, F), bestf! Ts, Fl),
F =< Fl,!.
insert; T, [Tl | Ts], [Ti | Tsl]):-
insert (T, Ts, Tsl).
% f-
f(1 (_, f/_;, F). % f- jthcts
|
|
f(t(_/F/_,J, F). % f-
bestf([|_], F):- % f-
, F}.
bestfl [], 9999). % : f-
mini X, Y, X):-X =< Y,!.
mint X, Y, Y).
, Tree Bound "" expand; , expand . expand , Solved, .
1. Solved = yes.
Solution , Tree Bound.
Treel .
2. Solved = no.
Treel - Tree, , f- Bound
(. 12,3).
Solution .
3. Solved = never.
Treel Solution .
, Tree - () . , f- Tree Bound, , (.. ) , , .
, expand, . , , , Tree , .. Tree = t(, F/G, [T | Ts)!
12.
, , , f- , Ts. , , , . f-. continue ; , . , , . , , Tree = 1 N, F/G!
- N N -. succlist , -, g- f- (. 12.4), , Bound. , , , - Solved = 'never'.
1
-> Bound
. 12.3. expand Tree , {- Bound, Tree 1
.
5< , )
- , N .
N, ,
N .
II. Prolog
|
|
. , . , * (. , ). * , . *.
g(M) = g(N)H
: 12.4. g . f- -
, (.. ), , . , , , . , h* in) . . , *, : *, h, , :
() < h* ()
. h*, h* h *. , * . , , :
-
.
. . --■- , - . *, h = 0, . , , .;, ') - 1 (,') . . h, h ■ ( ), h* ( ). ,
12.
h*, h* *, h*, , - .
12.1. s, goal h , ! . 12.2, . * .
12.2. :! " , * , Hi [.) < h* () ". - 1 ?
12.3. , hu h2 hj - (h:. £ h*), * -1 . ! , h, ! hi, .
12.4. - . ! , | . ? ] , . . -. (, ). *. . s(state, NewState, Cost) hi State, 11) l * (, -' ). , - goal {Xg/Yg), Xg Yg !' . :
|
|
obstacle(Xmin/Ymin, Xmax/Ymax}
xmin/Ymin , a Xmax/Ymax .
12.2. " "
, 12.1, , , . (" "), , . .
, , : s(Node, Model, Cost)
II. Prolog
, Node Nodel Cost. goal(Mode)
, Node .
h[ Node, H)
Node .
: " " ( 11.1) .
, " ", 12.2. . . , X/Y. .
1. .
2. 1.
3. 2.
4....
(. . 11.3), , :
goal! [2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2]).
12.2. " ", , 12.1
{* " "
,
:■: | 13 | ||
[ | |||
: [J2/2, 1/3, 2/3, 3/3, 3/2, 3/1, 2/1, 1/1, 1/2]
|
|
12 3
, - "" , . , ; , "" V
% s t Mode, SuccessorNode, Cost)
S([Empty I Tiles], [Tile ] Tilesl], 1):- % 1 swap* Empty, Tile, Tiles, Tilesl). %
% Empty Tile Tiles
swap Empty, Tile, [Tile [ Ts], [Empty | Ts]):-
mandist{ Empty, Tile, 1). % 1
12.
- |
% D - % |
I D |-| |
snap! Empty, Tile, [Tl I Ts], [Tl I Tsl] Swap! Empty, Tile, Ts, Tsl).
: -
mandistC X/Y, Xl/Yl, D)
dif (X, XI, Dx),
dif < , Yl, Dy),
D is Dx + Dy.
dif(, , D):-D is A-B, D >= 0,!
D is -.
% h 4 "" " "
hi [Empty I Tiles], H]:-
goal{ [Emptyl I GoalSquares]), totdist{ Tiles, GoalSquares, D), seq(Tiles, S), H is D + 3*S,
i
totdist([], [], 0).
totdistt [Tile I Tiles], [Square ] Squares], D) mandist! Tile, Square, Dl), totdistt Tiles, Squares, D2), D is Dl + D2.
-
% seq(TilePositions, Score):
seq([First I OtherTiles], s):-
seq([First I OtherTiles ], First, 3).
seq([Tilel, Tile2 I Tiles], First, S):-score(Tilel, Tile2, El), seq([Tile2 t Tiles), First, £2), S is SI + $ 2.
seq([Last], First, S}:-score(Last, First, SJ.
1. |
score) 2/2, _, 1)
score! 1/3, 2/3, 0!
score! 2/3, 3/3, 0)
score! 3/3, 3/2, 0)
score! 3/2, 3/1, 0)
score! 3/1, 2/1, Q)
score! 2/1, 1/1, 0)
score(1/1,1/2,0)
scoreC 1/2, 1/3, G)
: -
: - | !. |
: - | !. |
: - | !. |
: - | [, |
: - | !. |
: - | !. |
: - | !. |
% , , 1
% , % , 0
score! _, _, 2). ,
% , 2 goal [2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2]). %
showsolC []).
showsol([P I L] showsol(L),
)
-
258 II. Prolog
al, write('------- '),
showpost P]. i
Showposf [S0,S1,S2,S3,S4,S5,S6,S7,S8]):-
member; Y, [3,2,1) >, % Y
nl, member[ X, [1,2,3)), %
member! Tile-X/Y, % XY
[■ '-S0,1-S1,2-S2,3-S3,4-S4,5-S5,6-S6,7-S7,8-S8] (, write! Tile),
fail I
true, %
*
stactll [2/2,1/3,3/2,2/3,3/3,3/1,2/1,1/1,1/2]). I 4
start2(£2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3]). % 5
Start3([2/2,2/3,1/3,3/1,1/2,2/1,3/3,1/1,3/2]). % 19
% ^:?- Pos), beetHHt[ Pos, Sol), rtowsel t'Sol)..............................
:
mandist(SI, 52, D).
D S1 S2; S1 S2 .
. 1. , 12.2, , , . 12.5.
. | ||
a) fi] )
, 12,5. " ": ) . 4 ; ) . 5 ; ) 18
h : hi , ) Pos , ,
1. totdist. , , . , , . 12.5, , totdist = 4.
|
|
2. seq. , , , , , . -
12. 259
seq :
1;
, , , (, 1 2);
2, .
, , . 12.5, , seq = 6.
, , = totdist + 3 * seq.
, . , , . 12.5, , , , , , . , , - . , . 12.5, . , : , - . , h , h < h* . , , . 12.5, , :
h=4 + 3*6-22, * = 4
, " ", :
totdist <_ h*
: , , , . totdist. , , , totdj sj-.
12.5. , 12.1, , , . , retract assert . " " , .
260 II. Prolog
. , , tj, tj,..., , Dj, D;,.... .. , . , , , , , - . , . . , .
. 12.6 , . , , . , , 12.6, 2 -,■ , t-?.
/7/ |
s | 4 13 | 24 33 | ||||
fJ | h | |||||
h | '■ | |||||
, | ! 1 | \ | ||||||
h | ||||||||
^ | it -j | ; | ■ ■ ■■ | |||||
h | ■■:.: ■ i | |||||||
. 12.6. , , , . , £, 20 , tL. t2 £. : , 24, , 33. - (, [34], . 86), Prentice Hall, Englewood Cliffs, New Jersey
12.
, , . ( ), , . , . . :
;
- , ; , , , ;
;
, , ;
( ) ;
, ( ) , Fi , 2 - W\.
. -, , . , , . - , - , . , - , .
, .. . .
1. .
2. .
, .
3. ( ) , ,
.
:
[ Taskl/Dl, Task2/D2,... ]
, ; , :
Task/FinishingTime
, . , . -
262 II. Prolog
. ( , ) : WaitingList * ActiveTasks * FinisbingTime
, , : prec(TaskX, TaskY)
. , . . , .
, . , .
1. .
2. () , .
, Di, Dv,..., Fi, F... , Finall, , :
Finall = t!i £)/
i J
m . , : Fin = (F-)]
( , ) : Finall > Fin, - Finall - Fin, =
, , 12.3. , , . 12.6. , 12.1. , , , , . 12.6.
12.
12.3. , , . , . 12.6, () ,
/* , ,
, :
[ WaitingTaskl/Dlr WaitingTask2/D2,...] * [ Taskl/Fl, Task2/F2,...] * FinTime
; , , , , Fl =< F2, F2 =< F3... Fintime */
4 s(Mode, SuccessorNode, Cost)
s{ Tasksl * (_/F I Activel] * Finl, Tasks2 * Active2 * Fin2, Cost):-
del(Task/D, Tasksl, Tasks2), %
not (member! T/_, Tasks2], before(T, Task] }, %
% , not (member! Tl/Fl, Activel), F < Fl, befoxet Tl, Task}), %
% Time is F + D, % insert(Task/Time, Activel, Active2, Flnl, Fin2), Cost is Fln2 - Flnl.
s(Tasks *!_/F I Activel] * Fin, Tasks * Actlve2 * Fin, 0):-
insertidle (F, Activel, Active2}. %
before! Tl, 12): - %
% 1 2 prec(Tl, T2).
before! Tl, 12):- prec(T, T2), before { Tl, T).
Insert,- S/A, [T/B j L], [S/A, / I L], F, F):- % A =< ,!.
insert t S/A, [T/B I L], [T/B | LI], Fl, 2):-
insert f S/A, L, U, Fl, F2).
insert t s/A, [], [s/A], _, A).
insertidle(A, [T/B I L], [idle/B, T/B I L]):- %
A < ,!, %
% ,
lnsertldle(A, [T/B L], [/ 1 L1]):-insertidle (A, L, L1).
del (, [ | ], LI. %
del(, [3 [ L], [ I L1]):-del(A, L, L1).
goal { [] * *_), % ~ %
264 . Prolog
% % , %
{ Tasks * Processors * Fin,- HJ:-
totaltimeC Tasks, Tottime), % sumriumC Processors, Ftime, ), % Ftime -
% , N - Finall is (Tottime + Ftime)/N, (Finall > Fin,!, H is Finall - Fin
H = 0
).
totaltime([], 0).
totaltime([_/D | Tasks], T):-totaltiraet Tasks, Tl), T is II + D.
sumnurti([], 0, 0}.
suamum([_/T I Procs], FT, N):-sumrmrM Procs, FTlr Hit, II is HI + 1,
FT is FT1 + T.
%
prec(tl, t4). prec{ tl, t5).prec(tZ, t4). prec(t2, tS). prect t3, t5]. prec(t3, t6). prec(t3r t7>.
*
start([tl/4, t2/2, t3/2, t4/20, t5/20, t6/ll, t7/ll] * [idle/0, idle/0, idle/0] * 0).
% : start! Problem), bestfirst (Problem, Sol).
, . , .