All Downloads are FREE. Search and download functionalities are using the official Maven repository.

APT.all-test.towers-of-hanoi.towers-of-hanoi-faster.soar Maven / Gradle / Ivy


###
### FILE          : tower-of-hanoi8.soar
###

###
### ABSTRACT. This file provides a Soar system to solve the Tower 
### of Hanoi problems. This puzzle "involves three vertical pegs or 
### posts and a number of doughnut-like disks of graduated sizes that
### fit  on the pegs. At the outset, all the disks are arranged pyrami-
### dally on one of the pegs, say A, with the largest disk on the bottom. 
### The task is to move all of the disks to another peg, C, say, under 
### the constraints that (1) only one disk may be moved at a time, and 
### (2) a disk may never be placed on top of another smaller than itself. 
### Any number of disks may be used; the minimum number of moves for
### a solution is (2**n - 1), where n is the number of disks" (Simon, 
### 1975/1979, pp. 230-231).
###

### This version uses search control rules to solve the TOH in the
### minimum number of moves.
### The search control is as follows:
### Always alternate between moving the smallest disk and
### the other exposed disk that can move.  
###  Move the exposed disk to the one place it can move (the other disk
###  that is bigger than it or an empty peg).  
###  Move the smallest disk to the peg it was not at last.
###   This requires remembering the last peg the smallest disk was on,
###    and initializing this to the appropriate peg (the destination peg
###    if there is an even number of disks, and the other peg if there is
###    an odd number of disks).


### These declaration help Soar reorder the conditions of the rules
multi-attributes operator 2
multi-attributes disk 7
multi-attributes peg 4
multi-attributes holds 10
#multi-attributes upper-disk 
multi-attributes clear 3

learn --off

###
### TOWER-OF-HANOI:
### INITIAL-STATE AND DESIRED-STATE
###

sp {tower-of-hanoi*propose*state*initial-and-desired-states
    "A 3-Disk Tower of Hanoi Problem." 
    (state  ^superstate nil)
    -->
    ( ^peg a b c
         ^holds 

^last-disk1-peg b ^last-disk-moved 2 ^clear b c) (

^disk 1 ^above 2 ^on a) (

^disk 2 ^above 3 ^on a) (

^disk 3 ^above 4 ^on a) (

^disk 4 ^above 5 ^on a) (

^disk 5 ^above 6 ^on a) (
^disk 6 ^above 7 ^on a) ( ^disk 7 ^above 8 ^on a) ( ^disk 8 ^above 9 ^on a) ( ^disk 9 ^above 10 ^on a) ( ^disk 10 ^above 11 ^on a) ( ^disk 11 ^above none ^on a)} ### ### PROPOSE OPERATORS TO MOVE DISKS ### sp {tower-of-hanoi*propose*move-disk1 (state ^holds ^peg { <> <> } ^last-disk1-peg ^last-disk-moved <> 1) ( ^disk 1 ^on ) --> ( ^operator ) ( ^name move-disk ^disk 1 ^from ^to )} sp {tower-of-hanoi*propose*move-disk*disk-to-move*onto2 "Target peg is clear." (state ^upper-disk ^clear ^holds ^last-disk-moved 1) ( ^disk { <> 1 } ^on ) --> ( ^operator ) ( ^name move-disk ^disk ^from ^to )} sp {tower-of-hanoi*propose*move-disk*disk-to-move*above2 "Upper disk on the target peg is larger." (state ^upper-disk { > } ^holds { <> } ^last-disk-moved 1) ( ^disk { <> 1 } ^on ) ( ^disk { > } ^on ) --> ( ^operator ) ( ^name move-disk ^disk ^from ^to )} ### ### OPERATOR APPLICATION ### sp {apply*move-disk*source-clear*target-clear (state ^operator ^clear ^holds ) ( ^name move-disk ^disk ^from ^to ) ( ^disk ^above none ^on ) --> ( ^on -) ( ^clear -)} sp {apply*move-disk*source-not-clear*target-clear (state ^operator ^clear ^holds ) ( ^name move-disk ^disk ^from ^to ) ( ^disk ^above { <> none } ^on ) --> ( ^clear -) ( ^above none - ^on - )} sp {apply*move-disk*target-not-clear (state ^operator ^upper-disk ^holds { <> }) ( ^name move-disk ^disk ^from ^to ) ( ^disk ^above ^on ) ( ^disk ^on ) --> ( ^upper-disk -) ( ^above - ^on - )} sp {apply*move-disk*add-clear-source (state ^operator ^holds ) ( ^name move-disk ^disk ^from ) ( ^disk ^above none ^on ) --> ( ^clear )} sp {apply*move-disk*add*upper-disk*source (state ^operator ^holds ) ( ^name move-disk ^disk ^from ) ( ^disk ^above { <> none } ^on ) --> ( ^upper-disk )} ### Maintain last-disk-moved sp {apply*move-disk*record*last-disk-moved (state ^operator ^last-disk-moved { <> }) ( ^name move-disk ^disk ) --> ( ^last-disk-moved ^last-disk-moved -)} ### Maintain last peg disk 1 was moved to sp {apply*move-disk*record*last-disk1-peg (state ^operator ^last-disk1-peg { <> }) ( ^name move-disk ^disk 1 ^from ) --> ( ^last-disk1-peg ^last-disk1-peg -)} ### ### TOWER-OF-HANOI GOAL-TEST ### sp {tower-of-hanoi*desired-of-disk*satisfied (state ^holds

) (

^disk 1 ^on c) (

^disk 2 ^on c) (

^disk 3 ^on c) (

^disk 4 ^on c) (

^disk 5 ^on c) (
^disk 6 ^on c) ( ^disk 7 ^on c) ( ^disk 8 ^on c) ( ^disk 9 ^on c) ( ^disk 10 ^on c) ( ^disk 11 ^on c) --> (write (crlf) |Success!|) (halt)} ### ### TOWER-OF-HANOI MONITOR ### sp {tower-of-hanoi*monitor*operator-execution*move-disk (state ^operator ) ( ^name move-disk ^disk ^to ) --> (write (crlf) | Move Disk:| | to peg | )} excise tower-of-hanoi*monitor*operator-execution*move-disk ### eof of tower-of-hanoi.soar