
APT.all-test.towers-of-hanoi.towers-of-hanoi-recur.soar Maven / Gradle / Ivy
###
### FILE : tower-of-hanoi.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).
###
### MEA Recursive Stratgey
### Operators proposed to achieve current goal
###
### These help Soar reorder the conditions of rules
multi-attributes disk 3
multi-attributes peg 3
multi-attributes holds 4
multi-attributes upper-disk 2
multi-attributes clear 2
learn --off
###
###
### TOP GOAL: TOWER-OF-HANOI
###
sp {top-goal*elaborate*goal*tower-of-hanoi
(state ^superstate nil)
-->
( ^name 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 ^name tower-of-hanoi)
-->
( ^disk
^peg
^holds
^top-state )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above none ^on )
( ^name 1 ^size 1)
( ^name 2 ^size 2)
( ^name 3 ^size 3)
( ^name 4 ^size 4)
( ^name 5 ^size 5)
( ^name 6 ^size 6)
( ^name 7 ^size 7)
( ^name |A|)
( ^name |B|)
( ^name |C|)
( ^desired )
( ^holds )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above ^on )
( ^disk ^above none ^on )}
###
### STATE ELABORATION/INFERENCES:
### LARGER, UPPER-DISK, AND
### CLEAR
###
sp {tower-of-hanoi*object*disk*elaborate*state*larger
"If size-of(i) > size-of(j), then larger(i,j)."
(state ^name tower-of-hanoi
^disk { <> })
( ^size )
( ^size > )
-->
( ^larger )}
sp {tower-of-hanoi*object*disk*elaborate*state*upper-disk
"If not(above(x,disk)), then upper-disk(disk)."
(state ^name tower-of-hanoi
^disk )
-{ ( ^holds.above ) }
-->
( ^upper-disk )}
sp {tower-of-hanoi*object*peg*elaborate*state*clear
"If not(on(x,peg)), then clear(peg)."
(state ^name tower-of-hanoi
^peg )
-{ ( ^holds.on ) }
-->
( ^clear )}
###
### OPERATOR PROPOSAL
###
## Desired is to have disk on a peg so propose operator to do it
## Desired comes from original goal.
sp {tower-of-hanoi*propose*move-disk*mea*onpeg
(state ^desired )
( ^holds )
( ^disk
^on
^satisfied *no*)
-{( ^holds )
( ^disk
^satisfied *no*)
( ^larger )}
-->
( ^operator )
( ^name move-disk
^type put-on-peg
^disk
^to )}
## Desired is to have disk clear (so it can move)
## Propose moving disk above it
sp {tower-of-hanoi*propose*move-disk*mea*clear-disk
(state ^desired
^top-state )
( ^holds
^peg { <> <> })
( ^disk
^above
^on )
( ^move-disk )
( ^disk
^peg )
-->
( ^operator )
( ^name move-disk
^type clear-disk
^disk
^to )}
##
## Clear off smaller disk in the way of a larger disk
sp {tower-of-hanoi*propose*move-disk*mea*clear-smaller
(state ^desired
^top-state )
( ^holds
^peg { <> <> })
( ^disk
^on )
( ^disk
^on )
( ^move-disk )
( ^disk
^peg )
( ^larger )
-{( ^holds )
( ^disk
^on )
( ^larger )
( ^larger )}
-->
( ^operator +)
( ^name move-disk
^type clear-smaller
^disk
^to )}
###
### SEARCH CONTROL
### MAINTAIN OPERATOR IN GOAL STACK IF IT HAS BEEN SELECTED
### otherwise, new proposed operators would tie with it
sp {tower-of-hanoi*select*maintain*operator
(state ^operator )
-->
( ^operator >)}
###
sp {tower-of-hanoi*elaborate*desired*not-statisfied
(state ^name tower-of-hanoi
^desired
^top-state )
-{( ^holds )
( ^disk
^on )}
( ^holds )
( ^disk
^on )
-->
( ^satisfied *no*)}
sp {tower-of-hanoi*elaborate*desired*statisfied
(state ^name tower-of-hanoi
^desired.holds
^top-state.holds )
( ^disk
^on )
( ^disk
^on )
-->
( ^satisfied *yes*)}
###
### TOWER-OF-HANOI PROBLEM SPACE:
### OPERATOR APPLICATION
###
sp {apply*move-disk*source-clear*target-clear
(state ^operator
^top-state )
( ^name move-disk
^disk
^to )
( ^clear
^upper-disk
^holds )
( ^disk
^above none
^on { <> })
( ^name )
( ^name )
( ^name )
-->
(write (crlf) |Move | | from | | to | )
( ^on - )}
sp {apply*move-disk*source-not-clear*target-clear
(state ^operator