0% found this document useful (0 votes)
58 views42 pages

Artificial Intelligence: Adina Magda Florea

This document discusses problem solving strategies in artificial intelligence, including constraint satisfaction problems (CSPs) and game playing. It covers CSP representations, algorithms like backtracking with lookahead techniques, and heuristics to improve search efficiency. For game playing, it introduces the minimax algorithm to search game trees exhaustively and choose the move with the best minimum outcome against an optimal opponent.

Uploaded by

choconlilom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views42 pages

Artificial Intelligence: Adina Magda Florea

This document discusses problem solving strategies in artificial intelligence, including constraint satisfaction problems (CSPs) and game playing. It covers CSP representations, algorithms like backtracking with lookahead techniques, and heuristics to improve search efficiency. For game playing, it introduces the minimax algorithm to search game trees exhaustively and choose the move with the best minimum outcome against an optimal opponent.

Uploaded by

choconlilom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Artificial Intelligence

University Politehnica of Bucharest


2008-2009

Adina Magda Florea
http://turing.cs.pub.ro/aifils_08
Course no. 3
Problem solving strategies
Constraint satisfaction problems
Game playing
1. Constraint satisfaction problems
Degree of a variable
Arity of a restriction
Degree of a problem
Arity of a problem
{X ,...,X }
1 n
{D ,...,D }
1 n
{R R }
1
,...,
k
R D ... D
j i1 i j
c
X ,...,X
i1 i j
{(X , x ),..., (X , x )}
1 j
1
n j
n
1.1 CSP Instances
One solution or all solutions
Total CSP
Partial CSP
Binary CSP binara constraint graph
CSP serach problem, in NP
sub-classes with polynomial time
complexity
Reduce the search time (search space)
Algorithm: Nonrecursive Backtracking
1. OPEN {S
i
} /* S
i
is the initial state*/
2. if OPEN = { }
then return FAIL /* no solution /*
3. Be S the first state in OPEN
4. if all successor states of S have been generated
then
4.1. Remove S from OPEN
4.2. repeat from 2
5. else
5.1. Obtain S', the new successor of S
5.2. Insert S' at the beginning of OPEN
5.3. Make link S S
5.4. Mark in S that S' was generated
5.5. if S' final state
then
5.5.1. Display solution by following S S ..
5.5.2. return SUCCESS /* a solution was found*/
5.6. repeat from 2
end.
1.2 Conventions
X
1
, , X
N
problem variables, N no of problem variables,
U integer, the index of the current var
F a vector indexed by the variable indeces, in which we
store the selections for variable values from the first one to
the current variable

c
contrar caz in fals
D D R
]) F[U ], (F[U R restrictia exista daca adevarat
= ]) F[U , U ], F[U , (U Relatie
2 1 2 1
2 1
U U U U
2 1 U U
2 2 1 1
Algorithm: Recursive Backtracking

BKT (U, F)
for each value V of X
U
do
1. F[U] V
2. if Verify (U,F) = true
then
2.1. if U < N
then BKT(U+1, F)
2.2. else
2.2.1. Display the values in F
/* F is a solution */
2.2.2. break the for
end.

Verify (U,F)
1. test true
2. I U - 1
3. while I > 0 do
3.1. test Relatie(I, F[I], U, F[U])
3.2. I I - 1
3.3. if test = false
then break the while
4. return test
end.
1.3 Improving the BKT
Algorithms to improve the representation
Local consistency of arcs or paths in the constraint graph
Hybrid algorithm
Reduce no of tests
Look ahead techniques:
- Full look-ahead
- Partial look-ahead
- Forward checking
Look back techniques:
- backjumping
- backmarking

Using heuristics
Algorithms to improve the
representation
Constraint propagation
X
X X
R = {(a,b)} 1 2
3
12
R = {(a,c)}
13
X
X X
R = {(a,b),(b,c)} 1 2
3
12
R = {(a,c),(c,b)}
13 R = {b,c)}
23
(a) (b)
D = D = D = {a, b,c}
X X X
1 2 3
1.4 Local constraint propagation
values x and y for X
i
and X
j
is allowed by
R
ij
(x,y).

An arc (X
i
, X
j
) in a directed constraint graph is
called arc-consistent if and only if for any
value x e D
i
, domain of var X
i
, there is a value
y e D
j
, domain of X
j
, such that R
ij
(x,y).
arc-consistent direct contraint graph
algorithm: AC-3: arc-consistency for a constraint graph
1. make a queue Q { (X
i
, X
j
) | (X
i
, X
j
) e Set of arcs, i=j}
2. while Q is not empty do
2.1. Remove from Q an arc (X
k
, X
m
)
2.2. Check(X
k
, X
m
)
2.3. if Check made any changes in the domain of X
k

then
Q Q { (X
i
, X
k
) | (X
i
, X
k
)e Set of arcs, i=k,m}
end.
Check (X
k
, X
m
)
for each x e D
k
do
1. if there is no value y e D
m
such that R
km
(x,y)
then remove x from D
k
end.
Path consistency
A path of length m through nodes i
0
,,i
m
of a directed
constraint graph is called m-path-consistent if and
only if for any value x e D
i0
, domain of var i
0
and a
value y e D
jm
, domain of var i
m
, for which R
i0im
(x,y),
tehre si a sequence of values z
1
e D
i1
z
m-1
e D
im-1

such that R
i0i1
(x,z
1
), , R
im-1im
(z
m-1
,y)
Directed constraint graph m-path-consistent
Minimal constraint graph
m-path-consistency
Complexity
N no of variables
a - cardinality max of variables domains
e no of constraints
arc-consistency - AC-3: time complexity
O(e*a
3
); space complexity: O(e+N*a)
Even one of O(e*a
2
) AC-4
2-path-consistency - PC-4: time complexity
O(N
3
*a
3
)
1.5 CSP without bkt - conditions
Directed constraint graph
Width of a node
Width of an ordering
Width of a graph
A
B
C
R
AC
R
CB
A
A
A
B B
B
C
C
C
Theorems
if an arc-consistent graph has the width equal
to 1 (i.e. is a tree), then the problem has a
solution without backtracking.
if a 2-path-consistent graph has the width
equal to 2, then the problem has a solution
without backtracking.
1.6 Look-ahead techniques
Conventions
U, N, F (F[U]), T (T[U] X
U
), TNOU




Forward_check
Future_Check

Full look ahead
Partial look ahead
Forward checking

contrar caz n fals


) L , (L R daca adevarat
= L2) U2, L1, , Relatie(U1
2 1 U U
2 1
algorithm: Backtracking Full look ahead

Prediction(U, F, T)
for each element L in T[U] do
1. F[U] L
2. if U < N then //chack consistency of assignment
2.1 TNOU Forward_Check (U, F[U], T)
2.2 if TNOU = LINIE_VIDA
then TNOU Future_Check(U, TNOU)
2.3 if TNOU = LINIE_VIDA
then Prediction (U+1, F, TNOU)
3. else display assignments in F
end


Forward_Check (U, L, T)
1. TNOU empty table
2. for U2 U+1 to N do
2.1 for each element L2 in T[U2] do
2.1.1 if Relatie(U, L, U2, L2) = true
then insert L2 in TNOU[U2]
2.2 if TNOU[U2] is empty
then return LINIE_VIDA
3. return TNOU
end

Future_Check (U, TNOU)
if U+1 < N then
1. for U1 U+1 to N do
1.1 for each element L1 in TNOU[U1] do
1.1.1 for U2 U+1 to N, U2=U1 do
i. for each element L2 in TNOU[U2] do
- if Relatie (U1, L1, U2, L2) = true
then break the cycle //of L2
ii. if no consistent value was found for U2
then
- remove L1 from TNOU[U1]
- break the cycle // of U2
1.2 if TNOU[U1] empty line
then return LINIE_VIDA
2. return TNOU
end

BKT partial look ahead
Modify Verifica_Viitoare with the steps marked in red

Future_Check (U, TNOU)
if U+1 < N then
1. for U1 U+1 to N - 1 do
1.1 for each element L1 in TNOU[U1] do
1.1.1 for U2 U1+1 to N do
i. for each element L2 in TNOU[U2] do
- if Relatie (U1, L1, U2, L2) = true
then break the cycle //of L2
ii. if no consistent value was found for U2
then
- remove L1 from TNOU[U1]
- break the cycle // of U2
1.2 if TNOU[U1] este vida
then return LINIE_VIDA
2. return TNOU
end

BKT forward checking
Remove the call Future_Check(U, TNOU) in sub-
program Prediction
algorithm: Backtracking forward checking

Prediction(U, F, T)
for each element L in T[U] do
1. F[U] L
2. if U < N then
2.1 TNOU Forward_check (U, F[U], T)
2.2 if TNOU = LINIE_VIDA
then TNOU Future_Check (U, TNOU)
2.2 if TNOU = LINIE_VIDA
then Prediction (U+1, F, TNOU)
3. else display assignments in F
end


1.7 Look back techniques
Backjumping
(a)
Pantofi
Cmi
Pantaloni
D ={escarpeni, pantofi de tenis}
D ={gri, bleu, jeans} D ={verde, alb}
Pf
C Pt
{(escarpeni, gri), (pantofi de tenis, jeans)} {(escarpeni, alb)}
{(verde, gri), (verde, jeans), (alb, jeans), (alb, bleu)}
Pantofi escarpeni pantofi de tenis
jeans
verde alb
Pantaloni
Cmi
jeans
bleu
gri
verde alb
2
1
1
1
(b)
gris
bleu
verde
alba
verde
alba
(pantofi de tens, gris) (pantofi tenis, alba)
algorithm: Backjumping
Backjumping(U, F, Nivel)
/* NrBlocari, NivelVec, I, test, Nivel1 var locale */
1. NrBlocari 0, I 0, Nivel U
2 for each element V of X
U
do
2.1 F[U] V
2.2 test, NivelVec[I] Verify (U, F)
2.3 if test = true then
2.3.1 if U < N then
i. Backjumping (U+1, F, Nivel1)
ii. if Nivel1 < U then jump to end
2.3.2 else display the values in F // solution
2.4 else NrBlocari NrBlocari + 1
2.5 I I + 1
3. if NrBlocari = no of values of X[U] and
all elements in NivelVec are equal
then Nivel NivelVec[1]
end
Verify (U, F)
1. test true
2. I U-1
3. while I>0 do
3.1 test Relatie(I, F[I], U, F[U])
3.2 if test = false
then break the cycle
3.3 I I 1
4. NivelAflat I
5. return test, NivelAflat
end
1.8 Heuristics
All solutions try finding first blocking
One solution - try most promising paths
variable ordering variables that are linked by
explicit constraints should be sequential - all
solutions prefer vars that goes in a small no of contraints and have
small domains
value ordering - all solutions start with the most constraint
value of a variable
test ordering all solutions start testing with the most
constraint previous var
2. Game playing
2 payers
player
opponent
Investigate all search space
Cannot investigate all search space

2.1 Minimax for search space that can be
investigated exhaustively
Player MAX
Opponent MIN
Minimax principle
Label each level in GT with MAX (player) and
MIN (opponent)
Label leaves with evaluation of player
Go through the GT
if father node is MAX then label the node with the
maximal value of its successors
if father node is MIN then label the node with the
minimal value of its successors

Minimax Search space (GT)
MIN
A / 3
B / 3
MAX
C / 2 D / 2
MAX F / 12 E / 3 G / 8 H / 2 I / 4 J / 6 K / 14 L / 5 M / 2


Minimax Search space (GT)


MIN
MAX
MIN
MAX
MIN
MAX
7 / 1
6 - 1 / 1 5 - 2 / 1 4 - 3 / 1
5 - 1 - 1 / 0 4 - 2 - 1 / 1 3 - 2 - 2 / 0 3 - 3 - 1 / 1
4 - 1 - 1 - 1 / 0 3 - 2 - 1 - 1 / 1 2 - 2 - 2 - 1 / 0
3 - 1 - 1 - 1 - 1 / 0 2 - 2 - 1 - 1 - 1 / 1
2 - 1 - 1 - 1 - 1 - 1 / 0


Nim with 7 sticks
algorithm: Minimax for all search space
Minimax( S )
1. for successor S
j
of S (obtained by a move op
j
) do
val( S
j
) Minimax( S
j
)
2. apply op
j
for which val( S
j
) is maximal
end

Minimax( S )
1. if S is a final node then return eval( S )
2. else
2.1 if MAX moves in S then
2.1.1 for each successor S
j
of S do
val( S
j
) Minimax( S
j
)
2.1.2 return max( val( S
j
), j )
2.2 else { MIN moves in S }
2.2.1 for each successor S
j
of S do
val( S
j
) Minimax( S
j
)
2.2.2 return min( val( S
j
), j )
end
2.2 Minimax for search space investigated
up to depth n
Minimax principle
algorithm Minimax up to a depth n
level(S)
A heuristic evaluation function eval(S)
algorithm: Minimax with finite depth n
Minimax( S )
1. for each successor S
j
of S do
val( S
j
) Minimax( S
j
)
2. apply op
j
for which val( S
j
) is maximal
end

Minimax( S ) { returns an estimation of S }
0. if S is final node then return eval( S )
1. if level( S ) = n then return eval( S )
2. else
2.1 if MAX moves in S then
2.1.1 for each successor S
j
of S do
val( S
j
) Minimax( S
j
)
2.1.2 return max( val( S
j
), j )
2.2 else { MIN moves in S }
2.2.1 for each successor S
j
of S do
val( S
j
) Minimax( S
j
)
2.2.2 return min( val( S
j
), j )
end
Evaluation function
Tic-Tac-Toe (X si O)
Heuristic function eval( S ) conflict in state S.
eval( S ) = total possible of winning lines of MAX
in state S - total possible of winning lines of MIN
in state S.
if S is a state from which MAX can win with one
move, then eval( S ) = (big value)
if S is a state from which MIN can win with one
move, then eval( S ) = - (small value).
eval(S) in Tic-Tac-Toe


X has 6 possible winning lines



O has 5 possible winning lines



eval( S ) = 6 - 5 = 1

X













O



2.3 Alpha-beta pruning
It is possible to have the correct decision in
Minimax without going through all nodes
always
Eliminating part of the search tree =
pruning the tree

Alpha-beta pruning
Be o the best value (biggest) found for MAX and |
the best value (smallest) found for MIN.
The alpha-beta algorithm updates o and | while
going through the search tree and cuts all sub-trees for
which o or | are worst.
Search is finished along a branch according to 2 rules:
Stop searching bellow any node MIN with a value | smaller
than or equal to the value o of any node MAX predecessors
to the current MIN node.
Stop searching bellow any node MAX with a value o greater
than or equal to the value | of any node MIN predecessors
to the current MAX node.
Alpha-beta pruning of the tree
MIN
A / 3
B / 3
MAX
C / s 2 D / 2
MAX F / 12 E / 3 G / 8 H / 2 I / 4 J / 6 K / 14 L / 5 M / 2


algorithm: Alpha-beta
MAX(S, o, |) { return the maximum value of a state. }
0. if S is a final node then return eval( S )
1. if level( S ) = n then return eval( S )
2. else
2.1 for each successor S
j
of S do
2.1.1 o max(o, MIN(S
j
, o, |))
2.1.2 if o > | then return |
2.2 return o
end

MIN(S, o, |) { return the minimum value of a state. . }
0. if S is a final node then return eval( S )
1. if level( S ) = n then return eval( S )
2. else
2.1 for each successor S
j
of S do
2.1.1 | min(|, MAX(S
j
, o, |))
2.1.2 if | s o then return o
2.2 return |
end
2.4 Games that include an element of
chance
The player does not know the possible
moves of the opponent (e.g. backgammon)
3 types of nodes:
MAX
MIN
Chance nodes

42
MAX
MIN
MAX
Dice
Chance nodes
Decision nodes
36 ways to roll 2 dice
21 distinct roles (5-6 same as 6-5)
The 6 doubles have a chance of - > 1/36
Distinct roles a chance of -> 1/18
1/36
1/18
Dice
1/36 1/18
EXPECTMINIMAX(S) =
Utility(S) if n is a terminal node
Max
Sj suc S
[EXPECTMINIMAX(Sj)]
Min
Sj suc S
[EXPECTMINIMAX(Sj)]
Sum
Sj suc S
[ P(Sj)*EXPECTMINIMAX(Sj)]

You might also like