Attachment 'Hanoi-Tower-ENCODING.txt'
Download 1 % The meaning of the time predicate is self-evident. As for the disk
2 % predicate, there are k disks 1,2,...,k. Disks 1, 2, 3, 4 denote pegs.
3 % Disks 5, ... are "movable". The larger the number of the disk,
4 % the "smaller" it is.
5 %
6 % The program uses additional predicates:
7 % on(T,N,M), which is true iff at time T, disk M is on disk N
8 % move(t,N), which is true iff at time T, it is disk N that will be
9 % moved
10 % where(T,N), which is true iff at time T, the disk to be moved is moved
11 % on top of the disk N.
12 % goal, which is true iff the goal state is reached at time t
13 % steps(T), which is the number of time steps T, required to reach the goal (provided part of Input data)
14
15 % Read in data
16 on(0,N1,N) :- on0(N,N1).
17 onG(K,N1,N) :- ongoal(N,N1), steps(K).
18
19 % Specify valid arrangements of disks
20 % Basic condition. Smaller disks are on larger ones
21 :- time(T), on(T,N1,N), N1>=N.
22
23 % Specify a valid move (only for T<t)
24 % pick a disk to move
25 move(T,N) | noMove(T,N) :- disk(N), time(T), steps(K), T<K.
26 :- move(T,N1), move(T,N2), N1 != N2.
27 :- time(T), steps(K), T<K, not diskMoved(T).
28 diskMoved(T) :- move(T,Fv1).
29
30 % pick a disk onto which to move
31 where(T,N) | noWhere(T,N) :- disk(N), time(T), steps(K), T<K.
32 :- where(T,N1), where(T,N2), N1 != N2.
33 :- time(T), steps(K), T<K, not diskWhere(T).
34 diskWhere(T) :- where(T,Fv1).
35
36 % pegs cannot be moved
37 :- move(T,N), N<5.
38
39 % only top disk can be moved
40 :- on(T,N,N1), move(T,N).
41
42 % a disk can be placed on top only.
43 :- on(T,N,N1), where(T,N).
44
45 % no disk is moved in two consecutive moves
46 :- move(T,N), move(TM1,N), TM1=T-1.
47
48 % Specify effects of a move
49 on(TP1,N1,N) :- move(T,N), where(T,N1), TP1=T+1.
50
51 on(TP1,N,N1) :- time(T), steps(K), T<K,
52 on(T,N,N1), not move(T,N1), TP1=T+1.
53
54 % Goal description
55 :- not on(K,N,N1), onG(K,N,N1), steps(K).
56 :- on(K,N,N1), not onG(K,N,N1),steps(K).
57
58 % Solution
59 put(T,M,N) :- move(T,N), where(T,M), steps(K), T<K.
Attached Files
You are not allowed to attach a file to this page.