02 Statespace
02 Statespace
modeling search
path-finding
task solution
problem
graph-representation
Procedure Search-system
1. DATA := initial value
2. while termination condition(DATA) loop
3. SELECT R FROM rules that can be applied
4. DATA := R(DATA)
5. endloop
end
global workspace
stores the part of knowledge acquired
Procedure Search-system that is useful to preserve
(initial value, termination condition)
1. DATA := initial value
2. while termination condition(DATA) loop
3. SELECT R FROM rules that can be applied
4. DATA := R(DATA) searching rules
5. endloop can change the content
of the workspace
end control strategy (precondition, effect)
selects an appropriate rule
(general principle + heuristics)
[1,1,3] [2,2,3]
[3,1,3] [3,2,3]
[1,1,2] [2,2,1]
[2,2,2] [1,1,1]
goal
[1,2,2] [1,3,2] [3,3,2] [3,3,1] [2,3,1] [2,1,1]
State-space vs. problem space
♛ ♛ ♛
♛ ♛ ♛
♛ ♛ ♛
♛ ♛ ♛
number of nodes: n 2
n
♛ ♛ ♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
goal
♛ ♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛ ♛ ♛
Gregorics Tibor Artificial intelligence
Computational cost of the operator
❑ The computational complexity of an operator can be reduced
if the states are completed with extra information that are
maintained by the operator itself.
❑ For example
o The position of the next empty row can be stored in a state. It
may be increased after placing a new queen instead of
computing it over and over.
o To avoid the attacks on the chessboard the empty squares that
are under attack (not free) might be annotated in order to check
easily whether a queen is allowed to place on that square. In
this way there will be three kinds of squares: free, under attack
and occupied by queen.
♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
♛ ♛ ♛ ♛
♛ ♛
♛ goal ♛
♛ ♛
♛ ♛
Gregorics Tibor Artificial intelligence
8-puzzle
1 6 4 8 4
7 5 7 6 5
State-space: ST =rec(table:{0..8}3×3, empty:{1..3}×{1..3})
invariant: the elements of the table is a permutation of 0 .. 8
empty gives the coordinates of the empty cell that is
denoted with 0 it is computed
coordinate
Operator: Move(dir): ST → ST (this: ST) by coordinate
2 8 3 2 8 3 2 3 2 8 3 2 8 3
6 4 1 4 1 8 4 1 4 1 6
1 7 5 7 6 5 7 6 5 7 6 5 7 5 4
8 3 2 8 3 8 3 2 8 3 2 3 2 3 2 8 2 8 3 2 8 3 2 8
2 6 4 6 4 2 1 4 7 1 4 1 8 4 1 8 4 1 4 3 1 4 5 1 6 1 6 3
1 7 5 1 7 5 7 6 5 6 5 7 6 5 7 6 5 7 6 5 7 6 7 5 4 7 5 4
8 3 2 8 3 1 2 3 2 3 4 2 8 3 2 8 3
2 1 4 7 1 4 8 4 1 8 1 4 5 1 5 6
7 6 5 6 5 7 6 5 7 6 5 7 6 7 4
goal
8 1 3 8 3 1 2 3 1 2 3 2 8 3 2 8 3 2 8 3
2 4 2 1 4 8 4 7 8 4 1 4 5 1 5 1 5 6
7 6 5 7 6 5 7 6 5 6 5 7 6 7 4 6 7 4
8 1 3 1 3 1 3 2 8 3
2 4 8 2 4 8 2 4 1 5
7 6 5 7 6 5 7 6 5 7 4 6
Black&White puzzle
There are n black and m white stones and one empty place in a linear frame
with n+m+1 length. A stone can slide to the adjacent empty place or it can
jump over one stone onto an empty place. Initially black stones precede the
white stones. Let’s reverse the order of black and white stones!
State-space: ST =rec(s : {B, W,_}n+m+1, pos : [1.. n+m+1])
invariant: pos is the index of the single empty place, the number of B is n,
and the number of W is m
Operators: MoveLeft, MoveRight, JumpLeft, JumpRight
e.g.: MoveLeft : ST → ST (empty space is moved)
IF this.pos1 (this : ST)
THEN this.s[this.pos-1] this.s[this.pos] ; this.pos := this.pos-1
Initial: [B, … , B, W, … , W, _ ]
Final: i,j [1.. n+m+1], i<j : (this.s[i]=B this.s[j]=W)
Gregorics Tibor Artificial intelligence
state-graph of Black&White puzzle
start
goal
goal
goal goal
Gregorics Tibor Artificial intelligence
Travelling salesman problem
The traveling salesman must visit every city in his territory
exactly once and then return home covering the optimal total
cost. (n cities and cost of each pair of cities are known)
A n (n-1)!
4 2
E 1 B 5 24
11
3 1 9 50 6⸱1062
1
D 5 C possible solutions:
ABCDEA
ACBDEA ABDCEA ABCEDA
ABDECA
problem space
ADBECA …
*
State-space: ST = {cities} (set of finite sequences of cities
without home city)
Operator: Goto(city): ST → ST city∊{cities}
IF this.contains(city) (this: ST )
THEN this.append(city)
Initial state: <> (empty sequence)
Final state: this =n‒1 (length of this is n)
<BC> <BD> <BE> <CB> <CD> <CE> <DB> <DC> <DE> <EB> <EC> <ED>
invariant: all states are consistent (e.g.: on(C,B) and clear(B) is impossible)
A A
C B
B B C A C
start
C B
C
A B A B A B C A C
A
A B
B B
goal C C A C