welcome: please sign in
location: attachment:hanoi.core.asp.txt of EncodingsExamples

Attachment 'hanoi.core.asp.txt'

Download

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