State Space

Control Algorithms 2
Chapter 6
Production Systems
Emil Post (40’s): production systems as a
formal theory of computation. Equivalent
to a Turing machine. Set of rewrite rules
for strings.
 Newell and Simon (60’s, 70’s, 80’s):
General Problem Solver
 John Anderson, Newell and Simon (80’s):
learning models, ACT*, SOAR
 Everyone (80’s): Expert systems

A Model of Computation
1.
Set of rewrite rules
S  NP VP
LHS: Condition Part
RHS: Action Part
Components
2.
Working Memory
--Contains the current state of the world
--Contains pattern that is matched
against the condition of the production
--When a match occurs, an action is
performed
Components
3.
Recognize-Act Cycle
--Isolate a subset of productions whose
conditions match patterns in working
memory: conflict set
--Choose one of them
---Fire
---Change contents of working
memory
--Stop when there are no matches
Components
Productions
1.
N  0N0
2.
N  1N1
3.
N0
4.
N1
5.
Nλ
Iteration
0
1
2
3
4
Working Memory
N
0N0
00N00
001N100
0010100
Conflict Set
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
Fired
1
1
2
3
Example: Production system to
generate the set of palindromes over
the alphabet {0,1}
Knight’s Tour As a Production
System
Given a 3X3 matrix
 What squares can a knight land on
What values of X, Y satisfy mv(X,Y) X,Y are
elements of {1,2,…,9)
1
4
7
2
3
1. mv(1,8) 7. mv(4,9)
13. mv(8,3)
2. mv(1,6) 8. mv(4,3)
14. mv(8,1)
15. mv(9,2)
5
6
3. mv(2,9) 9. mv(6,1)
8
9
4. mv(2,7) 10. mv(6,7) 16. mv(9,4)
5. mv(3,4) 11. mv(7,2)
6. mv(3,8) 12. mv(7,6)
x( path( x, x))
xyz (mv( x, z )  path( z, y )  path( x, y )
The General Case
1. Every expression of the form mv(x,y)
becomes on(x)  on(y)
2. Use no path expression
3. Working memory is the current state and
goal state
4. Conflict set is the set of rules that match
the current state
5. Apply all rules until the current state
equals the goal state
Changes

1. mv(1,8) 7. mv(4,9)
13. mv(8,3)

2. mv(1,6) 8. mv(4,3)
14. mv(8,1)

3. mv(2,9) 9. mv(6,1)
15. mv(9,2)

4. mv(2,7) 10. mv(6,7) 16. mv(9,4)

5. mv(3,4) 11. mv(7,2)

6. mv(3,8) 12. mv(7,6)

1. on(1) -> on(8)
7. on(4) -> on(9)
13. on(8) -> on(3)

2. on(1) -> on(6)
8. on(4) -> on(3)
14. on(8) -> on(1)

3. on(2) -> on(9)
9. on(6) -> on(1)
15. on(9) -> on(2)

4. on(2) -> on(7)
10. on(6) -> on(7)
16. on(9) -> on(4)

5. on(3) -> on(4)
11. on(7) -> on(2)

6. on(3) -> on(8)
12. on(7) -> on(6)
Productions
Iteration
0
1
2
3
4
5
--Working Memory-Current
Goal
1
2
8
2
3
2
4
2
9
2
2
2
Conflict Set
Fired
1,2
13,14
5,6
7,8
15,16
1
13
5
7
15
Halt
Can We Get from 1 to 2?
path(1,2) {1/x,2/y}
mv(1,z)^path(z,2) {8/z}
mv(1,8)^path(8,2)
mv(8,z)^path(z,2) {3/z}
mv(8,3)^path(3,2)
mv(3,z)^path(z,2) {4/z}
mv(3,4)^path(4,2)
mv(4,z)^path(z,2) {9/z}
mv(4,9)^path(9,2)
mv(9,z)^path(z,2) {2/z}
mv(9,2)^path(2,2)
t
t
t
t
t
Pattern Search
Now look at working memory in the production system
Production System Pattern Search
Productions
working memory
Fire lowest numbered production
mv
path(X,Y)
Choose first rule that unifies
Conclusion:
Production Systems and pattern search are
almost equivalent
Equivalences
Production systems have no loop detection
mechanism
Solution:
Invent two new productions
1. assert(X) causes X to be stored in WM
2. been(X) is T if X has been visited
3. assert(been(X)) records in wm that
we’ve already visited X
Almost?
x( path( x, x))
xyz (mv( x, z )  been( z )^ assert (been( z ))
^ path( z , y )  path( x, y )
Can be expressed in PC
notation like this
Iteration
0
1
2
3
4
5
--Working Memory-Conflict Set
Current Goal been
1
7
1
1,2
8
7
8
13,14
3
7
3
5,6
4
7
4
7,8
9
7
9
15,16
2
7
2
3,4
(firing 3 causes been(9) to fail)
2
7
2
4
7
7
7
Notice that this search is data driven
Can We Get from 1 to 7?
Fired
1
13
5
7
15
3
4
Instead of starting with current state=1
and goal = 7
Start with current state = 7 and goal = 1
Can Also Be Goal Driven
What about 8x8?
Either enumerate all moves or encode them
8 possible situations
1. d(2),r(1)
5. u(2),r(1)
2. d(2),l(1)
6. u(2),l(1)
3. d(1),r(2)
7. u(1),r(2)
4. d(1),l(2)
8. u(1),l(2)
Works great for a 3x3 matrix
Situation have preconditions:
Pre: row <=6, col <=7
Situation 1: d(2),r(1)
Requires 4 new functions
sq(r,c) returns cell number, left to right, top to
bottom where r is row number, c is column
number
plus(r,2) returns r + 2
eq(X,Y) T if X = Y
lte(X,Y) T if X<=Y
Not applicable everywhere
mv(sq(R,C),sq(Nr,Nc)) 
lte(R,6)^eq(Nr,plus(R,2))
^lte(C,7)
^eq(Nc,plus(c,1)
There are 7 more just like this
Encoding of situation 1: d(2),r(1)
rc( path( sq (r , c), sq (r , c)
rcnrnc( path( sq (r , c), sq (nr , nc)) 
zrzc (mv( sq (r , c), sq ( zr , zc ))  been( sq ( zr , zc ))  assert (been( sq ( zr , zc ))) 
path( sq ( zr , zc ), sq (nr , nc))
Control Loop for Knight’s Tour
1.
2.
3.
4.
5.
6.
Said to model human cognition
Separation of knowledge from control
Natural mapping onto state space
search
Modularity of production rules
Simple Tracing and explanation—
compare a rule with a line of c++ code
Language independence
Strength of Production Systems
Production systems are easily rendered in
prolog
We’ll consider several versions of the
knight’s tour
But
Global Record of Squares Visited
Put Squares Visited on a List
Use a Stack to Display Path to
Goal
Use of Queue to Keep Track
of Visited Squares

Try: path(1,W)
◦ 1 is on the stack
◦ Base case succeeds: w = 1
◦ Now path1 is invoked, Start = 1
◦ Move(1,6) succeeds, stack: [6,1]
◦ Base case succeeds: w = 6, path is 1,6
◦ Now path1 invoked:, Start = 6
◦ Move (6,1) fails because 1 has been visited
◦ Move(6,7) succeeds: stack:[7,6,1]
◦ Base case succeeds: w = 7, path is 1,6,7
◦ This process continues until all possible states have been visited
◦ Occurs when path is 1,6,7,2,9,4,3,8
◦ Now we backtrack from 8
◦ But everything has been tried until we return to second move predicate starting with 1
(move(1,8))
◦ Now we go through the entire process again
Display Backtracking

!
◦ Always succeeds the first time it is encountered
◦ When backtracked to, it causes the entire goal
in which it was contained to fail
Without ! (4 2 path moves)
With ! (2 2 path moves, because the first move
cannot be backtracked to)
Cut
A farmer (f) has a dog (d), a goat (g),and
a cabbage (c)
 A river runs North and South
 The farmer has a boat that can hold only
the farmer and one other item
 Without the farmer

◦ The goat will eat the cabbage
◦ The dog will eat the goat

How does the farmer (and his cohort)
cross the river
Farmer Problem
Define a predicate:
state(F,D,G,C)
Where F,D,G,C can be set to e or w
indicating the side of the river each is on.
st(w,w,w,w)
st(e,e,w,w)
st(e,w,e,w)
s(e,w,w,e)
st(w,w,e,w)
s(e,e,e,w)
etc.
As State-Space
st(e,w,e,e)
st(e,e,-,-)  st(w,w,-,-)
Means Farmer and dog went from east to
west
Can be rewritten:
mv(st(X,X,G,C),st(Y,Y,G,C))
Constructing a Move Predicate
opp(e,w)
 opp(w,e)

Giving
mv(st(X,X,G,C),st(Y,Y,G,C)) :- opp(X,Y).
opp(e,w).
opp(w,e).
Facts
Suppose: st(e,e,G,C)
mv(st(X,X,G,C), st(Y,Y,G,C)) :- opp(X,Y).
opp(e,w).
opp(w,e).
w,w
mv(st(e,e,G,C),st(Y,Y,G,C))
opp(X,Y)
{e/X, w/Y)
opp(e,w)
This is one of 4 move predicates (3 items to move + return trip
alone)
In Motion

Goat and cabbage are together
◦ unsafe(st(X,D,Y,Y)) if x != y
◦ unsafe(st(X,D,Y,Y)) :- opp(X,Y)

Dog and goat are together
◦ Unsafe(st(X,Y,Y,C) if x != y
◦ Unsafe(st(X,Y,Y,C) :- opp(X,Y)
Unsafe
mv(st(X,X,G,C),st(Y,Y,G,C)) :- opp(X,Y),
not(unsafe(st(Y,Y,G,C))).
Never move to an unsafe state
Use the stack mechanism from
Knight3
Farmer Problem
Traversal Mechanism